Xception 迁移学习模型的错误

时间:2021-02-23 11:07:33

标签: tensorflow keras deep-learning conv-neural-network transfer-learning

我的数据集有 2 个文件夹 Test 和 Train,Test 包含 2 个文件夹,其中包含 Covid X 射线和普通 X 射线,Train 文件夹也是如此。在最后一个训练时期,我在 val_set 上获得了 99.4% 的准确率,但是在绘制混淆矩阵时,我什至没有得到 60% 的准确率。卡住严重,请帮助!而且我的 Y_pred 看起来很奇怪,而且不是 0-1 确定性,我需要帮助才能在我的混淆矩阵和分类报告上获得相同的 99.4% 结果。

import os
import numpy as np
import matplotlib.pyplot as plt
import keras
from keras.applications import xception
from keras.layers import *
from keras.models import *
from keras.preprocessing import image

model = xception.Xception(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
for layers in model.layers:
    layers.trainable=False
    
flat1 = Flatten()(model.layers[-1].output)
class1 = Dense(256, activation='relu')(flat1)
output = Dense(1, activation='sigmoid')(class1)

model = Model(inputs = model.inputs, outputs = output)


model.compile(loss = 'binary_crossentropy', optimizer='adam', metrics=['accuracy'])


train_datagen = image.ImageDataGenerator(
    rescale = 1./255,
    shear_range = 0.2,
    zoom_range = 0.2,
    horizontal_flip = True,
    )

test_datagen = image.ImageDataGenerator(rescale = 1./255)

train_generator = train_datagen.flow_from_directory(
    '/Users/xd_anshul/Desktop/Research/Major/CovidDataset/Train',
    target_size = (224,224),
    batch_size = 10,
    class_mode='binary')

validation_generator = test_datagen.flow_from_directory(
    '/Users/xd_anshul/Desktop/Research/Major/CovidDataset/Test',
    target_size = (224,224),
    batch_size = 10,
    class_mode='binary')

#model Fitting

hist = model.fit(
    train_generator,
    epochs=2,
    validation_data=validation_generator)



from sklearn.metrics import classification_report, confusion_matrix

Y_pred = model.predict_generator(validation_generator, steps = np.ceil(validation_generator.samples / validation_generator.batch_size), verbose=1, workers=0)
y_pred = [np.where(predictions>0.5, 1, 0) for predictions in Y_pred]
print('Confusion Matrix')
print(confusion_matrix(validation_generator.classes, y_pred))
print('Classification Report')
target_names = ['Covid', 'Normal']
print(classification_report(validation_generator.classes, y_pred, target_names=target_names))

OUTPUT::

Epoch 1/2
255/255 [==============================] - 464s 2s/step - loss: 1.4076 - accuracy: 0.9342 - val_loss: 0.3753 - val_accuracy: 0.9718
Epoch 2/2
255/255 [==============================] - 558s 2s/step - loss: 0.1218 - accuracy: 0.9885 - val_loss: 0.0181 - val_accuracy: 0.9944
Confusion Matrix
[[310 100]
 [ 97  25]]
Classification Report
              precision    recall  f1-score   support

       Covid       0.76      0.76      0.76       410
      Normal       0.20      0.20      0.20       122

    accuracy                           0.63       532
   macro avg       0.48      0.48      0.48       532
weighted avg       0.63      0.63      0.63       532

Y_pred looks like:

1.05698e-14
2.25061e-13
3.96925e-19
5.53114e-17
3.4461e-32
1.2413e-32
2.29092e-28
4.01138e-17
3.81177e-23
1.88817e-07
1.164e-07
.
.
.
(532 Values)

1 个答案:

答案 0 :(得分:0)

代码后

output = Dense(1, activation='sigmoid')(class1)

输入此代码

model=Model(inputs=model.input, outputs=output)

在您的验证生成器中,您没有指定目标大小,因此添加

target_size=(224,224)

另请注意,您的 train_generator 中的批次大小为 10。在model.fit 你有steps_per_epoch=9。这意味着您每个时期仅使用 90 张训练图像。 你有多少训练图像?此外,您没有为验证生成器指定 batch_size,因此它默认为 32。您在 model.fit 中将验证步骤设置为 2,这样您将在每个 epoch 中通过 64 个验证图像。您有多少验证图片?