Keras 使用ResNet50进行迁移学习-验证结果良好,但生产结果较差
我训练了一个二元分类模型,大约有8000张图像,每类大约4000张,我在resnet50上进行迁移学习,我冻结了所有层,结果是:val_损失:0.0340-val_acc:0.9890 但当我对模型进行测试时,我得到的结果几乎是随机的,概率非常高 我不明白这有什么意义? 毕竟,模型没有研究验证的图片,那么,图表中的结果和生产中的结果之间怎么会有这么大的差距呢 密集软最大激活 以下是所有代码:Keras 使用ResNet50进行迁移学习-验证结果良好,但生产结果较差,keras,deep-learning,classification,resnet,transfer-learning,Keras,Deep Learning,Classification,Resnet,Transfer Learning,我训练了一个二元分类模型,大约有8000张图像,每类大约4000张,我在resnet50上进行迁移学习,我冻结了所有层,结果是:val_损失:0.0340-val_acc:0.9890 但当我对模型进行测试时,我得到的结果几乎是随机的,概率非常高 我不明白这有什么意义? 毕竟,模型没有研究验证的图片,那么,图表中的结果和生产中的结果之间怎么会有这么大的差距呢 密集软最大激活 以下是所有代码: import numpy as np import pandas as pd import matplo
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import cv2
import os
from tensorflow.python.keras.applications import ResNet50
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense
from tensorflow.python.keras import optimizers
from keras.applications.resnet50 import preprocess_input
from keras.preprocessing.image import ImageDataGenerator
from tensorflow.python.keras.callbacks import EarlyStopping, ModelCheckpoint
NUM_CLASSES = 2
CHANNELS = 3
IMAGE_RESIZE = 224
RESNET50_POOLING_AVERAGE = 'avg'
DENSE_LAYER_ACTIVATION = 'softmax'
OBJECTIVE_FUNCTION = 'categorical_crossentropy'
LOSS_METRICS = ['accuracy']
NUM_EPOCHS = 10
EARLY_STOP_PATIENCE = 3
STEPS_PER_EPOCH_TRAINING = 10
STEPS_PER_EPOCH_VALIDATION = 10
BATCH_SIZE_TRAINING = 100
BATCH_SIZE_VALIDATION = 100
# Using 1 to easily manage mapping between test_generator & prediction for submission preparation
BATCH_SIZE_TESTING = 1
model = Sequential()
model.add(ResNet50(include_top = False, pooling = RESNET50_POOLING_AVERAGE, weights = 'imagenet'))
model.add(Dense(NUM_CLASSES, activation = DENSE_LAYER_ACTIVATION))
model.layers[0].trainable = False
sgd = optimizers.SGD(lr = 0.01, decay = 1e-6, momentum = 0.9, nesterov = True)
model.compile(optimizer = sgd, loss = OBJECTIVE_FUNCTION, metrics = LOSS_METRICS)
image_size = IMAGE_RESIZE
data_generator = ImageDataGenerator(preprocessing_function=preprocess_input)
train_generator = data_generator.flow_from_directory(
'./datasets/Helmet_verification_v2/train',
target_size=(image_size, image_size),
batch_size=BATCH_SIZE_TRAINING,
class_mode='categorical')
validation_generator = data_generator.flow_from_directory(
'./datasets/Helmet_verification_v2/validation',
target_size=(image_size, image_size),
batch_size=BATCH_SIZE_VALIDATION,
class_mode='categorical')
output_dir = "./models/working"
output_file = output_dir + "/best.hdf5"
if not os.path.exists(output_dir):
print("create folder: {}".format(output_dir))
os.makedirs(output_dir)
cb_early_stopper = EarlyStopping(monitor = 'val_loss', patience = EARLY_STOP_PATIENCE)
cb_checkpointer = ModelCheckpoint(filepath = output_file, monitor = 'val_loss', save_best_only = True, mode = 'auto')
fit_history = model.fit_generator(
train_generator,
steps_per_epoch=STEPS_PER_EPOCH_TRAINING,
epochs = NUM_EPOCHS,
validation_data=validation_generator,
validation_steps=STEPS_PER_EPOCH_VALIDATION,
callbacks=[cb_checkpointer, cb_early_stopper]
)
model.load_weights(output_file)
测试:
一些数据示例:
好的,我发现了问题,我使用了错误的优化器,我用Adam替换了SGD,它解决了问题,结果令人惊讶,只是概率仍然太高
model.compile(optimizer = optimizers.Adam(1e-3), loss = OBJECTIVE_FUNCTION, metrics = LOSS_METRICS)
提供更多的代码和一些示例数据以帮助您是的,您需要提供代码,如果没有它,问题将无法回答
test_generator = data_generator.flow_from_directory(
directory = './datasets/Helmet_verification_v2/test',
target_size = (image_size, image_size),
batch_size = BATCH_SIZE_TESTING,
class_mode = None,
shuffle = False,
seed = 123
)
test_generator.reset()
pred = model.predict_generator(test_generator, steps = len(test_generator), verbose = 1)
predicted_class_indices = np.argmax(pred, axis = 1)
model.compile(optimizer = optimizers.Adam(1e-3), loss = OBJECTIVE_FUNCTION, metrics = LOSS_METRICS)