【发布时间】:2021-09-20 00:21:25
【问题描述】:
我们目前正在进行一项图像分类任务,用于从胸部 X 射线图像中检测结核病。您可以在下面看到我们的代码。我们使用了 7000 张图像(每张 3500 张)并使用以下分数对其进行划分:训练集为 0.64,验证集为 0.16,测试集为 0.2。我们的训练和验证损失也很棒1。但是当我们将我们的模型用于测试集时,混淆矩阵没有意义2。我们的代码有问题吗?提前谢谢你。
#Imports
from tensorflow import keras
from keras.applications.mobilenet_v2 import MobileNetV2
from keras.applications.mobilenet_v2 import preprocess_input
from keras.layers import Dense
from keras.models import Model, Sequential
from keras.losses import BinaryCrossentropy
from keras.optimizer_v2.adam import Adam
from keras.preprocessing.image import ImageDataGenerator
from sklearn.metrics import confusion_matrix, classification_report
import numpy as np
#importing the model
image_size = 224
base_model = MobileNetV2(input_shape=(image_size,image_size,3),
weights='imagenet',
include_top=True)
#freezing the base model
for layer in base_model.layers:
layer.trainable = False
#adding a softmax layer with 2 outputs
y_layer = base_model.get_layer('global_average_pooling2d').output
z_layer = Dense(2, activation='softmax')(y_layer)
model = Model(inputs=base_model.input, outputs=z_layer)
#compiling the model
loss_func = BinaryCrossentropy()
opt = Adam(learning_rate=0.001)
model.compile(loss=loss_func,
optimizer=opt,
metrics=['accuracy'])
#Image augmentation
test_path = '...'
val_path = '...'
datagen = ImageDataGenerator(preprocessing_function=preprocess_input,
zoom_range=0.2,
brightness_range=[0.5,1.5],
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True)
batch_size=32
validation_size=32
train_set = datagen.flow_from_directory(test_path,
target_size = (image_size, image_size),
batch_size=batch_size,
class_mode = 'categorical')
validation_set = datagen.flow_from_directory(val_path,
target_size = (image_size, image_size),
batch_size=validation_size,
class_mode = 'categorical')
#Fitting the data into the model
model_history = model.fit(train_set,
validation_data=validation_set,
epochs=40,
steps_per_epoch=len(train_set)//batch_size,
validation_steps=len(validation_set)//validation_size,
verbose=1)
#testing the model on unseen data
test_path = '...'
test_datagen = ImageDataGenerator()
test_set = test_datagen.flow_from_directory(test_path,
target_size = (image_size, image_size),
class_mode = 'categorical')
predictions = model_testing.predict(test_set, verbose=1)
y_pred = np.argmax(predictions, axis=1)
class_labels = list(test_set.class_indices.keys())
print('Classification Report')
clsf = classification_report(test_set.classes, y_pred, target_names=class_labels)
print(clsf)
print('\n')
print('Confusion Matrix')
cfm = confusion_matrix(test_set.classes, y_pred)
print(cfm)
【问题讨论】:
标签: tensorflow machine-learning keras transfer-learning image-classification