0% found this document useful (0 votes)
21 views69 pages

DL Lab Record 1

The document outlines the implementation of three deep learning projects: XOR problem using a Deep Neural Network (DNN), digit recognition using Convolutional Neural Networks (CNN), and face recognition using CNN. Each project includes an aim, algorithm steps, and code snippets demonstrating the model creation, training, and evaluation processes. The results indicate successful implementations for all three tasks, with specific accuracy metrics provided for the digit and face recognition tasks.

Uploaded by

mohan prabhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views69 pages

DL Lab Record 1

The document outlines the implementation of three deep learning projects: XOR problem using a Deep Neural Network (DNN), digit recognition using Convolutional Neural Networks (CNN), and face recognition using CNN. Each project includes an aim, algorithm steps, and code snippets demonstrating the model creation, training, and evaluation processes. The results indicate successful implementations for all three tasks, with specific accuracy metrics provided for the digit and face recognition tasks.

Uploaded by

mohan prabhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 69

EX.NO.

1 SOLVING XOR USING DNN


AIM:

Implement XOR using Deep Neural Network.

ALGORITHM:

Step 1: Import Required Libraries.

Step 2: Create the input data (X) and the corresponding output data (y) for the XOR problem.

Step 3: Initialize a sequential model and add layers to it. For the XOR problem, a simple architecture
with one hidden layer is sufficient.

Step 4: Compile the Model by using Adam optimizer, Binary Crossentropy loss function, and metrics to
evaluate during training.

Step 5: Fit the model on the training data. You can specify the number of epochs (iterations over the
entire dataset) and set verbosity to control output during training.

Step 6: After training, evaluate the model's performance on the same dataset to see how well it learned.

Step 7: Use the trained model to make predictions on the input data. Round off predictions to get binary
outputs.

DESCRIPTION:

Among various logical gates, the XOR or also known as the “exclusive or” problem is one of the logical
operations when performed on binary inputs that yield output for different combinations of input, and
for the same combination of input no output is produced. The outputs generated by the XOR logic are
not linearly separable in the hyperplane.

Let us try to understand the XOR operating logic using a truth table.

From the below truth table it can be inferred that XOR produces an output for different states of inputs
and for the same inputs the XOR logic does not produce any output. The Output of XOR logic is yielded
by the equation as shown below.

X Y Output

0 0 0

0 1 1

1 0 1

1 1 0
Output= X.Y’+X’.Y

The XOR gate can be usually termed as a combination of NOT and AND gates and this type of logic finds
its vast application in cryptography and fault tolerance.

1
Linear separability of points is the ability to classify the data points in the hyperplane by avoiding the
overlapping of the classes in the planes. Each of the classes should fall above or below the separating
line and then they are termed as linearly separable data points. With respect to logical gates operations
like AND or OR the outputs generated by this logic are linearly separable in the hyperplane

The linear separable data points appear to be as shown below.

So here we can see that the pink dots and red triangle points in the plot do not overlap each other and
the linear line is easily separating the two classes where the upper boundary of the plot can be
considered as one classification and the below region can be considered as the other region of
classification.

SOLVING XOR USING DNN:

import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
# Define the XOR input and output data
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([[0], [1], [1], [0]])
# Build the neural network model
model = Sequential()
model.add(Dense(2, input_dim=2, activation='relu')) # Hidden layer with 2 neurons
model.add(Dense(1, activation='sigmoid')) # Output layer with 1 neuron
# Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# Train the model
model.fit(X, y, epochs=10000, verbose=0)
# Evaluate the model
_, accuracy = model.evaluate(X, y)
print(f"Accuracy: {accuracy * 100:.2f}%")
# Make predictions
predictions = model.predict(X)
predictions = np.round(predictions).astype(int)print("Predictions:")
for i in range(len(X)):
print(f"Input: {X[i]} => Predicted Output: {predictions[i]}, Actual Output: {y[i]}")

2
OUTPUT:

1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 168ms/step - accuracy: 0.5000 - loss: 0.6931

Accuracy: 50.00%

1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 83ms/step

Predictions:

Input: [0 0] => Predicted Output: [0], Actual Output: [0]

Input: [0 1] => Predicted Output: [0], Actual Output: [1]

Input: [1 0] => Predicted Output: [0], Actual Output: [1]

Input: [1 1] => Predicted Output: [0], Actual Output: [0]

RESULT:

Implementation of Digit Recognition was done successfully.

3
EX.NO.2 DIGIT RECOGNITION USING CNN

AIM:
Implement Digit Recognition using CNN.

ALGORITHM:

Step 1: Import Required Libraries

Step 2: Load the MNIST dataset, which consists of handwritten digits, and preprocess the data.

Step 3: Create a sequential model and add convolutional and pooling layers

Step 4: Set up the model for training by using Categorical crossentropy loss function, Adam optimizer,
and evaluation metrics.

Step 5: Fit the model on the training data while validating on a subset of it.

Step 6: Assess model performance on the test dataset to determine accuracy.

Step 7: Visualize some predictions to see how well your model performs on individual test images

DESCRIPTION:

Digit recognition using Convolutional Neural Networks (CNNs) is a common task in the field of computer
vision and is often used as a stepping stone for more complex image recognition tasks. In this response,
I'll provide you with a high-level overview of the steps involved in building a digit recognition system
using CNNs.

1. Dataset: To train and test your CNN model for digit recognition, you need a dataset of labeled
digit images. The MNIST dataset is a popular choice, which contains 28x28 grayscale images of
handwritten digits (0-9). You can also explore more challenging datasets like the Fashion MNIST
or SVHN (Street View House Numbers).
2. Preprocessing: Preprocess your dataset to make it suitable for training. Common preprocessing
steps include resizing the images to a consistent size, normalizing pixel values to a range
between 0 and 1, and splitting the dataset into training and testing sets.
3. Architecture Design: Design your CNN architecture. A simple architecture might consist of:
• Convolutional layers: These layers use convolutional filters to learn spatial features in
the images.
• Pooling layers: Pooling layers downsample the feature maps, reducing
computational load.
• Fully connected layers: These layers take the flattened output of the convolutional layers
and make predictions.

4
Hand Digit Recognition CNN:
import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt
%matplotlib inline
from tensorflow.keras import layers
(X_train, y_train) , (X_test, y_test) = keras.datasets.mnist.load_data()
X_train = X_train / 255
X_test = X_test / 255
num_class=10
input_shape=(28,28,1)
y_train=keras.utils.to_categorical(y_train,num_class)
y_test=keras.utils.to_categorical(y_test,num_class)
model=keras.Sequential([
keras.Input(shape=input_shape),
layers.Conv2D(32,kernel_size=(3,3),activation="relu"),
layers.MaxPooling2D(pool_size=(2,2)),
layers.Conv2D(64,kernel_size=(3,3),activation="relu"),
layers.MaxPooling2D(pool_size=(2,2)),
layers.Flatten(),
keras.layers.Dense(64, activation='relu'),
keras.layers.Dense(num_class, activation='softmax')
])
model.summary()

batch_size=128
epochs=15
model.compile(loss="categorical_crossentropy",optimizer="adam",metrics=["accuracy"])
model.fit(X_train,y_train,batch_size=batch_size,epochs=epochs,validation_split=0.1)

5
OUTPUT:

Epoch 1/15

422/422 ━━━━━━━━━━━━━━━━━━━━ 32s 72ms/step - accuracy: 0.8404 - loss:


0.5671 - val_accuracy: 0.9813 - val_loss: 0.0692

Epoch 2/15

422/422 ━━━━━━━━━━━━━━━━━━━━ 29s 69ms/step - accuracy: 0.9770 - loss:


0.0727 - val_accuracy: 0.9847 - val_loss: 0.0560

Epoch 3/15

422/422 ━━━━━━━━━━━━━━━━━━━━ 29s 69ms/step - accuracy: 0.9848 - loss:


0.0496 - val_accuracy: 0.9875 - val_loss: 0.0466

Epoch 15/15

422/422 ━━━━━━━━━━━━━━━━━━━━ 43s 74ms/step - accuracy: 0.9974 - loss:


0.0068 - val_accuracy: 0.9908 - val_loss: 0.0513

score=model.evaluate(X_test,y_test,verbose=0)

score

score[1]

0.9901999831199646

RESULT:

Implementation of Digit Recognition was done successfully.

6
EX.NO.3 FACE RECOGNITION USING CNN
AIM:

Implement Face Recognition using CNN.

ALGORITHM:

Step 1: Import Required Libraries

Step 2: Load and Preprocess the Dataset

Load the dataset and preprocess it for training.

a. Load Dataset:Use np.load() to load the dataset containing images and labels.
b. Normalize Images: Convert image pixel values to float32 and normalize them to the range [0, 1].
c. Load Labels:Extract training and testing labels from the dataset.
d. Display Data Format:Print shapes of training and testing data for verification.

Step 3: Split the Training Data for Validation

Split the training data into training and validation sets.

a. Split Data:Use train_test_split() to create a validation set from the training data.
b. Reshape Images:Reshape images to include a channel dimension suitable for CNN input.

Step 4: Build the CNN Model

Create a sequential CNN model with convolutional layers followed by pooling layers and dense layers.

a. Initialize Model:Create a sequential model using Sequential().


b. Add Layers:
i. Add convolutional layers with ReLU activation.
ii. Add max pooling layers to reduce dimensionality.
iii. Flatten the output before connecting to dense layers.
iv. Add dropout layers to prevent overfitting.
v. Add a final dense layer with softmax activation for classification.Step 5: Set up
the model for training by specifying loss function, optimizer, and evaluation
metrics.

Step 5: Compile the Model : Set up the model for training by specifying loss function, optimizer, and
evaluation metrics.

Step 6: Train the Model

Fit the model on the training data while validating on a separate validation set.

a. Set Training Parameters:Define batch size and number of epochs.


b. Train Model:Use model.fit() to train the model on training data with validation split.

Step 7: Evaluate the Model by Assess model performance on the test dataset to determine accuracy and
generate reports.

Step 8: Visualize some predictions or loss/accuracy curves during training to analyze performance
visually.

7
DESCRIPTION:

Face recognition using Convolutional Neural Networks (CNNs) is a popular application of deep learning
in computer vision. Below are the steps to build a face recognition system using CNNs:

1. Dataset: To create a face recognition system, you need a dataset of labeled face images. Common
datasets include LFW (Labeled Faces in the Wild), VGGFace, and CASIA WebFace. If you have a
specific use case or want to recognize a particular set of faces, you may need to create a custom
dataset.
2. Preprocessing: Preprocess your face dataset. Common preprocessing steps include resizing
images to a consistent size, normalizing pixel values, and augmenting the data (e.g., adding noise,
flipping, or rotating images). Additionally, face detection and alignment can be essential to
ensure that faces are consistently positioned and aligned.
3. Architecture Design: Design your CNN architecture. For face recognition, a common approach
is to use a Siamese network or a Triplet network. These networks are designed to learn
embeddings that represent faces in a way that makes them suitable for comparison. Key
components include:
• Base CNN: This part of the network extracts features from face images.
• Embedding Layer: It projects the features into an embedding space where similar faces
are closer and dissimilar faces are farther apart.

FACE REGONIGTION USING CNN CODE:

