简体   繁体   English

使用图像数据生成器评估 keras model

[英]Evaluate keras model with image data generator

So I have trained a Keras model using the ImageDataGenerator by calling the fit_generator method and passing it the ImageDataGenerator object.因此,我通过调用fit_generator方法并将其传递给ImageDataGenerator object,使用ImageDataGenerator训练了 Keras model。

Now I want to evaluate the model with the same ImageDataGenerator object.现在我想用相同的ImageDataGenerator object 评估 model。 But I think I'm missing something.但我想我错过了一些东西。

I have my data in 2 variables,我的数据包含在 2 个变量中,

ck_train = ImageDataGenerator().flow_from_directory(train_path, target_size=(
    224, 224), classes=['happy', 'neutral', 'surprise'], batch_size=32)
ck_test = ImageDataGenerator().flow_from_directory(test_path, target_size=(
    224, 224), classes=['happy', 'neutral', 'surprise'], batch_size=16)

I have tried to evaluate the model via我试图通过评估 model

deXpression.evaluate_generator(ck_test)

but I get this error但我得到这个错误

-----------------------------------------------------------------------
ValueError                            Traceback (most recent call last)
<ipython-input-6-0d318201cacd> in <module>
----> 1 deXpression.evaluate_generator(ck_test)

~/anaconda3/envs/gandola/lib/python3.7/site-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
     89                 warnings.warn('Update your `' + object_name + '` call to the ' +
     90                               'Keras 2 API: ' + signature, stacklevel=2)
---> 91             return func(*args, **kwargs)
     92         wrapper._original_function = func
     93         return wrapper

~/anaconda3/envs/gandola/lib/python3.7/site-packages/keras/engine/training.py in evaluate_generator(self, generator, steps, max_queue_size, workers, use_multiprocessing, verbose)
   1470             workers=workers,
   1471             use_multiprocessing=use_multiprocessing,
-> 1472             verbose=verbose)
   1473 
   1474     @interfaces.legacy_generator_methods_support

~/anaconda3/envs/gandola/lib/python3.7/site-packages/keras/engine/training_generator.py in evaluate_generator(model, generator, steps, max_queue_size, workers, use_multiprocessing, verbose)
    299             steps = len(generator)
    300         else:
--> 301             raise ValueError('`steps=None` is only valid for a generator'
    302                              ' based on the `keras.utils.Sequence` class.'
    303                              ' Please specify `steps` or use the'

ValueError: `steps=None` is only valid for a generator based on the `keras.utils.Sequence` class. Please specify `steps` or use the `keras.utils.Sequence` class.

Kindly tell me:请告诉我:

1) If I'm moving in the right direction? 1)如果我朝着正确的方向前进?
2) What am I missing if I'm? 2)如果我是,我错过了什么?
3) How can I do it by using the ImageDataGenerator object? 3) 如何使用 ImageDataGenerator object 来做到这一点?
4) What can be the proper approach to do the task I'm trying to achieve? 4) 什么是完成我想要完成的任务的正确方法?

I fixed the issue.我解决了这个问题。 The issue was of the steps parameter of model.evaluate_generator .问题在于model.evaluate_generatorsteps参数。

STEP_SIZE_TEST = ck_test.n // ck_test.batch_size
deXpression.evaluate_generator(generator=ck_test, steps=STEP_SIZE_TEST)

Maybe this way can give you an idea:也许这种方式可以给你一个想法:

train_datagen = ImageDataGenerator(
    rescale=1./255,)

test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
    train_data_dir,
    color_mode= "grayscale",
    target_size=(img_width, img_height),
    batch_size=128,
    class_mode='categorical',)    

validation_generator = test_datagen.flow_from_directory(
    validation_data_dir,
    color_mode= "grayscale",
    target_size=(img_width, img_height),
    batch_size=128,
    class_mode='categorical')

 #%%
hist = model.fit_generator(
    train_generator,
    samples_per_epoch=nb_train_samples,
    nb_epoch=nb_epoch,
    validation_data=validation_generator,
    nb_val_samples=nb_validation_samples)

scoreSeg = model.evaluate_generator(validation_generator, 400)

It seems that the method 'evaluate' supports generators directly yet, so the original approach may work nowadays.似乎“评估”方法直接支持生成器,因此原始方法现在可能有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM