为什么“ tf.data.Dataset.from_tensor_slices”在输出中打印图像的所有路径?

时间:2019-09-22 08:27:12

标签: python tensorflow tensorflow-datasets

我正在编写代码以在Tensorflow中读取图像。我使用this教程来做到这一点。问题是当我使用以下命令时:tf.data.Dataset.from_tensor_slices((all_image_paths, all_image_labels)),图像的整个路径都显示在输出控制台中。

这是我正在使用的代码:

def get_image_info(dir_path, file_url=None, file_name=None, untar=True):
    if file_url != None:
        dir_path = tf.keras.utils.get_file(fname=file_name, origin=file_url, untar=untar)

    data_root = pathlib.Path(dir_path)
    all_image_paths = list(data_root.glob('*/*'))
    label_names = sorted(item.name for item in data_root.glob('*/') if item.is_dir())
    label_dict = dict((name, index) for index, name in enumerate(label_names))
    all_image_labels = [label_dict[pathlib.Path(path).parent.name] for path in all_image_paths]

    return data_root, label_dict, all_image_paths, all_image_labels

def load_image_dataset(dir_path, file_url=None, file_name=None, untar=True):
    def load_and_preprocess_from_path_label(path, label):
        return load_and_preprocess_image(path), label

    data_root, label_dict, all_image_paths, all_image_labels = get_image_info(dir_path, file_url, file_name, untar)
    image_label_ds = tf.data.Dataset.from_tensor_slices((all_image_paths, all_image_labels))
    # image_label_ds = ds.map(load_and_preprocess_from_path_label)

    return image_label_ds, label_dict

image_label_ds, label_dict= load_image_dataset('', 'https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz', 'flower_photos')

这是我输出的一部分:

, WindowsPath('C:/Users/hajba/.keras/datasets/flower_photos/tulips/8838914676_8ef4db7f50_n.jpg'), WindowsPath('C:/Users/hajba/.keras/datasets/flower_photos/tulips/8838975946_f54194894e_m.jpg'), WindowsPath('C:/Users/hajba/.keras/datasets/flower_photos/tulips/8838983024_5c1a767878_n.jpg'), WindowsPath('C:/Users/hajba/.keras/datasets/flower_photos/tulips/8892851067_79242a7362_n.jpg'), WindowsPath('C:/Users/hajba/.keras/datasets/flower_photos/tulips/8904780994_8867d64155_n.jpg'), WindowsPath('C:/Users/hajba/.keras/datasets/flower_photos/tulips/8908062479_449200a1b4.jpg'), WindowsPath('C:/Users/hajba/.keras/datasets/flower_photos/tulips/8908097235_c3e746d36e_n.jpg'), WindowsPath('C:/Users/hajba/.keras/datasets/flower_photos/tulips/9019694597_2d3bbedb17.jpg'), WindowsPath('C:/Users/hajba/.keras/datasets/flower_photos/tulips/9030467406_05e93ff171_n.jpg'), WindowsPath('C:/Users/hajba/.keras/datasets/flower_photos/tulips/9048307967_40a164a459_m.jpg'), WindowsPath('C:/Users/hajba/.keras/datasets/flower_photos/tulips/924782410_94ed7913ca_m.jpg'), WindowsPath('C:/Users/hajba/.keras/datasets/flower_photos/tulips/9378657435_89fabf13c9_n.jpg'), WindowsPath('C:/Users/hajba/.keras/datasets/flower_photos/tulips/9444202147_405290415b_n.jpg'), WindowsPath('C:/Users/hajba/.keras/datasets/flower_photos/tulips/9446982168_06c4d71da3_n.jpg'), WindowsPath('C:/Users/hajba/.keras/datasets/flower_photos/tulips/9831362123_5aac525a99_n.jpg'), WindowsPath('C:/Users/hajba/.keras/datasets/flower_photos/tulips/9870557734_88eb3b9e3b_n.jpg'), WindowsPath('C:/Users/hajba/.keras/datasets/flower_photos/tulips/9947374414_fdf1d0861c_n.jpg'), WindowsPath('C:/Users/hajba/.keras/datasets/flower_photos/tulips/9947385346_3a8cacea02_n.jpg'), WindowsPath('C:/Users/hajba/.keras/datasets/flower_photos/tulips/9976515506_d496c5e72c.jpg')]. Consider casting elements to a supported type.

1 个答案:

答案 0 :(得分:0)

对于那些面临这个问题的人:这是Tensorflow尝试在输出中显示图像路径张量的一种错误。我使用Windows操作系统,并通过以下代码将路径类型(WindowsPath)转换为字符串,以解决此错误:

all_image_paths_str = list(map(lambda x: str(x), all_image_paths))

,然后将其用于生成输出数据集张量:

image_label_ds = tf.data.Dataset.from_tensor_slices((all_image_paths_str, all_image_labels))