import keras
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, Dropout
from keras.optimizers import Adam
from keras.callbacks import TensorBoard
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
from sklearn.metrics import roc_curve, auc
from sklearn.metrics import accuracy_score
#from keras.utils import np_utils
import itertools
#load dataset
data=np.load('E:/14_LABS/5_Deep_Learning_Lab/EXPT_3_FACE_REGONITION_CNN/ORL_faces.npz/OR
L_faces.npz')
# load the "Train Images"
x_train = data['trainX']
#normalize every image
x_train = np.array(x_train,dtype='float32')/255
x_test = data['testX']
x_test = np.array(x_test,dtype='float32')/255
# load the Label of Images
y_train= data['trainY']
y_test= data['testY']
# show the train and test Data format
print('x_train : {}'.format(x_train[:]))
print('Y-train shape: {}'.format(y_train))
print('x_test shape: {}'.format(x_test.shape))
8
x_train, x_valid, y_train, y_valid= train_test_split(
x_train, y_train, test_size=.05, random_state=1234,)
im_rows=112
im_cols=92
batch_size=512
im_shape=(im_rows, im_cols, 1)
#change the size of images
x_train = x_train.reshape(x_train.shape[0], *im_shape)
x_test = x_test.reshape(x_test.shape[0], *im_shape)
x_valid = x_valid.reshape(x_valid.shape[0], *im_shape)
print('x_train shape: {}'.format(y_train.shape[0]))
print('x_test shape: {}'.format(y_test.shape))
x_train shape: 228
x_test shape: (160)
cnn_model= Sequential([
Conv2D(filters=36, kernel_size=7, activation='relu', input_shape= im_shape),
MaxPooling2D(pool_size=2),
Conv2D(filters=54, kernel_size=5, activation='relu', input_shape= im_shape),
MaxPooling2D(pool_size=2),
Flatten(),
Dense(2024, activation='relu'),
Dropout(0.5),
Dense(1024, activation='relu'),
Dropout(0.5),
Dense(512, activation='relu'),
Dropout(0.5),
#20 is the number of outputs
Dense(20, activation='softmax')
])
cnn_model.compile(
loss='sparse_categorical_crossentropy',#'categorical_crossentropy',
optimizer=Adam(),
metrics=['accuracy']
)
cnn_model.summary()
Model: "sequential_1"

9
history=cnn_model.fit(
np.array(x_train), np.array(y_train), batch_size=512,
epochs=250, verbose=2,
validation_data=(np.array(x_valid),np.array(y_valid)),
)
from sklearn.metrics import confusion_matrix
y_predicted = model.predict(x_test)
mat = confusion_matrix(y_test.argmax(axis=1), y_predicted.argmax(axis=1))
sns.heatmap(mat.T, square=True, annot=True, fmt='d', cbar=False, cmap='Blues',
xticklabels=faces.target_names,
yticklabels=faces.target_names)
plt.xlabel('Predicted label')
plt.ylabel('Actual label')

from keras.preprocessing import image


x = image.load_img('Data/george.jpg', target_size=(face_images.shape[1:]))
plt.xticks([])
plt.yticks([])
plt.imshow(x)
x = image.img_to_array(x) / 255 x
= np.expand_dims(x, axis=0)
y = model.predict(x)[0]
for i in range(len(y)):
print(faces.target_names[i] + ': ' + str(y[i]))
OUTPUT:

Epoch 1/250

1/1 - 8s - 8s/step - accuracy: 0.0482 - loss: 3.0022 - val_accuracy: 0.0000e+00 - val_loss: 3.2229

Epoch 2/250

1/1 - 5s - 5s/step - accuracy: 0.0833 - loss: 3.7966 - val_accuracy: 0.0000e+00 - val_loss: 3.0160

Epoch 3/250

1/1 - 5s - 5s/step - accuracy: 0.0307 - loss: 3.4794 - val_accuracy: 0.0000e+00 - val_loss: 2.9844

Epoch 4/250

1/1 - 6s - 6s/step - accuracy: 0.0702 - loss: 3.0206 - val_accuracy: 0.0000e+00 - val_loss: 2.9804

Epoch 5/250

1/1 - 5s - 5s/step - accuracy: 0.0482 - loss: 3.0190 - val_accuracy: 0.1667 - val_loss: 2.9895

Epoch 249/250

1/1 - 5s - 5s/step - accuracy: 1.0000 - loss: 3.0164e-04 - val_accuracy: 1.0000 - val_loss: 1.1027e-06

10
Epoch 250/250

1/1 - 5s - 5s/step - accuracy: 1.0000 - loss: 2.6223e-04 - val_accuracy: 1.0000 - val_loss: 1.0530e-06

scor = cnn_model.evaluate( np.array(x_test), np.array(y_test), verbose=0)

print('test los {:.4f}'.format(scor[0]))

print('test acc {:.4f}'.format(scor[1]))

test los 0.6108

test acc 0.9500

Colin Powell: 3.500996e-08 Donald

Rumsfeld: 7.6241164e-08 George

W Bush: 0.96677566 Gerhard

Schroeder: 7.811276e-08 Tony

Blair: 0.033224277

RESULT:

Implementation of Face Recognition using CNN was done successfully.

11
EX.NO.4 LANGUAGE MODELING USING RNN

AIM:

Implement Language Modeling using RNN

ALGORITHM:

Step 1: Import Required Libraries

Step 2: Read the Text Corpus by Load your text data from a file.

Step 3: Tokenization : Create a tokenizer to convert the text into sequences of integers. Each unique
word will be assigned an index.

Step 4: Create Input Sequences : Generate input sequences for training the model. Each sequence will
consist of words leading up to the next word.

Step 5: Padding Sequence: Pad the sequences to ensure they all have the same length, which is
necessary for training.

Step 6: Prepare Training Data : Split the input sequences into features (X) and labels (y). Convert labels
to categorical format.

Step 7: Build the Model: Create a Sequential model with an embedding layer, an LSTM layer, and a dense
output layer.

Step 8: Compile the Model : Compile the model with a loss function and optimizer.

Step 10: Predict Next Words : Use the trained model to predict the next words based on an initial input
string.

DESCRIPTION:

Language modeling using Recurrent Neural Networks (RNNs) is a fundamental task in


natural language processing (NLP). RNNs are particularly suited for sequential data like text because
they can capture dependencies between words or characters over time. Here's how you can build a basic
language model using RNNs:

1. Data Preparation:
•Text Corpus: Start with a text corpus or dataset. This can be a collection of sentences,
paragraphs, or any text data.
• Tokenization: Split the text into words or subword tokens, which will serve as the input
units for your RNN.
2. Data Preprocessing:
• Vocabulary: Create a vocabulary by assigning a unique integer to each token in your
dataset. You can use libraries like TensorFlow's Tokenizer or NLTK to help with this.
• Sequences: Convert your text data into sequences of integers representing the tokens.

12
3. Model Architecture:
• Define your RNN model. You can choose from various RNN variants, such as vanilla RNN,
LSTM (Long Short-Term Memory), or GRU (Gated Recurrent Unit). LSTM and GRU are
often preferred for better handling of long-range dependencies.
4. Embedding Layer:
• Add an embedding layer as the first layer in your model. This layer learns dense
representations for each token in your vocabulary. These embeddings serve as the input
to the RNN.
5. RNN Layer:
• Stack one or more RNN layers on top of the embedding layer. The RNN layers will
capture sequential dependencies in the text.
6. Dense Layer:
• Add a dense layer with a softmax activation function at the output to predict the
probability distribution of the next token given the previous context. This is used for
language modeling.
7. Training:
• Compile the model with a suitable loss function, such as categorical cross-entropy. The
target for training will be the next token in each sequence.
• Train the model on your text data. You can use techniques like teacher forcing, where
you provide the model with the true previous tokens during training.
8. Hyperparameter Tuning:
• Experiment with hyperparameters like the number of RNN layers, the hidden state size,
the learning rate, and batch size to optimize your model's performance.
9. Text Generation:
• After training, you can use your model to generate text. Start with a seed sentence or
word and use the model to predict the next token. Repeatedly generate tokens and
append them to the input, forming a continuous text generation process.
10. Evaluation:
• Measure the quality of generated text using metrics like perplexity or by examining the
generated text qualitatively.

PROGRAM:

import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, LSTM, Dense
# Reading corpus the text file
with open("C:\\Users\\root\\NEXT_WORD\\IndiaUS.txt", 'r', encoding='utf-8') as myfile:
mytext = myfile.read()
mytokenizer = Tokenizer()
mytokenizer.fit_on_texts([mytext])
total_words = len(mytokenizer.word_index) + 1
mytokenizer.word_index
my_input_sequences = []
for line in mytext.split('\n'):
#print(line)
token_list = mytokenizer.texts_to_sequences([line])[0]
#print(token_list)
13
for i in range(1, len(token_list)):
my_n_gram_sequence = token_list[:i+1]
#print(my_n_gram_sequence)
my_input_sequences.append(my_n_gram_sequence)
#print(input_sequences)
max_sequence_len = max([len(seq) for seq in my_input_sequences])
input_sequences = np.array(pad_sequences(my_input_sequences, maxlen=max_sequence_len,
padding='pre'))
input_sequences[0]
X = input_sequences[:, :-1]
y = input_sequences[:, -1]
y = np.array(tf.keras.utils.to_categorical(y, num_classes=total_words))
model = Sequential()
model.add(Embedding(total_words, 100, input_length=max_sequence_len-1))
model.add(LSTM(150))
model.add(Dense(total_words, activation='softmax'))
print(model.summary())

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])


model.fit(X, y, epochs=100, verbose=1)
input_text = "Joe biden"
predict_next_words= 6
for _ in range(predict_next_words):
token_list = mytokenizer.texts_to_sequences([input_text])[0]
print(token_list)
token_list = pad_sequences([token_list], maxlen=max_sequence_len-1, padding='pre')
predicted = np.argmax(model.predict(token_list), axis=-1)
output_word = ""
for word, index in mytokenizer.word_index.items():
if index == predicted:
output_word = word
break
input_text += " " + output_word
print(input_text)

14
OUTPUT:

Epoch 1/100

43/43 ━━━━━━━━━━━━━━━━━━━━ 7s 64ms/step - accuracy: 0.0434 - loss: 6.2903

Epoch 2/100

43/43 ━━━━━━━━━━━━━━━━━━━━ 3s 61ms/step - accuracy: 0.0502 - loss: 5.7324

Epoch 3/100

43/43 ━━━━━━━━━━━━━━━━━━━━ 3s 62ms/step - accuracy: 0.0550 - loss: 5.7305

.
Epoch 99/100
43/43 ━━━━━━━━━━━━━━━━━━━━ 2s 57ms/step - accuracy: 0.9873 -
loss: 0.0481
Epoch 100/100
43/43 ━━━━━━━━━━━━━━━━━━━━ 2s 54ms/step - accuracy: 0.9904 -
loss: 0.0435

[72, 14]

1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 247ms/step

[72, 14, 5]

1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 31ms/step

[72, 14, 5, 28]

1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 31ms/step

[72, 14, 5, 28, 29]

1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 31ms/step

[72, 14, 5, 28, 29, 30]

1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 22ms/step

[72, 14, 5, 28, 29, 30, 71]

1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 38ms/step

Joe biden and indian prime minister narendra modi

RESULT:
Implementation of Language Modelling using RNN was done successfully.

15
EX.NO.5 SENTIMENT ANALYSIS USING LSTM

AIM:
Implement Sentiment Analysis using LSTM.

ALGORITHM:

Step 1: Import Required Libraries

Step 2: Load the IMDB dataset from a CSV file. Ensure you provide the correct path to your dataset.

Step 3: Explore the Dataset : Check the shape of the dataset and display the first few rows to understand
its structure.

Step 4: Analyse Sentiment Distribution: Count the number of positive and negative reviews to
understand class distribution.

Step 5: Encode Sentiments: Replace string labels ("positive" and "negative") with numeric values (1 and
0).

Step 6: Split Data into Training and Testing Sets: Use train_test_split to divide the dataset into training
(80%) and testing (20%) sets.

Step 7: Tokenize Text Data : Create a tokenizer to convert text reviews into sequences of integers.

Step 8: Prepare Labels for Training and Testing Data: Extract sentiment labels for both training and
testing datasets.

Step 9: Build the Model: Create a Sequential model with an embedding layer, an LSTM layer, and a dense
output layer.

Step 10: Compile the Model: Compile the model with an optimizer and loss function suitable for binary
classification.

Step 11: Train the Model: Fit the model on training data while validating on a portion of it (20%).

Step 12: Evaluate the Model: Evaluate the model's performance on the test dataset to check its accuracy
and loss.

Step 13: Define a Function to Predict Sentiment of New Reviews: Create a function that takes a review as
input and predicts its sentiment.

