MUTHAYAMMAL ENGINEERING COLLEGE
(AUTONOMOUS)
RASIPURAM – 637 408.
DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND DATA SCIENCE
ACADEMIC YEAR 2024-25
21ADC28 DEEP LEARNING LABORATORY
LAB MANUAL
FOR
VI SEMESTER
Name:
Year/Sem: Branch:
Roll No/Reg No:
MUTHAYAMMAL ENGINEERING COLLEGE
(AUTONOMOUS)
DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND DATA SCIENCE
Subject Name : DEEP LEARNING LABORATORY
Subject Code : 21ADC28
Year : 2024-25
PREPARED BY REVIEWED BY APPROVED BY
Name
Designation
Signature
Date
21ADC28 DEEP LEARNING LABORATORY
OBJECTIVES: The student should be made to:
1. To learn deep neural networks and apply for simple problems
2. To learn and apply Convolution Neural Network for image processing
3. To learn and apply Recurrent Neural Network and its variants for text analysis
4. To augment data using generative models
5. To explore real world applications with deep neural networks
LIST OF EXPERIMENTS:
1. Solving XOR problem using multilayer perceptron
2. Implement character and Digit recognition using ANN
3. Implement the analysis of X-ray image using autoencoders
4. Implement Speech Recognition using NLP
5. Develop a code to design object detection and classification for traffic analysis
using CNN
6. Implement online fraud detection of share market data using any one of the data
analytics tools
7. Implement image Augmentation using deep RBM
8. Implement Sentiment analysis using LSTM
9. Mini project: Number plate recognition of traffic video analysis
TOTAL: 30 PERIODS
OUTCOMES: At the end of the course, the student should be able to
1. Apply deep neural network for simple problems
2. Apply convolution Neural Network for image processing
3. Apply Recurrent Neural Network and its variants for text analysis
4. Apply generative models for data augmentation
5. Develop a real world application using suitable deep neural networks
MUTHAYAMMAL ENGINEERING COLLEGE
(AUTONOMOUS)
RASIPURAM – 637 408.
DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND DATA SCIENCE
Subject Name : DEEP LEARNING LABORATORY
University : Anna University, Chennai
Subject Code : 21ADC28
Year of Syllabus : 2024 – 2025
PREPARED BY REVIEWED BY APPROVED BY
Name of the
Faculty
Designation
Signature
Date
INDEX
S.No. Date Name of the Experiment Marks Signature
EX. NO. : 1 Solving XOR problem using Multilayer perception
DATE :
Aim:
To write the program for Solving XOR problem using Multilayer perception.
Procedure:
Step1: Import the required Python libraries
Step2: Define Activation Function: Sigmoid Function
Step3: Initialize neural network parameters (weights, bias) and define model hyperparameters (number
of iterations, learning rate)
Step4: Forward Propagation
Step5: Backward Propagation
Step6: Update weight and bias parameters
Step7: Train the learning model
Step8: Plot Loss value vs Epoch
Step9: Test the model performance
Program:
# Import Python Libraries
import numpy as np
from matplotlib import pyplot as plt
# Sigmoid Function
def sigmoid(z):
return 1 / (1 + np.exp(-z))
# Initialization of the neural network parameters
# Initialized all the weights in the range of between 0 and 1
# Bias values are initialized to 0
def initializeParameters(inputFeatures, neuronsInHiddenLayers, outputFeatures):
W1 = np.random.randn(neuronsInHiddenLayers, inputFeatures)
W2 = np.random.randn(outputFeatures, neuronsInHiddenLayers)
b1 = np.zeros((neuronsInHiddenLayers, 1))
b2 = np.zeros((outputFeatures, 1))
parameters = {"W1": W1, "b1": b1, "W2": W2, "b2": b2}
return parameters
# Forward Propagation
def forwardPropagation(X, Y, parameters):
m = X.shape[1]
W1 = parameters["W1"]
W2 = parameters["W2"]
b1 = parameters["b1"]
b2 = parameters["b2"]
Z1 = np.dot(W1, X) + b1
A1 = sigmoid(Z1)
Z2 = np.dot(W2, A1) + b2
A2 = sigmoid(Z2)
cache = (Z1, A1, W1, b1, Z2, A2, W2, b2)
logprobs = np.multiply(np.log(A2), Y) + np.multiply(np.log(1 - A2), (1 - Y))
cost = -np.sum(logprobs) / m
return cost, cache, A2
# Backward Propagation
def backwardPropagation(X, Y, cache):
m = X.shape[1]
(Z1, A1, W1, b1, Z2, A2, W2, b2) = cache
dZ2 = A2 - Y
dW2 = np.dot(dZ2, A1.T) / m
db2 = np.sum(dZ2, axis=1, keepdims=True)
dA1 = np.dot(W2.T, dZ2)
dZ1 = np.multiply(dA1, A1 * (1 - A1))
dW1 = np.dot(dZ1, X.T) / m
db1 = np.sum(dZ1, axis=1, keepdims=True) / m
gradients = {"dZ2": dZ2, "dW2": dW2, "db2": db2, "dZ1": dZ1, "dW1": dW1, "db1": db1}
return gradients
# Updating the weights based on the negative gradients
def updateParameters(parameters, gradients, learningRate):
parameters["W1"] = parameters["W1"] - learningRate * gradients["dW1"]
parameters["W2"] = parameters["W2"] - learningRate * gradients["dW2"]
parameters["b1"] = parameters["b1"] - learningRate * gradients["db1"]
parameters["b2"] = parameters["b2"] - learningRate * gradients["db2"]
return parameters
# Model to learn the XOR truth table
X = np.array([[0, 0, 1, 1], [0, 1, 0, 1]]) # XOR input
Y = np.array([[0, 1, 1, 0]]) # XOR output
# Define model parameters
neuronsInHiddenLayers = 2 # number of hidden layer neurons
inputFeatures = X.shape[0] # number of input features
outputFeatures = Y.shape[0] # number of output features
parameters = initializeParameters(inputFeatures, neuronsInHiddenLayers, outputFeatures)
epoch = 100000
learningRate = 0.01
losses = np.zeros((epoch, 1))
for i in range(epoch):
losses[i, 0], cache, A2 = forwardPropagation(X, Y, parameters)
gradients = backwardPropagation(X, Y, cache)
parameters = updateParameters(parameters, gradients, learningRate)
# Evaluating the performance
plt.figure()
plt.plot(losses)
plt.xlabel("EPOCHS")
plt.ylabel("Loss value")
plt.show()
# Testing
X_test = np.array([[1, 1, 0, 0], [0, 1, 0, 1]]) # XOR input
cost, _, A2 = forwardPropagation(X_test, Y, parameters)
prediction = (A2 > 0.5) * 1.0
print("Predictions:\n", prediction)
OUTPUT :
[[ 1. 0. 0. 1.]]
Result
Thus the Python program to implement Solving XOR problem using Multilayer perception was
executed successfully.
EX. NO. : 2 Implement character and Digit Recognition using ANN
DATE :
Aim:
To write the Program to implement character and digit Recognition using ANN.
Procedure:
Step 1: Import necessary modules and packages for the program
Step 2: Load the MNIST dataset and split it into training and testing sets
Step 3: Reshape and normalize the training and testing data
Step 4: Convert the training and testing labels into categorical format
Step 5: Set batch size, number of classes, and number of epochs for the model
Step 6: Define the architecture of the convolutional neural network model
Step 7: Compile the model using the categorical cross-entropy loss function, Adadelta optimizer,
and accuracy metric
Step 8: Train the model using the training data and validation data
Step 9: Print the test loss and test accuracy of the trained model
Step 10 : Save the trained model as a h5 file.
Program :
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
# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
print(x_train.shape, y_train.shape)
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
batch_size = 128
num_classes = 10
epochs = 10
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='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,optimizer=keras.optimizers.
Adadelta(),metrics=['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")
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
model.save('mnist.h5')
print("Saving the model as mnist.h5")
OUTPUT :
Result
Thus the Python program to Implement character and Digit Recognition using ANN was executed
successfully.
EX. NO. : 3 Implement the analysis of X-ray images using autoencoders
DATE :
Aim:
To write the program to implement the analysis of X-ray images using autoencoders.
Procedure:
1. Gather a dataset of X-ray images.
2. Preprocess the data by resizing, normalizing pixel values, and splitting into training,
validation, and testing sets.
3. Define the architecture of the autoencoder, including encoder and decoder components.
4. Compile the autoencoder with an appropriate loss function and optimizer.
5. Train the autoencoder on the training set of X-ray images.
6. Use the validation set to assess the performance of the autoencoder.
7. Use visualization techniques to analyze the quality of the reconstructed X-ray images.
8. Reconstruct the test set of X-ray images using the trained autoencoder.
9. Compute the reconstruction error between the original test set of X-ray images and their
reconstructed images.
10. Set a threshold for the reconstruction error above which an X-ray image is considered an
anomaly.
11. Explore the X-ray images identified as anomalies by the autoencoder.
12. Use techniques such as clustering and visualization to gain insight into the types of anomalies
present in the X-ray images.
13. Adjust the architecture or training procedure based on the analysis of the anomalies to
improve the performance of the autoencoder.
Program :
from mdai import Client
import mdai.visualize as visualize
# Initialize the MDAI client
mdai_client = Client(domain='public.md.ai', access_token="468b79fc61a3ffb0ecedb77dcf393ed3")
# Load the project
project = mdai_client.project('PVq9raBJ', path='./lesson1-data')
# Display label groups
project.show_label_groups()
# Map label IDs to class IDs
labels_dict = {
'L_38Y7Jl': 0, # Abdomen
'L_z8xEkB': 1 # Chest
}
print("Labels dictionary:", labels_dict)
# Set labels dictionary
project.set_labels_dict(labels_dict)
# Display available datasets
project.show_datasets()
# Create and prepare the training dataset
train_dataset = project.get_dataset_by_name('TRAIN')
train_dataset.prepare()
train_image_ids = train_dataset.get_image_ids()
print(f"Number of training images: {len(train_image_ids)}")
# Create and prepare the validation dataset
val_dataset = project.get_dataset_by_name('VAL')
val_dataset.prepare()
val_image_ids = val_dataset.get_image_ids()
print(f"Number of validation images: {len(val_image_ids)}")
# Visualize some training images
visualize.display_images(train_image_ids[:2], cols=2)
# Visualize some validation images
visualize.display_images(val_image_ids[:2], cols=2)
OUTPUT :
Result :
Thus the Python program to Implement the analysis of X-ray images using autoencoders was
executed successfully.
EX. NO. : 4 Implement Speech Recognition using NLP
DATE :
Aim:
To Write the program to Implement Speech Recognition using NLP
Procedure:
Step 1: Import the 'speech_recognition' module as 'sr'.
Step 2: Create a 'Recognizer' object using 'sr.Recognizer()'.
Step 3: Use the microphone as the audio source by creating a 'Microphone' object using
'sr.Microphone()'.
Step 4: Use the 'listen' method of the 'Recognizer' object to record the audio from the microphone.
Step 5: Use the 'recognize_google' method of the 'Recognizer' object to recognize the speech from the
recorded audio.
Step 6: Print the recognized speech.
Step 7: Handle 'UnknownValueError' and 'RequestError' exceptions that might occur while
recognizing speech.
Program :
import speech_recognition as sr
# Create a recognizer object
r = sr.Recognizer()
# Use microphone as the audio source
with sr.Microphone() as source:
print("Speak now...")
audio = r.listen(source)
# Recognize speech using Google Speech Recognition
try:
print("You said: " + r.recognize_google(audio))
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
OUTPUT :
You said : Deep learning is an Interesting subject
Result :
Thus the Python program to Implement Speech Recognition using NLP was executed
successfully.
EX. NO. : 5 Develop a code to design Object detection and Classification for
traffic analysis using CNN
DATE :
Aim:
To Write the program to design Object detection and Classification for traffic analysis using
CNN
Procedure :
1. Collect a diverse dataset of traffic scenarios, including different types of objects.
2. Preprocess the dataset by resizing, normalizing, and augmenting the images.
3. Split the dataset into training, validation, and testing sets.
4. Select an appropriate CNN architecture for object detection and classification, such as YOLO or
Faster R-CNN.
5. Train the CNN model on the training set using a loss function and optimizer.
6. Evaluate the trained model on the testing set using metrics like accuracy, precision, recall, and F1
score.
7. Deploy the trained model on new traffic data and measure its performance on real-world
scenarios.
Program :
import numpy as np
import matplotlib.pyplot as plt
import keras
import cv2
from keras.models import Sequential
from keras.optimizers import Adam
from keras.layers import Dense
from keras.utils.np_utils import to_categorical
from keras.layers import Dropout, Flatten
from keras.layers.convolutional import Conv2D, MaxPooling2D
import pickle
import random
import pandas as pd
np.random.seed(0)
!git clone https://bitbucket.org/jadslim/german-traffic-signs
with open('german-traffic-signs/train.p','rb') as f:
train_data = pickle.load(f)
with open('german-traffic-signs/valid.p','rb') as f:
val_data = pickle.load(f)
with open('german-traffic-signs/test.p','rb') as f:
test_data = pickle.load(f)
X_train, y_train = train_data['features'], train_data['labels']
X_val, y_val = val_data['features'], val_data['labels']
X_test, y_test = test_data['features'], test_data['labels']
print(X_train.shape)
print(X_val.shape)
print(X_test.shape)
assert(X_train.shape[0] == y_train.shape[0]), "The number of images is not equal to the number of
labels"
assert(X_val.shape[0] == y_val.shape[0]), "The number of images is not equal to the number of
labels"
assert(X_test.shape[0] == y_test.shape[0]), "The number of images is not equal to the number of
labels"
assert(X_train.shape[1:] == (32, 32, 3)), "The dimensions of the image is not 32*32*3"
assert(X_val.shape[1:] == (32, 32, 3)), "The dimensions of the image is not 32*32*3"
assert(X_test.shape[1:] == (32, 32, 3)), "The dimensions of the image is not 32*32*3"
data = pd.read_csv('german-traffic-signs/signnames.csv')
num_of_samples = []
cols = 5
num_classes = 43
fig, axs = plt.subplots(nrows = num_classes, ncols = cols, figsize = (5, 50))
fig.tight_layout()
for i in range(cols):
for j, row in data.iterrows():
x_selected = X_train[y_train == j]
axs[j][i].imshow(x_selected[random.randint(0, (len(x_selected)-1)), :, :], cmap =
plt.get_cmap("gray"))
axs[j][i].axis("off")
if i == 2:
axs[j][i].set_title(str(j) + "_" + row["SignName"])
num_of_samples.append(len(x_selected))
print(num_of_samples)
plt.figure(figsize = (12, 4))
plt.bar(range(0, num_classes), num_of_samples)
plt.title("Training Dataset Distribution")
plt.xlabel("Class number")
plt.ylabel("Number of images")
plt.imshow(X_train[1000])
plt.axis('off')
print(X_train[1000].shape)
print(y_train[1000])
def grayscale(img):
image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
plt.axis('off')
return imageimg = grayscale(X_train[1000])
plt.imshow(img, cmap = 'gray')
print(img.shape)
def equalize(img):
img = cv2.equalizeHist(img)
return img
img = equalize(img)
plt.imshow(img, cmap = 'gray')
plt.axis('off')
print(img.shape)
def preprocessing(img):
img = grayscale(img)
img = equalize(img)
img = img/255
return img
X_train = np.array(list(map(preprocessing, X_train)))
X_val = np.array(list(map(preprocessing, X_val)))
X_test = np.array(list(map(preprocessing, X_test)))
X_train = X_train.reshape(34799, 32, 32, 1)
X_val = X_val.reshape(4410, 32, 32, 1)
X_test = X_test.reshape(12630, 32, 32, 1)
from keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator(width_shift_range = 0.1,
height_shift_range = 0.1,
zoom_range = 0.2,
shear_range = 0.1,
rotation_range = 10)
datagen.fit(X_train)
batches = datagen.flow(X_train, y_train, batch_size = 20)
X_batch, y_batch = next(batches)
fig, axs = plt.subplots(1, 15, figsize = (20, 5))
fig.tight_layout()
for i in range(15):
axs[i].imshow(X_batch[i].reshape(32, 32))
axs[i].axis('off')
y_train = to_categorical(y_train, 43)
y_val = to_categorical(y_val, 43)
y_test = to_categorical(y_test, 43)
def neural_model():
model = Sequential()
model.add(Conv2D(60, (5, 5), input_shape = (32, 32, 1), activation = 'relu'))
model.add(Conv2D(60, (5, 5), input_shape = (32, 32, 1), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2,2)))
model.add(Conv2D(30, (3, 3), activation = 'relu'))
model.add(Conv2D(30, (3, 3), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))
#model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(500, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation = 'softmax'))
model.compile(Adam(lr = 0.001), loss = 'categorical_crossentropy', metrics = ['accuracy'])
return model
model = neural_model()
print(model.summary())
history = model.fit_generator(datagen.flow(X_train, y_train, batch_size = 50), steps_per_epoch =
2000, epochs = 10, validation_data =(X_val, y_val), shuffle = 1)
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.legend(['loss', 'val_loss', 'accuracy', 'val_acc'])
plt.title('Loss & Accuracy')
plt.xlabel('epoch')
score = model.evaluate(X_test, y_test, verbose = 1)
print('Test Score', score[0])
print('Test Accuracy', score[1])
import requests
from PIL import Image
url = 'https://c8.alamy.com/comp/A0RX23/cars-and-automobiles-must-turn-left-ahead-sign-
A0RX23.jpg'
r = requests.get(url, stream=True)
image = Image.open(r.raw)
plt.axis('off')
plt.imshow(image, cmap=plt.get_cmap('gray'))
img = np.asarray(image)
img = cv2.resize(img, (32, 32))
img = preprocessing(img)
plt.imshow(img, cmap = plt.get_cmap('gray'))
print(img.shape)
img = img.reshape(1, 32, 32, 1)
prediction = str(model.predict_classes(img))
prediction = prediction[1:-1]
##print("predicted sign: "+ prediction )
pred = int(prediction)
plt.imshow(image)
plt.axis('off')
for num, name in data.iteritems():
name = name.values
print("predicted sign: "+ str(name[pred]))
OUTPUT :
Result :
Thus the Python program to design Object detection and Classification for traffic analysis using
CNN was executed successfully.
EX. NO. : 6 Implement online fraud detection of share market data using anyone
of the data analytics tools
DATE :
AIM :
Write the program to Implement online fraud detection of share market data using anyone of
the data analytics tools
PROCEDURE :
1. Collect and clean data from various sources.
2. Engineer and select relevant features for fraud detection.
3. Build a machine learning model to detect fraudulent transactions.
4. Test and validate the model's performance.
5. Deploy the model to the system that monitors share market transactions.
PROGRAM :
import numpy as np
import pandas as pd
import torch
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style='white', palette='muted')
from pathlib import Path
path = Path('')
path.mkdir(parents=True, exist_ok=True)
path
# Download data from https://www.kaggle.com/mlg-ulb/creditcardfraud
df = pd.read_csv(path/f"creditcard.csv")
print(df.shape)
df.isnull().values.any()
df.Class.value_counts()
df.Amount.describe()
df.groupby(df.Class).Amount.describe()
sns.boxplot(x='Class', y='Amount', data=df[df.Amount < df.Amount.quantile(0.95)])
OUTPUT :
Result :
Thus the Python program to implement online fraud detection of share market data using anyone
of the data analytics tools was executed successfully.
EX. NO. : 7 Implement image augmentation using deep RBM.
DATE :
AIM :
To Write the program to implement image augmentation using deep RBM.
PROCEDURE :
1. Collect and prepare the data you'll use for training the image classification model.
2. Create an instance of the ImageDataGenerator class in TensorFlow to define the
augmentation techniques you want to apply to the images.
3. Define the augmentation techniques you want to apply, such as rotation, flipping, zooming,
and shifting.
4. Fit the ImageDataGenerator to the training data to generate the augmented images.
5. Train the image classification model using the augmented data generated by the
ImageDataGenerator.
6. Evaluate the performance of the trained model on the test data.
PROGRAM :
from IPython import display
from PIL import Image
import numpy as np
import tensorflow.compat.v1 as tf
import sys
sys.path.insert(0, 'tpu/models/official')
sys.path.insert(0, 'tpu/models/official/detection')
sys.path.insert(0, 'tpu/models/official/detection/utils')
from utils.object_detection import visualization_utils
from evaluation import coco_utils
ID_MAPPING = {
1: 'person',
2: 'bicycle',
3: 'car'
}
category_index = {k: {'id': k, 'name': ID_MAPPING[k]} for k in ID_MAPPING}
!wget
https://upload.wikimedia.org/wikipedia/commons/thumb/0/08/Kitano_Street_Kobe01s5s4110.jpg/25
60px-Kitano_Street_Kobe01s5s4110.jpg -O test.jpg
image_path = 'test.jpg'
with open(image_path, 'rb') as f:
np_image_string = np.array([f.read()])
image = Image.open(image_path)
width, height = image.size
np_image = np.array(image.getdata()).reshape(height, width, 3).astype(np.uint8)
display.display(display.Image(image_path, width=1024))
use_tpu = True #@param {type:"boolean"}
if use_tpu:
import os
import pprint
assert 'COLAB_TPU_ADDR' in os.environ, 'ERROR: Not connected to a TPU runtime; please see
the first cell in this notebook for instructions!'
TPU_ADDRESS = 'grpc://' + os.environ['COLAB_TPU_ADDR']
print('TPU address is', TPU_ADDRESS)
session = tf.Session(TPU_ADDRESS, graph=tf.Graph())
print('TPU devices:')
pprint.pprint(session.list_devices())
else:
session = tf.Session(graph=tf.Graph())
saved_model_dir = 'gs://cloud-tpu-checkpoints/shapemask/1571767330' #@param {type:"string"}
_ = tf.saved_model.loader.load(session, ['serve'], saved_model_dir)
num_detections, detection_boxes, detection_classes, detection_scores, detection_masks,
detection_outer_boxes, image_info = session.run(
['NumDetections:0', 'DetectionBoxes:0', 'DetectionClasses:0', 'DetectionScores:0',
'DetectionMasks:0', 'DetectionOuterBoxes:0', 'ImageInfo:0'],
feed_dict={'Placeholder:0': np_image_string})
num_detections = np.squeeze(num_detections.astype(np.int32), axis=(0,))
detection_boxes = np.squeeze(detection_boxes / min(image_info[0, 2]), axis=(0,))[0:num_detections]
detection_outer_boxes = np.squeeze(detection_outer_boxes / min(image_info[0, 2]),
axis=(0,))[0:num_detections]
detection_scores = np.squeeze(detection_scores, axis=(0,))[0:num_detections]
detection_classes = np.squeeze(detection_classes.astype(np.int32), axis=(0,))[0:num_detections]
instance_masks = np.squeeze(detection_masks, axis=(0,))[0:num_detections]
# Use outer boxes
ymin, xmin, ymax, xmax = np.split(detection_outer_boxes, 4, axis=-1)
processed_boxes = np.concatenate([xmin, ymin, xmax - xmin, ymax - ymin], axis=-1)
segmentations = coco_utils.generate_segmentation_from_masks(instance_masks, processed_boxes,
height, width)
max_boxes_to_draw = 25 #@param {type:"integer"}
min_score_thresh = 0.18 #@param {type:"slider", min:0, max:1, step:0.01}
image_with_detections =
visualization_utils.visualize_boxes_and_labels_on_image_array( np_image * 1,
detection_boxes,
detection_classes,
detection_scores,
category_index,
instance_masks=segmentations,
use_normalized_coordinates=False,
max_boxes_to_draw=max_boxes_to_draw,
min_score_thresh=min_score_thresh)
output_image_path = 'test_results.png'
Image.fromarray(image_with_detections.astype(np.uint8)).save(output_image_path)
display.display(display.Image(output_image_path, width=1024))
OUTPUT :
Result :
Thus the Python program to Implement image augmentation using deep RBM was executed
successfully.
EX. NO. : 8 Implement Sentiment Analysis using LSTM.
DATE :
AIM :
To Write the program to implement Sentiment Analysis using LSTM.
PROCEDURE :
1. Prepare and preprocess the data by cleaning, tokenizing, and encoding it into vectors.
2. Split the preprocessed data into training and testing sets.
3. Build an LSTM model with an input layer, one or more LSTM layers, and an output layer
with a sigmoid activation function.
4. Train the LSTM model using the training set and optimize its weights and biases using
backpropagation and gradient descent.
5. Evaluate the LSTM model's performance using the testing set and metrics such as accuracy,
precision, recall, and F1-score.
6. Use the trained LSTM model to make predictions on new data to classify the sentiment as
positive, negative, or neutral.
PROGRAM :
df = pd.read_csv("reviews.csv")
df.head()
df.shape
df.info()
sns.countplot(df.score)
plt.xlabel('review score');
def to_sentiment(rating):
rating = int(rating)
if rating <= 2:
return 0
elif rating == 3:
return 1
else:
return 2
df['sentiment'] = df.score.apply(to_sentiment)
class_names = ['negative', 'neutral', 'positive']
ax = sns.countplot(df.sentiment)
plt.xlabel('review sentiment')
ax.set_xticklabels(class_names);
PRE_TRAINED_MODEL_NAME = 'bert-base-cased'
tokenizer = BertTokenizer.from_pretrained(PRE_TRAINED_MODEL_NAME)
sample_txt = 'When was I last outside? I am stuck at home for 2 weeks.'
tokens = tokenizer.tokenize(sample_txt)
token_ids = tokenizer.convert_tokens_to_ids(tokens)
print(f' Sentence: {sample_txt}')
print(f' Tokens: {tokens}')
print(f'Token IDs: {token_ids}')
tokenizer.sep_token, tokenizer.sep_token_id
tokenizer.cls_token, tokenizer.cls_token_id
tokenizer.pad_token, tokenizer.pad_token_id
tokenizer.unk_token, tokenizer.unk_token_id
encoding =
tokenizer.encode_plus( sample_txt,
max_length=32,
add_special_tokens=True, # Add '[CLS]' and '[SEP]'
return_token_type_ids=False,
pad_to_max_length=True,
return_attention_mask=True,
return_tensors='pt', # Return PyTorch tensors
)
encoding.keys()
print(len(encoding['input_ids'][0]))
encoding['input_ids'][0]
print(len(encoding['attention_mask'][0]))
encoding['attention_mask']
tokenizer.convert_ids_to_tokens(encoding['input_ids'][0])
token_lens = []
for txt in df.content:
tokens = tokenizer.encode(txt, max_length=512)
token_lens.append(len(tokens))
sns.distplot(token_lens)
plt.xlim([0, 256]);
plt.xlabel('Token count');
MAX_LEN = 160
class GPReviewDataset(Dataset):
def init (self, reviews, targets, tokenizer, max_len):
self.reviews = reviews
self.targets = targets
self.tokenizer = tokenizer
self.max_len = max_len
def len (self):
return len(self.reviews)
def getitem (self, item):
review = str(self.reviews[item])
target = self.targets[item]
encoding =
self.tokenizer.encode_plus( review,
add_special_tokens=True,
max_length=self.max_len,
return_token_type_ids=False,
pad_to_max_length=True,
return_attention_mask=True,
return_tensors='pt',
)
return {
'review_text': review,
'input_ids': encoding['input_ids'].flatten(),
'attention_mask': encoding['attention_mask'].flatten(),
'targets': torch.tensor(target, dtype=torch.long)
}
df_train, df_test = train_test_split(df, test_size=0.1, random_state=RANDOM_SEED)
df_val, df_test = train_test_split(df_test, test_size=0.5, random_state=RANDOM_SEED)
df_train.shape, df_val.shape, df_test.shape
def create_data_loader(df, tokenizer, max_len, batch_size):
ds = GPReviewDataset(
reviews=df.content.to_numpy(),
targets=df.sentiment.to_numpy(),
tokenizer=tokenizer,
max_len=max_len
)
return
DataLoader( ds,
batch_size=batch_size,
num_workers=4
)
BATCH_SIZE = 16
train_data_loader = create_data_loader(df_train, tokenizer, MAX_LEN, BATCH_SIZE)
val_data_loader = create_data_loader(df_val, tokenizer, MAX_LEN, BATCH_SIZE)
test_data_loader = create_data_loader(df_test, tokenizer, MAX_LEN, BATCH_SIZE)
data = next(iter(train_data_loader))
data.keys()
print(data['input_ids'].shape)
print(data['attention_mask'].shape)
print(data['targets'].shape)
bert_model = BertModel.from_pretrained(PRE_TRAINED_MODEL_NAME)
last_hidden_state, pooled_output =
bert_model( input_ids=encoding['input_ids'],
attention_mask=encoding['attention_mask']
)
last_hidden_state.shape
bert_model.config.hidden_size
pooled_output.shape
class SentimentClassifier(nn.Module):
def init (self, n_classes):
super(SentimentClassifier, self). init ()
self.bert = BertModel.from_pretrained(PRE_TRAINED_MODEL_NAME)
self.drop = nn.Dropout(p=0.3)
self.out = nn.Linear(self.bert.config.hidden_size, n_classes)
def forward(self, input_ids, attention_mask):
_, pooled_output =
self.bert( input_ids=input_ids,
attention_mask=attention_mask
)
output = self.drop(pooled_output)
return self.out(output)
model = SentimentClassifier(len(class_names))
model = model.to(device)
input_ids = data['input_ids'].to(device)
attention_mask = data['attention_mask'].to(device)
print(input_ids.shape) # batch size x seq length
print(attention_mask.shape) # batch size x seq length
F.softmax(model(input_ids, attention_mask), dim=1)
EPOCHS = 10
optimizer = AdamW(model.parameters(), lr=2e-5, correct_bias=False)
total_steps = len(train_data_loader) * EPOCHS
scheduler =
get_linear_schedule_with_warmup( optimizer,
num_warmup_steps=0,
num_training_steps=total_steps
)
loss_fn = nn.CrossEntropyLoss().to(device)
def train_epoch(
model,
data_loader,
loss_fn,
optimizer,
device,
scheduler,
n_examples
):
model = model.train()
losses = []
correct_predictions = 0
for d in data_loader:
input_ids = d["input_ids"].to(device)
attention_mask = d["attention_mask"].to(device)
targets = d["targets"].to(device)
outputs =
model( input_ids=input_ids,
attention_mask=attention_mask
)
_, preds = torch.max(outputs, dim=1)
loss = loss_fn(outputs, targets)
correct_predictions += torch.sum(preds == targets)
losses.append(loss.item())
loss.backward()
nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
optimizer.step()
scheduler.step()
optimizer.zero_grad()
return correct_predictions.double() / n_examples, np.mean(losses)
def eval_model(model, data_loader, loss_fn, device, n_examples):
model = model.eval()
losses = []
correct_predictions = 0
with torch.no_grad():
for d in data_loader:
input_ids = d["input_ids"].to(device)
attention_mask = d["attention_mask"].to(device)
targets = d["targets"].to(device)
outputs =
model( input_ids=input_ids,
attention_mask=attention_mask
)
_, preds = torch.max(outputs, dim=1)
loss = loss_fn(outputs, targets)
correct_predictions += torch.sum(preds == targets)
losses.append(loss.item())
return correct_predictions.double() / n_examples, np.mean(losses)
%%time
history = defaultdict(list)
best_accuracy = 0
for epoch in range(EPOCHS):
print(f'Epoch {epoch + 1}/{EPOCHS}')
print('-' * 10)
train_acc, train_loss =
train_epoch( model,
train_data_loader,
loss_fn, optimizer,
device,
scheduler,
len(df_train)
)
print(f'Train loss {train_loss} accuracy {train_acc}')
val_acc, val_loss =
eval_model( model,
val_data_loader,
loss_fn,
device,
len(df_val)
)
print(f'Val loss {val_loss} accuracy {val_acc}')
print()
history['train_acc'].append(train_acc)
history['train_loss'].append(train_loss)
history['val_acc'].append(val_acc)
history['val_loss'].append(val_loss)
if val_acc > best_accuracy:
torch.save(model.state_dict(), 'best_model_state.bin')
best_accuracy = val_acc
plt.plot(history['train_acc'], label='train accuracy')
plt.plot(history['val_acc'], label='validation accuracy')
plt.title('Training history')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend()
plt.ylim([0, 1]);
# !gdown --id 1V8itWtowCYnb2Bc9KlK9SxGff9WwmogA
# model = SentimentClassifier(len(class_names))
# model.load_state_dict(torch.load('best_model_state.bin'))
# model = model.to(device)
test_acc, _ = eval_model(
model,
test_data_loader,
loss_fn,
device,
len(df_test)
)
test_acc.item()
def get_predictions(model, data_loader):
model = model.eval()
review_texts = []
predictions = []
prediction_probs = []
real_values = []
with torch.no_grad():
for d in data_loader:
texts = d["review_text"]
input_ids = d["input_ids"].to(device)
attention_mask = d["attention_mask"].to(device)
targets = d["targets"].to(device)
outputs =
model( input_ids=input_ids,
attention_mask=attention_mask
)
_, preds = torch.max(outputs, dim=1)
probs = F.softmax(outputs, dim=1)
review_texts.extend(texts)
predictions.extend(preds)
prediction_probs.extend(probs)
real_values.extend(targets)
predictions = torch.stack(predictions).cpu()
prediction_probs = torch.stack(prediction_probs).cpu()
real_values = torch.stack(real_values).cpu()
return review_texts, predictions, prediction_probs, real_values
y_review_texts, y_pred, y_pred_probs, y_test =
get_predictions( model,
test_data_loader
)
print(classification_report(y_test, y_pred, target_names=class_names))
def show_confusion_matrix(confusion_matrix):
hmap = sns.heatmap(confusion_matrix, annot=True, fmt="d", cmap="Blues")
hmap.yaxis.set_ticklabels(hmap.yaxis.get_ticklabels(), rotation=0, ha='right')
hmap.xaxis.set_ticklabels(hmap.xaxis.get_ticklabels(), rotation=30, ha='right')
plt.ylabel('True sentiment')
plt.xlabel('Predicted sentiment');
cm = confusion_matrix(y_test, y_pred)
df_cm = pd.DataFrame(cm, index=class_names, columns=class_names)
show_confusion_matrix(df_cm)
idx = 2
review_text = y_review_texts[idx]
true_sentiment = y_test[idx]
pred_df =
pd.DataFrame({ 'class_names':
class_names, 'values':
y_pred_probs[idx]
})
print("\n".join(wrap(review_text)))
print()
print(f'True sentiment: {class_names[true_sentiment]}')
sns.barplot(x='values', y='class_names', data=pred_df, orient='h')
plt.ylabel('sentiment')
plt.xlabel('probability')
plt.xlim([0, 1]);
review_text = "I love completing my todos! Best app ever!!!"
encoded_review = tokenizer.encode_plus(
review_text,
max_length=MAX_LEN,
add_special_tokens=True,
return_token_type_ids=False,
pad_to_max_length=True,
return_attention_mask=True,
return_tensors='pt',
)
input_ids = encoded_review['input_ids'].to(device)
attention_mask = encoded_review['attention_mask'].to(device)
output = model(input_ids, attention_mask)
_, prediction = torch.max(output, dim=1)
print(f'Review text: {review_text}')
print(f'Sentiment : {class_names[prediction]}')
OUTPUT :
Review text: I like completing my todos! Best app ever!!!
Sentiment : positive
Result :
Thus the Python program to Implement Sentiment Analysis using LSTM was executed
successfully.
EX. NO. : 9 Mini Project : Number Plate recognition of traffic video analysis
DATE :
AIM :
To Write the program to implement the number plate recognition of traffic video analysis.
PROCEDURE :
1. Preprocess the video data to improve the image quality.
2. Use an object detection algorithm to identify vehicles in the video frames.
3. Extract the regions of interest (ROIs) around the license plates of the vehicles.
4. Apply image processing techniques, such as thresholding and edge detection, to extract the
characters from the license plates.
5. Use Optical Character Recognition (OCR) to recognize the characters and extract the license
plate number.
PROGRAM :
import numpy as np
import os
import six.moves.urllib as urllib
import sys
import tarfile
import tensorflow as tf
import zipfile
from collections import defaultdict
from io import StringIO
from matplotlib import pyplot as plt
from PIL import Image
# This is needed since the notebook is stored in the object_detection folder.
sys.path.append("..")
from object_detection.utils import ops as utils_ops
# This is needed to display the images.
%matplotlib inline
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(
label_map, max_num_classes=num_classes, use_display_name=True)
category_index = label_map_util.create_category_index(categories)
def load_image_into_numpy_array(image):
(im_width, im_height) = image.size
return np.array(image.getdata()).reshape(
(im_height, im_width, 3)).astype(np.uint8)
# Size, in inches, of the output images.
IMAGE_SIZE = (16, 10)
def run_inference_for_single_image(image, graph):
with graph.as_default():
with tf.Session() as sess:
# Get handles to input and output tensors
ops = tf.get_default_graph().get_operations()
all_tensor_names = {
output.name for op in ops for output in op.outputs}
tensor_dict = {}
for key in [
'num_detections', 'detection_boxes', 'detection_scores',
'detection_classes', 'detection_masks'
]:
tensor_name = key + ':0'
if tensor_name in all_tensor_names:
tensor_dict[key] =
tf.get_default_graph().get_tensor_by_name( tensor_name)
if 'detection_masks' in tensor_dict:
# The following processing is only for single image
detection_boxes = tf.squeeze(
tensor_dict['detection_boxes'], [0])
detection_masks = tf.squeeze(
tensor_dict['detection_masks'], [0])
# Reframe is required to translate mask from box coordinates to image coordinates and fit
the image size.
real_num_detection =
tf.cast( tensor_dict['num_detections'][0],
tf.int32)
detection_boxes = tf.slice(detection_boxes, [0, 0], [
real_num_detection, -1])
detection_masks = tf.slice(detection_masks, [0, 0, 0], [
real_num_detection, -1, -1])
detection_masks_reframed =
utils_ops.reframe_box_masks_to_image_masks( detection_masks, detection_boxes,
image.shape[0], image.shape[1])
detection_masks_reframed =
tf.cast( tf.greater(detection_masks_reframed, 0.5), tf.uint8)
# Follow the convention by adding back the batch dimension and proceeding
tensor_dict['detection_masks'] = tf.expand_dims(
detection_masks_reframed, 0)
image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0')
# Run inference
output_dict = sess.run(tensor_dict,
feed_dict={image_tensor: np.expand_dims(image, 0)})
# all outputs are float32 numpy arrays, so convert types as appropriate
output_dict['num_detections'] = int(
output_dict['num_detections'][0])
output_dict['detection_classes'] = output_dict[
'detection_classes'][0].astype(np.uint8)
output_dict['detection_boxes'] = output_dict['detection_boxes'][0]
output_dict['detection_scores'] = output_dict['detection_scores'][0]
if 'detection_masks' in output_dict:
output_dict['detection_masks'] = output_dict['detection_masks'][0]
return output_dict
for image_path in TEST_IMAGE_PATHS:
image = Image.open(image_path)
# the array based representation of the image will be used later in order to prepare the
# result image with boxes and labels on it.
image_np = load_image_into_numpy_array(image)
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
image_np_expanded = np.expand_dims(image_np, axis=0)
# Actual detection.
output_dict = run_inference_for_single_image(image_np, detection_graph)
# Visualization of the results of a detection.
vis_util.visualize_boxes_and_labels_on_image_array(
image_np,
output_dict['detection_boxes'],
output_dict['detection_classes'],
output_dict['detection_scores'],
category_index,
instance_masks=output_dict.get('detection_masks'),
use_normalized_coordinates=True, line_thickness=4)
plt.figure(figsize=IMAGE_SIZE)
plt.imshow(image_np)
OUTPUT :
Result :
Thus the Python program to Implement Number Plate recognition of traffic video analysis was
executed successfully.