-2

I wanted to do image classification using CNN and now I am getting abnormal results cause the even no of epochs are not working as expected even if i change the no of epochs

import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import tensorflow as tf
import kagglehub

data_dir = kagglehub.dataset_download("ryanholbrook/car-or-truck")


from tensorflow.keras.preprocessing.image import ImageDataGenerator


tf.random.set_seed(42)

train_datagen = ImageDataGenerator(rescale=1./255)
valid_datagen = ImageDataGenerator(rescale=1./255)


train_dir = "/root/.cache/kagglehub/datasets/ryanholbrook/car-or-truck/versions/1/train/"
test_dir = "/root/.cache/kagglehub/datasets/ryanholbrook/car-or-truck/versions/1/valid/"


train_data = train_datagen.flow_from_directory(directory = train_dir, batch_size=32, target_size=(224,224), class_mode="binary", seed=42, shuffle =True)
valid_data = valid_datagen.flow_from_directory(directory = test_dir, batch_size=32, target_size=(224,224), class_mode="binary", seed=42, shuffle =True)

model_1 = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(filters=10, kernel_size=3, input_shape=(224,224,3), activation="relu"),
    tf.keras.layers.Conv2D(10,3, activation="relu"),
    tf.keras.layers.MaxPool2D(pool_size=2, padding="valid"),
    tf.keras.layers.Conv2D(10,3, activation="relu"),
    tf.keras.layers.Conv2D(10,3, activation="relu"),
    tf.keras.layers.MaxPool2D(pool_size=2, padding="valid"),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(1, activation="sigmoid")
])

# Compile our CNN
model_1.compile(loss="binary_crossentropy", optimizer=tf.keras.optimizers.Adam(), metrics=["accuracy"])

# Fit the CNN to the data
history_1 = model_1.fit(train_data, epochs=25, steps_per_epoch=len(train_data), validation_data=valid_data, validation_steps=len(valid_data))

Getting this type of result:

Found 5117 images belonging to 2 classes.
Found 5051 images belonging to 2 classes.
Epoch 1/25
/usr/local/lib/python3.10/dist-packages/keras/src/layers/convolutional/base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.
  super().__init__(activity_regularizer=activity_regularizer, **kwargs)
/usr/local/lib/python3.10/dist-packages/keras/src/trainers/data_adapters/py_dataset_adapter.py:122: UserWarning: Your `PyDataset` class should call `super().__init__(**kwargs)` in its constructor. `**kwargs` can include `workers`, `use_multiprocessing`, `max_queue_size`. Do not pass these arguments to `fit()`, as they will be ignored.
  self._warn_if_super_not_called()
160/160 ━━━━━━━━━━━━━━━━━━━━ 18s 93ms/step - accuracy: 0.6169 - loss: 0.6532 - val_accuracy: 0.6391 - val_loss: 0.6453

Epoch 2/25
160/160 ━━━━━━━━━━━━━━━━━━━━ 0s 109us/step - accuracy: 0.0000e+00 - loss: 0.0000e+00

Epoch 3/25
/usr/lib/python3.10/contextlib.py:153: UserWarning: Your input ran out of data; interrupting training. Make sure that your dataset or generator can generate at least `steps_per_epoch * epochs` batches. You may need to use the `.repeat()` function when building your dataset.
  self.gen.throw(typ, value, traceback)
160/160 ━━━━━━━━━━━━━━━━━━━━ 17s 80ms/step - accuracy: 0.6780 - loss: 0.5921 - val_accuracy: 0.7020 - val_loss: 0.5711

Epoch 4/25
160/160 ━━━━━━━━━━━━━━━━━━━━ 4s 26ms/step - accuracy: 0.0000e+00 - loss: 0.0000e+00

Epoch 5/25
160/160 ━━━━━━━━━━━━━━━━━━━━ 13s 81ms/step - accuracy: 0.7679 - loss: 0.4824 - val_accuracy: 0.7193 - val_loss: 0.5556

Epoch 6/25
160/160 ━━━━━━━━━━━━━━━━━━━━ 4s 25ms/step - accuracy: 0.0000e+00 - loss: 0.0000e+00

Epoch 7/25
160/160 ━━━━━━━━━━━━━━━━━━━━ 18s 88ms/step - accuracy: 0.8320 - loss: 0.3964 - val_accuracy: 0.7242 - val_loss: 0.5764

Epoch 8/25
160/160 ━━━━━━━━━━━━━━━━━━━━ 4s 25ms/step - accuracy: 0.0000e+00 - loss: 0.0000e+00

Epoch 9/25
160/160 ━━━━━━━━━━━━━━━━━━━━ 15s 78ms/step - accuracy: 0.8764 - loss: 0.3101 - val_accuracy: 0.6995 - val_loss: 0.6768

