我有一个keras模型,我尝试用测试数据和评价方法来测试它。我有一个用例,该方法中的回调会派上用场。在keras文档中:evaluate_generator中有一个回调参数。但是,当我用以下代码测试时,我得到一个错误。
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'
当我编辑代码并像这样省略关键字时:
model.evaluate_generator(test_generator, STEP_SIZE_TEST, [test_callback])
我收到以下错误:
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
答案 0 :(得分:2)
您看到的文档适用于master分支。 2.2.4不支持'callbacks'参数。在2.2.4中,第三个参数仍然是max_queue_size,因此在将[test_callback]列表解释为int时会出现错误。