【发布时间】: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