2

我想使用 Tensorflow 数据集 API 为每个文件夹创建一批(每个文件夹包含图像)。我有以下简单的代码片段:

import tensorflow as tf
import os
import pdb

def parse_file(filename):
    image_string = tf.read_file(filename)
    image_decoded = tf.image.decode_png(image_string)
    image_resized = tf.image.resize_images(image_decoded, [48, 48])
    return image_resized #, label

def parse_dir(frame_dir):
    filenames = tf.gfile.ListDirectory(frame_dir)
    batch = tf.constant(5)
    batch = tf.map_fn(parse_file, filenames)
    return batch

directory = "../Detections/NAC20171125"
# filenames = tf.constant([os.path.join(directory, f) for f in os.listdir(directory)])
frames = [os.path.join(directory, str(f)) for f in range(10)]


dataset = tf.data.Dataset.from_tensor_slices((frames))
dataset = dataset.map(parse_dir)

dataset = dataset.batch(256)
iterator = dataset.make_initializable_iterator()
next_element = iterator.get_next()


with tf.Session() as sess:
    sess.run(iterator.initializer)
    while True:
        try:
            batch = sess.run(next_element)
            print(batch.shape)
        except tf.errors.OutOfRangeError:
            break

但是,tf.gfile.ListDirectory(在 parse_dir 中)需要一个普通字符串而不是张量。所以现在的错误是

TypeError: Expected binary or unicode string, got <tf.Tensor 'arg0:0' shape=() dtype=string>

有没有简单的方法来解决这个问题?

4

1 回答 1

4

这里的问题是,它tf.gfile.ListDirectory()是一个 Python 函数,它需要一个 Python 字符串,而frame_dirto 的参数parse_dir()是一个tf.Tensor. 因此,您需要等效的 TensorFlow 操作来列出目录中的文件,并且tf.data.Dataset.list_files()(基于tf.matching_files())可能是最接近的等效操作。

directory = "../Detections/NAC20171125"
frames = [os.path.join(directory, str(f)) for f in range(10)]

# Start with a dataset of directory names.
dataset = tf.data.Dataset.from_tensor_slices(frames)

# Maps each subdirectory to the list of files in that subdirectory and flattens
# the result.
dataset = dataset.flat_map(lambda dir: tf.data.Dataset.list_files(dir + "/*"))

# Maps each filename to the parsed and resized image data.
dataset = dataset.map(parse_file)

dataset = dataset.batch(256)

iterator = dataset.make_initializable_iterator()
next_element = iterator.get_next()
于 2017-12-19T16:14:01.343 回答