RuntimeError:自定义培训Keras_Ocr时,生成器引发StopIteration

时间:2020-09-24 02:53:24

标签: python tensorflow ocr

https://keras-ocr.readthedocs.io/en/latest/examples/end_to_end_training.html

你好!我正在遵循此模型来训练Keras_ocr模型,该模型实际上可以从图像中预测文本,数字和特殊符号。 我无法运行此代码,因为我在下面提到的部分代码中遇到问题:

data_dir=r'.'
detector_batch_size = 1
detector_basepath = os.path.join(data_dir,'detector_{datetime.datetime.now().isoformat()}')
detection_train_generator, detection_val_generator, detection_test_generator = [
    detector.get_batch_generator(
        image_generator=image_generator,
        batch_size=detector_batch_size
    ) for image_generator in image_generators
]

detector.model.fit_generator(
    generator=detection_train_generator,
    steps_per_epoch=math.ceil(len(background_splits[0]) / detector_batch_size),
    epochs=1000,
    workers=0,
    callbacks=[
        tf.keras.callbacks.EarlyStopping(restore_best_weights=True, patience=5),
        tf.keras.callbacks.CSVLogger('f{detector_basepath}.csv'),
        tf.keras.callbacks.ModelCheckpoint(filepath=f'{detector_basepath}.h5')
    ],
    validation_data=detection_val_generator,
    validation_steps=math.ceil(len(background_splits[1]) / detector_batch_size)
)

该代码中出现以下错误:

Epoch 1/1000
Traceback (most recent call last):

  File "C:\Anaconda3\lib\site-packages\keras_ocr\detection.py", line 633, in get_batch_generator
    batch = [next(image_generator) for n in range(batch_size)]

  File "C:\Anaconda3\lib\site-packages\keras_ocr\detection.py", line 633, in <listcomp>
    batch = [next(image_generator) for n in range(batch_size)]

StopIteration


The above exception was the direct cause of the following exception:

Traceback (most recent call last):

  File "D:\FYP Proposal\Generate Fonts and Backgrounds\Keras_Ocr_Training.py", line 117, in <module>
    validation_steps=math.ceil(len(background_splits[1]) / detector_batch_size)

  File "C:\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 1297, in fit_generator
    steps_name='steps_per_epoch')

  File "C:\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training_generator.py", line 221, in model_iteration
    batch_data = _get_next_batch(generator)

  File "C:\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training_generator.py", line 363, in _get_next_batch
    generator_output = next(generator)

RuntimeError: generator raised StopIteration

任何人都可以帮助我解决此错误,谢谢。

0 个答案:

没有答案