我正在尝试使用生成器在Keras中训练转换网络。但是,当我使用valuate_generator和predict_generator时,它们会产生不同的准确性得分(简单准确性,(TP + TN)/(TP + FP + TN + FN))。
我看到一些帖子说这是关于多处理的,将worker设置为1并将pickle_safe设置为True可以,但是没有。这是我的代码:
batch_size = 16
classes_list = [str(i) for i in range(num_classes)]
data_flow_train_param = {
"dataframe": data_train,
"x_col": "path",
"y_col": "diagnosis",
"classes": classes_list,
"batch_size": batch_size,
"target_size": IMAGE_SIZE,
"shuffle": True
}
data_flow_train = image_generator.flow_from_dataframe(**data_flow_train_param)
data_flow_val_param = {
"dataframe": data_val,
"x_col": "path",
"y_col": "diagnosis",
"classes": classes_list,
"batch_size": 1,
"target_size": IMAGE_SIZE,
"shuffle": False
}
data_flow_val = image_generator.flow_from_dataframe(**data_flow_val_param)
fit_params = {
"generator": data_flow_train,
"steps_per_epoch": data_train.shape[0] // batch_size,
"epochs": 10,
"verbose": 1,
"validation_data": data_flow_val,
"validation_steps": data_val.shape[0],
"callbacks": [checkpoint, lr_decay, early_stopping]
}
print("Training the model...")
history = model.fit_generator(**fit_params)
print("Done!")
model.evaluate_generator(data_flow_val, steps=data_val.shape[0], verbose=1, pickle_safe=True)
y_val_true = np.array(data_val["diagnosis"]).astype("int")
y_val_pred = np.argmax(model.predict_generator(data_flow_val, steps=data_val.shape[0], verbose=1, pickle_safe=True), axis=-1)
print(accuracy_score(y_val_true, y_val_pred))
valuate_generator函数使我的准确度达到80%左右,而predict_generator只有30%。