准确度高,但对keras模式的预测较差

wko9yo5t  于 2022-11-24  发布在  其他
关注(0)|答案(1)|浏览(127)

我是机器学习的新手,我正在尝试使用Google Colab和Tensorflow/Keras来训练使用迁移学习(Resnet50)的图像分类模型。
我首先使用图像数据集,使用以下代码:

data_root = '/tmp/OCT2017'
 
batch_size = 32
img_height = 160
img_width = 160
 
data_train = tf.keras.preprocessing.image_dataset_from_directory(data_root + '/train',labels='inferred',
                                                                 image_size=(img_height,img_width),
                                                                 batch_size=batch_size)

对于小的测试数据集,这个方法非常有效,我得到了很好的准确性和预测。但是当尝试使用更大的数据集时,Colab提供的所有RAM都被消耗了,所以我切换到生成器,使用:

data_generator = tf.keras.preprocessing.image.ImageDataGenerator()

data_train_gen = data_generator.flow_from_directory(data_root + '/train',
                                                target_size=(img_height,img_width),
                                                class_mode='sparse',
                                                batch_size=batch_size,
                                                shuffle=False)

并使用以下各项训练模型:

base_learning_rate = 0.0001
model.compile(optimizer=tf.keras.optimizers.Adam(lr=base_learning_rate),
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

with tf.device('/device:GPU:0'):
  epochs = 10
  history =model.fit(
    data_train_gen,
    validation_data=data_val_gen,
    epochs=epochs,
    callbacks=[csv_logger]
  )

我使用此设置获得了良好的准确性:

model.evaluate(data_test)

31/31 [=========================================]-3秒93毫秒/步-损耗:0.0925-精度:0.9742
【0.09248838573694229,0.9741735458374023】【0.09248838573694229,0.9741735458374023】【0.09248838573694229,0.9741735458374023】【0.092488374023,【0.0924835742229,0.9741735458374023】【0.09741735458374023】【0.097
然而,当要求预测,为了使一个混乱矩阵,我得到了可怕的结果

from sklearn.metrics import classification_report, confusion_matrix
import seaborn as sns

y_pred = model.predict(data_test)
predicted_categories = tf.argmax(y_pred, axis=1)
true_categories = tf.concat([y for x, y in data_test_gen], axis=0)

cm = confusion_matrix(predicted_categories, true_categories)

heatmap = sns.heatmap(cm, annot=True, cmap='YlGn', xticklabels=['CNV','DME','DRUSEN','NORMAL'],yticklabels=['CNV','DME','DRUSEN','NORMAL'])
plt.xlabel("True Labels")
plt.ylabel("Predictions")
plt.show()

预测正确率约为40%混淆矩阵完全随机

classification_report(true_categories, predicted_categories, target_names=class_names, output_dict=True)

{'CNV ':{'f1-分数':0.256198347107438,"精密度":0.256198347107438,"召回":0.256198347107438,"支持度":242},"DME":{'f1-分数':0.23236514522821577,"精密度":0.23333333333333334,"召回":0.23140495867768596,"支持度":242},"德鲁森":{'f1-分数':0.25311203319502074,"精密度":0.25416666666666665,"召回":0.25206611570247933,"支持度":242},"正常":{'f1-分数':0.2827868852459016,"精密度":0.2804878048780488,"召回":0.28512396694214875,"支持度":242},"准确性":0.256198347107438,"宏平均值":{'f1-分数':0.256115602694144,"精密度":0.25604653799637167,"召回":0.256198347107438,"支持度":968},"加权平均值":{'f1-分数':0.256115602694144,"精密度":0.2560465379963717,"召回":0.256198347107438,"支持度":九百六十八}}

ljsrvy3e

ljsrvy3e1#

您不显示测试生成器,但确保设置shuffle=False。data_test和data_test_gen之间有什么区别?如果您有一个名为data_test的测试生成器,您可以从以下位置获取真实标签:

true_categories=data_test.labels

然后在混淆矩阵和分类报告中使用它们

相关问题