Epoch 10/25
160/160 ━━━━━━━━━━━━━━━━━━━━ 0s 93us/step - accuracy: 0.0000e+00 - loss: 0.0000e+00

Epoch 11/25
160/160 ━━━━━━━━━━━━━━━━━━━━ 13s 80ms/step - accuracy: 0.9233 - loss: 0.2111 - val_accuracy: 0.7145 - val_loss: 0.7014

Epoch 12/25
160/160 ━━━━━━━━━━━━━━━━━━━━ 0s 97us/step - accuracy: 0.0000e+00 - loss: 0.0000e+00

Epoch 13/25
160/160 ━━━━━━━━━━━━━━━━━━━━ 13s 77ms/step - accuracy: 0.9491 - loss: 0.1485 - val_accuracy: 0.7123 - val_loss: 0.8927

Epoch 14/25
160/160 ━━━━━━━━━━━━━━━━━━━━ 5s 31ms/step - accuracy: 0.0000e+00 - loss: 0.0000e+00

Epoch 15/25
160/160 ━━━━━━━━━━━━━━━━━━━━ 16s 82ms/step - accuracy: 0.9814 - loss: 0.0733 - val_accuracy: 0.7104 - val_loss: 1.1305

Epoch 16/25
160/160 ━━━━━━━━━━━━━━━━━━━━ 0s 71us/step - accuracy: 0.0000e+00 - loss: 0.0000e+00

Epoch 17/25
160/160 ━━━━━━━━━━━━━━━━━━━━ 20s 80ms/step - accuracy: 0.9903 - loss: 0.0431 - val_accuracy: 0.7143 - val_loss: 1.3878

Epoch 18/25
160/160 ━━━━━━━━━━━━━━━━━━━━ 0s 98us/step - accuracy: 0.0000e+00 - loss: 0.0000e+00

Epoch 19/25
160/160 ━━━━━━━━━━━━━━━━━━━━ 20s 80ms/step - accuracy: 0.9915 - loss: 0.0323 - val_accuracy: 0.7165 - val_loss: 1.5466

Epoch 20/25
160/160 ━━━━━━━━━━━━━━━━━━━━ 4s 26ms/step - accuracy: 0.0000e+00 - loss: 0.0000e+00

Epoch 21/25
160/160 ━━━━━━━━━━━━━━━━━━━━ 15s 75ms/step - accuracy: 0.9967 - loss: 0.0177 - val_accuracy: 0.7058 - val_loss: 1.6167

Epoch 22/25
160/160 ━━━━━━━━━━━━━━━━━━━━ 0s 144us/step - accuracy: 0.0000e+00 - loss: 0.0000e+00

Epoch 23/25
160/160 ━━━━━━━━━━━━━━━━━━━━ 21s 79ms/step - accuracy: 0.9939 - loss: 0.0196 - val_accuracy: 0.7111 - val_loss: 1.7555

Epoch 24/25
160/160 ━━━━━━━━━━━━━━━━━━━━ 0s 97us/step - accuracy: 0.0000e+00 - loss: 0.0000e+00

Epoch 25/25
160/160 ━━━━━━━━━━━━━━━━━━━━ 15s 90ms/step - accuracy: 0.9942 - loss: 0.0211 - val_accuracy: 0.7076 - val_loss: 1.9112
1
  • You should fix your warnings, especially this one:" Make sure that your dataset or generator can generate at least steps_per_epoch * epochs batches." You have a batch size of 32, 25 epochs and len(train_data) as steps per epoch. That means you are immideatly running out of data. Fix this and see if this helps. Also maybe take a look here stackoverflow.com/questions/38340311/… . Commented Jan 13 at 12:14

2 Answers 2

1

The issue is about steps_per_epoch and validation_steps. When I deleted them the problem was solved.

history = model.fit(
    train_generator,
    epochs=32,
    validation_data=val_generator,
    callbacks=callbacks,
    # steps_per_epoch=train_steps,
    # validation_steps=val_steps
)

This is what it looks like. When I did this, I realized that my formula had calculated 272 but the model took it as 273 and that was the problem. I don't know why this is a problem but it worked. The formula was that:

train_steps = train_generator.samples // train_generator.batch_size
val_steps = val_generator.samples // val_generator.batch_size
Sign up to request clarification or add additional context in comments.

Comments

0

Since the model is actually changing between the odd epochs I it is more likely to be a hyperparameter issue than an architecture issue. I would try adjusting the learning rate and batch size and see if that that helps.

1 Comment

With steps_per_epochs=len(train_data) the only batch size that would work is 1. As long as this issue is not fixed there is no reason to believe that the learning rate is a problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.