【问题标题】:Create a TensorFlow Dataset for a CNN from a local dataset从本地数据集中为 CNN 创建 TensorFlow 数据集
【发布时间】:2019-08-09 04:51:55
【问题描述】:

我有一个包含两个类的黑白图像的大数据集,其中目录的名称是类的名称:

  • 目录SELECTION 包含所有带有label = selection 的图像;
  • 目录NEUTRAL 包含标签=中性的所有图像。

我需要将所有这些图像加载到 TensorFlow 数据集中,以更改 this 教程中的 MNIST 数据集。

我尝试遵循this 指南,它看起来不错,但有一些我不知道如何解决的问题。按照指南,我到了这里:

    from __future__ import absolute_import, division, print_function
    import os
    import pathlib
    import IPython.display as display
    import tensorflow as tf
    import numpy as np
    import matplotlib.pyplot as plt
    np.set_printoptions(threshold=np.nan)

    tf.enable_eager_execution()
    tf.__version__
    os.system('clear')

    #### some tries for the SELECTION dataset ####

    data_root = pathlib.Path('/Users/matteo/Desktop/DATASET_X/SELECTION/TRAIN_IMG')

    all_image_paths = []
    all_image_labels = []
    for item in data_root.iterdir():
        item_tmp = str(item)
        if 'selection.png' in item_tmp:
            all_image_paths.append(str(item))
            all_image_labels.append(0)

    image_count = len(all_image_paths)
    label_names = ['selection', 'neutral']
    label_to_index = dict((name, index) for index, name in enumerate(label_names))
    img_path = all_image_paths[0]
    img_raw = tf.read_file(img_path)

    img_tensor = tf.image.decode_png(
        contents=img_raw,
        channels=1
    )
    print(img_tensor.numpy().min())
    print(img_tensor.numpy().max())
    #### it works fine till here ####

    #### trying to make a function ####
    #### problems from here ####

    def load_and_decode_image(path):
        print('[LOG:load_and_decode_image]: ' + str(path))
        image = tf.read_file(path)

        image = tf.image.decode_png(
            contents=image,
            channels=3
        )

        return image


    image_path = all_image_paths[0]
    label = all_image_labels[0]

    image = load_and_decode_image(image_path)
    print('[LOG:image.shape]: ' + str(image.shape))

    path_ds = tf.data.Dataset.from_tensor_slices(all_image_paths)

    print('shape: ', repr(path_ds.output_shapes))
    print('type: ', path_ds.output_types)
    print()
    print('[LOG:path_ds]:' + str(path_ds))

如果我只加载一项,它可以工作,但是当我尝试这样做时:

path_ds = tf.data.Dataset.from_tensor_slices(all_image_paths)

如果我打印 path_ds.shape 它返回 shape: TensorShape([]) 所以它似乎不起作用。如果我尝试使用此块继续按照教程进行操作

image_ds = path_ds.map(load_and_decode_image, num_parallel_calls=AUTOTUNE)
plt.figure(figsize=(8, 8))
for n, image in enumerate(image_ds.take(4)):
    print('[LOG:n, image]: ' + str(n) + ', ' + str(image))
    plt.subplot(2, 2, n+1)
    plt.imshow(image)
    plt.grid(False)
    plt.xticks([])
    plt.yticks([])
    plt.xlabel(' selection'.encode('utf-8'))
    plt.title(label_names[label].title())
plt.show()

它给了我以下错误:

It's not possible open ' < string >': The file was not found (file: // /Users/matteo/Documents/GitHub/Cnn_Genetic/cnn_genetic/<string > ).

但问题是我不知道这个文件是什么以及它为什么要寻找它。我不需要绘制我的图像,但我想了解它为什么不起作用。如果我复制/粘贴教程代码,我会遇到同样的问题,所以我认为新的 tf 版本有问题。

所以....如果有人能告诉我哪里出错了,我将不胜感激。 感谢您的宝贵时间。

【问题讨论】:

  • 如您的错误消息所述,其中一个文件丢失。在会话中运行 for 循环以找出
  • 我认为程序会去寻找一个不存在的文件,因为它读取了错误的值。一个名为&lt;string&gt; 的文件实在太奇怪了。对我来说,问题是如果我打印 path_ds.shape 它会返回 shape: TensorShape([])

标签: python tensorflow dataset load local


【解决方案1】:

您的问题是 path_ds 应该是字符串形式的图像路径,但您尝试将它们转换为张量列表。

所以要获得你只需要的张量:

image_ds = all_image_paths.map(load_and_decode_image, num_parallel_calls=AUTOTUNE)

【讨论】:

    猜你喜欢
    • 2019-07-02
    • 1970-01-01
    • 2016-03-25
    • 1970-01-01
    • 1970-01-01
    • 2020-05-21
    • 2020-09-10
    • 2020-01-17
    • 2018-10-01
    相关资源
    最近更新 更多