【问题标题】:Cannot batch tensors with different shapes in component 0无法在组件 0 中批量处理不同形状的张量
【发布时间】:2021-01-06 18:40:09
【问题描述】:

InvalidArgumentError:无法在组件 0 中批处理具有不同形状的张量。第一个元素的形状为 [224,224,3],元素 25 的形状为 [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)

问题可能出在损坏的图像中还是我在代码中遗漏了某些内容?

【问题讨论】:

  • 元素 25 为灰度图像,其余为 RGB 图像。
  • 尝试检查所有数据的大小是否为 [224,224,3]

标签: python image tensorflow keras tf.data.dataset


【解决方案1】:

彩色图像有 3 个通道,R、G、B。但是,灰度图像只有一个通道。列表的元素 25 是灰度图像,而之前的索引是彩色的。对此的解决方案是将通道数传递给tf.image.decode_jpeg,如下所示:

imagefile=tf.image.decode_jpeg(image_file, channels=3)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    • 2020-11-18
    • 2019-11-03
    相关资源
    最近更新 更多