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

Deep Learning Manual (1)

The document outlines multiple programs aimed at feature extraction, image classification, image recognition, video recognition, and transfer learning using Python. Each section includes an aim, algorithm, and program code demonstrating the implementation of these tasks using libraries such as NumPy, TensorFlow, and OpenCV. The results indicate successful execution of each program for their respective tasks.

Uploaded by

febin rani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views44 pages

Deep Learning Manual (1)

The document outlines multiple programs aimed at feature extraction, image classification, image recognition, video recognition, and transfer learning using Python. Each section includes an aim, algorithm, and program code demonstrating the implementation of these tasks using libraries such as NumPy, TensorFlow, and OpenCV. The results indicate successful execution of each program for their respective tasks.

Uploaded by

febin rani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 44

Ex.No.

1 PROGRAM FOR 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.

PROGRAM

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

%matplotlib inline

from skimage.io import imread, imshow

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

imshow(image)

print(image.shape)

print(image)

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

imshow(image)

print(image.shape)

(524, 800, 3)

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

feature_matrix_image = np.zeros((375,500))

feature_matrix_image

2
feature_matrix_image.shape

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

3
Ex.No.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

4
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:

5
ffmpeg_loglevel = 'info'

6
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:

7
Thus the program for video feature extraction was executed.

8
Ex.No.3 PROGRAM FOR IMAGE CLASSIFICATION
AIM
To write a program for image classification using python
ALGORITHM
Import the necessary libraries which are required for performing CNN tasks.

A convoluted image can be too large and so it is reduced without losing features or patterns, so

pooling is done.

Here Creating a Neural network is to initialize the network using the Sequential model from
Keras.
we are required to specify optimizers.

Optimizer is used to reduce the cost calculated by cross-entropy

The loss function is used to calculate the error

The metrics term is used to represent the efficiency of the model

In this step, we will see how to set the data directory and generate image data.

PROGRAM

import numpy as np

import random

import matplotlib.pyplot as plt

from tensorflow.keras.models import Sequential

from tensorflow.keras.layers import Conv2D, Activation, MaxPooling2D, Dense, Flatten

from tensorflow.keras.preprocessing.image import ImageDataGenerator

X_train = np.loadtxt("C:/python/input.csv",delimiter=',')

Y_train = np.loadtxt("C:/python/labels.csv",delimiter=',')

X_test = np.loadtxt("C:/python/input_test.csv",delimiter=',')

9
Y_test = np.loadtxt("C:/python/labels_test.csv",delimiter=',')

X_train = X_train.reshape(len(X_train),100,100,3)

Y_train = Y_train.reshape(len(Y_train),1)

X_train = X_train/255.0

X_test = X_test/255.0

X_test = X_test.reshape(len(X_test),100,100,3)

Y_test =Y_test.reshape(len(Y_test),1)

print("Shape of X_train: ", X_train.shape)

print("Shape of Y_train: ", Y_train.shape)

print("Shape of X_test: ", X_test.shape)

print("Shape of Y_test: ", Y_test.shape)

idx = random.randint(0,

len(X_train)) plt.imshow(X_train[idx,

:]) plt.show()

10
model = Sequential([

Conv2D(32, (3,3), activation='relu', input_shape=(100,100,3)),

MaxPoolin2D((2,2)),

Conv2D(32, (3,3), activation = 'relu')

MaxPooling2D(2,2)),

Flatten(),

Dense(64, activation='relu'),

Dense(1, activation = 'sigmoid')

])

model= Sequential()

model.add(Conv2D(32, (3,3), activation='relu', input_shape=(100,100,3)))

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

11
model.add(Conv2D(32, (3,3), activation='relu'))

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

model.add(Flatten())

model.add(Dense(64, activation = 'relu'))

model.add(Dense(1, activation='sigmoid'))

model.compile(loss = 'binary_crossentropy' ,optimizer = 'adam', metrics=['accuracy'])

model.fit(X_train, Y_train, epochs=5, batch_size=64)

model.evaluate(X_test, Y_test)

idX2=random.randint(0, len(Y_test))

plt.imshow(X_test[idX2, :])

plt.show()

Y_pred = model.predict(X_test[idX2, :].reshape(1, 100, 100, 3))

Y_pred= Y_pred>0.5

if(Y_pred==0):

pred='dog'

else:

pred='cat'

12
print("Our model says it is a :", pred)

RESULT:
Thus the program for image classification was executed successfully

13
Ex.No.4 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 ImagenDataGenerator 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

14
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)))

15
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_data_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'

16
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))

17
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])

RESULT:
Thus the program for image recognition was executed successfully