Step 14: Example Usage of Prediction Function: Test the function with new reviews to see how it
performs.

DESCRIPTION:

Sentiment analysis using Long Short-Term Memory (LSTM) is a common natural language processing
(NLP) task. LSTM is a type of recurrent neural network (RNN) that is well- suited for sequence data like
text. Sentiment analysis involves determining the sentiment or emotional tone expressed in a piece of
text, such as whether a movie review is positive or negative. Here's a step-by-step guide on how to
perform sentiment analysis using LSTM:

16
1. Data Preparation:
• Dataset: Gather a labeled dataset for sentiment analysis. Common datasets include movie
reviews, tweets, and product reviews, labeled as positive, negative, or neutral
sentiments.
• Text Preprocessing: Clean and preprocess the text data. Common preprocessing steps
include lowercasing, removing punctuation, and tokenization.
2. Data Preprocessing:
• Tokenization: Split the text into individual words or subword tokens, which will be
used as the input to the LSTM.
• Padding: Ensure that all sequences have the same length by padding or truncating
them. This is necessary for batch processing.
3. Model Architecture:
• Build an LSTM-based model for sentiment analysis. The model typically consists of
the following layers:
• Embedding Layer: To convert words into dense vector representations.
• LSTM Layer(s): To capture sequential information and context in the text.
• Dense Layer: To produce the output (positive or negative sentiment).
4. Model Compilation:
• Compile the model with an appropriate loss function and optimizer. For binary
sentiment classification, binary cross-entropy loss is commonly used. You can use
Adam, SGD, or other optimizers.

5. Training:
• Split your dataset into training, validation, and test sets.
• Train the LSTM model on the training set using the compiled model and the
validation set to monitor model performance.
• Experiment with hyperparameters like the number of LSTM units, the learning rate,
and batch size to optimize model performance.
• Train for a fixed number of epochs or until validation performance converges.
6. Evaluation:
• Evaluate the trained model on the test set to assess its performance in terms of
accuracy, precision, recall, F1-score, or other relevant metrics.

SENTIMENT ANALYSIS USING LSTM:

import pandas as pd
from sklearn.model_selection import train_test_split
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Embedding, LSTM
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
data = pd.read_csv("C:/Users/root/Downloads/sentiment/IMDB Dataset.csv")
data.shape
data.head()
data["sentiment"].value_counts()
data.replace({"sentiment": {"positive": 1, "negative": 0}}, inplace=True)
# split data into training data and test data
train_data, test_data = train_test_split(data, test_size=0.2, random_state=42)
17
print(train_data.shape)
print(test_data.shape)

# Tokenize text data


tokenizer = Tokenizer(num_words=5000)
tokenizer.fit_on_texts(train_data["review"])
X_train = pad_sequences(tokenizer.texts_to_sequences(train_data["review"]), maxlen=200)
X_test = pad_sequences(tokenizer.texts_to_sequences(test_data["review"]), maxlen=200)
Y_train = train_data["sentiment"]
Y_test = test_data["sentiment"]

# build the model


