无法在组件 0 中批处理具有不同形状的张量
Cannot batch tensors with different shapes in component 0
InvalidArgumentError: Cannot batch tensors with different shapes in component 0. First element had shape [224,224,3] and element 25 had shape [224,224,1].
我已经重塑了图片,如您所见。
def process_path(file_path=train_data):
image_file= tf.io.read_file(image_dir+file_path+'.jpg')
image_file=tf.image.decode_jpeg(image_file)
image_file=tf.image.convert_image_dtype(image_file,tf.float32)
image_file=tf.image.resize(image_file,[224,224])
return image_file
X_train = train_data.map(process_path)
然后我只是合并标签和图像数据
train=tf.data.Dataset.zip((X_train,y_train))
train=train.shuffle(buffer_size=64).batch(32).prefetch(1)
base_res_model.fit(train,epochs=10,verbose=2)
问题可能出在损坏的图像中,还是我在代码中遗漏了什么?
一张彩色图像有 3 个通道,R、G、B。但是,灰度图像只有一个通道。列表中的元素 25 是灰度图像,而前面的索引是彩色的。一个解决方案是将通道数传递给 tf.image.decode_jpeg
,如下所示:
imagefile=tf.image.decode_jpeg(image_file, channels=3)
InvalidArgumentError: Cannot batch tensors with different shapes in component 0. First element had shape [224,224,3] and element 25 had shape [224,224,1].
我已经重塑了图片,如您所见。
def process_path(file_path=train_data):
image_file= tf.io.read_file(image_dir+file_path+'.jpg')
image_file=tf.image.decode_jpeg(image_file)
image_file=tf.image.convert_image_dtype(image_file,tf.float32)
image_file=tf.image.resize(image_file,[224,224])
return image_file
X_train = train_data.map(process_path)
然后我只是合并标签和图像数据
train=tf.data.Dataset.zip((X_train,y_train))
train=train.shuffle(buffer_size=64).batch(32).prefetch(1)
base_res_model.fit(train,epochs=10,verbose=2)
问题可能出在损坏的图像中,还是我在代码中遗漏了什么?
一张彩色图像有 3 个通道,R、G、B。但是,灰度图像只有一个通道。列表中的元素 25 是灰度图像,而前面的索引是彩色的。一个解决方案是将通道数传递给 tf.image.decode_jpeg
,如下所示:
imagefile=tf.image.decode_jpeg(image_file, channels=3)