0% found this document useful (0 votes)
6 views44 pages

Deep Learning Overview - Kuntal Chakraborty

The document presents an overview of deep learning, focusing on its architecture, features, and applications in object detection. It explains the role of neural networks, data preparation, and the use of frameworks like PyTorch for model training. Additionally, it covers convolutional layers, popular CNN models, and natural language processing techniques such as Word2Vec and recurrent neural networks.

Uploaded by

diyashabasu8777
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)
6 views44 pages

Deep Learning Overview - Kuntal Chakraborty

The document presents an overview of deep learning, focusing on its architecture, features, and applications in object detection. It explains the role of neural networks, data preparation, and the use of frameworks like PyTorch for model training. Additionally, it covers convolutional layers, popular CNN models, and natural language processing techniques such as Word2Vec and recurrent neural networks.

Uploaded by

diyashabasu8777
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/ 44

8/7/24, 3:54 PM 003-object-detection-dl slides

DEEP LEARNING - USE CASES

Kuntal Chakraborty
Director, Data Science & Analytics
TABS Analytics by Telus

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 1/44
8/7/24, 3:54 PM 003-object-detection-dl slides

What is Deep Learning?


Deep learning is a subset of machine learning
which is based on artificial neural network
architecture
It uses layers of interconnected nodes called
neurons that work together to process and learn
from the input data
In a fully connected Deep neural network there is
an input layer, one or more hidden layers and an
output layer

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 2/44
8/7/24, 3:54 PM 003-object-detection-dl slides

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 3/44
8/7/24, 3:54 PM 003-object-detection-dl slides

Features
Composed of layers of interconnected nodes or
neurons. The input layer receives the data, the
output layer produces the predictions and the
hidden layers perform the computations in
between.
activation functions are applied to the outputs of
each layer to introduce non-linearity and increase
the model's expressiveness. Common activation
functions are ReLU, Sigmoid and Tanh.
Backpropagation is a method for training neural
networks by iteratively adjusting the weights in
each layer to minimize the difference between the
predicted outputs and actual outputs. It Works by
propagating the error backwards from the output
layer to the input layer and adjusting the weights
accordingly.

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 4/44
8/7/24, 3:54 PM 003-object-detection-dl slides

PyTorch
PyTorch is an open-source deep learning framework
that's known for its flexibility and ease of use, This is
enabled in part by its compatibility with the popular
Python high-level programming language.

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 5/44
8/7/24, 3:54 PM 003-object-detection-dl slides

Data Preparation
"Data" in machine learning can be almost anything
you can imagine. A table of numbers (like a big excel),
images of any kind, videos, audio files, text etc.
So machine learning consists of two parts
Turn your data into numbers (a representation)
Build a model to learn the representation as best
as possible
When you have the data ready split it into training and
test

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 6/44
8/7/24, 3:54 PM 003-object-detection-dl slides

Easy for us to figure out that the above picture


represents the digit 4, we don't even bother about the
resolution of the image.

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 7/44
8/7/24, 3:54 PM 003-object-detection-dl slides

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 8/44
8/7/24, 3:54 PM 003-object-detection-dl slides

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 9/44
8/7/24, 3:54 PM 003-object-detection-dl slides

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 10/44
8/7/24, 3:54 PM 003-object-detection-dl slides

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 11/44
8/7/24, 3:54 PM 003-object-detection-dl slides

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 12/44
8/7/24, 3:54 PM 003-object-detection-dl slides

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 13/44
8/7/24, 3:54 PM 003-object-detection-dl slides

In [55]:

import numpy as np
import torch
import torchvision
import matplotlib.pyplot as plt
from time import time
from torchvision import datasets, transforms
from torch import nn, optim

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 14/44
8/7/24, 3:54 PM 003-object-detection-dl slides

In [56]:

transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))
])

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 15/44
8/7/24, 3:54 PM 003-object-detection-dl slides

In [57]:

