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日

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算