使用predict_generator时如何返回项目的真实标签?

时间:2017-07-07 12:10:39

标签: python-3.x machine-learning neural-network deep-learning keras

我正在观察具有predict_generator()函数的神经网络的输出,但我无法看到预测项目的真实标签。如何实现块以查看输入项的真实标签?

test_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=45,
width_shift_range=0.25,
height_shift_range=0.25,
horizontal_flip=True,
)
test_generator = test_datagen.flow_from_directory(
    evaluate_path,
    target_size=(width, height),
    batch_size=batch_size,
    class_mode='categorical')

model.compile(optimizer=SGD(lr=0.0001, momentum=0.9),      loss='categorical_crossentropy', metrics=['accuracy'])
x = model.predict_generator(test_generator, val_samples=1)
print(x)

1 个答案:

答案 0 :(得分:2)

尝试以下功能:

from six import next

def generator_with_true_classes(model, generator):
    while True:
        x, y = next(generator)
        yield x, model.predict(x), y

它将生成原始数据y_predy_true。以下列方式使用它:

nb_of_samples = 0
nb_of_samples_to_compute = 100 # set your own value
for x, y_pred, y_true in generator_with_true_classes(model, test_generator):
    # do something with data, eg. print it.
    nb_of_samples += 1
    if nb_of_samples == nb_of_samples_to_compute:
         break