Deep Learning Lab Manual
Deep Learning Lab Manual
(AUTONOMOUS)
FOR
VI SEMESTER
Name:
Year/Sem: Branch:
Year : 2024-25
Designation
Signature
Date
21ADC28 DEEP LEARNING LABORATORY
LIST OF EXPERIMENTS:
TOTAL: 30 PERIODS
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:
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))
Z1 = np.dot(W1, X) + b1
A1 = sigmoid(Z1)
Z2 = np.dot(W2, A1) + b2
A2 = sigmoid(Z2)
# 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)
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
for i in range(epoch):
losses[i, 0], cache, A2 = forwardPropagation(X, Y, parameters)
gradients = backwardPropagation(X, Y, cache)
parameters = updateParameters(parameters, gradients, learningRate)
# 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:
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
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'])
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:
Program :
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:
Program :
import speech_recognition as sr
# Create a recognizer object
r = sr.Recognizer()
OUTPUT :
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 :
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
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.fit(X_train)
batches = datagen.flow(X_train, y_train, batch_size = 20)
X_batch, y_batch = next(batches)
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(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')
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
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'
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))
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)
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)
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 = []
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
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):
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
)
loss.backward()
nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
optimizer.step()
scheduler.step()
optimizer.zero_grad()
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)
history = defaultdict(list)
best_accuracy = 0
train_acc, train_loss =
train_epoch( model,
train_data_loader,
loss_fn, optimizer,
device,
scheduler,
len(df_train)
)
val_acc, val_loss =
eval_model( model,
val_data_loader,
loss_fn,
device,
len(df_val)
)
history['train_acc'].append(train_acc)
history['train_loss'].append(train_loss)
history['val_acc'].append(val_acc)
history['val_loss'].append(val_loss)
plt.title('Training history')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend()
plt.ylim([0, 1]);
# 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()
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)
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
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)
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
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)
# Run inference
output_dict = sess.run(tensor_dict,
feed_dict={image_tensor: np.expand_dims(image, 0)})
OUTPUT :
Result :
Thus the Python program to Implement Number Plate recognition of traffic video analysis was
executed successfully.