我想使用 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>
有没有简单的方法来解决这个问题?