经过多年的阅读,终于是我提出第一个问题的时候了:
使用jupyter笔记本中的tensorflow和keras,我在20k声谱图(我自己的数据集)上训练了一个VGG16模型,并使用数据生成器进行了一些数据增强,以进行4级多类分类。下面,我的代码:
import tensorflow as tf
from tensorflow.keras.applications.vgg16 import VGG16
model = VGG16(include_top=True,
weights=None,
input_tensor=None,
pooling=None,
classes=len(labels),
classifier_activation="softmax")
from keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import optimizers
# Rescale by 1/255, add data augmentation:
train_datagen = ImageDataGenerator(
rescale=1./255,
width_shift_range=0.2,
brightness_range=[0.8,1.2],
fill_mode='nearest')
# Note that the validation data should not be augmented!
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
# This is the target directory
train_dir,
# All images will be resized to 224x224
target_size=(224, 224),
batch_size=20,
# one hot label for multiclass
class_mode='categorical')
validation_generator = test_datagen.flow_from_directory(
validation_dir,
target_size=(224, 224),
batch_size=20,
class_mode='categorical')
model.compile(loss='categorical_crossentropy',
optimizer=optimizers.RMSprop(learning_rate=2e-5),
metrics=[tf.keras.metrics.CategoricalAccuracy(),
tf.keras.metrics.Precision(),
tf.keras.metrics.Recall()])
# Train the model:
history = model.fit(
train_generator,
steps_per_epoch=100,
epochs=100,
validation_data=validation_generator,
validation_steps=50,
verbose=2)
为了评估训练过程,我绘制了acc,损失,精确,回忆和F1分数。他们看起来都很好,这表明训练进行得很顺利。
当我在我的测试集上使用modell.evaluate
时,我得到了91%的acc。
test_generator = test_datagen.flow_from_directory(
test_dir,
target_size=(224, 224),
batch_size=20,
class_mode='categorical')
test_loss, test_acc, test_precison, test_recall = model.evaluate(test_generator, steps=50)
print('test_acc:' + str(test_acc))
发现4724幅图像,分属于4类。50/50 ============================== - 2s 49 2s/步进损失: 0.2739 - categorical_accuracy: 0.9120 -精度: 0.9244 -召回: 0.9050 test_acc:0.9120000004768372
但是,当我试图用下面的方式绘制一个混淆矩阵时,它看起来很可怕,当我从创建混淆矩阵的数据中手工计算acc时,我得到了25%的acc。这意味着我的模型完全没有学到任何…
import numpy as np
import sklearn.metrics
# Print confuision matrix for test set:
test_pred_raw = model.predict(test_generator)
print('raw preditcitons:')
print(test_pred_raw)
test_pred = np.argmax(test_pred_raw, axis=1)
print('prediction:')
print(test_pred)
test_labels = test_generator.classes
print('labels')
print(test_labels)
# Calculate accuracy manualy:
my_test_acc = sum(test_pred == test_labels) / len(test_labels)
print('My_acc:')
print(my_test_acc)
# Calculate the confusion matrix using sklearn.metrics
cm = sklearn.metrics.confusion_matrix(test_labels, test_pred)
figure = plot_confusion_matrix(cm, class_names=labels)
原始预处理:
[2.9204198e-12 2.8631955e-09 1.0000000e+00 7.3386294e-16
0.0000000e+00 1.0000000e+00 0.0000000e+00 0.0000000e+00
..。
2.2919695e-03 3.8061540e-07 9.9770677e-01 8.1024604e-07
0.0000000e+00 1.0000000e+00 4.0776377e-37 2.6318860e-38]
预测:
2 2 1.2 1
标签
0 0 0.3 3
My_acc:
0.2491532599491956\
我现在的问题是,我可以信任哪些指标,另一个指标有什么问题?
发布于 2022-03-31 05:29:35
好吧。我想我明白了!
在shuffle = False
中设置test_datagen.flow_from_directory()
似乎可以解决问题。现在,混淆矩阵看起来要好得多,my_acc = 89%看起来也不错。
当调用两次时,数据生成器似乎会产生不同的批。首先,通过model.predict(test_generator)
,然后再通过test_generator.classes
,基本使标签和预测不匹配,因为它们是针对不同的批次。
有人能确认我说得对吗?
发布于 2022-03-31 03:06:31
问题可能是:
my_test_acc = sum(test_pred == test_labels) / len(test_labels)
也许您应该添加一个舍入步骤,以确保预测值实际上是1.0,而不是0.99。
https://stackoverflow.com/questions/71691097
复制相似问题