我正在执行图像分类任务,以对眼底图像的可分级性进行3类分类:好,可用和不良。问题是在训练过程中,验证准确度在最后一个时期显示为0.8115,但是当我尝试使用model.predict时,即使在如下图所示的相同验证数据集上,结果也很差
这是我编写的使用ImageGenerator加载图像的代码
# Generator
train_datagen = ImageDataGenerator(preprocessing_function=preprocess_input, validation_split=0.2)
# Load image
size_expected = (224,224)
train_generator = train_datagen.flow_from_directory('./train_gradability',
batch_size = 100,
shuffle=True,
class_mode = 'categorical',
target_size = size_expected,
subset = 'training',
seed = 42)
val_generator = train_datagen.flow_from_directory('./train_gradability',
batch_size = 100,
class_mode = 'categorical',
target_size = size_expected,
subset = 'validation',
seed = 42)
用于创建模型的代码,我使用了InceptionResNetV2架构:
incp_resnet = InceptionResNetV2(input_shape = (size_expected[0],
size_expected[1], 3),
include_top = False,
weights='imagenet')
# Freeze layers
for layer in incp_resnet.layers:
layer.trainable = False
# Add customize layers
model = tf.keras.models.Sequential([
incp_resnet,
Flatten(),
# First hidden layer
Dense(4096, activation='relu'),
Dropout(0.5),
# Second hidden layer
Dense(2048, activation='relu'),
Dropout(0.5),
# Output layer
Dense(3, activation = 'softmax')
])
# Training
model.compile(optimizer = 'Adam', loss = 'categorical_crossentropy', metrics=
['categorical_accuracy'])
history = model.fit(train_generator,
epochs=1,
steps_per_epoch = (train_generator.samples//100+1),
validation_data = val_generator,
validation_steps = (val_generator.samples//100+1),
verbose = 1)
预测部分:
pred = model.predict(val_generator, steps=(val_generator.samples//100 + 1), verbose =1)
pred_classes = np.argmax(pred, axis = 1)
ground_truth = val_generator.classes
print(confusion_matrix(ground_truth, pred_classes))
print(classification_report(ground_truth, pred_classes))
我使用Tensorflow 2.1.0版和Keras 2.3.1版