0% found this document useful (0 votes)
14 views

Final Deep Learning Manual

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Final Deep Learning Manual

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Deep Learning Manual

M.A.M. COLLEGE OF ENGINEERING 1


Ex.No.1 FEATURE EXTRACTION FROM IMAGE DATA

AIM:
To write a program for feature extraction from image data
ALGORITHM:

1. Importing the required libraries

We will look at all the aspects of the image so we need to import different libraries including
NumPy, pandas, etc.

2. Loading the image

You can use any image from your system. I have an image named’image.jpg’ for which I will
be performing feature extraction.

3. Analyzing both the images

Here we can see that the colored image contains rows, columns, and channels as it is a
colored image there are three channels RGB while grayscale pictures have only one channel.
So we can clearly identify the colored and grayscale images by their shapes.

4. Feature Extraction

Pixel Features
The number of pixels in an image is the same as the size of the image for grayscale images we
can find the pixel features by reshaping the shape of the image and returning the array form of
the image.

M.A.M. COLLEGE OF ENGINEERING 2


IF4071 - DEEP LEARNING LAB MANUAL
PROGRAM

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

%matplotlib inline

from skimage.io import imread, imshow

image = imread("C:/python/image.jpg", as_gray=True)

imshow(image)

image.shape, image

M.A.M. COLLEGE OF ENGINEERING 3


IF4071 - DEEP LEARNING LAB MANUAL

import numpy as np

from skimage.io import imread, imshow

from skimage.filters import prewitt_h,prewitt_v

import matplotlib.pyplot as plt

%matplotlib inline
#reading the image

image = imread("C:/python/image.jpg",as_gray=True)

#calculating horizontal edges using prewitt kernel

edges_prewitt_horizontal = prewitt_h(image)

#calculating vertical edges using prewitt kernel

edges_prewitt_vertical = prewitt_v(image)

imshow(edges_prewitt_vertical, cmap='gray')

RESULT:
Thus the program for feature extraction from image was executed successfully.

M.A.M. COLLEGE OF ENGINEERING 4


IF4071 - DEEP LEARNING LAB MANUAL

Ex.No:1.2 PROGRAM FOR FEATURE EXTRACTION FROM VIDEO


AIM::
To write a program for feature extraction from video data.
ALGORITHM:

1. Importing the required libraries

We will look at all the aspects of the image so we need to import different libraries including
NumPy, pandas, etc.

2. Loading the video

You can use any image from your system, stored as video.mp3 performing feature extraction.

3.Analyzing the video

Here we can see that the video contains rows, columns, and channels as it is a colored image
there are three channels RGB while grayscale pictures have only one channel. So we can
clearly identify the colored and grayscale images by their shapes.

4. Feature Extraction

Pixel Features
The number of pixels in an video is the same as the size of the video where we can find the
pixel features

PROGRAM

import os

import sys

import json

import subprocess

import numpy as np

import torch

from torch import nn


from opts import parse_opts

from model import generate_model

M.A.M. COLLEGE OF ENGINEERING 5


IF4071 - DEEP LEARNING LAB MANUAL
from mean import get_mean

from classify import classify_video

def parse_opts():

if name ==" main ":

opt = parse_opts()

opt.mean = get_mean()

opt.arch = '{}−{}'.format(opt.model_name, opt.model_depth)

opt.sample_size = 112

opt.sample_duration = 16

opt.n_classes = 400

model = generate_model(opt)

print('loading model {}'.format(opt.model))

model_data = torch.load(opt.model)

assert opt.arch == model_data['arch']

model.load_state_dict(model_data['state_dict'])

model.eval()

if opt.verbose:

print(model)

input_files = []

with open(opt.input, 'r') as f:

for row in f:

input_files.append(row[:−1])

class_names = []

with open('class_names_list') as f:

for row in f:

class_names.append(row[:−1])

ffmpeg_loglevel = 'quiet'

if opt.verbose:

ffmpeg_loglevel = 'info'

M.A.M. COLLEGE OF ENGINEERING 6


IF4071 - DEEP LEARNING LAB MANUAL

if os.path.exists('tmp'):

subprocess.call('rm −rf tmp', shell=True)

outputs = []

for input_file in input_files:

video_path = os.path.join(opt.video_root, "C:/Users/Thinkpad/Downloadsinput.mp4")

