Mayur Goyal
Mayur Goyal

Reputation: 13

Model works fine in Keras but not in Tensorflow

While working with Tensorflow, After fitting the model, in the first epoch it shows unknown (1/unknown) but when using only keras it works fine. What is the problem or am I doing something wrong

Tensorflow code:-

import tensorFlow as tf
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255,
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip = True)
training_set = train_datagen.flow_from_directory('dataset/train',
                                                 target_size = (150, 150),
                                                 batch_size = 10,
                                                 class_mode = 'binary')
test_datagen = ImageDataGenerator(rescale = 1./255)
test_set = test_datagen.flow_from_directory('dataset/test',
                                            target_size = (150, 150),
                                            batch_size = 10,
                                            class_mode = 'binary')
cnn=tf.keras.models.Sequential()
cnn.add(tf.keras.layers.Conv2D(filters=100,kernel_size=3,activation='relu',input_shape=(150,150,3)))
cnn.add(tf.keras.layers.MaxPool2D(pool_size=2))
cnn.add(tf.keras.layers.Conv2D(filters=100,kernel_size=3,activation='relu',))
cnn.add(tf.keras.layers.MaxPool2D(pool_size=2))
cnn.add(tf.keras.layers.Flatten())

cnn.add(tf.keras.layers.Dense(units=50, activation='relu'))
cnn.add(tf.keras.layers.Dense(units=2, activation='softmax'))
cnn.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
cnn.fit(x=training_set,validation_data=test_set,epochs=10)

It shows

 Epoch 1/10
    28/Unknown - 12s 416ms/step - loss: 0.9420 - accuracy: 0.4964

But after removing tf from every line it works fine

Upvotes: 1

Views: 776

Answers (1)

user11530462
user11530462

Reputation:

Since you are ImageDataGenerator , the argument, steps_per_epoch is mandatory in cnn.fit while using tf.keras (not sure how it is implemented in native keras)

In the Arguments section of the Tensorflow Documentation for model.fit it states:

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.

So, if you replace

cnn.fit(x=training_set,validation_data=test_set,epochs=10)

with

batch_size = 20
No_Of_Training_Images = Train_Generator.classes.shape[0]
steps_per_epoch = No_Of_Training_Images/batch_size
cnn.fit(x=training_set,validation_data=test_set,epochs=10, 
steps_per_epoch = steps_per_epoch)

the Output will be like:

Epoch 1/10
    28/100 - 12s 416ms/step - loss: 0.9420 - accuracy: 0.4964

Upvotes: 1

Related Questions