Image

Complete CNN Image Classification Models for Real Time Prediction

In this project we cover the topic of Convolutional Neural Networks (CNN) and using these models how we can classify images into different categories. CNNs are one of the most common methods used for visual data analysis as they can pick up on complex trends and features. The project's goal is to teach people everything they need to know about CNNs and related ideas while also showing them how to use a CNN model for image classification. It makes possible to predict in real time which gives us useful information about how CNN can be used.

Explanation All Code

Step 1:

You can mount your Google Drive in a Google Colab notebook with this piece of code. This makes it easy to view files stored in Google Drive for data manipulation, analysis, and training models in the Colab environment.

from google.colab import drive
drive.mount('/content/drive')
Install required packages
!pip install numpy
!pip install keras
!pip install tensorflow
!pip install matplotlib

Import required packages

import numpy as np
import pathlib
from tensorflow import keras
from tensorflow.keras import layers
import PIL
import tensorflow as tf
from tensorflow.keras.models import Sequential

Step 2:

Load the data

data_dir = pathlib.Path("/content/drive/MyDrive/Aionlinecourse/dataset/training")
image_count = len(list(data_dir.glob('*/*')))
print(image_count)

This block of code makes lists of image file paths in the "buildings" and "forest" subdirectories of the directory 'data_dir' that was given. It finds files that fit certain patterns using the "glob" method.

buildings = list(data_dir.glob('buildings/*'))
forest = list(data_dir.glob('forest/*'))
PIL.Image.open(str(buildings[2]))
PIL.Image.open(str(forest[10]))

This block of code tells the computer what size image size must use for preprocessing images while the model is being trained and sets the batch size to 32.

batch_size = 32
img_height = 180
img_width = 180
train_ds = tf.keras.utils.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="training",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)

This block of code takes the directory name ('data_dir') and makes a TensorFlow image dataset called 'val_ds' for validation. It sets the image size to 180x180 pixels, uses a batch size of 32, and sets aside 20% of the data for validation.

val_ds = tf.keras.utils.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="validation",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)

This code gets the names of the classes from the "train_ds" dataset and prints them out. The different labels or groups in the dataset are shown by their class names. In this case, "buildings" and "forest" are class names.

class_names = train_ds.class_names
print(class_names)

Step 3:

This code shows the first set of images and labels from the ('train_ds') dataset. It sets up a 3x3 grid of subplots and shows the first 9 images with the class names of those images as titles. The plot is shown with matplotlib.

import matplotlib.pyplot as plt
# Create a new figure with a specified size
plt.figure(figsize=(10, 10))
# Loop through the first batch of images and labels in the training dataset
for images, labels in train_ds.take(1):
# Iterate through the first 9 images in the batch
for i in range(9):
# Create a subplot within the 3x3 grid
ax = plt.subplot(3, 3, i + 1)
# Display the image as a plot and convert it to uint8 format
plt.imshow(images[i].numpy().astype("uint8"))
# Set the title of the subplot to the corresponding class name
plt.title(class_names[labels[i]])
# Turn off the axis to remove axis labels
plt.axis("off")
plt.show()

This code goes through the training dataset's "train_ds" groups of images and names one by one. It prints the image batch shape and the label batch shape for each batch. It confirms that only the information for the first batch is printed by the "break" line.

for image_batch, labels_batch in train_ds:
print(image_batch.shape)
print(labels_batch.shape)
break

The training and validation datasets ('train_ds' and 'val_ds') are loaded and preprocessed more efficiently with TensorFlow's AUTOTUNE feature by this code. It stores datasets in memory, mixes up the training dataset, and gets data batches ahead of time to improve training performance.

AUTOTUNE = tf.data.AUTOTUNE
train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)

This code makes a rescaling layer called "normalization_layer." It divides picture pixel values by 255 to bring them back to the range [0, 1]. This Normalization method is often used to make neural network models work better and faster.

normalization_layer = layers.Rescaling(1./255)

The rescaling layer ('normalization_layer') and the'map' function are used in this code to set the pixel values of images in the training dataset ('train_ds') to the range [0, 1]. Then, it prints the lowest and highest pixel values of the first group of normalised images to make sure the normalisation worked.

normalized_ds = train_ds.map(lambda x, y: (normalization_layer(x), y))
image_batch, labels_batch = next(iter(normalized_ds))
first_image = image_batch[0]
# Notice the pixel values are now in `[0,1]`.
print(np.min(first_image), np.max(first_image))

Step 4:

Using Keras Sequential CNN, you can resize, convolute, pool, smooth, and classify data. Normalization is done by rescaling, features are extracted by convolutions, dimensions are shrunk by pooling, the image is flattened to 1D, and robust layers classify using ReLU activation. The output layer uses softmax to send multiple classes of data.