if os.path.exists(video_path):

print(video_path)

subprocess.call('mkdir tmp', shell=True)

subprocess.call('ffmpeg −i {} tmp/image_%05d.jpg'.format(video_path),

shell=True)

result = classify_video('tmp', input_file, class_names, model, opt)

outputs.append(result)

subprocess.call('rm −rf tmp', shell=True)

else:

print('{} does not exist'.format(input_file))

if os.path.exists('tmp'):

subprocess.call('rm −rf tmp', shell=True)

with open(opt.output, 'w') as f:

json.dump(outputs, f)

RESULT:
Thus the program for video feature extraction was executed.

M.A.M. COLLEGE OF ENGINEERING 7


IF4071 - DEEP LEARNING LAB MANUAL

Ex.No.2 PROGRAM FOR IMAGE RECOGNITION


AIM:
To write a program for image recognition
ALGORITHM
 Train Data: Train data contains the 200 images of each car and plane, i.e. in total, there
are 400 images in the training dataset
 Test Data: Test data contains 50 images of each car and plane i.e., includes a total.
There are 100 images in the test dataset

Conv2D is the layer to convolve the image into multiple images


Activation is the activation function.
MaxPooling2D is used to max pool the value from the given size matrix and same is used
for the next 2 layers. then,
Flatten is used to flatten the dimensions of the image obtained after convolving it.
Dense is used to make this a fully connected model and is the hidden layer.
Dropout is used to avoid overfitting on the dataset.
Dense is the output layer contains only one neuron which decide to which category image
belongs.
Compile function is used here that involve the use of loss, optimizers and metrics. Here loss
function used is binary_crossentropy, optimizer used is rmsprop.
ImageDataGenerator that rescales the image, applies shear in some range, zooms the
image and does horizontal flipping with the image. This ImageDataGenerator includes all
possible orientation of the image.
train_datagen.flow_from_directory is the function that is used to prepare data from the
train_dataset directory Target_size specifies the target size of the image.
test_datagen.flow_from_directory is used to prepare test data for the model and all is
similar as above.
fit_generator is used to fit the data into the model made above, other factors used are
steps_per_epochs tells us about the number of times the model will execute for the training
data.
epochs tells us the number of times model will be trained in forward and backward pass.
validation_data is used to feed the validation/test data into the model.
validation_steps denotes the number of validation/test samples.

M.A.M. COLLEGE OF ENGINEERING 8


IF4071 - DEEP LEARNING LAB MANUAL

PROGRAM

# Importing all necessary libraries

from keras.preprocessing.image import ImageDataGenerator

from keras.models import Sequential

from keras.layers import Conv2D, MaxPooling2D

from keras.layers import Activation, Dropout, Flatten, Dense

from keras import backend as K

img_width, img_height = 224, 224

train_data_dir = 'v_data/train'

validation_data_dir = 'v_data/test'

nb_train_samples =400

nb_validation_samples = 100

epochs = 10

batch_size = 16

if K.image_data_format() == 'channels_first':

input_shape = (3, img_width, img_height)

else:

input_shape = (img_width, img_height, 3)

model = Sequential()

model.add(Conv2D(32, (2, 2), input_shape=input_shape))

model.add(Activation('relu'))

model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(32, (2, 2)))

model.add(Activation('relu'))

model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, (2, 2)))

M.A.M. COLLEGE OF ENGINEERING 9


IF4071 - DEEP LEARNING LAB MANUAL
model.add(Activation('relu'))

model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())

model.add(Dense(64))

model.add(Activation('relu'))

model.add(Dropout(0.5))

model.add(Dense(1))

model.add(Activation('sigmoid'))

model.compile(loss='binary_crossentropy',

optimizer='rmsprop',

metrics=['accuracy'])

train_datagen =

ImageDataGenerator( rescale=

1. / 255, shear_range=0.2,

zoom_range=0.2,

horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1. / 255)

train_generator =

train_datagen.flow_from_directory( train_dat

a_dir,

target_size=(img_width, img_height),

batch_size=batch_size,

class_mode='binary')

validation_generator =

test_datagen.flow_from_directory( validation_data_dir,

target_size=(img_width, img_height),

batch_size=batch_size,

class_mode='binary')

M.A.M. COLLEGE OF ENGINEERING 10


