Keras评估带有回调的\u生成器

Keras评估带有回调的\u生成器,keras,Keras,我有一个keras模型,我试着用测试数据和evaluate_生成器方法对其进行测试。我有一个用例,这个方法中的回调会派上用场。在keras文档中:有一个回调参数。然而,当我用下面的代码测试时,我得到了一个错误 model = load_model('/models/model.h5') img_width = 120 img_height = 120 batch_size = 32 data_generator = ImageDataGenerator(rescale=1. / 255.)

我有一个keras模型,我试着用测试数据和evaluate_生成器方法对其进行测试。我有一个用例,这个方法中的回调会派上用场。在keras文档中:有一个回调参数。然而,当我用下面的代码测试时,我得到了一个错误

model = load_model('/models/model.h5')

img_width = 120
img_height = 120
batch_size = 32

data_generator = ImageDataGenerator(rescale=1. / 255.)

test_generator = data_generator.flow_from_directory(
        '/data/test',
        target_size=(img_width, img_height),
        batch_size=batch_size,
        class_mode='binary')

STEP_SIZE_TEST = test_generator.n // test_generator.batch_size


class TestCallback(Callback):
    def on_test_batch_begin(self, batch, logs=None):
        print('Evaluating batch: ' + str(batch))


test_callback = TestCallback()

model.evaluate_generator(test_generator, STEP_SIZE_TEST, callbacks=[test_callback])
错误:

Traceback (most recent call last):
  File "/test_callback.py", line 34, in <module>
    model.evaluate_generator(generator=test_generator, steps=STEP_SIZE_TEST, callbacks=[test_callback])
  File "/miniconda3/envs/models/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
TypeError: evaluate_generator() got an unexpected keyword argument 'callbacks'
我发现以下错误:

Exception in thread Thread-16:
Traceback (most recent call last):
  File "/miniconda3/envs/models/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/miniconda3/envs/models/lib/python3.6/threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "/miniconda3/envs/models/lib/python3.6/site-packages/keras/utils/data_utils.py", line 671, in _run
    executor.apply_async(next_sample, (self.uid,)), block=True)
  File "/miniconda3/envs/models/lib/python3.6/queue.py", line 127, in put
    if self.maxsize > 0:
TypeError: '>' not supported between instances of 'list' and 'int'

我的keras版本是2.2.4

您看到的文档是针对master branch的。2.2.4不支持“回调”参数。在2.2.4中,第三个参数仍然是max_queue_size,因此将[test_callback]列表解释为int时出错

Exception in thread Thread-16:
Traceback (most recent call last):
  File "/miniconda3/envs/models/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/miniconda3/envs/models/lib/python3.6/threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "/miniconda3/envs/models/lib/python3.6/site-packages/keras/utils/data_utils.py", line 671, in _run
    executor.apply_async(next_sample, (self.uid,)), block=True)
  File "/miniconda3/envs/models/lib/python3.6/queue.py", line 127, in put
    if self.maxsize > 0:
TypeError: '>' not supported between instances of 'list' and 'int'