DL Lab Manual 2022-23
DL Lab Manual 2022-23
LABORATORY MANUAL
Department
Program Educational Objectives (PEOs)
PEO1: To Impart fundamentals in science, mathematics, and engineering to cater the needs of
society and Industries.
PEO2: Encourage graduates to involve in research, higher studies, and/or to become entrepreneurs.
Course Objectives:
1. To be able to formulate deep learning problems corresponding to different applications.
2. To be able to apply deep learning algorithms to solve problems of moderate complexity.
3. To apply the algorithms to a real-world problem, optimize the models learned and report on the
4. expected accuracy that can be achieved by applying the models.
Course Outcomes:
On completion of the course, student will be able to-
CO1. Learn and Use various Deep Learning tools and packages.
CO2. Build and train deep Neural Network models for use in various applications.
CO3. Apply Deep Learning techniques like CNN, RNN Auto encoders to solve real word
Problems.
CO4. Evaluate the performance of the model build using Deep Learning.
List of Assignments
Build the Image classification model by dividing the model into following 4
stages:
3 a. Loading and preprocessing the image data.
b. Defining the model’s architecture.
c. Training the model.
d. Estimating the model’s performance.
Use Autoencoder to implement anomaly detection. Build the model by using:
a. Import required libraries.
4 b. Upload / access the dataset.
c. Encoder converts it into latent representation.
d. Decoder networks convert it back to the original input.
e. Compile the models with Optimizer, Loss, and Evaluation Metrics.
Implement the Continuous Bag of Words (CBOW) Model. Stages can be:
a. Data preparation.
5 b. Generate training data.
c. Train model.
d. Output.
Assignment No.1
Title: Study of Deep learning Packages: Tensorflow, Keras, Theano and PyTorch. Document the
distinct features and functionality of the packages.
Objective: Study and installation of following Deep learning Packages:
i. Tensor Flow
ii. Keras
iii. Theno
iV . PyTorch
Theory:
Python Libraries and functions required
1. Tensorflow,keras
numpy : NumPy is a Python library used for working with arrays. It also has functions for
working in domain of linear algebra, fourier transform, and matrices. NumPy stands for
Numerical Python. To import numpy use
import numpy as np
pandas: pandas is a fast, powerful, flexible and easy to use open source data analysis and
manipulation tool, built on top of the Python programming language. To import pandas use
import pandas as pd
sklearn : Scikit-learn (Sklearn) is the most useful and robust library for machine learning in
Python. It provides a selection of efficient tools for machine learning and statistical modeling
including classification, regression, clustering and dimensionality reduction via a consistence
interface in Python. This library, which is largely written in Python, is built upon NumPy,
SciPy and Matplotlib. For importing train_test_ split use from sklearn.model_selection import
train_test_split
2. For TheaonRequirements:
• Python3
• Python3-pip
• NumPy
• SciPy
• BLAS
Sample Code with comments
Assignment No.2
Title : Implementing Feedforward neural networks
Objective: Implementing Feedforward neural networks with Keras and TensorFlow
a. Import the necessary packages.
b. Load the training and testing data (MNIST/CIFAR10)
c. Define the network architecture using Keras
d. Train the model using SGD.
e. Evaluate the network.
f. Plot the training loss and accuracy.
Theory :
Steps/ Algorithm
1. Dataset link and libraries :
Dataset : MNIST or CIFAR 10 : kaggel.com
You can download dataset from above mentioned website. Libraries required :
Pandas and Numpy for data manipulation Tensorflow/Keras for Neural Networks
Scikit-learn library for splitting the data into train-test samples, and for some basic model
evaluation
https://pyimagesearch.com/2021/05/06/implementing-feedforward-neural-networks-with-keras-
and-tensorflow/
a) Import following libraries from SKlearn : i) LabelBinarizer (sklearn.preprocessing) ii)
classification_report (sklearn.metrics) .
b) Flatten the dataset.
c) If required do the normalization of data .
d) Convert the labels from integers to vectors.( specially for one hot coding)
e) Decide the Neural Network Architecture : i) Select model (Sequential recommended )
ii) Activation function (sigmoid recommended ) iii) Select the input shape iv) see the
weights in the output layer
f) Train the model : i) Select optimizer (SGD recommended ) ii) use model that .fit to start
training ii) Set Epochs and batch size
g) Call model.predict for class prediction.
h) Plot training and loss accuracy
i) Calculate Precision, Recall, F1-score, Support
j) Repeat for CIFAR dataset.
import math
import pandas as pd
from keras import models, layers, optimizers, regularizers
import numpy as np
import random
from sklearn import model_selection, preprocessing
import tensorflow as tf
from tqdm import tqdm
import matplotlib.pyplot as plt
file_name = '../input/SAheart.data'
data = pd.read_csv(file_name, sep=',', index_col=0)
data['famhist'] = data['famhist'] == 'Present'
data.head()
n_test = int(math.ceil(len(data) * 0.3))
random.seed(42)
test_ixs = random.sample(list(range(len(data))), n_test)
train_ixs = [ix for ix in range(len(data)) if ix not in test_ixs]
train = data.iloc[train_ixs, :]
test = data.iloc[test_ixs, :]
print(len(train))
print(len(test))
#features = ['sbp', 'tobacco', 'ldl', 'adiposity', 'famhist', 'typea', 'obesity', 'alcohol', 'age']
features = ['adiposity', 'age']
response = 'chd'
x_train = train[features]
y_train = train[response]
x_test = test[features]
y_test = test[response]
x_train = preprocessing.normalize(x_train)
x_test = preprocessing.normalize(x_test)
hidden_units = 10 # how many neurons in the hidden layer
# evaluate accuracy
train_acc = model.evaluate(x_train, y_train, batch_size=32)[1]
test_acc = model.evaluate(x_test, y_test, batch_size=32)[1]
print('Training accuracy: %s' % train_acc)
print('Testing accuracy: %s' % test_acc)
losses = history.history['loss']
plt.plot(range(len(losses)), losses, 'r')
plt.show()
units=hidden_units,
activation=activation))
Assignment No.3
Title: Build the Image classification model
Objective: Build the Image classification model by dividing the model into following 4 stages:
a. Loading and pre-processing the image data
2. Prepare Dataset for Training : //Preparing our dataset for training will involve assigning paths and
creating categories(labels), resizing our images.
3. Create a Training a Data : // Training is an array that will contain image pixel values and the index
at which the image in the CATEGORIES list.
4. Shuffle the Dataset
5. Assigning Labels and Features
6. Normalising X and converting labels to categorical data
7. Split X and Y for use in CNN
8. Define, compile and train the CNN Model
9. Accuracy and Score of model.
#
pytho
n
image
libra
ry of
range
[0,
1]
# transform them to tensors of normalized range[-1, 1]
# set batch_size
batch_size = 4
def
imshow(im
g):
''' function to show image '''
img = img / 2 + 0.5 # unnormalize
npimg = img.numpy() # convert to numpy objects
class
Net(nn.Module):
''' Models a simple Convolutional Neural Network'''
def __init__(self):
''' initialize the network '''
super(Net, self).__init__()
# 3 input image channel, 6 output channels,
# 5x5 square convolution kernel
self.conv1 = nn.Conv2d(3, 6, 5)
# Max pooling over a (2, 2) window
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(6, 16, 5)
self.fc1 = nn.Linear(16 * 5 * 5, 120)# 5x5 from image dimension
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
x = self.fc3(x)
return x
net = Net()
print(net)
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
start =
torch.cuda.Event(enable_ti
ming=True)
end = torch.cuda.Event(enable_timing=True)
start.record()
running_loss = 0.0
for i, data in enumerate(trainloader, 0):
# get the inputs; data is a list of [inputs, labels]
inputs, labels = data
# print statistics
running_loss += loss.item()
if i % 2000 == 1999: # print every 2000 mini-
batches
print('[%d, %5d] loss: %.3f' %
(epoch + 1, i + 1, running_loss / 2000))
running_loss = 0.0
print('Finished Training')
print(start.elapsed_time(end)) # milliseconds
dataiter =
iter(testlo
ader)
images, labels = dataiter.next()
# print images
imshow(torchvision.utils.make_grid(images)
)
print('GroundTruth: ', ' '.join('%s' %
classes[labels[j]] for j in range(4)))
Assignment No.4
Title : ECG Anomaly detection using Autoencoders
Objective: Use Autoencoder to implement anomaly detection. Build the model by using:
a. Import required libraries
b. Upload / access the dataset
c. Encoder converts it into latent representation
d. Decoder networks convert it back to the original input
e. Compile the models with Optimizer, Loss, and Evaluation Metrics
Theory :
Steps/ Algorithm
1. Dataset link and libraries :
Dataset : http://storage.googleapis.com/download.tensorflow.org/data/ecg.csv Libraries required
:
Pandas and Numpy for data manipulation Tensorflow/Keras for Neural Networks
Scikit-learn library for splitting the data into train-test samples, and for some basic model
evaluation
For Model building and evaluation following libraries: sklearn.metrics import accuracy_score
tensorflow.keras.optimizers import Adam
sklearn.preprocessing import MinMaxScaler
tensorflow.keras import Model, Sequential
tensorflow.keras.layers import Dense, Dropout
tensorflow.keras.losses import MeanSquaredLogarithmicError
Ref:https://www.analyticsvidhya.com/blog/2021/05/anomaly-detection-using-autoencoders-a-
walk-through-in-python/
a) Import following libraries from SKlearn : i) MinMaxscaler (sklearn.preprocessing) ii)
Accuracy(sklearn.metrics) . iii) train_test_split (model_selection)
b) Import Following libraries from tensorflow.keras : models , layers,optimizers,datasets , and
set to respective values.
c) Grab to ECG.csv required dataset
d) Find shape of dataset
e) Use train_test_split from sklearn to build model (e.g. train_test_split( features, target,
test_size=0.2, stratify=target)
f) Take usecase Novelty detection hence select training data set as Target class is 1 i.e.
Normal class
g) Scale the data using MinMaxScaler.
h) Create Autoencoder Subclass by extending model class from keras.
i) Select parameters as i)Encoder : 4 layers ii) Decoder : 4 layers iii) Activation Function :
Relu iv) Model : sequential.
j) Configure model with following parametrs : epoch = 20 , batch size =512 and compile with
Mean Squared Logarithmic loss and Adam optimizer.
e.g. model = AutoEncoder(output_units=x_train_scaled.shape[1]) # configurations of
model
model.compile(loss='msle', metrics=['mse'], optimizer='adam')
history = model.fit( x_train_scaled, x_train_scaled, epochs=20, batch_size=512,
validation_data=(x_test_scaled, x_test_scaled)
k) Plot loss,Val_loss, Epochs and msle loss
l) Find threshold for anomaly and do predictions :
e.g. : find_threshold(model, x_train_scaled): reconstructions =
model.predict(x_train_scaled) # provides losses of individual instances
reconstruction_errors = tf.keras.losses.msle(reconstructions, x_train_scaled) # threshold
for anomaly scores
threshold = np.mean(reconstruction_errors.numpy()) \
+ np.std(reconstruction_errors.numpy()) return threshold
m) Get accuracy score
# visualisations
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style='whitegrid', context='notebook')
%matplotlib notebook
# misc
import random as rn
# manual parameters
RANDOM_SEED = 42
TRAINING_SAMPLE = 200000
VALIDATE_SIZE = 0.2
# let's quickly convert the columns to lower case and rename the Class column
# so as to not cause syntax errors
df.columns = map(str.lower, df.columns)
df.rename(columns={'class': 'label'}, inplace=True)
# print first 5 rows to get an initial impression of the data we're dealing with
df.head()
# add a negligible amount to avoid taking the log of 0
df['log10_amount'] = np.log10(df.amount + 0.00001)
# manual parameter
RATIO_TO_FRAUD = 15
# splitting by class
fraud = df[df.label == 1]
clean = df[df.label == 0]
# undersample clean transactions
clean_undersampled = clean.sample(
int(len(fraud) * RATIO_TO_FRAUD),
random_state=RANDOM_SEED
)
# concatenate with fraud transactions into a single dataframe
visualisation_initial = pd.concat([fraud, clean_undersampled])
column_names = list(visualisation_initial.drop('label', axis=1).columns)
# isolate features from labels
features, labels = visualisation_initial.drop('label', axis=1).values, \
visualisation_initial.label.values # manual parameter
RATIO_TO_FRAUD = 15
# dropping redundant columns
df = df.drop(['time', 'amount'], axis=1)
# splitting by class
fraud = df[df.label == 1]
clean = df[df.label == 0]
# undersample clean transactions
clean_undersampled = clean.sample(
int(len(fraud) * RATIO_TO_FRAUD),
random_state=RANDOM_SEED
)
# concatenate with fraud transactions into a single dataframe
visualisation_initial = pd.concat([fraud, clean_undersampled])
column_names = list(visualisation_initial.drop('label', axis=1).columns)
Assignment No.5
Title : Implement the Continuous Bag of Words (CBOW) Model.
Objective: Implement the Continuous Bag of Words (CBOW) Model. Stages can be:
a. Data preparation
b. Generate training data
c. Train model
d. Output
Theory :
Steps/ Algorithm
1. Dataset link and libraries :
Create any English 5 to 10 sententece paragraph as input Import following data from keras :
keras.models import Sequential
keras.layers import Dense, Embedding, Lambda
keras.utils import np_utils
keras.preprocessing import sequence
keras.preprocessing.text import Tokenizer
Import Gensim for NLP operations : requirements :
Gensim runs on Linux, Windows and Mac OS X, and should run on any other platform that
supports Python 3.6+ and NumPy. Gensim depends on the following software: Python, tested
with versions 3.6, 3.7 and 3.8. NumPy for number crunching.
Ref: https://analyticsindiamag.com/the-continuous-bag-of-words-cbow-model-in-nlp-hands-on-
implementation-with-codes/
a) Import following libraries gemsim and numpy set i.e. text file created . It should be
preprocessed.
b) Tokenize the every word from the paragraph . You can call in built tokenizer present in
Gensim
c) Fit the data to tokenizer
d) Find total no of words and total no of sentences.
e) Generate the pairs of Context words and target words :
e.g. cbow_model(data, window_size, total_vocab): total_length = window_size*2
for text in data: text_len = len(text)
for idx, word in enumerate(text): context_word = []
target = []
Dataset
In [2]:
sentences = """We are about to study the idea of a computational process.
Computational processes are abstract beings that inhabit computers.
As they evolve, processes manipulate other abstract things called data.
The evolution of a process is directed by a pattern of rules
called a program. People create programs to direct processes. In effect,
we conjure the spirits of the computer with our spells."""
Clean Data
In [3]:
# remove special characters
sentences = re.sub('[^A-Za-z0-9]+', ' ', sentences)
data = []
for i in range(2, len(words) - 2):
context = [words[i - 2], words[i - 1], words[i + 1], words[i + 2]]
target = words[i]
data.append((context, target))
print(data[:5])
[(['we', 'are', 'to', 'study'], 'about'), (['are', 'about', 'study', 'the'], 'to'), (['about', 'to', 'the', 'idea'], 'study'), (['to', 'study', 'idea', '
of'], 'the'), (['study', 'the', 'of', 'computational'], 'idea')]
Embeddings
In [8]:
embeddings = np.random.random_sample((vocab_size, embed_dim))
Linear Model
In [9]:
def linear(m, theta):
w = theta
return m.dot(w)
Log softmax + NLLloss = Cross Entropy
In [10]:
def log_softmax(x):
e_x = np.exp(x - np.max(x))
return np.log(e_x / e_x.sum())
In [11]:
def NLLLoss(logs, targets):
out = logs[range(len(targets)), targets]
return -out.sum()/ len(out)
In [12]:
def log_softmax_crossentropy_with_logits(logits,target):
out = np.zeros_like(logits)
out[np.arange(len(logits)),target] = 1
return m, n, o
Backward function
In [14]:
def backward(preds, theta, target_idxs):
m, n, o = preds
return dw
Optimize function
In [15]:
def optimize(theta, grad, lr=0.03):
theta -= grad * lr
return theta
Training
In [16]:
theta = np.random.uniform(-1, 1, (2 * context_size * embed_dim, vocab_size))
In [17]:
epoch_losses = {}
losses = []
target_idxs = np.array([word_to_ix[target]])
loss = NLLLoss(preds[-1], target_idxs)
losses.append(loss)
epoch_losses[epoch] = losses
Analyze
Plot loss/epoch
In [18]:
ix = np.arange(0,80)
fig = plt.figure()
fig.suptitle('Epoch/ Losses', fontsize=20)
plt.plot(ix,[epoch_losses[i][0] for i in ix])
plt.xlabel('Epochs', fontsize=12)
plt.ylabel('Losses', fontsize=12)
Out[18]:
Text(0, 0.5, 'Losses')
Predict function
In [19]:
def predict(words):
context_idxs = np.array([word_to_ix[w] for w in words])
preds = forward(context_idxs, theta)
word = ix_to_word[np.argmax(preds[-1])]
return word
In [20]:
# (['we', 'are', 'to', 'study'], 'about')
predict(['we', 'are', 'to', 'study'])
Out[20]:
'about'
Accuracy
In [21]:
def accuracy():
wrong = 0
for context, target in data:
if(predict(context) != target):
wrong += 1
Out[22]:
1.0
Assignment No.6
Title : Object detection using Transfer Learning of CNN architectures Objective: Object
detection using Transfer Learning of CNN architectures
a. Load in a pre-trained CNN model trained on a large dataset
b. Freeze parameters (weights) in model’s lower convolutional layers
c. Add custom classifier with several layers of trainable parameters to model
d. Train classifier layers on training data available for task
e. Fine-tune hyper parameters and unfreeze more layers as needed
Theory :
Steps/ Algorithm
1. Dataset link and libraries :
https://data.caltech.edu/records/mzrjq-6wc02 separate the data into training, validation,
and
/test
/class1
/class2
Libraries required :
PyTorch
torchvision import transforms
torchvision import d atasets
torch.utils.data import DataLoader torchvision import models torch.nn as nn
torch import optim
Ref: https://towardsdatascience.com/transfer-learning-with-convolutional-neural-
networks-in- pytorch-dd09190245ce
m) Prepare the dataset in splitting in three directories Train , alidation and test with 50 25
25
n) Do pre-processing on data with transform from Pytorch Training dataset
transformation as follows : transforms.Compose([
transforms.RandomResizedCrop(size=256, scale=(0.8, 1.0)),
transforms.RandomRotation(degrees=15), transforms.ColorJitter(),
transforms.RandomHorizontalFlip(), transforms.CenterCrop(size=224), #
}
dataloaders = {
'train': DataLoader(data['train'], batch_size=batch_size, shuffle=True), 'val':
DataLoader(data['valid'], batch_size=batch_size, shuffle=True)
}
(2) : Dropout(p=0.5)
(3) : Linear(in_features=4096, out_features=4096, bias=True) (4): ReLU(inplace)
(5) : Dropout(p=0.5)
(6) : Sequential(
(0): Linear(in_features=4096, out_features=256, bias=True) (1): ReLU()
(2) : Dropout(p=0.4)
(3) : Linear(in_features=256, out_features=100, bias=True) (4): LogSoftmax()
)
)
************************