对于二进制分类问题,我得到的模型精度与keras evaluate_generator()
和predict_generator()
不同:
def evaluate_model(model, generator, nBatches):
score = model.evaluate_generator(generator=generator, # Generator yielding tuples
steps=generator.samples//nBatches, # number of steps (batches of samples) to yield from generator before stopping
max_queue_size=10, # maximum size for the generator queue
workers=1, # maximum number of processes to spin up when using process based threading
use_multiprocessing=False, # whether to use process-based threading
verbose=0)
print("loss: %.3f - acc: %.3f" % (score[0], score[1]))
使用evaluate_generator()
,我得到的acc
值最高为 0.7 。
def evaluate_predcitions(model, generator):
predictions = model.predict_generator(generator=generator,
steps=generator.samples/nBatches,
max_queue_size=10,
workers=1,
use_multiprocessing=False,
verbose=0)
# Evaluate predictions
predictedClass = np.argmax(predictions, axis=1)
trueClass = generator.classes
classLabels = list(generator.class_indices.keys())
# Create confusion matrix
confusionMatrix = (confusion_matrix(
y_true=trueClass, # ground truth (correct) target values
y_pred=predictedClass)) # estimated targets as returned by a classifier
print(confusionMatrix)
使用predict_generator()
,我得到的acc
值为 0.5 。
我正在将acc
计算为(TP+TN)/(TP+TN+FP+FN)
acc
的{{1}}是基于evaluate_generator()
的吗? TP+TN/(TP+TN+FP+FN)
有何不同?答案 0 :(得分:2)
解决此问题(evaluate_generate和predict_generator精度)。您只需要在代码中做三件事:
(1)设置
test_datagen.flow_from_directory
在test_datagen.flow_from_dataframe
或workers = 0
中,
(2)设置
model.predict_generator
trueClass = generator.classes[generator.index_array]
和(3)更改
JSONArray snippet = jsonObject.getJSONArray("items");
这些更改将使您的程序在主线程上执行,保留索引并与图像ID匹配。然后,两个精度应该相同。