IF4071 - DEEP LEARNING LAB MANUAL

model.fit_generator(

train_generator,

steps_per_epoch=nb_train_samples // batch_size,

epochs=epochs,

validation_data=validation_generator,

validation_steps=nb_validation_samples // batch_size)

model.save_weights('model_saved.h5')

from keras.models import load_model


from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.vgg16 import preprocess_input
from keras.applications.vgg16 import decode_predictions
from keras.applications.vgg16 import VGG16

import numpy as np
from keras.models import load_model
model = load_model('model_saved.h5')
image = load_img('v_data/test/planes/5.jpg', target_size=(224, 224))
img = np.array(image)

img = img / 255.0

img = img.reshape(1,224,224,3)

label = model.predict(img)

print("Predicted Class (0 − Cars , 1− Planes): ", label[0][0])

M.A.M. COLLEGE OF ENGINEERING 11


IF4071 - DEEP LEARNING LAB MANUAL

RESULT:
Thus the program for image recognition was executed successfully.

M.A.M. COLLEGE OF ENGINEERING 12


IF4071 - DEEP LEARNING LAB MANUAL

Ex.No.2.1 PROGRAM FOR VIDEO RECOGNITION


AIM::
To write a program for video recognition
ALGORITHM:
1. Loop over all frames in the video file

2. For each frame, pass the frame through the CNN

3. Classify each frame individually and independently of each other

4. Choose the label with the largest corresponding probability

5. Label the frame and write the output frame to disk

6. Training data is in the same directory currently working.

7. Classifier files are in the model/ directory.

8. An empty output/ folder is the location where we’ll store video classification results.

PROGRAM

import cv2

import face_recognition

input_movie = cv2.VideoCapture("sample_video.mp4")

length = int(input_movie.get(cv2.CAP_PROP_FRAME_COUNT))

image = face_recognition.load_image_file("sample_image.jpeg")

face_encoding = face_recognition.face_encodings(image)[0]

known_faces =

[ face_encoding,

# Initialize variables

M.A.M. COLLEGE OF ENGINEERING 13


IF4071 - DEEP LEARNING LAB MANUAL
face_locations = []

face_encodings = []

face_names = []

frame_number = 0

while True:

# Grab a single frame of video

ret, frame = input_movie.read()

frame_number += 1

# Quit when the input video file ends

if not ret:

break

# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition
uses)

rgb_frame = frame[:, :, ::−1]

# Find all the faces and face encodings in the current frame of video

face_locations = face_recognition.face_locations(rgb_frame, model="cnn")

face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)

face_names = []

for face_encoding in face_encodings:

# See if the face is a match for the known face(s)

match = face_recognition.compare_faces(known_faces, face_encoding, tolerance=0.50)

name = None

if match[0]:

name = "Phani Srikant"

M.A.M. COLLEGE OF ENGINEERING 14


IF4071 - DEEP LEARNING LAB MANUAL
face_names.append(name)

# Label the results

for (top, right, bottom, left), name in zip(face_locations, face_names):

if not name:

continue

# Draw a box around the face

cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

# Draw a label with a name below the face

cv2.rectangle(frame, (left, bottom − 25), (right, bottom), (0, 0, 255), cv2.FILLED)

font = cv2.FONT_HERSHEY_DUPLEX

cv2.putText(frame, name, (left + 6, bottom − 6), font, 0.5, (255, 255, 255), 1)

# Write the resulting image to the output video file

print("Writing frame {} / {}".format(frame_number, length))

output_movie.write(frame)

# All done!

input_movie.release()

cv2.destroyAllWindows()

M.A.M. COLLEGE OF ENGINEERING 15


IF4071 - DEEP LEARNING LAB MANUAL

RESULT:
Thus the program for video recognition was executed successfully

M.A.M. COLLEGE OF ENGINEERING 16


IF4071 - DEEP LEARNING LAB MANUAL

Ex.No.3 PROGRAM FOR IMAGE COLORIZATION


AIM::
To write a program for image colorization
ALGORITHM:
1. Load the model and the convolution/kernel points
2. Read and preprocess the image
3. Generate model predictions using the L channel from our input image
4. Use the output -> ab channel to create a resulting image
Lab colour has 3 channels L, a, and b. But here instead of pixel values,
these have different significances i.e :
 L-channel: light intensity
 a channel: green-red encoding
 b channel: blue-red encoding

