0
$\begingroup$

When i run my code with 5 epochs, code gets stuck at first epoch and run continuesly. I tried applying various parameters but couldn't make it.

here is my code...

import tensorflow as tf
import numpy as np
import os
from google.colab import drive
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow import keras
from tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D

training_path = 'drive/My Drive/tesla'
validation_path = 'drive/My Drive/validation'

training_generator = ImageDataGenerator(rescale=1.0/255)
validation_generator = ImageDataGenerator(rescale=1.0/255)

training_set = training_generator.flow_from_directory(batch_size=2,
                                                      directory=training_path,
                                                      target_size=(150,150),
                                                      class_mode='binary',
                                                      shuffle=True)

validation_set = validation_generator.flow_from_directory(batch_size=2,
                                                          directory=validation_path,
                                                          target_size=(150,150),

                                                          class_mode='binary')

model = keras.models.Sequential([
                                 Conv2D(8, 3, padding='same', activation='relu', input_shape=(150,150,3)),
                                 MaxPooling2D(),
                                 Conv2D(16, 3, padding='same', activation='relu'),
                                 MaxPooling2D(),
                                 Conv2D(16, 3, padding='same', activation='relu'),
                                 MaxPooling2D(),
                                 Conv2D(4, 3, activation='relu'),
                                 Flatten(),
                                 Dense(4, activation='relu'),
                                 Dense(1)

])

model.compile(optimizer='adam',
              loss=tf.losses.BinaryCrossentropy(from_logits=True),
              metrics=['accuracy'])

model.fit(training_set,
          validation_data=validation_set,
          epochs=5,
          verbose=1,
          validation_steps=20/2)

but when i run my code, it shows this and run non stop at first epoch.

Epoch 1/5
  14781/Unknown - 630s 43ms/step - loss: 4.0421e-06 - accuracy: 1.0000

Although my training sample size is only 40 (20 cats and 20 dogs) and validation sample size is 20 (10 cats and 10 dogs). I am coding in google colab.

Thanks in advance.

$\endgroup$
2
  • $\begingroup$ Check for the images once, check for the consistency of the formats. For testing, try reading them in OpenCV and see if they are getting read properly. Sometimes some images get corrupted for one reason or another. So if there is any corruption, you might want to make a copy of the image and use that instead of the original. $\endgroup$ May 14 '20 at 4:41
  • $\begingroup$ 14781/Unknown .This value should not go above 20 in your case as your batch size is 2 and the image count is 40. Seems It's something wrong with the directory $\endgroup$
    – 10xAI
    May 14 '20 at 11:25
0
$\begingroup$

You are missing one argument in your code. Following will fix your problem.

model.fit(training_set,
          training_steps= training_set.samples//training_set.batch_size,
          validation_data=validation_set,
          epochs=5,
          verbose=1,
          validation_steps=validation_set.samples//validation_set.batch_size)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.