训练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]