【问题标题】:Cannot add tensor to the batch: number of elements does not match. Shapes are: [tensor]: [128,128,4], [batch]: [128,128,3] [Op:IteratorGetNext]无法将张量添加到批次:元素数量不匹配。形状是:[张量]:[128,128,4],[批次]:[128,128,3] [Op:IteratorGetNext]
【发布时间】:2021-07-07 19:41:10
【问题描述】:

我正在尝试将数据集加载到 tensorflow,对其进行预处理,然后创建批次以提供给 gan,但由于某种原因,某些图像有 4 个通道!无法将张量添加到批次:元素数量不匹配。形状为:[张量]:[128,128,4],[批次]:[128,128,3] [Op:IteratorGetNext]

这是预处理数据然后将它们添加到批处理中的功能

    BATCH_SIZE = 32

 def map_images(file):
  img = tf.io.decode_jpeg(tf.io.read_file(file))
  img = tf.dtypes.cast(img, tf.float32)
  img = tf.image.resize(img, size=[128, 128])
  img = img / 255.0
  reimg = tf.reshape(img, [128, 128, 3])
  
  return reimg

# create training batches
filename_dataset = tf.data.Dataset.list_files("/content/drive/MyDrive/Dataset/Damage type dataset/Damage type/Broken Glass/*.JPG")
image_dataset = filename_dataset.map(map_images).batch(BATCH_SIZE)

【问题讨论】:

    标签: python tensorflow deep-learning neural-network


    【解决方案1】:

    我通过在 tf.io.decode_jpeg 中设置 channels 参数 = 3 解决了这个问题,因为它的默认值为 0,所以使其等于 3 将强制所有图像只有 3 个通道

    【讨论】:

      猜你喜欢
      • 2022-01-02
      • 2020-03-19
      • 1970-01-01
      • 2018-02-26
      • 1970-01-01
      • 1970-01-01
      • 2021-09-21
      • 2020-03-09
      • 2021-01-06
      相关资源
      最近更新 更多