model = Sequential()
model.add(Embedding(input_dim=5000, output_dim=128, input_length=200))
model.add(LSTM(128, dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(1, activation="sigmoid"))
model.summary()

# compile the model


model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
model.fit(X_train, Y_train, epochs=5, batch_size=64, validation_split=0.2)
loss, accuracy = model.evaluate(X_test, Y_test)
print(f"Test Loss: {loss}")
print(f"Test Accuracy: {accuracy}")
def predict_sentiment(review):
# tokenize and pad the review
sequence = tokenizer.texts_to_sequences([review])
padded_sequence = pad_sequences(sequence, maxlen=200)
prediction = model.predict(padded_sequence)
sentiment = "positive" if prediction[0][0] > 0.5 else "negative"
return sentiment

# example usage
new_review = "This movie was fantastic. I loved it."
sentiment = predict_sentiment(new_review)
print(f"The sentiment of the review is: {sentiment}")

# example usage
new_review = "This movie was not that good"
sentiment = predict_sentiment(new_review)
print(f"The sentiment of the review is: {sentiment}")

18
OUTPUT:

The movie was very touching and heart whelming

Predicted sentiment : Positive

I have never seen a terrible movie like this

Predicted sentiment : Negative

the movie plot is terrible but it had good acting Predicted

sentiment : Negative

RESULT:

Implementation of Sentiment Analysis using LSTM was done successfully.


19
EX.NO.6 : PARTS OF SPEECH TAGGING USING SEQUENCE TO SEQUENCE ARCHITECTURE

AIM:

Implement Parts Of Speech Tagging Using Sequence To Sequence Architecture.

ALGORITHM:

Step 1: Import Required Libraries

Step 2: Prepare the Dataset : Create a dataset consisting of sentences and their corresponding POS tags.

Step 3: Tokenize the Sentences and POS Tags: Use Keras's Tokenizer to convert words and POS tags into
sequences of integers.

Step 4: Convert Texts to Sequences and Pad Them : Transform the sentences and POS tags into
sequences of integers and pad them to ensure uniform length.

Step 5: Split Data into Training and Testing Sets: Divide the data into training and testing sets for model
evaluation.

Step 6: Define Model Parameters and Architecture: Set up the model parameters and define the
architecture of the sequence-to-sequence model.

Step 7: Prepare Data for Training with Start/End Tokens: Add start and end tokens to the POS tags to
help with training.

Step 8: Train the Model: Fit the model on the training data.

Step 10: Create Encoder Model for Predictions: Define an encoder model to retrieve internal states for
predictions.

Step 11: Implement Prediction Function for POS Tags: Create a function that takes a sentence as input
and predicts its POS tags using the trained model.

DESCRIPTION:

Parts of speech (POS) tagging is a crucial task in natural language processing that involves assigning
parts of speech to each word in a sentence, such as nouns, verbs, adjectives, etc. Using a sequence-to-
sequence (Seq2Seq) architecture for POS tagging can be an effective approach, especially when you
want to leverage deep learning techniques.

Sequence-to-Sequence Architecture for POS Tagging

Here's a general outline of how you might implement POS tagging using a Seq2Seq architecture:

1. Data Preparation:
• Corpus Collection: Gather a labeled dataset with sentences and their corresponding POS
tags. Common datasets include the Penn Treebank or Universal Dependencies Tokenize
the sentences.

20
2. Preprocessing:
• Create a vocabulary for words and tags.
• Convert words and tags to numerical representations (using techniques like word
embeddings or one-hot encoding).
3. Model Architecture:
• Encoder: This part of the model takes the input sequence (sentence) and processes it.
• You can use an LSTM or GRU layer to encode the input sequence.
• Add an embedding layer to convert word indices to dense vectors.
• Decoder: The decoder generates the output sequence (POS tags).
• This can also be implemented using LSTM/GRU.
• The decoder's input is the output from the encoder.
• Attention Mechanism: Incorporate an attention mechanism to help the decoder focus on
relevant parts of the input sequence.
4. Training:
• Define a loss function (typically categorical cross-entropy).
• Use an optimizer like Adam or SGD.
• Train the model on the training dataset, validating with a separate validation set.
5. Inference:
• During inference, pass the input sequence through the encoder to obtain context vectors.
• Use the decoder to generate the POS tags. This can be done using greedy decoding or
beam search.
6. Post-processing:
• Convert the predicted tag indices back to their corresponding tags.
• Evaluate the model using metrics such as accuracy or F1-score.

PARTS OF SPEECH TAGGING USING SEQUENCE TO SEQUENCE ARCHITECTURE CODE:

import numpy as np
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Embedding, LSTM, Dense
from sklearn.model_selection import train_test_split
# Example dataset: list of sentences and corresponding POS tags
sentences = [
"The cat sat on the mat",
"Dogs are in the yard",
"We are learning new techniques"
]
pos_tags = [
"DET NOUN VERB ADP DET NOUN",
"NOUN AUX ADP DET NOUN",
"PRON AUX VERB ADJ NOUN"
]
# Tokenize the words in the sentences
word_tokenizer = Tokenizer()
word_tokenizer.fit_on_texts(sentences)
vocab_size = len(word_tokenizer.word_index) + 1
print(f"Vocabulary Size: {vocab_size}")
# Tokenize the POS tags
tag_tokenizer = Tokenizer()
tag_tokenizer.fit_on_texts(pos_tags)
21
num_tags = len(tag_tokenizer.word_index) + 1
print(f"Number of POS Tags: {num_tags}")
# Convert sentences and POS tags to sequences of integers
X = word_tokenizer.texts_to_sequences(sentences)
y = tag_tokenizer.texts_to_sequences(pos_tags)
# Pad sequences to ensure uniform length
max_len = max([len(seq) for seq in X])
X = pad_sequences(X, maxlen=max_len, padding='post')
y = pad_sequences(y, maxlen=max_len, padding='post')
# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# Define model parameters
embedding_dim = 64
lstm_units = 64
# Encoder
encoder_inputs = Input(shape=(max_len,))
encoder_embedding = Embedding(input_dim=vocab_size, output_dim=embedding_dim,
input_length=max_len)(encoder_inputs)
encoder_lstm = LSTM(lstm_units, return_sequences=False, return_state=True)
encoder_output, state_h, state_c = encoder_lstm(encoder_embedding)
# Decoder
decoder_inputs = Input(shape=(max_len,))
decoder_embedding = Embedding(input_dim=num_tags, output_dim=embedding_dim,
input_length=max_len)(decoder_inputs)
decoder_lstm = LSTM(lstm_units, return_sequences=True, return_state=False)
decoder_outputs = decoder_lstm(decoder_embedding, initial_state=[state_h, state_c])
# Dense layer for outputting the POS tags
output_layer = Dense(num_tags, activation='softmax')
decoder_outputs = output_layer(decoder_outputs)
# Create the model
model = Model([encoder_inputs, decoder_inputs], decoder_outputs)
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# Model summary
model.summary()

# Add 'start' and 'end' tokens to the POS tags


pos_tags_with_start_end = []
22
for tags in pos_tags:
pos_tags_with_start_end.append('start ' + tags + ' end')
# Fit the tokenizer on POS tags with 'start' and 'end' tokens
tag_tokenizer = Tokenizer()
tag_tokenizer.fit_on_texts(pos_tags_with_start_end)
# Convert sentences and POS tags to sequences of integers
y = tag_tokenizer.texts_to_sequences(pos_tags_with_start_end)
# Create shifted decoder inputs
decoder_input_data = np.zeros_like(y_train)
decoder_input_data[:, 1:] = y_train[:, :-1]
# Assign the 'start' token to the first position of the decoder input
decoder_input_data[:, 0] = tag_tokenizer.word_index['start']
# Train the model
history = model.fit([X_train, decoder_input_data], y_train, batch_size=64, epochs=10,
validation_split=0.2)
# Evaluate the model
test_loss, test_acc = model.evaluate([X_test, np.zeros_like(y_test)], y_test)
print(f"Test Accuracy: {test_acc * 100:.2f}%")
# Encoder model: it will return the encoder's internal state (state_h, state_c)
encoder_model = Model(encoder_inputs, [state_h, state_c])
def predict_pos_tags(sentence):
# Convert sentence to sequence
sentence_seq = word_tokenizer.texts_to_sequences([sentence])
sentence_seq = pad_sequences(sentence_seq, maxlen=max_len, padding='post')
# Encode the input sentence to get the encoder states
state_values = encoder_model.predict(sentence_seq)
# Generate empty target sequence (decoder input)
decoder_input = np.zeros((1, max_len))
decoder_input[0, 0] = tag_tokenizer.word_index['start'] # Assuming 'start' token is used
# Predict POS tags one by one
pos_tag_seq = []
for i in range(max_len - 1): # Run up to max_len - 1
decoder_output = model.predict([sentence_seq, decoder_input])
predicted_tag = np.argmax(decoder_output[0, i])
pos_tag_seq.append(tag_tokenizer.index_word[predicted_tag])
decoder_input[0, i + 1] = predicted_tag
# Optional: Break if end token is predicted
if predicted_tag == tag_tokenizer.word_index.get('end'):
break
return pos_tag_seq

# Example sentence
new_sentence = "We are exploring deep learning"
predicted_tags = predict_pos_tags(new_sentence)
print("Predicted POS Tags:", predicted_tags)

23
OUTPUT:

Predicted POS Tags: ['adp', 'adp', 'det', 'aux', 'noun']

RESULT:

Implementation of Parts of Speech Tagging Using Sequence To Sequence Architecture is done


successfully.

24
EX.NO.7 : ENCODER – DECODER MODEL
AIM:

Implement Machine Translation using Encoder – Decoder Model.

ALGORITHM:

Step 1 :Import Required Libraries

Step 2 : Prepare the Dataset: This corresponds to defining the sample English and French sentences.

Step 3 : Set Parameters: This is to define various parameters like maximum sequence lengths,
vocabulary sizes, embedding dimensions, and LSTM units.

Step 4 :Tokenize Input Sentences: This step involves tokenizing the English sentences, which is reflected
in your code where you use Tokenizer and pad_sequences.

Step 5: Tokenize Target Sentences: Similar to the previous step, this involves tokenizing and padding the
French sentences.

Step 6: Build the Encoder Model: This part of the breakdown explains how you create the encoder using
Keras layers.

Step 7 : Build the Decoder Model: This mirrors how you set up the decoder in your original code.

Step 8 : Define Output Layer and Compile Model: This corresponds to adding a dense layer for output
and compiling the model.

Step 9 : Prepare Decoder Input Data for Training: This explains how you prepare shifted target
sequences for training.

Step 9 : Train the Model: This directly relates to fitting your model on the training data.

Step 10 : Define Translation Function: This is where you create a function to handle translating new
sentences using your trained model.

Step 11 : Example Translation Call: Finally, this shows how to use the translation function with an
example sentence.

DESCRIPTION:

Seq2Seq (Sequence-to-Sequence) is a type of model in machine learning that is used for


tasks such as machine translation, text summarization, and image captioning. The model consists of two
main components:

• Encoder
• Decoder

Seq2Seq models are trained using a dataset of input-output pairs, where the input is a
sequence of tokens and the output is also a sequence of tokens. The model is trained to maximize the
likelihood of the correct output sequence given the input sequence.

25
Encoder-Decoder Stack:

As the name suggests, seq2seq takes as input a sequence of words(sentence or sentences)


and generates an output sequence of words. It does so by use of the recurrent neural network (RNN).
Although the vanilla version of RNN is rarely used, its more advanced version i.e. LSTM or GRU is used.
This is because RNN suffers from the problem of vanishing gradient. LSTM is used in the version
proposed by Google. It develops the context of the word by taking 2 inputs at each point in time. One
from the user and the other from its previous output, hence the name recurrent (output goes as input).

The encoder and decoder are typically implemented as Recurrent Neural Networks
(RNNs) or Transformers.

Encoder Stack:

It uses deep neural network layers and converts the input words to corresponding
hidden vectors. Each vector represents the current word and the context of the word. The encoder takes
the input sequence, one token at a time, and uses an RNN or transformer to update its hidden state,
which summarizes the information in the input sequence. The final hidden state of the encoder is then
passed as the context vector to the decoder.

Decoder Stack:

It is similar to the encoder. It takes as input the hidden vector generated by the encoder,
its own hidden states, and the current word to produce the next hidden vector and finally predict the
next word. The decoder uses the context vector and an initial hidden state to generate the output
sequence, one token at a time. At each time step, the decoder uses the current hidden state, the context
vector, and the previous output token to generate a probability distribution over the possible next
tokens. The token with the highest probability is then chosen as the output, and the process continues
until the end of the output sequence is reached.

Components of seq2seq Model in Machine Learning

Apart from these two, many optimizations have led to other components of seq2seq:

• Attention: The input to the decoder is a single vector that has to store all the information about
the context. This becomes a problem with large sequences. Hence the attention mechanism is
applied which allows the decoder to look at the input sequence selectively.
• Beam Search: The highest probability word is selected as the output by the decoder. But this
does not always yield the best results, because of the basic problem of greedy algorithms. Hence
beam search is applied which suggests possible translations at each step. This is done by making
a tree of top k-results.
• Bucketing: Variable-length sequences are possible in a seq2seq model because of the padding of
0’s which is done to both input and output. However, if the max length set by us is 100 and the
sentence is just 3 words long it causes a huge waste of space. So we use the concept of bucketing.
We make buckets of different sizes like (4, 8) (8, 15), and so on, where 4 is the max input length
defined by us and 8 is the max output length defined.

26
ENCODER – DECODER MODEL CODE:

import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, LSTM, Embedding, Dense
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
# Sample English-French sentence pairs
english_sentences = [
"I am a student.",
"How are you?",
"I love programming.",
"Deep learning is fascinating.",
"Machine learning is powerful."
]
french_sentences = [
"Je suis étudiant.",
"Comment ça va?",
"J'aime programmer.",
"L'apprentissage profond est fascinant.",
"L'apprentissage automatique est puissant."
]
# Parameters
max_len_input = 10 # Maximum length of input sequences
max_len_target = 10 # Maximum length of target sequences
vocab_size_input = 50 # Size of the vocabulary for input language
vocab_size_target = 50 # Size of the vocabulary for target language
embedding_dim = 8 # Dimension of embedding vectors
lstm_units = 64 # Number of units in LSTM layers
# Tokenize English sentences
input_tokenizer = Tokenizer(num_words=vocab_size_input)
input_tokenizer.fit_on_texts(english_sentences)
input_sequences = input_tokenizer.texts_to_sequences(english_sentences)
input_padded = pad_sequences(input_sequences, maxlen=max_len_input, padding='post')
# Tokenize French sentences
target_tokenizer = Tokenizer(num_words=vocab_size_target)
target_tokenizer.fit_on_texts(french_sentences)
target_sequences = target_tokenizer.texts_to_sequences(french_sentences)
target_padded = pad_sequences(target_sequences, maxlen=max_len_target, padding='post')
# Encoder
encoder_inputs = Input(shape=(max_len_input,))
encoder_embedding=Embedding(input_dim=vocab_size_input,
output_dim=embedding_dim)(encoder_inputs)
encoder_lstm = LSTM(lstm_units, return_state=True)
encoder_outputs, state_h, state_c = encoder_lstm(encoder_embedding)
# Decoder
decoder_inputs = Input(shape=(max_len_target,))
decoder_embedding=Embedding(input_dim=vocab_size_target,
output_dim=embedding_dim)(decoder_inputs)
decoder_lstm = LSTM(lstm_units, return_sequences=True)(decoder_embedding, initial_state=[state_h,
state_c])
# Output layer
decoder_outputs = Dense(vocab_size_target, activation='softmax')(decoder_lstm)
# Complete model
model = Model([encoder_inputs, decoder_inputs], decoder_outputs)
27
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# Prepare decoder input data (shifted target sequences)
decoder_input_data = np.zeros_like(target_padded)
decoder_input_data[:, 1:] = target_padded[:, :-1]
# Train the model
model.fit([input_padded, decoder_input_data], np.expand_dims(target_padded, -1), epochs=100,
verbose=1)
def translate(sentence):
# Encode the input sentence
input_seq = input_tokenizer.texts_to_sequences([sentence])
input_padded_seq = pad_sequences(input_seq, maxlen=max_len_input, padding='post')
# Initialize the decoder input with a start token (optional)
decoder_input = np.zeros((1, max_len_target))
decoder_input[0, 0] = target_tokenizer.word_index.get('start', 1) # Assume 'start' is in your
vocabulary
# Generate the translation
translated_sentence = []
for i in range(max_len_target - 1):
output = model.predict([input_padded_seq, decoder_input])
predicted_index = np.argmax(output[0, i]) # Get the predicted word index
predicted_word = target_tokenizer.index_word.get(predicted_index, '')
# Convert index to word
if predicted_word == '':
break # Stop if there is no predicted word
translated_sentence.append(predicted_word)
decoder_input[0, i + 1] = predicted_index # Update the decoder input
return ' '.join(translated_sentence)
def translate(sentence):
# Encode the input sentence
input_seq = input_tokenizer.texts_to_sequences([sentence])
input_padded_seq = pad_sequences(input_seq, maxlen=max_len_input, padding='post')
# Initialize the decoder input with a start token (optional)
decoder_input = np.zeros((1, max_len_target))
decoder_input[0, 0] = target_tokenizer.word_index.get('start', 1) # Assume 'start' is in your
vocabulary
# Generate the translation
translated_sentence = []
for i in range(max_len_target - 1):
output = model.predict([input_padded_seq, decoder_input])
predicted_index = np.argmax(output[0, i]) # Get the predicted word index
predicted_word = target_tokenizer.index_word.get(predicted_index, '') # Convert index to word
if predicted_word == '':
break # Stop if there is no predicted word
translated_sentence.append(predicted_word)
decoder_input[0, i + 1] = predicted_index # Update the decoder input
return ' '.join(translated_sentence)

# Example translation
new_sentence = "I love programming."
translated = translate(new_sentence)
print("Translated Sentence:", translated)

28
OUTPUT:

Input sentence: Hi. Decoded

sentence: Salut!

Input sentence: Run!

Decoded sentence: Cours !

Input sentence: Hello!

Decoded sentence: Salut!

RESULT:

Implementation of Machine Translation using Encode – Decoder Model was done successfully.

29
EX.NO.8 : IMAGE AUGMENTATION USING GANs
AIM:

Implement Image Augmentation using GANs.

ALGORITHM:

Step 1: Import Required Libraries

Step 2: Load and Visualize the CIFAR-10 Dataset : Load the CIFAR-10 dataset and visualize a sample of
images.

Step 3: Define the Discriminator Model : Create the discriminator model that will classify images as real
or fake.

Step 4: Define the Generator Model : Create the generator model that will generate new images from
random noise.

Step 5: Define the GAN Model : Combine the generator and discriminator into a single GAN model.

Step 6: Load Real Samples from CIFAR-10 Dataset : Prepare the CIFAR-10 dataset for training by scaling
pixel values.

Step 7: Generate Real Samples for Training the Discriminator : Select random real images from the
dataset.

Step 8: Generate Latent Points for Input to Generator : Create random latent vectors to feed into the
generator.

Step 9: Generate Fake Samples Using the Generator: Use the generator to create fake images from latent
vectors.

Step 10: Train the GAN Model : Train both the generator and discriminator models in a loop over
multiple epochs.

Step 11: Initialize and Train the GAN : Set up the GAN by creating instances of the generator and
discriminator models and then train it.

Step 12: Generate Images Using Trained Generator Model : Load the trained generator and produce new
images from random latent vectors.

DESCRIPTION:

Image augmentation using Generative Adversarial Networks (GANs) is a technique that


leverages the power of GANs to generate synthetic data, which can be used to increase the size and
diversity of your dataset. This can be particularly useful in scenarios where you have limited real data or
want to improve the generalization and robustness of machine learning models.

Here's an overview of how image augmentation with GANs works:

1. Understand GANs: Generative Adversarial Networks consist of two neural networks – a


generator and a discriminator. The generator tries to create realistic data, while the
discriminator tries to distinguish between real and fake data. These networks are trained
simultaneously, leading to a game-like situation where the generator gets better at creating
realistic data over time.
30
2. Training a GAN: To use GANs for image augmentation, you first need to train a GAN model. This
involves feeding it a dataset of real images and having it generate synthetic images that resemble
the real ones. The GAN is trained iteratively until it produces convincing synthetic images.
3. Data Augmentation: Once the GAN is trained, you can use it to augment your dataset by
generating new synthetic images. You can generate images with variations in style, content,
lighting, or other relevant factors to make your dataset more diverse.
4. Mix Real and Synthetic Data: Combine your original dataset with the synthetic images
generated by the GAN to create a larger, more diverse dataset for training your machine learning
model. Ensure that you maintain a balanced and representative distribution of data classes.
5. Benefits:
a. Improved Generalization: The augmented dataset can help your model generalize better
to unseen data, reducing overfitting.
b. Enhanced Robustness: Increased diversity in your dataset can make your model more
robust to variations in real-world data.
c. Data Expansion: You can significantly increase your dataset size, which is especially
valuable when you have limited real data.
6. Considerations:
a. Quality Control: Ensure that the generated images are of sufficient quality and resemble
the characteristics of your real data.
b. Diversity: Experiment with different GAN models and training techniques to create
diverse synthetic data.
c. Ethical Concerns: Be aware of potential biases and ethical issues in the GAN- generated
data.
7. Tools and Libraries: There are various GAN architectures and libraries available for image
generation, including DCGAN, StyleGAN, and BigGAN. Libraries like TensorFlow and PyTorch
provide GAN implementations for easy use.
8. Fine-Tuning: Depending on your specific task, you might need to fine-tune your machine
learning model with the augmented dataset to ensure it performs optimally.

Keep in mind that using GANs for data augmentation requires significant computational resources, as
training a GAN can be a resource-intensive process. Additionally, it's essential to carefully evaluate the
impact of data augmentation on your specific machine learning task to ensure it leads to better model
performance.

IMAGE AUGMENTATION USING GANs CODE:

from numpy import zeros


from numpy import ones
from numpy.random import randn
from numpy.random import randint
from keras.datasets.cifar10 import load_data
from keras.optimizers import Adam
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Reshape
from keras.layers import Flatten
from keras.layers import Conv2D
from keras.layers import Conv2DTranspose
from keras.layers import LeakyReLU
from keras.layers import Dropout

31
from matplotlib import pyplot as plt
(trainX, trainy), (testX, testy) = load_data()
# plot 25 images
for i in range(25):
plt.subplot(5, 5, 1 + i)
plt.axis('off')
plt.imshow(trainX[i])
plt.show()
def define_discriminator(in_shape=(32,32,3)):
model = Sequential()
model.add(Conv2D(128, (3,3), strides=(2,2), padding='same', input_shape=in_shape))
#16x16x128
model.add(LeakyReLU(alpha=0.2))
model.add(Conv2D(128, (3,3), strides=(2,2), padding='same')) #8x8x128
model.add(LeakyReLU(alpha=0.2))
model.add(Flatten()) #shape of 8192
model.add(Dropout(0.4))
model.add(Dense(1, activation='sigmoid')) #shape of 1
# compile model
opt = Adam(lr=0.0002, beta_1=0.5)
model.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy'])
return model
test_discr = define_discriminator()
print(test_discr.summary())

def define_generator(latent_dim): #latent_dim is the dimension of the latent vector (e.g., 100)
model = Sequential()
# We will reshape input latent vector into 8x8 image as a starting point.
#So n_nodes for the Dense layer can be 128x8x8 so when we reshape the output
#it would be 8x8x128 and that can be slowly upscaled to 32x32 image for output.
n_nodes = 128 * 8 * 8 #8192 nodes
model.add(Dense(n_nodes, input_dim=latent_dim)) #Dense layer so we can work with 1D latent
vector
model.add(LeakyReLU(alpha=0.2))
model.add(Reshape((8, 8, 128))) #8x8x128 dataset from the latent vector.
# upsample to 16x16
model.add(Conv2DTranspose(128, (4,4), strides=(2,2), padding='same')) #16x16x128
model.add(LeakyReLU(alpha=0.2))

32
# upsample to 32x32
model.add(Conv2DTranspose(128, (4,4), strides=(2,2), padding='same')) #32x32x128
model.add(LeakyReLU(alpha=0.2))
# generate
model.add(Conv2D(3, (8,8), activation='tanh', padding='same')) #32x32x3
return model #Model not compiled as it is not directly trained like the discriminator.
#Generator is trained via GAN combined model.
test_gen = define_generator(100)
print(test_gen.summary())

def define_gan(generator, discriminator):


discriminator.trainable = False #Discriminator is trained separately. So set to not trainable.
# connect generator and discriminator
model = Sequential()
model.add(generator)
model.add(discriminator)
# compile model
opt = Adam(lr=0.0002, beta_1=0.5)
model.compile(loss='binary_crossentropy', optimizer=opt)
return model
# load cifar training images
def load_real_samples():
(trainX, _), (_, _) = load_data()
# cConvert to float and scale.
X = trainX.astype('float32')
# scale from [0,255] to [-1,1]
X = (X - 127.5) / 127.5 #Generator uses tanh activation so rescale
#original images to -1 to 1 to match the output of generator.
return X
def generate_real_samples(dataset, n_samples):
# choose random images
ix = randint(0, dataset.shape[0], n_samples)
# select the random images and assign it to X
X = dataset[ix]
# generate class labels and assign to y
y = ones((n_samples, 1)) ##Label=1 indicating they are real

33
return X, y
# generate n_samples number of latent vectors as input for the generator
def generate_latent_points(latent_dim, n_samples):
# generate points in the latent space
x_input = randn(latent_dim * n_samples)
# reshape into a batch of inputs for the network
x_input = x_input.reshape(n_samples, latent_dim)
return x_input
def generate_fake_samples(generator, latent_dim, n_samples):
# generate points in latent space
x_input = generate_latent_points(latent_dim, n_samples)
# predict using generator to generate fake samples.
X = generator.predict(x_input)
# Class labels will be 0 as these samples are fake.
y = zeros((n_samples, 1)) #Label=0 indicating they are fake
return X, y
#Finally, set the loss parameters for both the real and fake images, as well as the combined loss.
def train(g_model, d_model, gan_model, dataset, latent_dim, n_epochs=100, n_batch=128):
bat_per_epo = int(dataset.shape[0] / n_batch)
half_batch = int(n_batch / 2) #the discriminator model is updated for a half batch of real
samples
#and a half batch of fake samples, combined a single batch.
# manually enumerate epochs and bacthes.
for i in range(n_epochs):
# enumerate batches over the training set
for j in range(bat_per_epo)
# Train the discriminator on real and fake images, separately (half batch each)
#Research showed that separate training is more effective.
# get randomly selected 'real' samples
X_real, y_real = generate_real_samples(dataset, half_batch)
#Let us just capture loss and ignore accuracy value (2nd output below)
d_loss_real, _ = d_model.train_on_batch(X_real, y_real)
# generate 'fake' examples
X_fake, y_fake = generate_fake_samples(g_model, latent_dim, half_batch)
# update discriminator model weights
d_loss_fake, _ = d_model.train_on_batch(X_fake, y_fake)
#d_loss = 0.5 * np.add(d_loss_real, d_loss_fake) #Average loss if you want to report single
# prepare points in latent space as input for the generator
X_gan = generate_latent_points(latent_dim, n_batch)
#the generated image is true (hence value of 1 for y)
y_gan = ones((n_batch, 1))
#job of folling the discriminator then the output would be 1 (true)
# update the generator via the discriminator's error
g_loss = gan_model.train_on_batch(X_gan, y_gan)
# Print losses on this batch
print('Epoch>%d, Batch %d/%d, d1=%.3f, d2=%.3f g=%.3f' %
(i+1, j+1, bat_per_epo, d_loss_real, d_loss_fake, g_loss))
# save the generator model
g_model.save('cifar_generator_2epochs.h5')
#Train the GAN
# size of the latent space
latent_dim = 100
# create the discriminator
discriminator = define_discriminator()
34
# create the generator
generator = define_generator(latent_dim)
# create the gan
gan_model = define_gan(generator, discriminator)
# load image data
dataset = load_real_samples()
# train model
train(generator, discriminator, gan_model, dataset, latent_dim, n_epochs=2)
# Now, let us load the generator model and generate image
from keras.models import load_model
from numpy.random import randn
# Plot generated images
def show_plot(examples, n):
for i in range(n * n):
plt.subplot(n, n, 1 + i)
plt.axis('off')
plt.imshow(examples[i, :, :, :])
plt.show()
# load model
model = load_model('cifar_generator_2epochs.h5') #Model trained for 100 epochs
# generate images
latent_points = generate_latent_points(100, 25) #Latent dim and n_samples
# generate images
X = model.predict(latent_points)
# scale from [-1,1] to [0,1]
X = (X + 1) / 2.0
import numpy as np
X = (X*255).astype(np.uint8)
# plot the result
show_plot(X, 5)
#Note: CIFAR10 classes are: airplane, automobile, bird, cat, deer, dog, frog, horse,ship, truck
show_plot(X, 5)

OUTPUT:

Epoch>1, Batch 1/390, d1=0.669, d2=0.698 g=0.689

Epoch>1, Batch 2/390, d1=0.601, d2=0.705 g=0.683

Epoch>1, Batch 3/390, d1=0.541, d2=0.715 g=0.673

Epoch>2, Batch 388/390, d1=0.736, d2=0.682 g=0.793

Epoch>2, Batch 389/390, d1=0.746, d2=0.645 g=0.866

Epoch>2, Batch 390/390, d1=0.728, d2=0.604 g=0.946

35
RESULT:

Implementation of Image Augmentation using GANs was done successfully.

36
EX.NO.9A: MINI PROJECT (FACE RECOGNITION)
AIM:

Implement Face Recognition using CNN.

PROCEDURE:

1. CAPTURING IMAGE DATA

Step 1: Import Required Libraries

Step 2: Define the Function to Create Folder and Capture Images: The function
Create_Folder_Images is defined to handle folder creation and image capturing.

Step 3: Create Directory if it Doesn't Exist: Inside the function, the code checks if the
specified directory exists and creates it if it doesn't.

Step 4: Initialize Face Detection and Video Capture: The function initializes a face detector
using Haar cascades and starts capturing video from the webcam.

Step 5: Capture Images in a Loop: A loop runs for the specified number of images (tn),
capturing frames from the webcam.

Step 6: Process Detected Faces:If faces are detected, they are processed and saved as images.

Step 7: Draw Rectangle Around Detected Faces: The code draws rectangles around detected
faces on the original image for visualization.

Step 8: Display Captured Image: The current frame is displayed in a window titled "Dataset
Creator".

Step 9: Release Resources After Capture: After capturing all images, resources are released
and all OpenCV windows are closed.

Step 10: Take User Input for Name and Number of Images: At the end of the script, user
inputs are taken to specify their name and how many images to capture.

2. IMAGE DATA INTO CSV

Step 1: Import Required Libraries

Step 2: Define Paths and Parameters: Set the path to the images and define other parameters.

Step 3: Create a DataFrame to Store Image Data: Initialize a Pandas DataFrame to store pixel
values and class labels.

Step 4: Loop Through Each Image File: Iterate through each image file in the specified
directory.

Step 5: Save DataFrame to CSV File:After processing all images, save the DataFrame to a CSV
file.

3. CSV COMBINE:

Step 1: Import Required Libraries

Step 2: Combine CSV Files: The code reads two CSV files (mohan.csv and priya.csv) and
concatenates them into a single DataFrame.

37
Step 3: Prepare Image Data and Labels: The next step involves reshaping the image data and
extracting labels

Step 4: One-Hot Encoding of Labels: The labels are converted to a one-hot encoded format.

Step 5: Display Random Image with Label: A random image from the dataset is selected and
displayed along with its label.

4. FACE RECOGNITION :

Step 1: Import Required Libraries

Step 2: Load and Preprocess Data: The script reads image data from a CSV file and processes
it for training.

Step 3: Visualize Random Image and Label: A random image from the dataset is displayed
along with its label.

Step 4: Split Data into Training and Testing Sets: The dataset is split into training and testing
sets.

Step 5: Build the CNN Model: A Sequential model is created with convolutional layers
followed by pooling layers.

Step 6: Compile the Model: The model is compiled with a loss function and optimizer.

Step 7: Train the Model: The model is trained using the training data.

Step 8: Define Another CNN Architecture : An alternative CNN architecture is defined but not
trained in this snippet. You can choose to keep or remove it based on your needs.

Step 9: Data Augmentation Setup:Data augmentation is set up to artificially increase the size
of the training set.

Step 10: Define Callbacks for Training: Callbacks are defined to manage learning rate
adjustments and early stopping during training.

Step 11: Train the Model with Data Augmentation: Finally, train the model using augmented
data.

CAPTURNG IMAGE DATA CODE :

# Importing Modules
import cv2
import numpy as np
import os
from tqdm import tqdm
def Crete_Folder_Images(name, directory, tn):
# Creating directory in the file
if not os.path.exists(directory):
os.makedirs(directory)
else:
print('DIRECTORY with name %s EXIST'%name)
# Opening vedio mode with frontal face detector
faceDetect=cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
#cv2.CascadeClassifier('haarcascade_frontalface_default.xml');
cam=cv2.VideoCapture(0)
size =(100, 100)
for i in tqdm(range(1, 1+ tn)):
38
ret,img=cam.read()
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #for GRAY SCALE IMAGES
faces=faceDetect.detectMultiScale(gray,1.3,5)
# faces=faceDetect.detectMultiScale(img,1.3,5) # FOR COLOR IMAGES
for(x,y,w,h) in faces:
res = cv2.resize(gray[y:y+h,x:x+w], size, interpolation = cv2.INTER_AREA)
cv2.imwrite('Datasets/'+str(name)+'/'+str(name)+'_'+str(i)+'.jpg', res) #for GRAY SCALE IMAGES

# cv2.imwrite(directory+'/'+str(name)+'-'+str(sn)+'.jpg'
# ,img[y:y+h,x:x+w]) # FOR COLOR IMAGES
cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
cv2.waitKey(200)
cv2.imshow('DAtaset Creator',img)
cv2.waitKey(1)
cam.release()
cv2.destroyAllWindows()
print('TASK COMPLETED')
# Taking inputs
name=input('\nEnter your name: ')
directory='Datasets/'+name
tn= int(input('Enter no.of images to be taken: '))
Crete_Folder_Images(name, directory, tn)

OUTPUT:

IMAGE DATA INTO CSV CODE:

import matplotlib.image as image


import os
import pandas as pd
import cv2
from tqdm import tqdm
import numpy as np
from PIL import Image
path = 'Datasets/priya/'
filename = 'priya.csv'
files = os.listdir(path)
dim = (100, 100)
cls = 1
df = pd.DataFrame(columns = [f'pix-{i}' for i in range(1, 1+(dim[0]*dim[1]))]+['class'])
for i in tqdm(range(1, 1+len(files))):
img =Image.open(path+files[i-1])
df.loc[i] = list(img.getdata()) + [cls]

df.to_csv(filename,index = False)
print('Task Completed')

39
OUTPUT:

CSV COMBINE CODE:

import pandas as pd
df=pd.concat(map(pd.read_csv,['mohan.csv','priya.csv']),ignore_index=True)
df.to_csv('IMAGE_COMBINE.csv',index=False)
X = df.iloc[:, :100*100].values.reshape(-1, 100, 100, 1)
y = df.iloc[:, -1].values
X.shape, y.shape
from tensorflow.keras.utils import to_categorical
# One Hot Encoding the labels
y = to_categorical(y, num_classes= 1+ df.loc[:, 'class'].unique().shape[0])
y.shape
# random Image
q = np.random.randint(100*100)
plt.imshow(X[q][:,:,0], cmap='gray')
plt.title(f'Label-{np.argmax(y[q])}')
plt.axis('off')
plt.show()

FACE RECOGNITION CODE :

import tensorflow as tf
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras.layers import BatchNormalization
from keras.callbacks import ModelCheckpoint
from tensorflow.keras.layers import ReLU
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dense
from tensorflow.keras.utils import to_categorical
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
# Reading Data
df = pd.read_csv('IMAGE_COMBINE.csv')
df.shape
40
X = df.iloc[:, :100*100].values.reshape(-1, 100, 100, 1)
y = df.iloc[:, -1].values
X.shape, y.shape
## One Hot Encoding the labels
y = to_categorical(y, num_classes= 0+ df.loc[:, 'class'].unique().shape[0])
y.shape
# random Image
q = np.random.randint(10*10)
plt.imshow(X[q][:,:,0], cmap='gray')
plt.title(f'Label-{np.argmax(y[q])}')
plt.axis('off')
plt.show()
y.shape
X_train,X_test,y_train,y_test=train_test_split(X, y, random_state=42, test_size=0.15)
print(f'Train Size - {X_train.shape}\nTest Size - {X_test.shape}')

Train Size - (252, 100, 100, 1)


Test Size - (45, 100, 100, 1)

print('X_train:', X_train.shape)
print('y_train:', y_train.shape)
print('X_validation:', X_test.shape)
print('y_validation:', y_test.shape)

X_train: (252, 100, 100, 1)


y_train: (252, 2)
X_validation: (45, 100, 100, 1)
y_validation: (45, 2)

#X_train = X_train / 255


#X_test = X_test / 255
#batch_size = 8
#num_classes = 2
epochs = 10
input_shape = (100, 100, 1)
model = Sequential()
model.add(Conv2D(32, kernel_size=(5, 5),activation='relu',input_shape=input_shape))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='sigmoid'))
model.compile(loss=keras.losses.categorical_crossentropy,optimizer=keras.optimizers.Adadelta(),metri
cs=['accuracy'])
hist = model.fit(X_train,y_train,batch_size=batch_size,epochs=epochs,verbose=1,validation_data=(X_test,
y_test))
print("The model has successfully trained")

