I was playing with this function from tf.data called tf.data.Dataset.from_generator() where it takes a ImageDataGenerator object and turns it into a Dataset Object. Everything was fine but when I fit my model I can’t use steps_per_epoch and validation_steps including them throws an error,
Then I commented them out and continued to fit the model but its training for infinitely seems there is no stopping. I have attached my images. (edited)
When I use .from_generator it automatically converts from ImageDatagenerator to a Dataset object but not sure why it’s still considering it as a generator and throwing error.
I also checked for similar Issues here (keyword: tf.data.Dataset.from_generator - Issues · tensorflow/tensorflow · GitHub) - in case someone has encountered a similar issue.
You will see that cardinality is -2 that is tf.data.experimental.UNKNOWN_CARDINALITY.
So it can’t quickly estimate the cardinality on datasets using from_generator
Thanks a lot, Bhack with your help I was able to fix it.
It seems during the conversion of the generator to the dataset object length of the dataset is unknown and infinite. By using the tf.data.experimental.cardinality() we can get the number of samples in our dataset.
Like as I said before during the conversion the length is infinite and unknown so it will return -2 .
We can fix this by entering the number of samples explicitly in our dataset by using tf.data.experimental.assert_cardinality(num_of_samples) and now we can even use the len() function.
I have shared the link to the complete notebook here Google Colab