Final Deep Learning Manual
Final Deep Learning Manual
AIM:
To write a program for feature extraction from image data
ALGORITHM:
We will look at all the aspects of the image so we need to import different libraries including
NumPy, pandas, etc.
You can use any image from your system. I have an image named’image.jpg’ for which I will
be performing feature extraction.
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.
import pandas as pd
import numpy as np
%matplotlib inline
imshow(image)
image.shape, image
import numpy as np
%matplotlib inline
#reading the image
image = imread("C:/python/image.jpg",as_gray=True)
edges_prewitt_horizontal = prewitt_h(image)
edges_prewitt_vertical = prewitt_v(image)
imshow(edges_prewitt_vertical, cmap='gray')
RESULT:
Thus the program for feature extraction from image was executed successfully.
We will look at all the aspects of the image so we need to import different libraries including
NumPy, pandas, etc.
You can use any image from your system, stored as video.mp3 performing feature extraction.
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
def parse_opts():
opt = parse_opts()
opt.mean = get_mean()
opt.sample_size = 112
opt.sample_duration = 16
opt.n_classes = 400
model = generate_model(opt)
model_data = torch.load(opt.model)
model.load_state_dict(model_data['state_dict'])
model.eval()
if opt.verbose:
print(model)
input_files = []
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'
if os.path.exists('tmp'):
outputs = []
if os.path.exists(video_path):
print(video_path)
subprocess.call('ffmpeg −i {} tmp/image_%05d.jpg'.format(video_path),
shell=True)
outputs.append(result)
else:
if os.path.exists('tmp'):
json.dump(outputs, f)
RESULT:
Thus the program for video feature extraction was executed.
PROGRAM
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':
else:
model = Sequential()
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
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)
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')
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')
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.reshape(1,224,224,3)
label = model.predict(img)
RESULT:
Thus the program for image recognition was executed successfully.
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
face_encodings = []
face_names = []
frame_number = 0
while True:
frame_number += 1
if not ret:
break
# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition
uses)
# Find all the faces and face encodings in the current frame of video
face_names = []
name = None
if match[0]:
if not name:
continue
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom − 6), font, 0.5, (255, 255, 255), 1)
output_movie.write(frame)
# All done!
input_movie.release()
cv2.destroyAllWindows()
RESULT:
Thus the program for video recognition was executed successfully
PROGRAM
import numpy as np
import cv2
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'
#−−−−−−−−−−−−−−#−−−−−−−−−−−−−−#
net = dnn.readNetFromCaffe(proto_file,model_file)
kernel = np.load(hull_pts)
#−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−#−−−−−−−−−−−−−−−−−−−−−#
img = cv2.imread(img_path)
#−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−#−−−−−−−−−−−−−−−−−−−−−#
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")]
#−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−#−−−−−−−−−−−−−−−−−−−−−#
L = cv2.split(resized)[0]
# mean subtraction
L −= 50
#−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−#−−−−−−−−−−−−−−−−−−−−−#
net.setInput(cv2.dnn.blobFromImage(L))
# input image
L = cv2.split(lab_img)[0]
colorized = np.clip(colorized, 0, 1)
# change the image to 0−255 range and convert it from float32 to int
img = cv2.resize(img,(640,640))
colorized = cv2.resize(colorized,(640,640))
result = cv2.hconcat([img,colorized])
cv2.waitKey(0)
OUTPUT:
RESULT:
Thus the program for image colorization was executed successfully.
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
# 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."
]
return results
OUTPUT:
RESULT:
Thus the program for aspect oriented topic detection & sentiment analysis was executed
successfully.
AIM::
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
# Autoencoder Model
input_img = layers.Input(shape=(28, 28, 1))
# 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()
OUTPUT:
RESULT:
Thus the program for object detection using autoencoder was executed successfully