PROGRAM

import numpy as np

import cv2

from cv2 import dnn

#−−−−−−−−Model file paths−−−−−−−−#

proto_file = 'Model\colorization_deploy_v2.prototxt'

model_file = 'Model\colorization_release_v2.caffemodel'

hull_pts = 'Model\pts_in_hull.npy'

img_path = 'images/img1.jpg'

#−−−−−−−−−−−−−−#−−−−−−−−−−−−−−#

#−−−−−−−−Reading the model params−−−−−−−−#

net = dnn.readNetFromCaffe(proto_file,model_file)

kernel = np.load(hull_pts)

#−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−#−−−−−−−−−−−−−−−−−−−−−#

M.A.M. COLLEGE OF ENGINEERING 17


IF4071 - DEEP LEARNING LAB MANUAL
#−−−−−Reading and preprocessing image−−−−−−−−#

img = cv2.imread(img_path)

scaled = img.astype("float32") / 255.0

lab_img = cv2.cvtColor(scaled, cv2.COLOR_BGR2LAB)

#−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−#−−−−−−−−−−−−−−−−−−−−−#

# add the cluster centers as 1x1 convolutions to the model

class8 = net.getLayerId("class8_ab")

conv8 = net.getLayerId("conv8_313_rh")
pts = kernel.transpose().reshape(2, 313, 1, 1)

net.getLayer(class8).blobs = [pts.astype("float32")]

net.getLayer(conv8).blobs = [np.full([1, 313], 2.606, dtype="float32")]

#−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−#−−−−−−−−−−−−−−−−−−−−−#

# we'll resize the image for the network

resized = cv2.resize(lab_img, (224, 224))

# split the L channel

L = cv2.split(resized)[0]

# mean subtraction

L −= 50

#−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−#−−−−−−−−−−−−−−−−−−−−−#

# predicting the ab channels from the input L channel

net.setInput(cv2.dnn.blobFromImage(L))

ab_channel = net.forward()[0, :, :, :].transpose((1, 2, 0))

# resize the predicted 'ab' volume to the same dimensions as our

# input image

ab_channel = cv2.resize(ab_channel, (img.shape[1], img.shape[0]))

M.A.M. COLLEGE OF ENGINEERING 18


IF4071 - DEEP LEARNING LAB MANUAL
# Take the L channel from the image

L = cv2.split(lab_img)[0]

# Join the L channel with predicted ab channel

colorized = np.concatenate((L[:, :, np.newaxis], ab_channel), axis=2)

# Then convert the image from Lab to BGR

colorized = cv2.cvtColor(colorized, cv2.COLOR_LAB2BGR)

colorized = np.clip(colorized, 0, 1)

# change the image to 0−255 range and convert it from float32 to int

colorized = (255 * colorized).astype("uint8")

# Let's resize the images and show them together

img = cv2.resize(img,(640,640))

colorized = cv2.resize(colorized,(640,640))

result = cv2.hconcat([img,colorized])

cv2.imshow("Grayscale −> Colour", result)

cv2.waitKey(0)

M.A.M. COLLEGE OF ENGINEERING 19


IF4071 - DEEP LEARNING LAB MANUAL

OUTPUT:

RESULT:
Thus the program for image colorization was executed successfully.

M.A.M. COLLEGE OF ENGINEERING 20


IF4071 - DEEP LEARNING LAB MANUAL

Ex.No:04 PROGRAM FOR ASPECT ORIENTED TOPIC DETECTION &


SENTIMENT ANALYSIS

AIM::
To write a program for aspect oriented topic detection & sentiment analysis.
ALGORITHM
1. Collect and preprocess text data.
2. Identify key aspects using keywords or topic modeling.
3. Segment sentences to focus on individual aspects.
4. Analyze sentiment for each aspect-related sentence.
5. Aggregate and present overall sentiment for each aspect.

PROGRAM

from textblob import TextBlob


import re

# Sample data
reviews = [
"The camera quality is excellent, but the battery life is disappointing.",
"I love the screen resolution, but the sound quality is poor.",
"Great design and display, but the processor is slow."
]

# Predefined aspect keywords


aspects = ["camera", "battery", "screen", "sound", "design", "processor"]

def aspect_based_sentiment_analysis(text, aspects):


