python - 如何解释训练结果
问题描述
我是深度学习的新手,所以我正在尝试越来越多地了解所有内容,所以我尝试从 github 执行有关活体人脸检测的代码,所以当我使用 mobilenet 训练该模型时,我确实得到了这个图表 ans这个结果,那么我如何提取信息并解释它们:模型准确性
我用分类报告得到了这个混淆矩阵:混淆矩阵
[[1436 234]
[ 14 1461]]
Classification Report
precision recall f1-score support
0 0.99 0.86 0.92 1670
1 0.86 0.99 0.92 1475
accuracy 0.92 3145
macro avg 0.93 0.93 0.92 3145
weighted avg 0.93 0.92 0.92 3145
这是我执行的代码:
import keras
from keras import backend as K
from keras.layers.core import Dense, Activation
from keras.optimizers import Adam
from keras.metrics import categorical_crossentropy
from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing import image
from keras.models import Model
from keras.applications import imagenet_utils
from keras.layers import Dense,GlobalAveragePooling2D
from keras.applications import MobileNet
from keras.applications.mobilenet import preprocess_input
import numpy as np
import matplotlib.pyplot as plt
from keras.optimizers import Adam
from sklearn.metrics import classification_report, confusion_matrix
INIT_LR = 1e-5
BS = 32
EPOCHS = 20
# MobileNetV2
base_model = keras.applications.mobilenet_v2.MobileNetV2(input_shape=None, alpha=1.0, include_top=False, weights='imagenet', input_tensor=None, pooling=None, classes=2)
x=base_model.output # a luat outputul inainte de FC
x=GlobalAveragePooling2D()(x)
x=Dense(512,activation='relu')(x) #dense layer 3
preds=Dense(2,activation='softmax')(x) #final layer with softmax activation
# specify the inputs
# specify the outputs
# now a model has been created based on our architecture
new_model=Model(inputs=base_model.input, outputs=preds)
# check the model architecture
for i,layer in enumerate(new_model.layers):
print(i,layer.name)
# We are not using weights pre-trained on ImageNet.
for layer in new_model.layers:
layer.trainable=True
train_datagen=ImageDataGenerator()
train_generator=train_datagen.flow_from_directory('../db_faces/train',
target_size=(64,64),
color_mode='rgb',
batch_size=BS,
class_mode='binary',
shuffle=True)
validation_generator=train_datagen.flow_from_directory('../db_faces/test',
target_size=(64,64),
color_mode='rgb',
batch_size=BS,
class_mode='binary',
shuffle=False)
# Adam optimizer
adam_opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)
# loss function will be binary cross entropy
# evaluation metric will be accuracy
new_model.compile(optimizer=adam_opt, loss="sparse_categorical_crossentropy", metrics=['accuracy'])
step_size_train = train_generator.n//train_generator.batch_size
step_size_validation = validation_generator.samples // validation_generator.batch_size
H = new_model.fit(train_generator,
steps_per_epoch=step_size_train,
validation_data = validation_generator,
validation_steps = step_size_validation,
epochs=EPOCHS)
# save the network to disk
print("[INFO] serializing network and save it to disk...")
new_model.save('liveness.model')
print("[INFO] Class indices")
labels = (train_generator.class_indices)
print(labels)
# summarize history for accuracy
plt.plot(H.history['accuracy'])
plt.plot(H.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
# summarize history for loss
plt.plot(H.history['loss'])
plt.plot(H.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
Y_pred = new_model.predict(validation_generator, validation_generator.samples // BS + 1)
y_pred = np.argmax(Y_pred, axis=1)
print('Confusion Matrix')
print(confusion_matrix(validation_generator.classes, y_pred))
print('Classification Report')
target_names = ['Fake', 'Real']
print(classification_report(validation_generator.classes, y_pred))
解决方案
对于二元分类问题,可以根据模型精度图结果和分类报告来验证模型性能。
在您的结果中,训练数据的模型准确度达到 95%,它在 20 纪元之前保持稳定,而对于测试数据,它从 8 纪元开始一直在 93% 到 95% 之间波动。我们可以说该模型针对训练数据和测试数据训练得很好准确率为 92%。模型的泛化能力足够好。
从模型损失图结果来看,训练和测试数据从 epoch 13 开始相互收敛。如果训练和测试损失 = 0,那么它是具有更高预测能力的好模型。
从分类报告中,模型性能可以通过 F1 分数来验证。分数越高,模型越好。
推荐阅读
- java - 实现 IP 网关(在 Java 中?)
- google-maps - 如何通过“mid”参数从 Google Maps API 下载 KML 文件?
- c++ - 为什么 for 循环只取这个简单代码中的最后一个字符串?
- left-join - cx_Oracle 在左加入表两次时冻结(两个别名)
- vb.net - 您将如何在 VB.NET 中构造具有不同数据类型的数据
- c++ - 右值引用可以作为 const 引用传递吗?
- java - Spring Boot 选择了错误的 CorsFilter
- c++ - 为什么按位 OR 和 AND 逻辑没有按预期工作?
- sql-server - T-SQL - 大表中的总 SUM 行和列
- java - 将 List 添加到嵌套 List 时,为什么需要使用构造函数来创建新的列表实例,而不是仅添加现有列表?