【问题标题】:TensorFlow giving error "InvalidArgumentError: Input is empty" when training or validatingTensorFlow 在训练或验证时给出错误“InvalidArgumentError:输入为空”
【发布时间】:2021-07-19 21:17:47
【问题描述】:

我正在尝试在自定义图像上运行 Resnet 模型(迁移学习)。

我的目录树如下所示:

|-train
|  |-class1
|  |    |-image1
|  |    |-image2
|  |    |-....
|  |-class2
|       |-image1
|       |-image2
|       |-....
|-val
   |-class1
   |    |-image1
   |    |-image2
   |    |-....
   |-class2
        |-image1
        |-image2
        |-....

我创建了这样的 tensorflow 数据集:

train_ds = tf.keras.preprocessing.image_dataset_from_directory( "train", labels='inferred', label_mode='int', image_size=(img_height, img_width), batch_size=batch_size)

val_ds = tf.keras.preprocessing.image_dataset_from_directory( "val", labels='inferred', label_mode='int', image_size=(img_height, img_width), batch_size=batch_size)

但是当我训练或测试数据集时,在几张图片之后它给了我一个错误:

InvalidArgumentError: Input is empty [[{{node decode_image/DecodeImage}}]] [Op:IteratorGetNext]

<Figure size 720x720 with 0 Axes>.

我使用的数据集在这里 - https://github.com/xuequanlu/I-Nema - 我已将所有 .tif 图像转换为 .jpg。这可能是什么原因造成的?

提前致谢!

编辑:这是错误日志:https://pastecode.io/s/82hk68ar

【问题讨论】:

    标签: python tensorflow keras image-classification


    【解决方案1】:

    在你的代码中

    train_ds = tf.keras.preprocessing.image_dataset_from_directory( "train", labels='inferred', label_mode='int', image_size=(img_height, img_width), batch_size=batch_size)
    

    “train”应该是训练图像目录的完整路径,而不是字符串。 例如 train_dir=os.path.join(my_dir, 'train') 其中 my_dir 保存 train 和 val 子目录

    train_dir=os.path.join(my_dir, 'train')
    val_dir=os.path.join(my_dir, 'val')
    train_ds = tf.keras.preprocessing.image_dataset_from_directory( train_dir, labels='inferred', label_mode='int', image_size=(img_height, img_width), batch_size=batch_size)
    val_ds = tf.keras.preprocessing.image_dataset_from_directory( val_dir, labels='inferred', label_mode='int', image_size=(img_height, img_width), batch_size=batch_size)
    

    还要注意,因为您在 model.compile 中使用的是 label_mode='int' 将损失指定为 sparse_categorical_crossentropy

    【讨论】:

      猜你喜欢
      • 2022-10-24
      • 2017-01-29
      • 1970-01-01
      • 2016-12-31
      • 2021-06-30
      • 2018-12-15
      • 1970-01-01
      • 1970-01-01
      • 2022-06-22
      相关资源
      最近更新 更多