18
Ex.No.5 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

19
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"

20
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()

21
RESULT:
Thus the program for video recognition was executed successfully

22
Ex.No.6 PROGRAM FOR TRANSFER LEARNING

AIM
To write a program for transfer learning
ALGORITHM

Input data files are available in the "../input/" directory.


import os
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
Any results you write to the current directory are saved as output.
Instantiate convolutional base
Extract features and pass through convolutional base
Test the feartures test_features, test_labels = extract_features(test_dir, test_size)
Compile and train the model

PROGRAM

import numpy as np

import cv2

import PIL.Image as Image

import os

import matplotlib.pylab as plt

import tensorflow as tf

import tensorflow_hub as hub

from tensorflow import keras

from tensorflow.keras import layers

23
from tensorflow.keras.models import Sequential

IMAGE_SHAPE = (224, 224)

classifier = tf.keras.Sequential([

hub.KerasLayer("https://tfhub.dev/google/tf2-preview/mobilenet_v2/classification/4",
input_shape=IMAGE_SHAPE+(3,))

])

gold_fish = Image.open("C:/python/goldfish.jpg").resize(IMAGE_SHAPE)

gold_fish

gold_fish = np.array(gold_fish)/255.0

gold_fish.shape

gold_fish[np.newaxis, ...]

24
result = classifier.predict(gold_fish[np.newaxis, ...])

result.shape

result = classifier.predict(gold_fish[np.newaxis, ...])

result.shape

predicted_label_index = np.argmax(result)

predicted_label_index

image_labels = []

with open("C:/python/ImageNetLabels.txt", "r") as f:

image_labels = f.read().splitlines()

image_labels[:5]

image_labels[predicted_label_index]

RESULT:

Thus the program for transfer learning was executed successfully

25
Ex.No.7 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.

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)

26
imshow(image)

image.shape, image

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

27
#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

28
Ex.No.8 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)

# # #

29
#-----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]))

30
# 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)

31
OUTPUT:

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

32
Ex.No.9 PROGRAM FOR TWITTER SENTIMENT ANALYSIS AIM
To write a program for twitter sentiment analysis
ALGORITHM
1. Sentiment analysis is the process of determining whether a piece of writing is
positive, negative or neutral.
2. Install Tweepy: is the python client for the official
3. Twitter API

Install it by pip install tweepy

Textblob: is the python library for processing textual data.

PROGRAM

from transformers import AutoTokenizer, AutoModelForSequenceClassification

from scipy.special import softmax

# tweet = "@MehranShakarami today's cold @ home 😒 https://mehranshakarami.com"


tweet = 'Great content! subscribed 😉'

# precprcess tweet

tweet_words = []

for word in tweet.split(' '):

if word.startswith('@') and len(word) > 1:

word = '@user'

elif word.startswith('http'):

word = "http"

tweet_words.append(word)

33
tweet_proc = " ".join(tweet_words)

# load model and tokenizer

roberta = "cardiffnlp/twitter-roberta-base-sentiment"

model = AutoModelForSequenceClassification.from_pretrained(roberta)

tokenizer = AutoTokenizer.from_pretrained(roberta)

labels = ['Negative', 'Neutral', 'Positive']

# sentiment analysis

encoded_tweet = tokenizer(tweet_proc, return_tensors='pt')

# output = model(encoded_tweet['input_ids'], encoded_tweet['attention_mask'])

output = model(**encoded_tweet)

scores = output[0][0].detach().numpy()

scores = softmax(scores)

for i in range(len(scores)):

l = labels[i]

s = scores[i]

print(l,s)

34
OUTPUT:

RESULT:
Thus the program for twitter sentiment analysis was executed successfully

35
Ex.No.10 PROGRAM FOR BACKPROPAGATION
AIM
To write a program for backpropagation with CNN.
ALGORITHM

1. Visualizing the input data


2. Deciding the shapes of Weight and bias matrix
3. Initializing matrix, function to be used
4. Implementing the backpropagation method
5. Implementing the cost calculation
6. Backpropagation and optimizing
7. prediction and visualizing the output

PROGRAM

import numpy as np

X = np.array(([2, 9], [1, 5], [3, 6]), dtype=float)

y = np.array(([92], [86], [89]), dtype=float)

X = X/np.amax(X,axis=0) #maximum of X array longitudinally

y = y/100

def sigmoid (x):

return 1/(1 + np.exp(-x))

def derivatives_sigmoid(x):

return x * (1 - x)

epoch=5

lr=0.1

inputlayer_neurons = 2

hiddenlayer_neurons = 3

output_neurons = 1

36
bh=np.random.uniform(size=(1,hiddenlayer_neurons))

