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日

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题