41
model = tf.keras.models.Sequential()

model.add(tf.keras.layers.Convolution2D(
input_shape=(100, 100, 1),
kernel_size=5,
filters=8,
strides=1,
activation=tf.keras.activations.relu,
kernel_initializer=tf.keras.initializers.VarianceScaling()
))
model.add(tf.keras.layers.MaxPooling2D(
pool_size=(2, 2),
strides=(2, 2)
))
model.add(tf.keras.layers.Convolution2D(
kernel_size=5,
filters=16,
strides=1,
activation=tf.keras.activations.relu,
kernel_initializer=tf.keras.initializers.VarianceScaling()
))
model.add(tf.keras.layers.MaxPooling2D(
pool_size=(2, 2),
strides=(2, 2)
))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(
units=128,
activation=tf.keras.activations.relu
));
model.add(tf.keras.layers.Dropout(0.2))
model.add(tf.keras.layers.Dense(
units=2,
activation=tf.keras.activations.softmax,
kernel_initializer=tf.keras.initializers.VarianceScaling()
))
42
model.summary()

tf.keras.utils.plot_model(
model,
show_shapes=True,
show_layer_names=True,
)

# Artificially increase training set


train_datagen = ImageDataGenerator(rescale=1./255.,
rotation_range=10,
width_shift_range=0.25,
height_shift_range=0.25,
shear_range=0.1,
zoom_range=0.25,
horizontal_flip=False)
valid_datagen = ImageDataGenerator(rescale=1./255.)

