我刚刚第一次使用ModelCheckpoint
函数来保存最佳模型(best_model = True
)并想测试其性能。模型保存后,表示保存之前val_acc
为83.3%。我加载了模型并在validation_generator上使用了evaluate_generator,但val_acc
的结果是0.639。我感到困惑并再次使用它并获得0.654然后0.647,0.744等等。我在我的PC上测试了相同的配置(没有GPU),并且它始终显示相同的结果(有时候可能会出现小的舍入错误)
我正在使用Keras的Tensorflows实现。
model.compile(loss='categorical_crossentropy',
optimizer=optimizers.SGD(lr=1e-4, momentum=0.9),
metrics=['accuracy'])
checkpointer = ModelCheckpoint(filepath='/tmp/weights.hdf5', monitor = "val_acc", verbose=1, save_best_only=True)
# prepare data augmentation configuration
train_datagen = ImageDataGenerator(
rescale = 1./ 255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale=1. / 255)
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size = (img_height, img_width),
batch_size = batch_size)
validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size = (img_height, img_width),
batch_size = batch_size)
# fine-tune the model
model.fit_generator(
train_generator,
steps_per_epoch = math.ceil(train_samples/batch_size),
epochs=100,
workers = 120,
validation_data=validation_generator,
validation_steps=math.ceil(val_samples/batch_size),
callbacks=[checkpointer])
model.load_weights(filepath='/tmp/weights.hdf5')
model.predict_generator(validation_generator, steps = math.ceil(val_samples/batch_size) )
temp_model = load_model('/tmp/weights.hdf5')
temp_model.evaluate_generator(validation_generator, steps = math.ceil(val_samples/batch_size), workers = 120)
>>> [2.1996076788221086, 0.17857142857142858]
temp_model.evaluate_generator(validation_generator, steps = math.ceil(val_samples/batch_size), workers = 120)
>>> [2.2661823204585483, 0.25]
答案 0 :(得分:0)
这是因为您只保存模型权重。这意味着您没有保存优化器状态,这说明了重新加载模型时精度的差异。如果您在创建save_weights_only=False
时添加ModelCheckpoint
,问题将得到解决:
如果您重新加载模型,请使用load_model
Keras
函数。否则你仍然只会加载权重。
checkpointer = ModelCheckpoint(filepath='/tmp/full_model.hdf5', monitor = "val_acc", verbose=1, save_best_only=True, save_weights_only=False)
#reload model
from keras.models import load_model
model = load_model('/tmp/full_model.hdf5')
答案 1 :(得分:0)
好的,问题是以下 - batch_size
!我花了很长时间才想出这个 -
steps = math.ceil(val_samples/batch_size)
由于batch_size不是number_of_samples的除数,因此它产生了问题。设置workers
变量时也会出现一些小错误 - 使用GPU实际使用它是没有意义的。