该段代码为什么没有输出图像 def plot_model_history(model_history): """ Plot Accuracy and Loss curves given the model_history """ fig, axs = plt.subplots(1, 2, figsize=(15, 5)) # summarize history for accuracy axs[0].plot(range(1, len(model_history.history['acc']) + 1), model_history.history['acc']) axs[0].plot(range(1, len(model_history.history['val_acc']) + 1), model_history.history['val_acc']) axs[0].set_title('Model Accuracy') axs[0].set_ylabel('Accuracy') axs[0].set_xlabel('Epoch') axs[0].set_xticks(np.arange(1, len(model_history.history['acc']) + 1), len(model_history.history['acc']) / 10) axs[0].legend(['train', 'val'], loc='best') # summarize history for loss axs[1].plot(range(1, len(model_history.history['loss']) + 1), model_history.history['loss']) axs[1].plot(range(1, len(model_history.history['val_loss']) + 1), model_history.history['val_loss']) axs[1].set_title('Model Loss') axs[1].set_ylabel('Loss') axs[1].set_xlabel('Epoch') axs[1].set_xticks(np.arange(1, len(model_history.history['loss']) + 1), len(model_history.history['loss']) / 10) axs[1].legend(['train', 'val'], loc='best') fig.savefig('plot.png') plt.show() # Create the model model = Sequential() model.add(tf.keras.layers.Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(48, 48, 1))) model.add(tf.keras.layers.Conv2D(64, kernel_size=(3, 3), activation='relu')) model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2))) model.add(tf.keras.layers.Dropout(0.25)) model.add(tf.keras.layers.Conv2D(128, kernel_size=(3, 3), activation='relu')) model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2))) model.add(tf.keras.layers.Conv2D(128, kernel_size=(3, 3), activation='relu')) model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2))) model.add(tf.keras.layers.Dropout(0.25)) model.add(tf.keras.layers.Flatten()) model.add(tf.keras.layers.Dense(1024, activation='relu')) model.add(tf.keras.layers.Dropout(0.5)) model.add(tf.keras.layers.Dense(7, activation='softmax')) # emotions will be displayed on your face from the webcam feed model.build(input_shape=(32, 48, 48, 1)) model.load_weights( r'D:\pythonProject\model.h5')
时间: 2024-01-28 16:04:44 浏览: 92
该段代码中定义了一个绘制模型训练过程中准确率和损失变化的函数`plot_model_history()`,但是在代码中没有调用该函数进行绘图。因此,即使该函数的代码正确,也不会输出图像。
如果想要输出图像,可以在代码中添加一行`plot_model_history(model_history)`来调用该函数,其中`model_history`指的是模型训练过程中保存的训练历史数据。例如:
```
history = model.fit(x_train, y_train, batch_size=32, epochs=10, validation_data=(x_val, y_val))
plot_model_history(history)
```
这样就可以在模型训练完成后绘制准确率和损失变化的图像。
阅读全文