muhua233 2022-12-24 08:57 采纳率: 100%
浏览 64
已结题

训练AlexNet模型到一半时报错

训练AlexNet模型到一半时报错

tensorflow.python.framework.errors_impl.InvalidArgumentError: Graph execution error:

Unknown image file format. One of JPEG, PNG, GIF, BMP required.
     [[{{node decode_image/DecodeImage}}]]
     [[IteratorGetNext]] [Op:__inference_train_function_2769]

以下是代码部分:

import matplotlib.pyplot as plt
from data import DataSource
import tensorflow as tf
from model import AlexNet


class Train:
    def __init__(self):
        self.data = DataSource()
        self.cnn = AlexNet()

    def train(self):
        self.cnn.model.compile(optimizer='adam',
                               loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
                               metrics=['accuracy'])
        # 启动TensorBoard
        tensorboard = tf.keras.callbacks.TensorBoard()  # log_dir参数可以设置日志路径
        # callbacks回调函数为TensorBoard
        history = self.cnn.model.fit(self.data.train_ds, validation_data=self.data.val_ds, epochs=1,
                                     callbacks=[tensorboard])
        # 保存模型
        self.cnn.model.save_weights('fruits.h5')

        print(history.epoch)
        print(history.history)
        plt.plot(history.history['accuracy'], label='accuracy')
        plt.plot(history.history['val_accuracy'], label='val_accuracy')
        plt.xlabel('Epoch')
        plt.ylabel('Accuracy')
        plt.legend(loc='lower right')

        loss = history.history['loss']
        val_loss = history.history['val_loss']

        epochs = range(1, len(loss) + 1)

        plt.plot(epochs, loss, 'bo', label='training loss')
        plt.plot(epochs, val_loss, 'b', label='test loss')
        plt.title('training And test Loss')
        plt.xlabel('Epochs')
        plt.ylabel('Loss')
        plt.show()

        test_loss, test_acc = self.cnn.model.evaluate(self.data.val_ds, verbose=1)
        print("验证准确率为:", test_acc)


if __name__ == "__main__":
    train = Train()
    train.train()


运行结果及详细报错内容:

 UserWarning: "`sparse_categorical_crossentropy` received `from_logits=True`, but the `output` argument was produced by a Softmax activation and thus does not represent logits. Was this intended?
  output, from_logits = _get_logits(
104/468 [=====>........................] - ETA: 8:30 - loss: 1.4576 - accuracy: 0.6250Traceback (most recent call last):
  File "E:/fruits/train.py", line 50, in <module>
    train.train()
  File "E:/fruits/train.py", line 19, in train
    history = self.cnn.model.fit(self.data.train_ds, validation_data=self.data.val_ds, epochs=1,
  File "D:\Python38\lib\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "D:\Python38\lib\site-packages\tensorflow\python\eager\execute.py", line 52, in quick_execute
    tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
tensorflow.python.framework.errors_impl.InvalidArgumentError: Graph execution error:

Unknown image file format. One of JPEG, PNG, GIF, BMP required.
     [[{{node decode_image/DecodeImage}}]]
     [[IteratorGetNext]] [Op:__inference_train_function_2769]


已检查过三个训练集文件夹,已确定都是jpg格式的图片。
  • 写回答

3条回答 默认 最新

  • muhua233 2022-12-24 16:53
    关注

    已解决,参考:http://t.csdn.cn/kSBa1
    可能是批量下载时出的问题,检查出错误图片并删除就行。(错误图片在电脑里可以正常打开查看,所以需要用代码检查)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 1月1日
  • 已采纳回答 12月24日
  • 创建了问题 12月24日

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程