results = {aspect: "Neutral" for aspect in aspects} # Initialize results with neutral sentiment
# Split sentences for individual analysis
sentences = text.split('.')
for sentence in sentences:
clean_sentence = re.sub(r'[^\w\s]', '', sentence) # Clean the sentence
sentiment = TextBlob(clean_sentence).sentiment.polarity

for aspect in aspects:


if aspect in clean_sentence.lower():
# Classify sentiment as positive, negative, or neutral
if sentiment > 0:
results[aspect] = "Positive"
elif sentiment < 0:
results[aspect] = "Negative"

return results

M.A.M. COLLEGE OF ENGINEERING 21


IF4071 - DEEP LEARNING LAB MANUAL

# Run analysis for each review


for i, review in enumerate(reviews, 1):
print(f"Review {i}: {review}")
print("Aspect Sentiment Analysis:", aspect_based_sentiment_analysis(review, aspects))
print()

OUTPUT:

M.A.M. COLLEGE OF ENGINEERING 22


IF4071 - DEEP LEARNING LAB MANUAL

RESULT:
Thus the program for aspect oriented topic detection & sentiment analysis was executed
successfully.

M.A.M. COLLEGE OF ENGINEERING 23


IF4071 - DEEP LEARNING LAB MANUAL
Ex.No.5 PROGRAM FOR OBJECT DETECTION USING AUTOENCODER

AIM::

To build a model for object detection using an autoencoder by training it to recognize


key features of objects and distinguish them from the background.
ALGORITHM:

1. Preprocess and normalize the image dataset.

2. Build an autoencoder with an encoder to capture features and a decoder for


reconstruction.

3. Train the model on both original and noisy images.

4. Use the encoder's feature representations to detect objects.

5. Compare original and reconstructed images to evaluate detection accuracy.

PROGRAM

import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras import layers, models
from tensorflow.keras.datasets import mnist # Use a simple dataset, e.g., MNIST for
demonstration

# Load dataset and preprocess


(train_data, _), (test_data, _) = mnist.load_data()
train_data = train_data.astype('float32') / 255.
test_data = test_data.astype('float32') / 255.
train_data = np.reshape(train_data, (len(train_data), 28, 28, 1)) # Shape for CNN input
test_data = np.reshape(test_data, (len(test_data), 28, 28, 1))

# Add noise to dataset


noise_factor = 0.5
noisy_train_data = train_data + noise_factor * np.random.normal(loc=0.0, scale=1.0,
size=train_data.shape)
noisy_test_data = test_data + noise_factor * np.random.normal(loc=0.0, scale=1.0,
size=test_data.shape)
noisy_train_data = np.clip(noisy_train_data, 0., 1.)
noisy_test_data = np.clip(noisy_test_data, 0., 1.)

# Autoencoder Model
input_img = layers.Input(shape=(28, 28, 1))

M.A.M. COLLEGE OF ENGINEERING 24


IF4071 - DEEP LEARNING LAB MANUAL

# Encoder
x = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)
x = layers.MaxPooling2D((2, 2), padding='same')(x)
x = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(x)
encoded = layers.MaxPooling2D((2, 2), padding='same')(x)

# Decoder
x = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(encoded)
x = layers.UpSampling2D((2, 2))(x)
x = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(x)
x = layers.UpSampling2D((2, 2))(x)
decoded = layers.Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)

# Compile Model
autoencoder = models.Model(input_img, decoded)
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')

# Train Model
autoencoder.fit(noisy_train_data, train_data,
epochs=10,
batch_size=128,
shuffle=True,
validation_data=(noisy_test_data, test_data))

# Test Model
decoded_imgs = autoencoder.predict(noisy_test_data)

# Display Results
n = 10 # Number of images to display
plt.figure(figsize=(20, 4))
for i in range(n):
# Display original + noise
ax = plt.subplot(3, n, i + 1)
plt.imshow(noisy_test_data[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)

# Display reconstruction
ax = plt.subplot(3, n, i + n + 1)
plt.imshow(decoded_imgs[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()

M.A.M. COLLEGE OF ENGINEERING 25


IF4071 - DEEP LEARNING LAB MANUAL

OUTPUT:

RESULT:
Thus the program for object detection using autoencoder was executed successfully

M.A.M. COLLEGE OF ENGINEERING 26

You might also like