43
from datetime import datetime
num_classes = 2
model_name = 'Face_trained_model_'+datetime.now().strftime("%H_%M_%S_")
model = Sequential(name = model_name)
model.add(Conv2D(64, kernel_size=3, activation='relu', input_shape=(100, 100, 1)))
model.add(BatchNormalization()) #----------------
model.add(Conv2D(64, kernel_size=3, activation='relu'))
model.add(BatchNormalization()) #----------------
model.add(Conv2D(64, kernel_size=5, padding='same', activation='relu'))
44
model.add(BatchNormalization()) #----------------
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2)) #----------------
model.add(Conv2D(128, kernel_size=3, activation='relu'))
model.add(BatchNormalization())
model.add(Conv2D(128, kernel_size=3, activation='relu'))
model.add(BatchNormalization())
model.add(Conv2D(128, kernel_size=5, padding='same', activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(Conv2D(256, kernel_size=3, activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(256))
model.add(BatchNormalization())
model.add(Dense(128))
model.add(BatchNormalization())
model.add(Dense(1, activation='sigmoid'))
model.summary()

45
learning_rate = 0.001
optimizer = RMSprop(lr=learning_rate)
model.compile(loss='binary_crossentropy',optimizer=optimizer,metrics=['accuracy'])
learning_rate_reduction = ReduceLROnPlateau(monitor='val_loss',
patience=200,
verbose=1,
factor=0.2)

ch = ModelCheckpoint('models/'+model_name+'.h5', monitor='val_acc', verbose=0,


save_best_only=True, mode='max')
es = EarlyStopping(monitor='val_loss', mode='min', verbose=0, patience=200)
tensorboard_callback =
tf.keras.callbacks.TensorBoard(log_dir="logs/"+datetime.now().strftime("%Y%m%d-%H%M%S"))
epochs = 10
batch_size = 10
history = model.fit(train_datagen.flow(X_train, y_train, batch_size=batch_size),
steps_per_epoch= X_train.shape[0]//batch_size,
epochs=epochs,
validation_data=valid_datagen.flow(X_test, y_test),
validation_steps=50,
verbose = 1,
callbacks=[learning_rate_reduction, es, ch, tensorboard_callback])

46
OUTPUT:

RESULT:

Implementation of Face Recognition using CNN was done successfully.

47
EX.NO.9B: MINI PROJECT (TRANSFER LEARNING)

AIM ;

Implement Transfer Learning models.

Transfer Learning

Transfer learning is a machine learning technique where a model trained on one task
is adapted or fine-tuned for a different but related task. Instead of training a model from scratch for a
new task, transfer learning leverages the knowledge acquired from a source task to improve
performance on a target task. This approach is particularly useful when you have limited data for the
target task or want to save computational resources and time. Here are some key aspects of transfer
learning:

1. Pre-trained Model: Transfer learning often starts with a pre-trained model. These models are
typically deep neural networks, like convolutional neural networks (CNNs) for image tasks or
recurrent neural networks (RNNs) for sequential data. The pre-trained model is trained on a
large dataset for a specific task, such as image classification or natural language processing.
2. Feature Extraction: In transfer learning, the lower layers of the pre-trained model (often called
the feature extractor) are usually frozen, which means they are not updated during the fine-
tuning process. These layers have learned general features that are valuable for various related
tasks. For example, in image recognition, these lower layers might detect edges, textures, or
basic shapes.
3. Fine-tuning: The upper layers of the pre-trained model are replaced or augmented with new
layers to adapt the model for the target task. These new layers are randomly initialized and then
trained on the target dataset. The entire model or a part of it can be fine-tuned depending on the
specific requirements of the task.
4. Choice of Layers: Depending on the similarity between the source and target tasks, you can
choose how many layers to fine-tune. If the tasks are closely related, you may fine-tune fewer
layers. If they are significantly different, you may fine-tune more layers.
5. Data Augmentation: Data augmentation techniques, such as adding noise, rotating, flipping, or
cropping data, can be employed during fine-tuning to further adapt the model to the target task
and prevent overfitting. Transfer learning is widely used in various domains, including:
• Computer Vision: Image classification, object detection, and image segmentation.
• Natural Language Processing: Sentiment analysis, text classification, and named entity
recognition.
• Reinforcement Learning: Fine-tuning pre-trained agents for specific tasks. Common
pre-trained models for transfer learning include VGG, ResNet, Inception, BERT, GPT, and
more. These models have achieved state-of-the-art performance on their source tasks
and can be fine-tuned for a wide range of related applications.

ALGORITHM:

1. VGG 16

Step 1: Import Required Libraries

Step 2: Organize the Dataset

Step 3: Create Image Data Generators: Use ‘ImageDataGenerator’ to load and preprocess
images for training, validation, and testing
48
Step 4: Load the VGG16 Model: Load the pre-trained VGG16 model without its top layers and
freeze its weights

Step 5: Build the Model : Add Custom Layers to the Model by Create a new model by adding
custom layers on top of the VGG16 base model.

Step 6: Compile the Model: Compile your model by specifying an optimizer and loss function

Step 7: Train the Model: Define a function to train your model and fit it to the training data

Step 8:Plot Training and Validation Loss and Accuracy: Visualize how well your model is
performing during training.

Step 9: Evaluate Model on Test Data: Assess how well the model performs on unseen data

Step 10 : Load an Image and Make Predictions: Use the trained model to make predictions on
new images.

2. RESTNET :

Step 1: Import Required Libraries.

Step 2: Load an Example Image: This step is optional but can help you verify that your image
loading works correctly.

Step 4: Split the Dataset into Training, Validation, and Test Sets : Use ‘splitfolders’ to organize
your dataset into training, validation, and test sets.

Step 4 : Create Data Generators: Set up ‘ImageDataGenerator’ to load and preprocess images
for training, validation, and testing.

Step 5 : Load ResNet50 and Build Model: Load the pre-trained ResNet50 model and add
custom layers on top.

Step 6 : Compile the Model: Specify an optimizer and loss function for training.

Step 7 : Train Your Model: Define a function to train your model and fit it to the training data.

Step 8 : Plot Training History: Visualize how well your model is performing during training.

Step 9 : Evaluate Your Model: Assess how well your model performs on unseen data.

Step 10 : Load an Image for Prediction: Use your trained model to make predictions on new
images.

Step 11 : Save and Load Your Model: Save the trained model to disk and load it .

3. ALEXNET :

Step 1: Import Required Libraries

Step 2 : Split the Dataset: Use splitfolders to organize your dataset into training, validation,
and test sets.

Step 3 : Create Data Generators: Set up ImageDataGenerator to load and preprocess images
for training, validation, and testing.

Step 4: Build the CNN Model Architecture : Create a custom CNN model using Keras.

Step 5 : Compile the Model: Specify an optimizer and loss function for training.

49
Step 6 : Train Your Model: Launch the training process for a specified number of epochs (e.g.,
10).

Step 7 : Plot Training Accuracy: Visualize how well your model is performing during training.

Step 8 : Evaluate Your Model: Assess how well your trained CNN performs on unseen test
data.

Step 9 : Make Predictions on New Images: Use your trained CNN to make predictions on new
images.

4. DENSENET:

Step 1: Import Required Libraries

Step 2: Split the Dataset into Training, Validation, and Test Sets : Use ‘splitfolders’ to organize
your dataset into training, validation, and test sets.

Step 3: Create Image Data Generators: Set up ‘ImageDataGenerator’ to load and preprocess
images for training, validation, and testing.

Step 4: Load the DenseNet169 Model and Build Your Custom Model: Load the pre-trained
DenseNet169 model and add custom layers on top.

Step 5: Compile the Model : Specify an optimizer and loss function for training.

Step 6: Train the Model : Launch the training process for a specified number of epochs (e.g.,
10).

Step 7: Visualize Training History ; Visualize how well your model is performing during
training.

Step 8: Evaluate the Model on Test Data : Assess how well your trained DenseNet performs
on unseen test data.

Step 9: Make Predictions on New Images : Use your trained DenseNet to make predictions on
new images.

VGG16 CODE:

import numpy as np
import cv2 as cv
import os
import splitfolders
import matplotlib.pyplot as plt
# tensorflow
import tensorflow.keras as keras
import tensorflow as tf
# image processing
from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img
# model / neural network
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential, Model
from keras.applications.vgg16 import VGG16
from tensorflow.keras.applications.vgg16 import preprocess_input
datagen = ImageDataGenerator()
# define classes name
class_names = ['daisy','dandelion','roses','sunflowers','tulips']
50
# training data
train_generator = datagen.flow_from_directory(
directory="C:/Users/root/flower_data/data-split/train/",
classes = class_names,
target_size=(224, 224),
batch_size=32,
class_mode="binary",
)
# validation data
valid_generator = datagen.flow_from_directory(
directory="C:/Users/root/flower_data/data-split/val/",
classes = class_names,
target_size=(224, 224),
batch_size=32,
class_mode="binary",
)
# test data
test_generator = datagen.flow_from_directory(
directory="C:/Users/root/flower_data/data-split/test/",
classes = class_names,
target_size=(224, 224),
batch_size=32,
class_mode="binary",
)
## Loading VGG16 model
base_model = VGG16(weights="imagenet", include_top=False,␣
↪input_shape=(224,224,3))
## will not train base mode
base_model.trainable = False
from tensorflow.keras import layers, models
flatten_layer = layers.Flatten()
dense_layer_1 = layers.Dense(50, activation='relu')
dense_layer_2 = layers.Dense(20, activation='relu')
prediction_layer = layers.Dense(5, activation='softmax')
model = models.Sequential([
base_model,
flatten_layer,
dense_layer_1,
dense_layer_2,
prediction_layer
])

# define training function


def trainModel(model, epochs, optimizer):
batch_size = 32
model.compile(optimizer=optimizer, loss="sparse_categorical_crossentropy",␣
↪metrics=["accuracy"])
return model.fit(train_generator, validation_data=valid_generator,␣
↪epochs=epochs, batch_size=batch_size)
# launch the training
model_history = trainModel(model = model, epochs = 10, optimizer = "Adam")
loss_train_curve = model_history.history["loss"]
loss_val_curve = model_history.history["val_loss"]
plt.plot(loss_train_curve, label = "Train")
plt.plot(loss_val_curve, label = "Validation")

51
plt.legend(loc = 'upper right')
plt.title("Loss")
plt.show()
acc_train_curve = model_history.history["accuracy"]
acc_val_curve = model_history.history["val_accuracy"]
plt.plot(acc_train_curve, label = "Train")
plt.plot(acc_val_curve, label = "Validation")
plt.legend(loc = 'lower right')
plt.title("Accuracy")
plt.show()
test_loss, test_acc = model.evaluate(test_generator)
print("The test loss is: ", test_loss)
print("The best accuracy is: ", test_acc*100)
img = tf.keras.preprocessing.image.load_img('C:/Users/root/flower_data/
↪data-split/test/tulip/112428665_d8f3632f36_n.jpg', target_size=(224, 224))
img_array = tf.keras.preprocessing.image.img_to_array(img)
img_array = np.array([img_array])
img
# generate predictions for samples
predictions = model.predict(img_array)
print(predictions)
# generate argmax for predictions
class_id = np.argmax(predictions, axis = 1)
print(class_id)
# transform classes number into classes name
class_names[class_id.item()]

OUTPUT:

52
53
RESTNET CODE :

#display, transform, read, split ...


import numpy as np
import cv2 as cv
import os
import splitfolders
import matplotlib.pyplot as plt
# tensorflow
import tensorflow.keras as keras
import tensorflow as tf
# image processing
from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img
# model / neural network
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.applications.resnet50 import preprocess_input
# daisy
img_daisy = image.load_img("flower_data/daisy/100080576_f52e8ee070_n.jpg")
img_daisy
# split data in a new folder named data-split
splitfolders.ratio("C:/Users/root/flower_data/", output="C:/Users/root/flower_data/data-s
plit", seed=1337, ratio=(0.7, 0.2, 0.1), group_prefix=None, move=False)
datagen = ImageDataGenerator()
# define classes name
class_names = ['daisy','dandelion','roses','sunflowers','tulips']
# training data
train_generator = datagen.flow_from_directory(

54
directory="C:/Users/root/flower_data/data-split/train/",
classes = class_names,
target_size=(224, 224),
batch_size=32,
class_mode="binary",
)
# validation data
valid_generator = datagen.flow_from_directory(
directory="C:/Users/root/flower_data/data-split/val/",
classes = class_names,
target_size=(224, 224),
batch_size=32,
class_mode="binary",
)
# test data
test_generator = datagen.flow_from_directory(
directory="C:/Users/root/flower_data/data-split/test/",
classes = class_names,
target_size=(224, 224),
batch_size=32,
class_mode="binary",
)
# ResNet50 model
resnet_50 = ResNet50(include_top=False, weights='imagenet', input_shape=(224,224,3))
for layer in resnet_50.layers:
layer.trainable = False
# build the entire model
x = resnet_50.output
x = layers.GlobalAveragePooling2D()(x)
x = layers.Dense(512, activation='relu')(x)
x = layers.Dropout(0.5)(x)
x = layers.Dense(256, activation='relu')(x)
x = layers.Dropout(0.5)(x)
x = layers.Dense(128, activation='relu')(x)
x = layers.Dropout(0.5)(x)
x = layers.Dense(64, activation='relu')(x)
x = layers.Dropout(0.5)(x)
predictions = layers.Dense(5, activation='softmax')(x)
model = Model(inputs = resnet_50.input, outputs = predictions)
# define training function
def trainModel(model, epochs, optimizer):
batch_size = 32
model.compile(optimizer=optimizer, loss="sparse_categorical_crossentropy", metrics=[
"accuracy"])
return model.fit(train_generator, validation_data=valid_generator, epochs=epochs, ba
tch_size=batch_size)
# launch the training
model_history = trainModel(model = model, epochs = 10, optimizer = "Adam")
loss_train_curve = model_history.history["loss"]
loss_val_curve = model_history.history["val_loss"]
plt.plot(loss_train_curve, label = "Train")
plt.plot(loss_val_curve, label = "Validation")
plt.legend(loc = 'upper right')
plt.title("Loss")
plt.show()
acc_train_curve = model_history.history["accuracy"]
55
acc_val_curve = model_history.history["val_accuracy"]
plt.plot(acc_train_curve, label = "Train")
plt.plot(acc_val_curve, label = "Validation")
plt.legend(loc = 'lower right')
plt.title("Accuracy")
plt.show()
test_loss, test_acc = model.evaluate(test_generator)
print("The test loss is: ", test_loss)
print("The best accuracy is: ", test_acc*100)
img = tf.keras.preprocessing.image.load_img('C:/Users/root/flower_data/data-split/test/tu
lip/112428665_d8f3632f36_n.jpg', target_size=(224, 224))
img_array = tf.keras.preprocessing.image.img_to_array(img)
img_array = np.array([img_array])
img
# generate predictions for samples
predictions = model.predict(img_array)
print(predictions)
# generate argmax for predictions
class_id = np.argmax(predictions, axis = 1)
print(class_id)
# transform classes number into classes name
class_names[class_id.item()]
model.save('my_flower_model.keras')
model = tf.keras.models.load_model('my_flower_model.keras')
model.summary()

56
57
OUTPUT:

ALEXNET CODE :

import numpy as np
import cv2 as cv
import os
import splitfolders
import matplotlib.pyplot as plt
# tensorflow
import tensorflow.keras as keras
import tensorflow as tf
# image processing
from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img
# model / neural network
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import BatchNormalization, Activation, Conv2D
from tensorflow.keras.layers import Dense, Flatten, MaxPooling2D, Dense, Dropout
datagen = ImageDataGenerator()
# define classes name
class_names = ['daisy','dandelion','roses','sunflowers','tulips']
# training data
train_generator = datagen.flow_from_directory(
directory="C:/Users/root/flower_data/data-split/train/",
classes = class_names,
target_size=(224, 224),
batch_size=32,
class_mode="binary",
58
)
# validation data
valid_generator = datagen.flow_from_directory(
directory="C:/Users/root/flower_data/data-split/val/",
classes = class_names,
target_size=(224, 224),
batch_size=32,
class_mode="binary",
)
# test data
test_generator = datagen.flow_from_directory(
directory="C:/Users/root/flower_data/data-split/test/",
classes = class_names,
target_size=(224, 224),
batch_size=32,
class_mode="binary",
)
model = Sequential()
# 1st Convolutional Layer
model.add(Conv2D(filters = 96, input_shape = (224, 224, 3),
kernel_size = (11, 11), strides = (4, 4),
padding = 'valid'))
model.add(Activation('relu'))
# Max-Pooling
model.add(MaxPooling2D(pool_size = (2, 2),
strides = (2, 2), padding = 'valid'))
# Batch Normalisation
model.add(BatchNormalization())
# 2nd Convolutional Layer
model.add(Conv2D(filters = 256, kernel_size = (11, 11),
strides = (1, 1), padding = 'valid'))
model.add(Activation('relu'))
# Max-Pooling
model.add(MaxPooling2D(pool_size = (2, 2), strides = (2, 2),
padding = 'valid'))
# Batch Normalisation
model.add(BatchNormalization())
# 3rd Convolutional Layer
model.add(Conv2D(filters = 384, kernel_size = (3, 3),
strides = (1, 1), padding = 'valid'))
model.add(Activation('relu'))
# Batch Normalisation
model.add(BatchNormalization())
# 4th Convolutional Layer
model.add(Conv2D(filters = 384, kernel_size = (3, 3),
strides = (1, 1), padding = 'valid'))
model.add(Activation('relu'))
# Batch Normalisation
model.add(BatchNormalization())
# 5th Convolutional Layer
model.add(Conv2D(filters = 256, kernel_size = (3, 3),
strides = (1, 1), padding = 'valid'))
model.add(Activation('relu'))
# Max-Pooling
model.add(MaxPooling2D(pool_size = (2, 2), strides = (2, 2),
padding = 'valid'))
59
# Batch Normalisation
model.add(BatchNormalization())
# Flattening
model.add(Flatten())
# 1st Dense Layer
model.add(Dense(4096, input_shape = (224*224*3, )))
model.add(Activation('relu'))
# Add Dropout to prevent overfitting
model.add(Dropout(0.4))
# Batch Normalisation
model.add(BatchNormalization())
# 2nd Dense Layer
model.add(Dense(4096))
model.add(Activation('relu'))
# Add Dropout
model.add(Dropout(0.4))
# Batch Normalisation
model.add(BatchNormalization())
# Output Softmax Layer
model.add(Dense(len(class_names)))
model.add(Activation('softmax'))
model.summary()

60
# define training function
def trainModel(model, epochs, optimizer):
batch_size = 32
model.compile(optimizer=optimizer, loss="sparse_categorical_crossentropy",␣
↪metrics=["accuracy"])
return model.fit(train_generator, validation_data=valid_generator,␣
↪epochs=epochs, batch_size=batch_size)
# launch the training
model_history = trainModel(model = model, epochs = 10, optimizer = "Adam")
acc_train_curve = model_history.history["accuracy"]
acc_val_curve = model_history.history["val_accuracy"]
plt.plot(acc_train_curve, label = "Train")
plt.plot(acc_val_curve, label = "Validation")
plt.legend(loc = 'lower right')
plt.title("Accuracy")
plt.show()
test_loss, test_acc = model.evaluate(test_generator)
print("The test loss is: ", test_loss)
print("The best accuracy is: ", test_acc*100)
img = tf.keras.preprocessing.image.load_img('C:/Users/root/flower_data/
↪data-split/test/tulip/112428665_d8f3632f36_n.jpg', target_size=(224, 224))
img_array = tf.keras.preprocessing.image.img_to_array(img)
img
# generate predictions for samples
predictions = model.predict(img_array)
print(predictions)
# generate argmax for predictions
class_id = np.argmax(predictions, axis = 1)
print(class_id)
# transform classes number into classes name
class_names[class_id.item()

61
OUTPUT:

DENSENET CODE :

# display, transform, read, split ...


import numpy as np
import cv2 as cv
import os
import splitfolders
import matplotlib.pyplot as plt
# tensorflow
import tensorflow.keras as keras
import tensorflow as tf
# image processing
from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img
# model / neural network
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential, Model
from keras.applications.vgg16 import VGG16, preprocess_input
from tensorflow.keras.applications.densenet import DenseNet169
from tensorflow.keras.layers import InputLayer, BatchNormalization, Dropout, Flatten, Dense,
Activation, MaxPool2D, Conv2D
datagen = ImageDataGenerator()

# define classes name


class_names = ['daisy','dandelion','roses','sunflowers','tulips']

# training data
train_generator = datagen.flow_from_directory(
directory="C:/Users/root/flower_data/data-split/train/",
classes = class_names,

62
target_size=(224, 224),
batch_size=32,
class_mode="binary",
)

# validation data
valid_generator = datagen.flow_from_directory(
directory="C:/Users/root/flower_data/data-split/val/",
classes = class_names,
target_size=(224, 224),
batch_size=32,
class_mode="binary",
)

# test data
test_generator = datagen.flow_from_directory(
directory="C:/Users/root/flower_data/data-split/test/",
classes = class_names,
target_size=(224, 224),
batch_size=32,
class_mode="binary",
)
base_model = DenseNet169(input_shape=(224,224,3),
include_top=False,
weights="imagenet")
for layer in base_model.layers:
layer.trainable=False
model=Sequential()
model.add(base_model)
model.add(Dropout(0.5))
model.add(tf.keras.layers.GlobalAveragePooling2D())
model.add(Flatten())
model.add(BatchNormalization())
model.add(Dense(64,kernel_initializer='he_uniform'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(64,kernel_initializer='he_uniform'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(64,kernel_initializer='he_uniform'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(32,kernel_initializer='he_uniform'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(32,kernel_initializer='he_uniform'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Dense(5,activation='softmax'))

# define training function


def trainModel(model, epochs, optimizer):
63
batch_size = 32
model.compile(optimizer=optimizer, loss="sparse_categorical_crossentropy", metrics=[
"accuracy"])
return model.fit(train_generator, validation_data=valid_generator, epochs=epochs, ba
tch_size=batch_size)

# launch the training


model_history = trainModel(model = model, epochs = 10, optimizer = "Adam")
loss_train_curve = model_history.history["loss"]
loss_val_curve = model_history.history["val_loss"]
plt.plot(loss_train_curve, label = "Train")
plt.plot(loss_val_curve, label = "Validation")
plt.legend(loc = 'upper right')
plt.title("Loss")
plt.show()
acc_train_curve = model_history.history["accuracy"]
acc_val_curve = model_history.history["val_accuracy"]
plt.plot(acc_train_curve, label = "Train")
plt.plot(acc_val_curve, label = "Validation")
plt.legend(loc = 'lower right')
plt.title("Accuracy")
plt.show()

test_loss, test_acc = model.evaluate(test_generator)


print("The test loss is: ", test_loss)
print("The best accuracy is: ", test_acc*100)
img = tf.keras.preprocessing.image.load_img('C:/Users/root/flower_data/data-split/test/tu
lip/112428665_d8f3632f36_n.jpg', target_size=(224, 224))
img_array = tf.keras.preprocessing.image.img_to_array(img)
img_array = np.array([img_array])
img

# generate predictions for samples


predictions = model.predict(img_array)
print(predictions)

# generate argmax for predictions


class_id = np.argmax(predictions, axis = 1)
print(class_id)

# transform classes number into classes name


class_names[class_id.item()]

64
OUTPUT:

RESULT:
Implementation of transfer learning models has been executed successfully.

65
EX.NO.9C: MINI PROJECT (FLOWER RECOGNITION)

AIM :
Implement face recognition using CNN.

ALGORITHM:
Step 1: Import Required Libraries

Step 2: Load an Example Image: Load an example image to verify that your image loading works
correctly .

Step 3: Split the Dataset into Training, Validation, and Test Sets: Use ‘splitfolders’ to organize your
dataset into training, validation, and test sets.

Step 4: Create Image Data Generators : Set up ‘ImageDataGenerator’ to load and preprocess images for
training, validation, and testing.

Step 5: Load the ResNet50 Model and Build the Custom Model : Load the pre-trained ResNet50 model
without its top layers and add custom layers on top.

Step 6: Compile the Model : Specify an optimizer and loss function for training.

Step 7: Train the Model: Launch the training process for a specified number of epochs (e.g., 10).

Step 8: Visualize Training History :Visualize how well your model is performing during training.

Step 9: Evaluate the Model on Test Data : Assess how well your trained ResNet performs on unseen test
data.

Step 10: Make Predictions on New Images : Use your trained ResNet to make predictions on new images.

Step 11: Save and Load Your Model : Save your trained model to disk and load it .

FLOWER RECOGNITION USING CNN CODE :

# display, transform, read, split ...


import numpy as np
import cv2 as cv
import os
import splitfolders
import matplotlib.pyplot as plt
# tensorflow
import tensorflow.keras as keras
import tensorflow as tf
# image processing
from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img
# model / neural network
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.applications.resnet50 import preprocess_input
# daisy
img_daisy = image.load_img("flower_data/daisy/100080576_f52e8ee070_n.jpg")
img_daisy
66
# split data in a new folder named data-split
splitfolders.ratio("C:/Users/root/flower_data/", output="C:/Users/root/
flower_data/data-split", seed=1337, ratio=(0.7, 0.2, 0.1),␣
group_prefix=None, move=False)
datagen = ImageDataGenerator()
# define classes name
class_names = ['daisy','dandelion','roses','sunflowers','tulips']
# training data
train_generator = datagen.flow_from_directory(
directory="C:/Users/root/flower_data/data-split/train/",
classes = class_names,
target_size=(224, 224),
batch_size=32,
class_mode="binary",
)
# validation data
valid_generator = datagen.flow_from_directory(
directory="C:/Users/root/flower_data/data-split/val/",
classes = class_names,
target_size=(224, 224),
batch_size=32,
class_mode="binary",
)
# test data
test_generator = datagen.flow_from_directory(
directory="C:/Users/root/flower_data/data-split/test/",
classes = class_names,
target_size=(224, 224),
batch_size=32,
class_mode="binary",
)
# ResNet50 model
resnet_50 = ResNet50(include_top=False, weights='imagenet',␣
↪input_shape=(224,224,3))
for layer in resnet_50.layers:
layer.trainable = False
# build the entire model
x = resnet_50.output
x = layers.GlobalAveragePooling2D()(x)
x = layers.Dense(512, activation='relu')(x)
x = layers.Dropout(0.5)(x)
x = layers.Dense(256, activation='relu')(x)
x = layers.Dropout(0.5)(x)
x = layers.Dense(128, activation='relu')(x)
x = layers.Dropout(0.5)(x)
x = layers.Dense(64, activation='relu')(x)
x = layers.Dropout(0.5)(x)
predictions = layers.Dense(5, activation='softmax')(x)
model = Model(inputs = resnet_50.input, outputs = predictions)
# define training function
def trainModel(model, epochs, optimizer):
batch_size = 32
model.compile(optimizer=optimizer, loss="sparse_categorical_crossentropy",␣
↪metrics=["accuracy"])
return model.fit(train_generator, validation_data=valid_generator,␣

67
↪epochs=epochs, batch_size=batch_size)
# launch the training
model_history = trainModel(model = model, epochs = 10, optimizer = "Adam")
loss_train_curve = model_history.history["loss"]
loss_val_curve = model_history.history["val_loss"]
plt.plot(loss_train_curve, label = "Train")
plt.plot(loss_val_curve, label = "Validation")
plt.legend(loc = 'upper right')
plt.title("Loss")
plt.show()
acc_train_curve = model_history.history["accuracy"]
acc_val_curve = model_history.history["val_accuracy"]
plt.plot(acc_train_curve, label = "Train")
plt.plot(acc_val_curve, label = "Validation")
plt.legend(loc = 'lower right')
plt.title("Accuracy")
plt.show()
test_loss, test_acc = model.evaluate(test_generator)
print("The test loss is: ", test_loss)
print("The best accuracy is: ", test_acc*100)
img = tf.keras.preprocessing.image.load_img('C:/Users/root/flower_data/
↪data-split/test/tulip/112428665_d8f3632f36_n.jpg', target_size=(224, 224))
img_array = tf.keras.preprocessing.image.img_to_array(img)
img_array = np.array([img_array])
img
# generate predictions for samples
predictions = model.predict(img_array
print(predictions)
# generate argmax for predictions
class_id = np.argmax(predictions, axis = 1)
print(class_id)
# transform classes number into classes name
class_names[class_id.item()]
model.save('my_flower_model.keras')
model = tf.keras.models.load_model('my_flower_model.keras')
model.summary()

68
RESULT:
Implementation of face recognition using CNN has been executed successfully.

69

You might also like