2

I am trying to run a port of the SSD on a Win10 system from the ssd300_training.ipynb code in this github repo https://github.com/pierluigiferrari/ssd_keras . I am getting a "StopIteration" error when training the model and the solutions out there, with my little research, suggest adding a while True block in the data generator function. However, I am not as proficient enough to do that with the code as it wasn't written like a function format. Here's the data generator code:

# 6: Create the generator handles that will be passed to Keras' `fit_generator()` function.
train_generator = train_dataset.generate(batch_size=batch_size,
                                     shuffle=True,
                                     transformations=[ssd_data_augmentation],
                                     label_encoder=ssd_input_encoder,
                                     returns={'processed_images',
                                              'encoded_labels'},
                                     keep_images_without_gt=False)

val_generator = val_dataset.generate(batch_size=batch_size,
                                 shuffle=False,
                                 transformations=[convert_to_3_channels,
                                                  resize],
                                 label_encoder=ssd_input_encoder,
                                 returns={'processed_images',
                                          'encoded_labels'},
                                 keep_images_without_gt=False)  

Please how can I add a while True: loop to this so it can stop giving the StopIteration error. Here's the training code and the error it gives:

initial_epoch   = 0
final_epoch     = 50
steps_per_epoch = 10



history = model.fit_generator(generator=train_generator,
                          steps_per_epoch=steps_per_epoch,
                          epochs=final_epoch,
                          callbacks=callbacks,
                          validation_data=val_generator,
                          validation_steps=ceil(val_dataset_size/batch_size),
                          initial_epoch=initial_epoch
                         )

Error:

    Epoch 1/50
 9/10 [==========================>...] - ETA: 24s - loss: 3.45 - ETA: 13s - loss: 3.45 - ETA: 9s - loss: 3.4506 - ETA: 6s - loss: 3.450 - ETA: 5s - loss: 3.450 - ETA: 3s - loss: 3.450 - ETA: 2s - loss: 3.450 - ETA: 1s - loss: 3.450 - ETA: 0s - loss: 3.4504
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-16-484d98ebb6b5> in <module>()
     11                               #callbacks=callbacks,
     12                               validation_data=val_generator,
---> 13                               validation_steps=ceil(val_dataset_size/batch_size),
     14                               #initial_epoch=initial_epoch
     15                              )

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

c:\users\keboc\anaconda3\envs\tensorflow_1.8\lib\site-packages\keras\engine\training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
   1413             use_multiprocessing=use_multiprocessing,
   1414             shuffle=shuffle,
-> 1415             initial_epoch=initial_epoch)
   1416 
   1417     @interfaces.legacy_generator_methods_support

c:\users\keboc\anaconda3\envs\tensorflow_1.8\lib\site-packages\keras\engine\training_generator.py in fit_generator(model, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
    228                             val_enqueuer_gen,
    229                             validation_steps,
--> 230                             workers=0)
    231                     else:
    232                         # No need for try/except because

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

c:\users\keboc\anaconda3\envs\tensorflow_1.8\lib\site-packages\keras\engine\training.py in evaluate_generator(self, generator, steps, max_queue_size, workers, use_multiprocessing, verbose)
   1467             workers=workers,
   1468             use_multiprocessing=use_multiprocessing,
-> 1469             verbose=verbose)
   1470 
   1471     @interfaces.legacy_generator_methods_support

c:\users\keboc\anaconda3\envs\tensorflow_1.8\lib\site-packages\keras\engine\training_generator.py in evaluate_generator(model, generator, steps, max_queue_size, workers, use_multiprocessing, verbose)
    325 
    326         while steps_done < steps:
--> 327             generator_output = next(output_generator)
    328             if not hasattr(generator_output, '__len__'):
    329                 raise ValueError('Output of generator should be a tuple '

c:\users\keboc\anaconda3\envs\tensorflow_1.8\lib\site-packages\keras\utils\data_utils.py in get(self)
    783                 all_finished = all([not thread.is_alive() for thread in self._threads])
    784                 if all_finished and self.queue.empty():
--> 785                     raise StopIteration()
    786                 else:
    787                     time.sleep(self.wait_time)

StopIteration: 

Thanks for your help

kebochiG
  • 41
  • 1
  • 1
  • 3
  • Here is a similar problem where they used a whileTrue: loop to solve the problem https://stackoverflow.com/questions/46302911/what-raises-stopiteration-in-mine-keras-model-fit-generator – kebochiG Oct 30 '18 at 12:20
  • If your invocation of model.fit_generator is raising StopIteration at the same sample count everytime then double check if the generator that you've passed to the fit_generator method is producing the same amount of samples as specified in "steps_per_epoch" parameter. That was the cause of this error on my side. – Kamil Stadryniak Jun 26 '19 at 21:19

0 Answers0