Search code examples
tensorflowtransfer-learningmobilenet

Transfer Learning with Tensorflow (MobileNet)


In Transfer learning, I think my model.fit_generator goes in an infinite loop. I don't know-how. Here is my Colab notebook link https://colab.research.google.com/drive/1o9GNCQdMeh4HZdiZ5QAjiDDkixn-OsXx

Here is the image of model.fit_generator


Solution

  • If you update the last line as follows then it takes around 40 seconds for 5 epochs.

    from

    model.fit_generator(train_generator, epochs=5, validation_data=valid_generator)
    

    to

    model.fit_generator(train_generator, epochs=5, steps_per_epoch=len(train_generator), validation_data=valid_generator, validation_steps=len(valid_generator))
    

    Please check the description of what is expected when the input to model.fit is a generator. So when 'steps_per_epoch' is None, the epoch will run until the input dataset is exhausted. So the generator running infinitely if your dataset is infinitely repeating dataset.

    steps_per_epoch: Integer or None. Total number of steps (batches of samples) before declaring one epoch finished and starting the next epoch. When training with input tensors such as TensorFlow data tensors, the default None is equal to the number of samples in your dataset divided by the batch size, or 1 if that cannot be determined. If x is a tf.data dataset, and 'steps_per_epoch' is None, the epoch will run until the input dataset is exhausted. When passing an infinitely repeating dataset, you must specify the steps_per_epoch argument. This argument is not supported with array inputs.