Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
24 views

ML Lab Session 05 - CNN Implementation

This document provides a 7-step reference for implementing a convolutional neural network (CNN) in TensorFlow Keras for a binary image classification problem of cats vs dogs. The CNN model is built with convolutional and max pooling layers, followed by dense layers. It is trained on augmented image data with hyperparameters like batch size and epochs. The trained model is then used to predict the class of new test images.

Uploaded by

chatgptlogin2001
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

ML Lab Session 05 - CNN Implementation

This document provides a 7-step reference for implementing a convolutional neural network (CNN) in TensorFlow Keras for a binary image classification problem of cats vs dogs. The CNN model is built with convolutional and max pooling layers, followed by dense layers. It is trained on augmented image data with hyperparameters like batch size and epochs. The trained model is then used to predict the class of new test images.

Uploaded by

chatgptlogin2001
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

UNIVERSITY OF SRI JAYEWARDENEPURA

Faculty of Technology
Department of Information and Communication Technology
ITC2252_ Introduction to Machine Learning

Lab Session 05 – CNN Implementation reference


1.

● import numpy as np: This imports the NumPy library for numerical computations and array
operations. It's given the shorthand alias np for easier use.
● import random: This imports the random library for generating random numbers.
● import matplotlib.pyplot as plt: This imports the Matplotlib library for creating visualizations
and plots. It's given the alias plt.
● from tensorflow.keras.models import Sequential: This imports the Sequential model class from
the TensorFlow Keras library, which is used for building neural networks.
● from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Flatten: This imports
several layers commonly used in convolutional neural networks (CNNs) from TensorFlow Keras:
1. Conv2D: Convolutional layer for extracting features from images.
2. MaxPooling2D: Downsampling layer for reducing dimensionality.
3. Dense: Fully connected layer for final classification or regression.
4. Flatten: Layer for reshaping multidimensional data into a single vector.
2.

● ImageDataGenerator is a class in Keras that helps generate augmented images for training neural
networks.
● rescale=1./255: Scales the pixel values of the images to be between 0 and 1. This is a standard
preprocessing step for neural networks.
● shear_range=0.2: Introduces shearing transformations, which slant the image in a specified
direction.
● zoom_range=0.2: Applies random zooming to the images.
● horizontal_flip=True: Allows horizontal flipping of the images.

3.

● train_datagen.flow_from_directory: Creates a data generator for training images. It reads images


from the 'dataset/train' directory, resizes them to (64, 64) pixels, processes them with specified
augmentation techniques, and assigns binary labels (class_mode='binary').
● test_datagen.flow_from_directory: Similar to the training generator, it creates a data generator for
testing images from the 'dataset/test' directory.
● target_size=(64, 64): Resizes the input images to have a height and width of 64 pixels.
● batch_size=32: Divides the dataset into batches of 32 images each during training and testing.
● class_mode='binary': Specifies that the classification task is binary (cat or dog in this case).
4.

● Sequential(): Initializes a sequential model, which is a linear stack of layers.


● Conv2D(32, (3, 3), input_shape=(64, 64, 3), activation='relu'): Adds a convolutional layer with 32
filters, each of size (3, 3), using the ReLU activation function. The input shape is set to (64, 64, 3),
indicating images of size 64x64 pixels with three color channels (RGB).
● MaxPooling2D(pool_size=(2, 2)): Adds a max-pooling layer with a pool size of (2, 2). This layer
reduces the spatial dimensions of the input.
● Conv2D(64, (3, 3), activation='relu'): Adds another convolutional layer with 64 filters and ReLU
activation.
● MaxPooling2D(pool_size=(2, 2)): Adds another max-pooling layer.
● Flatten(): Flattens the output from the previous layer into a one-dimensional vector.
● Dense(units=128, activation='relu'): Adds a fully connected (dense) layer with 128 units and ReLU
activation.
● Dense(units=1, activation='sigmoid'): Adds the output layer with a single unit and sigmoid
activation, suitable for binary classification tasks.

5.

● model.compile: Configures the model for training.


● optimizer='adam': Specifies the Adam optimizer, a popular optimization algorithm.
● loss='binary_crossentropy': Sets the loss function to binary cross-entropy, which is commonly used
for binary classification problems.
● metrics=['accuracy']: Evaluates the model based on accuracy during training.

6.

● model.fit(training_set, epochs=25, validation_data=test_set): Initiates the training process.


● training_set: The generator for training data created using ImageDataGenerator.
● epochs=25: Specifies the number of epochs or passes through the entire training dataset during
training.
● validation_data=test_set: Uses the test set for validation during training to monitor model
performance on unseen data.
7.

● image.load_img('path_to_test_image.jpg', target_size=(64, 64)): Loads and resizes the test image


to the required input size (64x64 pixels).
● image.img_to_array(test_image): Converts the image to a NumPy array.
● np.expand_dims(test_image, axis=0): Adds an extra dimension to the array to match the model's
expected input shape.
● result = model.predict(test_image): Uses the trained model to predict the class probabilities for the
test image.
● if result[0][0] == 1: prediction = 'dog' else: prediction = 'cat': Determines the predicted class based
on the model output. If the predicted probability is close to 1, it's classified as a dog; otherwise, it's
classified as a cat.
● plt.imshow(image.load_img('path_to_test_image.jpg')): Displays the original test image.
● plt.title(f'Prediction: {prediction}'): Sets the title of the displayed image to include the predicted
class.
● plt.show(): Shows the image along with the predicted class label.

You might also like