num_classes = len(class_names)
model = Sequential([
layers.Rescaling(1./255, input_shape=(img_height, img_width, 3)),
layers.Conv2D(16, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(32, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(64, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Flatten(),
layers.Dense(128, activation='relu'),
layers.Dense(num_classes)
])
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
model.summary()
The neural network model ('model') is trained using the training dataset ('train_ds'), and its success is checked on the validation dataset ('val_ds'). The number of training epochs sets to 10 and uses the "history" variable to store the training information.
epochs=10
history = model.fit(
train_ds,
validation_data=val_ds,
epochs=epochs
)

This block of code generates a graph of the accuracy during training and validation, as well as the loss during training and validation, over the training epochs. It gets the accuracy and loss numbers for both the training and validation sets from the training history ('history'). The plots help you see how well the model is doing and find problems like overfitting or underfitting.

acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs_range = range(epochs)
plt.figure(figsize=(8, 8))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')
plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()

Using the Keras Sequential API, this code sets up a data enhancement process. It lets you randomly flip and rotate images horizontally up to 10%, and zoom them randomly up to 10%. These methods are added to the input images to make the training data more varied and improve how well the model generalizes.

data_augmentation = keras.Sequential(
[
layers.RandomFlip("horizontal",
input_shape=(img_height,
img_width,
3)),
layers.RandomRotation(0.1),
layers.RandomZoom(0.1),
]
)
plt.figure(figsize=(10, 10))
for images, _ in train_ds.take(1):
for i in range(9):
augmented_images = data_augmentation(images)
ax = plt.subplot(3, 3, i + 1)
plt.imshow(augmented_images[0].numpy().astype("uint8"))
plt.axis("off")

Step 5:

Using the Keras Sequential API, this code sets up a neural network model. It has levels for adding more data, rescaling, convolutional, max pooling, dropout, flattening, and making the data dense. Adding more training data makes it more diverse, rescaling makes pixel values more uniform and neural layers pull out features. Dropout stops overfitting, max pooling cuts down on dimensions, and thick layers with ReLU activation and softmax output handle classification.

model = Sequential([
data_augmentation,
layers.Rescaling(1./255),
layers.Conv2D(16, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(32, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(64, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Dropout(0.2),
layers.Flatten(),
layers.Dense(128, activation='relu'),
layers.Dense(num_classes)
])
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
model.summary()

The neural network model ('model') is trained using the training dataset ('train_ds'), and its success is checked on the validation dataset ('val_ds'). It sets 10 as the number of training epochs and uses the "history" variable to store the training information.

epochs = 10
history = model.fit(
train_ds,
validation_data=val_ds,
epochs=epochs
)
model.save("cnn-model.h5")
model = tf.keras.models.load_model("/content/cnn-model.h5")

This code makes a graph of the accuracy during training and validation, as well as the loss during training and validation, over the training epochs. It gets the accuracy and loss result for both the training and validation sets from the training history ('history'). The plots help you see how well the model is doing and find problems like overfitting or underfitting.

acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs_range = range(epochs)
plt.figure(figsize=(8, 8))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')
plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()

Prediction 

This block of code finds the area that has the product's test data. There is a test data directory named "/content/drive/MyDrive/Aionlinecourse/dataset/test" which is attached.

test_data_dir = pathlib.Path("/content/drive/MyDrive/Aionlinecourse/dataset/test")
image_count = len(list(test_data_dir.glob('*/*')))
print(image_count)
test_buildings = list(test_data_dir.glob('buildings/*'))
test_fotest = list(test_data_dir.glob('forest/*'))
test_ds = tf.keras.utils.image_dataset_from_directory(
test_data_dir,
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 10))
for images, labels in test_ds.take(1):
for i in range(9):
ax = plt.subplot(3, 3, i + 1)
plt.imshow(images[i].numpy().astype("uint8"))
plt.title(class_names[labels[i]])
plt.axis("off")

Step 6:

This code checks the neural network model (named "model") on the test dataset (named "test_ds"). It shows the model’s performance on test data by calculating the loss and accuracy result.

model.evaluate(test_ds)


This code gets the image from "/content/drive/MyDrive/Aionlinecourse/dataset/prediction/18733.jpg" and changes its dimensions to the ones you give it (img_height, img_width). Then, it turns the image into a NumPy array and makes it bigger so that it can be used for prediction.

img = tf.keras.utils.load_img(
"/content/drive/MyDrive/Aionlinecourse/dataset/prediction/18733.jpg", target_size=(img_height, img_width)
)
img_array = tf.keras.utils.img_to_array(img)
img_array = tf.expand_dims(img_array, 0) # Create a batch
predictions = model.predict(img_array)
score = tf.nn.softmax(predictions[0])
print(
"This image most likely belongs to {} with a {:.2f} percent confidence."
.format(class_names[np.argmax(score)], 100 * np.max(score))
)

Conclusion

Finally, this project has taught you everything you need to know about Convolutional Neural Networks (CNNs) and how they can be used to sort images into different categories. Learners have gained useful knowledge and hands-on experience in this area by learning CNN architecture and related ideas and putting together a CNN model for classifying images. The model has also been shown to work well in real-life situations by being tested for real-time predicting tasks. Going forward, more research and testing with CNNs can lead to better understanding and progress in image analysis, which can help many different fields and businesses. Overall, this project is a good starting point for more learning and research in the interesting area of CNNs and computer vision.

Code Editor