【问题标题】:Load Images Tensorflow using tf.Data using scalars throws exception使用标量使用 tf.Data 加载图像 Tensorflow 会引发异常
【发布时间】:2020-03-17 02:34:55
【问题描述】:

我正在关注link 上有关如何使用 tf.Data 加载您自己的图像数据的官方指南。

我正在尝试使用 tf.Data 进程。

这段代码:

def get_label(file_path):
  # convert the path to a list of path components
  parts = tf.strings.split(file_path, os.path.sep)
  # The second to last is the class-directory
  return parts[-2] == CLASS_NAMES

返回由布尔值组成的等级为 1 的 EagerTensor。在以下代码中使用时有效:

def decode_img(img):
  # convert the compressed string to a 3D uint8 tensor
  img = tf.image.decode_jpeg(img, channels=3)
  # Use `convert_image_dtype` to convert to floats in the [0,1] range.
  img = tf.image.convert_image_dtype(img, tf.float32)
  # resize the image to the desired size.
  return tf.image.resize(img, [IMG_WIDTH, IMG_HEIGHT])

def process_path(file_path):
  label = get_label(file_path)
  # load the raw data from the file as a string
  img = tf.io.read_file(file_path)
  img = decode_img(img)
  return img, label

# Set `num_parallel_calls` so multiple images are loaded/processed in parallel.
labeled_ds = list_ds.map(process_path, num_parallel_calls=AUTOTUNE)

这段代码:

def get_label_nums(file_path):
  # convert the path to a list of path components
  parts = tf.strings.split(file_path, os.path.sep)
  # The second to last is the class-directory
  class_num = CLASS_NAMES.tolist().index(parts[-2])
  return class_num

def process_path_with_nums_as_labels(file_path):
  label = get_label_nums(file_path)
  # load the raw data from the file as a string
  img = tf.io.read_file(file_path)
  img = decode_img(img)
  return img, label

# Set `num_parallel_calls` so multiple images are loaded/processed in parallel.
labeled_nums_ds = list_ds.map(process_path_with_nums_as_labels, num_parallel_calls=AUTOTUNE)

没有。

我曾尝试使用定标器数字,因为这是我习惯看到的,但每次我尝试处理我得到的数据时: OperatorNotAllowedInGraphError: using atf.Tensoras a Pythonboolis not allowed: AutoGraph did not convert this function. Try decorating it directly with @tf.function.

我尝试将其更改为 ndarray 常规张量,并使用 @tf.function 装饰函数,但我仍然无法使其工作。据我所知,我没有将张量用作布尔值,只是试图将其用作我的标签。请告诉我为什么我可以使用布尔值的张量而不是缩放标签。

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    OperatorNotAllowedInGraphError 是一个非常常见的错误,当您的函数返回需要张量的 python 数据类型/数据结构时会抛出该错误。与 np 数组或 python 列表不同,张量嵌入到 tf 图中。由于您的 class_num 变量不是张量,因此无法映射到数据集。
    您可以通过将 class_num 索引解析为 tf.constant 来解决此问题,如下所示:
    class_num = tf.constant(class_num,dtype=tf.int32)
    OR
    通过将 python 函数包装为 tf.py_function() ,如下所示:

    def get_label_nums(file_path):
      # convert the path to a list of path components
      parts = tf.strings.split(file_path, os.path.sep)
      # The second to last is the class-directory
      class_num = CLASS_NAMES.tolist().index(parts[-2])
      return class_num
    
    def process_path_with_nums_as_labels(file_path):
      label = get_label_nums(file_path)
      # load the raw data from the file as a string
      img = open(file_path).read() # note that this returns the image string, similar to tf.io.read_file()
      img = decode_img(img)
      return img, label
    
    def create_targets_map_fn(img_path):
      img,label = tf.py_function(process_path_with_nums_as_labels,inp=[img_path],Tout=(tf.float32,tf.int32))
      return img,label  
    labeled_nums_ds = list_ds.map(create_targets_map_fn)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-13
      • 1970-01-01
      • 2022-01-10
      • 1970-01-01
      • 2015-01-27
      相关资源
      最近更新 更多