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

时间:2019-11-01 07:44:27

标签: python tensorflow keras

因此,我通过调用ImageDataGenerator方法并将其传递给fit_generator对象,使用ImageDataGenerator训练了Keras模型。

现在,我想使用相同的ImageDataGenerator对象评估模型。但是我想我缺少了一些东西。

我的数据有两个变量,

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)

我试图通过评估模型

deXpression.evaluate_generator(ck_test)

但是我得到这个错误

-----------------------------------------------------------------------
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.

请告诉我:

1)如果我朝着正确的方向前进?
2)如果我想念我什么?
3)如何使用ImageDataGenerator对象来做到这一点?
4)什么是完成我要完成的任务的正确方法?

2 个答案:

答案 0 :(得分:0)

我解决了这个问题。问题出在steps的{​​{1}}参数上。

model.evaluate_generator

答案 1 :(得分:0)

也许这样可以给你一个主意:

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)