Keras:如何评估模型的准确性(evaluate_generator与predict_generator)?

时间:2019-04-26 13:44:00

标签: python tensorflow keras deep-learning

对于二进制分类问题,我得到的模型精度与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)有何不同?

1 个答案:

答案 0 :(得分:2)

解决此问题(evaluate_generate和predict_generator精度)。您只需要在代码中做三件事:

(1)设置

test_datagen.flow_from_directory

test_datagen.flow_from_dataframeworkers = 0 中,

(2)设置

model.predict_generator

trueClass = generator.classes[generator.index_array] 和(3)更改

JSONArray snippet = jsonObject.getJSONArray("items");

这些更改将使您的程序在主线程上执行,保留索引并与图像ID匹配。然后,两个精度应该相同。