wout=np.random.uniform(size=(hiddenlayer_neurons,output_neurons))

bout=np.random.uniform(size=(1,output_neurons))

EO = y-output

outgrad = derivatives_sigmoid(output)

d_output = EO * outgrad

EH = d_output.dot(wout.T)

hiddengrad = derivatives_sigmoid(hlayer_act)

d_hiddenlayer = EH * hiddengrad

wout += hlayer_act.T.dot(d_output) *lr

wh += X.T.dot(d_hiddenlayer) *lr

print ("-----------Epoch-", i+1, "Starts----------")

print("Input: \n" + str(X))

print("Actual Output: \n" + str(y))

print("Predicted Output: \n" ,output)

print ("-----------Epoch-", i+1, "Ends----------\n")

print("Input: \n" + str(X))

print("Actual Output: \n" + str(y))

print("Predicted Output: \n" ,output)

37
RESULT:
Thus the program for backpropagation with CNN was executed successfully

38
Ex.No.11 PROGRAM FOR DENOISING IMAGE USING AUTOENCODERS AIM
To write a program for denoising image using autoencoders
ALGORITHM

1. Implement a deep convolutional autoencoder for image denoising, mapping noisy


digits images from the MNIST dataset to clean digits images.
2. This implementation is based on an original blog post titled Building Autoencoders
in Keras
3. Setup the necessary library files.
4. Build the autoencoder
5. We can train our autoencoder using train_data as both our input data and target
6. Predict on our test dataset and display the original image together with
the prediction from our autoencoder.
7. Using the noisy data as our input and the clean data as our target, we want our
autoencoder to learn how to denoise the images.
8. Now predict on the noisy data and display the results of our autoencoder.
9. The autoencoder finally removes the noise from the input images.

PROGRAM

import numpy as np

import tensorflow as tf

import matplotlib.pyplot as plt

from tensorflow.keras import layers

from tensorflow.keras.datasets import mnist

from tensorflow.keras.models import Model

def preprocess(array):

39
array = array.astype("float32") / 255.0

array = np.reshape(array, (len(array), 28, 28, 1))

return array

def noise(array):

noise_factor = 0.4

noisy_array = array + noise_factor *

np.random.normal( loc=0.0, scale=1.0, size=array.shape

return np.clip(noisy_array, 0.0, 1.0)

def display(array1, array2):

n = 10

indices = np.random.randint(len(array1), size=n)

images1 = array1[indices, :]

images2 = array2[indices, :]

plt.figure(figsize=(20, 4))

for i, (image1, image2) in enumerate(zip(images1, images2)):

ax = plt.subplot(2, n, i + 1)

plt.imshow(image1.reshape(28, 28))

plt.gray()

ax.get_xaxis().set_visible(False)

ax.get_yaxis().set_visible(False)

40
ax = plt.subplot(2, n, i + 1 + n)

plt.imshow(image2.reshape(28, 28))

plt.gray()

ax.get_xaxis().set_visible(False)

ax.get_yaxis().set_visible(False)

plt.show()

(train_data, _), (test_data, _) = mnist.load_data()

# Normalize and reshape the data

train_data = preprocess(train_data)

test_data = preprocess(test_data)

# Create a copy of the data with added noise

noisy_train_data = noise(train_data)

noisy_test_data = noise(test_data)

# Display the train data and a version of it with added noise

display(train_data, noisy_train_data)

input = layers.Input(shape=(28, 28, 1))

# Encoder

x = layers.Conv2D(32, (3, 3), activation="relu", padding="same")(input)

x = layers.MaxPooling2D((2, 2), padding="same")(x)

41
x = layers.Conv2D(32, (3, 3), activation="relu", padding="same")(x)

x = layers.MaxPooling2D((2, 2), padding="same")(x)

# Decoder

x = layers.Conv2DTranspose(32, (3, 3), strides=2, activation="relu", padding="same")(x)

x = layers.Conv2DTranspose(32, (3, 3), strides=2, activation="relu", padding="same")(x)

x = layers.Conv2D(1, (3, 3), activation="sigmoid", padding="same")(x)

# Autoencoder

autoencoder = Model(input, x)

autoencoder.compile(optimizer="adam", loss="binary_crossentropy")

autoencoder.summary()

autoencoder.fit( x=train_

data,
42
y=train_data,

epochs=50,

batch_size=128,

shuffle=True,

validation_data=(test_data, test_data),

predictions = autoencoder.predict(noisy_test_data)

display(noisy_test_data, predictions)

43
RESULT:

Thus the program for denoising image using autoencoders was executed successfully.

44

You might also like