trainset = datasets.MNIST('PATH_TO_STORE_TRAINSET', download=True,


valset = datasets.MNIST('PATH_TO_STORE_TESTSET', download=True, tr
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64,
valloader = torch.utils.data.DataLoader(valset, batch_size=64, shu

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 16/44
8/7/24, 3:54 PM 003-object-detection-dl slides

In [58]:

input_size = 784
hidden_sizes = [128, 64]
output_size = 10

model = nn.Sequential(nn.Linear(input_size, hidden_sizes[0]),


nn.ReLU(),
nn.Linear(hidden_sizes[0], hidden_sizes[1]),
nn.ReLU(),
nn.Linear(hidden_sizes[1], output_size),
nn.LogSoftmax(dim=1))
print(model)

Sequential(
(0): Linear(in_features=784, out_features=128, bias=Tr
ue)
(1): ReLU()
(2): Linear(in_features=128, out_features=64, bias=Tru
e)
(3): ReLU()
(4): Linear(in_features=64, out_features=10, bias=Tru
e)
(5): LogSoftmax(dim=1)
)

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 17/44
8/7/24, 3:54 PM 003-object-detection-dl slides

In [59]:

criterion = nn.NLLLoss()
images, labels = next(iter(trainloader))
images = images.view(images.shape[0], -1)

logps = model(images) #log probabilities


loss = criterion(logps, labels) #calculate the NLL loss

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 18/44
8/7/24, 3:54 PM 003-object-detection-dl slides

In [60]:

optimizer = optim.SGD(model.parameters(), lr=0.003, momentum=0.9)


time0 = time()
epochs = 15
for e in range(epochs):
running_loss = 0
for images, labels in trainloader:
# Flatten MNIST images into a 784 long vector
images = images.view(images.shape[0], -1)

# Training pass
optimizer.zero_grad()

output = model(images)
loss = criterion(output, labels)

#This is where the model learns by backpropagating


loss.backward()

#And optimizes its weights here


optimizer.step()

running_loss += loss.item()
else:
print("Epoch {} - Training loss: {}".format(e, running_los
print("\nTraining Time (in minutes) =",(time()-time0)/60)

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 19/44
8/7/24, 3:54 PM 003-object-detection-dl slides

Epoch 0 - Training loss: 0.6331630812239036


Epoch 1 - Training loss: 0.2873122069412775
Epoch 2 - Training loss: 0.22130842766623254
Epoch 3 - Training loss: 0.1757350927357798
Epoch 4 - Training loss: 0.14643871235146896
Epoch 5 - Training loss: 0.12516195103347397
Epoch 6 - Training loss: 0.10939654523232725
Epoch 7 - Training loss: 0.09755499328452863
Epoch 8 - Training loss: 0.0873729043743456
Epoch 9 - Training loss: 0.07926795158340637
Epoch 10 - Training loss: 0.07225375629063926
Epoch 11 - Training loss: 0.0659943895353906
Epoch 12 - Training loss: 0.06130977433129176
Epoch 13 - Training loss: 0.055123179195089335
Epoch 14 - Training loss: 0.05286966172232628

Training Time (in minutes) = 2.4722554008165996

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 20/44
8/7/24, 3:54 PM 003-object-detection-dl slides

In [61]:

def view_classify(img, ps):


''' Function for viewing an image and it's predicted classes.
'''
ps = ps.cpu().data.numpy().squeeze()

fig, (ax1, ax2) = plt.subplots(figsize=(6,9), ncols=2)


ax1.imshow(img.resize_(1, 28, 28).numpy().squeeze())
ax1.axis('off')
ax2.barh(np.arange(10), ps)
ax2.set_aspect(0.1)
ax2.set_yticks(np.arange(10))
ax2.set_yticklabels(np.arange(10))
ax2.set_title('Class Probability')
ax2.set_xlim(0, 1.1)
plt.tight_layout()

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 21/44
8/7/24, 3:54 PM 003-object-detection-dl slides

In [62]:

images, labels = next(iter(valloader))

img = images[0].view(1, 784)


with torch.no_grad():
logps = model(img)

ps = torch.exp(logps)
probab = list(ps.numpy()[0])
print("Predicted Digit =", probab.index(max(probab)))
view_classify(img.view(1, 28, 28), ps)

Predicted Digit = 4

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 22/44
8/7/24, 3:54 PM 003-object-detection-dl slides

Convolutional Layers
The convolutional layers are the foundation of CNN, as
they contain the learned kernels (weights), which
extract features that distinguish different images from
one another,this is what we want for classification! As
you interact with the convolutional layer, you will
notice links between the previous layers and the
convolutional layers. Each link represents a unique
kernel, which is used for the convolution operation to
produce the current convolutional neuron’s output or
activation map.

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 23/44
8/7/24, 3:54 PM 003-object-detection-dl slides

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 24/44
8/7/24, 3:54 PM 003-object-detection-dl slides

The size of these kernels is a hyper-parameter


specified by the designers of the network architecture.

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 25/44
8/7/24, 3:54 PM 003-object-detection-dl slides

In [63]:

import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 26/44
8/7/24, 3:54 PM 003-object-detection-dl slides

In [64]:

class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
self.conv2_drop = nn.Dropout2d()
self.fc1 = nn.Linear(320, 50)
self.fc2 = nn.Linear(50, 10)

def forward(self, x):


x = F.relu(F.max_pool2d(self.conv1(x), 2))
x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2)
x = x.view(-1, 320)
x = F.relu(self.fc1(x))
x = F.dropout(x, training=self.training)
x = self.fc2(x)
return F.log_softmax(x)

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 27/44
8/7/24, 3:54 PM 003-object-detection-dl slides

Popular CNN models


AlexNet (2012)
VGGNet (2014)
GoogLeNet (2014)

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 28/44
8/7/24, 3:54 PM 003-object-detection-dl slides

InceptionNet

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 29/44
8/7/24, 3:54 PM 003-object-detection-dl slides

Natural Language Processing


When we read a sentence we read it word by word
keep the prior words / context in memory and then
update our understanding based on the new words
which we incrementally read to understand the whole
sentence.

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 30/44
8/7/24, 3:54 PM 003-object-detection-dl slides

Representations for words

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 31/44
8/7/24, 3:54 PM 003-object-detection-dl slides

Word2Vec comes pretrained and computationally


efficient model for learning word embeddings

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 32/44
8/7/24, 3:54 PM 003-object-detection-dl slides

Recurrent Neural Network is basically a


generalization of feed forward neural network that
has an internal memory.
Special kind of neural networks that are designed
to effectively deal with sequential data

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 33/44
8/7/24, 3:54 PM 003-object-detection-dl slides

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 34/44
8/7/24, 3:54 PM 003-object-detection-dl slides

In [65]:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import torch
import torch.nn as nn
from torch.autograd import Variable
from sklearn.preprocessing import MinMaxScaler

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 35/44
8/7/24, 3:54 PM 003-object-detection-dl slides

In [66]:

training_set = pd.read_csv('data/airline-passengers.csv')
training_set = training_set.iloc[:,1:2].values

plt.plot(training_set, label = 'Airline Passangers Data')


plt.show()

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 36/44
8/7/24, 3:54 PM 003-object-detection-dl slides

In [67]:

def sliding_windows(data, seq_length):


x = []
y = []

for i in range(len(data)-seq_length-1):
_x = data[i:(i+seq_length)]
_y = data[i+seq_length]
x.append(_x)
y.append(_y)

return np.array(x),np.array(y)

sc = MinMaxScaler()
training_data = sc.fit_transform(training_set)

seq_length = 4
x, y = sliding_windows(training_data, seq_length)

train_size = int(len(y) * 0.67)


test_size = len(y) - train_size

dataX = Variable(torch.Tensor(np.array(x)))
dataY = Variable(torch.Tensor(np.array(y)))

trainX = Variable(torch.Tensor(np.array(x[0:train_size])))
trainY = Variable(torch.Tensor(np.array(y[0:train_size])))

testX = Variable(torch.Tensor(np.array(x[train_size:len(x)])))
testY = Variable(torch.Tensor(np.array(y[train_size:len(y)])))

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 37/44
8/7/24, 3:54 PM 003-object-detection-dl slides

In [68]:

class LSTM(nn.Module):

def __init__(self, num_classes, input_size, hidden_size, num_l


super(LSTM, self).__init__()

self.num_classes = num_classes
self.num_layers = num_layers
self.input_size = input_size
self.hidden_size = hidden_size
self.seq_length = seq_length

self.lstm = nn.LSTM(input_size=input_size, hidden_size=hid


num_layers=num_layers, batch_first=Tru

self.fc = nn.Linear(hidden_size, num_classes)

def forward(self, x):


h_0 = Variable(torch.zeros(
self.num_layers, x.size(0), self.hidden_size))

c_0 = Variable(torch.zeros(
self.num_layers, x.size(0), self.hidden_size))

# Propagate input through LSTM


ula, (h_out, _) = self.lstm(x, (h_0, c_0))

h_out = h_out.view(-1, self.hidden_size)

out = self.fc(h_out)

return out

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 38/44
8/7/24, 3:54 PM 003-object-detection-dl slides

In [69]:

num_epochs = 2000
learning_rate = 0.01

input_size = 1
hidden_size = 2
num_layers = 1

num_classes = 1

lstm = LSTM(num_classes, input_size, hidden_size, num_layers)

criterion = torch.nn.MSELoss() # mean-squared error for regress


optimizer = torch.optim.Adam(lstm.parameters(), lr=learning_rate)
#optimizer = torch.optim.SGD(lstm.parameters(), lr=learning_rate)

# Train the model


for epoch in range(num_epochs):
outputs = lstm(trainX)
optimizer.zero_grad()

# obtain the loss function


loss = criterion(outputs, trainY)

loss.backward()

optimizer.step()
if epoch % 100 == 0:
print("Epoch: %d, loss: %1.5f" % (epoch, loss.item()))

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 39/44
8/7/24, 3:54 PM 003-object-detection-dl slides

Epoch: 0, loss: 0.07611


Epoch: 100, loss: 0.00996
Epoch: 200, loss: 0.00310
Epoch: 300, loss: 0.00273
Epoch: 400, loss: 0.00239
Epoch: 500, loss: 0.00214
Epoch: 600, loss: 0.00197
Epoch: 700, loss: 0.00186
Epoch: 800, loss: 0.00179
Epoch: 900, loss: 0.00176
Epoch: 1000, loss: 0.00175
Epoch: 1100, loss: 0.00174
Epoch: 1200, loss: 0.00173
Epoch: 1300, loss: 0.00172
Epoch: 1400, loss: 0.00172
Epoch: 1500, loss: 0.00171
Epoch: 1600, loss: 0.00170
Epoch: 1700, loss: 0.00169
Epoch: 1800, loss: 0.00168
Epoch: 1900, loss: 0.00168

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 40/44
8/7/24, 3:54 PM 003-object-detection-dl slides

In [ ]:

lstm.eval()
train_predict = lstm(dataX)

data_predict = train_predict.data.numpy()
dataY_plot = dataY.data.numpy()

data_predict = sc.inverse_transform(data_predict)
dataY_plot = sc.inverse_transform(dataY_plot)

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 41/44
8/7/24, 3:54 PM 003-object-detection-dl slides

In [70]:

plt.axvline(x=train_size, c='r', linestyle='--')

plt.plot(dataY_plot)
plt.plot(data_predict)
plt.suptitle('Time-Series Prediction')
plt.show()

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 42/44
8/7/24, 3:54 PM 003-object-detection-dl slides

Challenges

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 43/44
8/7/24, 3:54 PM 003-object-detection-dl slides

Speaker notes

127.0.0.1:8000/003-object-detection-dl.slides.html#/ 44/44

You might also like