我正在使用TF 2.2,并且正在尝试使用tf.data创建管道。
以下功能正常:
def load_image(filePath, label):
print('Loading File: {}' + filePath)
raw_bytes = tf.io.read_file(filePath)
image = tf.io.decode_image(raw_bytes, expand_animations = False)
return image, label
# TrainDS Pipeline
trainDS = getDataset()
trainDS = trainDS.shuffle(size['train'])
trainDS = trainDS.map(load_image, num_parallel_calls=AUTOTUNE)
for d in trainDS:
print('Image: {} - Label: {}'.format(d[0], d[1]))
我想将load_image()
与Dataset.interleave()
一起使用。然后我尝试:
# TrainDS Pipeline
trainDS = getDataset()
trainDS = trainDS.shuffle(size['train'])
trainDS = trainDS.interleave(lambda x, y: load_image_with_label(x, y), cycle_length=4)
for d in trainDS:
print('Image: {} - Label: {}'.format(d[0], d[1]))
但是我收到以下错误:
Exception has occurred: TypeError
`map_func` must return a `Dataset` object. Got <class 'tuple'>
File "/data/dev/train_daninhas.py", line 44, in <module>
trainDS = trainDS.interleave(lambda x, y: load_image_with_label(x, y), cycle_length=4)
如何修改我的代码以使Dataset.interleave()
与load_image()
一起读取并行图像?
正如错误所暗示的,您需要修改load_image
以使其返回Dataset
对象,我已经显示了一个示例,其中有两个图像说明了如何在tensorflow 2.2.0
中执行此操作:
import tensorflow as tf
filenames = ["./img1.jpg", "./img2.jpg"]
labels = ["A", "B"]
def load_image(filePath, label):
print('Loading File: {}' + filePath)
raw_bytes = tf.io.read_file(filePath)
image = tf.io.decode_image(raw_bytes, expand_animations = False)
return tf.data.Dataset.from_tensors((image, label))
dataset = tf.data.Dataset.from_tensor_slices((filenames, labels))
dataset = dataset.interleave(lambda x, y: load_image(x, y), cycle_length=4)
for i in dataset.as_numpy_iterator():
image = i[0]
label = i[1]
print(image.shape)
print(label.decode())
# (275, 183, 3)
# A
# (275, 183, 3)
# B
希望这会有所帮助!