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

Deep Learning Lab Manual

The document is a lab manual for deep learning, detailing various programs for feature extraction from images and videos, image classification, image recognition, and video recognition. Each exercise includes an aim, algorithm, and program code, demonstrating the use of libraries like TensorFlow, Keras, and OpenCV. The results indicate successful execution of each program, showcasing practical applications of deep learning techniques.

Uploaded by

chitra
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)
89 views44 pages

Deep Learning Lab Manual

The document is a lab manual for deep learning, detailing various programs for feature extraction from images and videos, image classification, image recognition, and video recognition. Each exercise includes an aim, algorithm, and program code, demonstrating the use of libraries like TensorFlow, Keras, and OpenCV. The results indicate successful execution of each program, showcasing practical applications of deep learning techniques.

Uploaded by

chitra
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

IF4071-DEEPLEARNINGLABMANUAL

Ex.No.1PROGRAMFORFEATUREEXTRACTIONFROMIMAGEDATA
AIM:
Towriteaprogramfor featureextractionfromimage data.
ALGORITHM

1. Importingtherequired libraries

Wewilllookatalltheaspectsoftheimagesoweneedtoimportdifferentlibrariesincluding NumPy,

pandas, etc.

2. Loadingthe image

Youcanuse anyimagefrom yoursystem.Ihave animagenamed’image.jpg’ forwhichIwill


beperformingfeatureextraction.

3. Analyzingboththeimages

Here we can see that the colored image contains rows, columns, and channels as it is a
coloredimagetherearethreechannelsRGBwhilegrayscalepictureshaveonlyonechannel. So we
can clearly identify the colored and grayscale images by their shapes.

4. FeatureExtraction

Pixel Features
Thenumberofpixelsinanimageisthesameasthesizeoftheimageforgrayscaleimageswe
canfindthepixelfeaturesbyreshapingtheshapeoftheimageandreturningthearrayformof the
image.

PROGRAM

importpandasaspdimpor

tnumpyasnp

importmatplotlib.pyplotasplt

%matplotlibinline

fromskimage.ioimportimread,imshow

1
IF4071-DEEPLEARNINGLABMANUAL
image=imread("C:/python/

image.jpg",as_gray=True)imshow(image)

print(image.shape)print(imag

e)

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
IF4071-DEEPLEARNINGLABMANUAL

feature_matrix_image.shape

RESULT:
Thustheprogram forfeatureextractionfromimagedata wasexecutedsuccessfully.

3
IF4071-DEEPLEARNINGLABMANUAL

Ex.No.2PROGRAMFORFEATUREEXTRACTIONFROMVIDEO
AIM:
Towriteaprogramfor featureextractionfrom videodata.
ALGORITHM

1. Importingtherequired libraries

Wewilllookatalltheaspectsoftheimagesoweneedtoimportdifferentlibrariesincluding NumPy,
pandas, etc.

2. Loadingthevideo

Youcanuseanyimagefromyoursystem,storedasvideo.mp3performingfeatureextraction.

3.Analyzingthe video

Herewecanseethatthe videocontainsrows,columns,andchannelsasitis acoloredimage 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.FeatureExtraction

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

PROGRAM

importosi

mportsysi

mportjson

importsubprocessimport

numpyasnpimporttorch

fromtorchimportnn

fromoptsimportparse_opts

frommodelimportgenerate_model

4
IF4071-DEEPLEARNINGLABMANUAL
frommeanimportget_meanfromcl

assifyimportclassify_video

defparse_opts():

ifname=="main":

opt=parse_opts()opt.mean

=get_mean()

opt.arch='{}−

{}'.format(opt.model_name,opt.model_depth)opt.sample_size=1

12

opt.sample_duration=16

opt.n_classes=400

model=generate_model(opt)print('loading

model{}'.format(opt.model))model_data=t

orch.load(opt.model)

assertopt.arch==model_data['arch']model.load_st

ate_dict(model_data['state_dict'])model.eval()

ifopt.verbose:print(mo

del)input_files=[]

withopen(opt.input,'r')asf:forr

owinf:

input_files.append(row[:−1])

class_names=[]

withopen('class_names_list')asf:forr

owinf:

class_names.append(row[:

−1])ffmpeg_loglevel='quiet'

ifopt.verbose:ffmpeg_logle

vel='info'

5
IF4071-DEEPLEARNINGLABMANUAL

ifos.path.exists('tmp'):

subprocess.call('rm−rftmp',shell=True)ou

tputs=[]

forinput_fileininput_files:

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

Downloadsinput.mp4")ifos.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−rftmp',shell=True)el

se:

print('{}doesnotexist'.format(input_file))

ifos.path.exists('tmp'):subprocess.call('rm

−rftmp',shell=True)

withopen(opt.output,'w')asf:json.du

mp(outputs,f)

RESULT:
Thustheprogramforvideofeatureextractionwasexecuted.

6
IF4071-DEEPLEARNINGLABMANUAL

Ex.No.3PROGRAMFORIMAGECLASSIFICATION
AIM
Towriteaprogram forimageclassification usingpython
ALGORITHM
Importthenecessarylibrarieswhicharerequired forperformingCNNtasks.

Aconvolutedimagecanbetoolargeandsoitisreducedwithoutlosingfeaturesorpatterns,so pooling is

done.

HereCreatingaNeuralnetworkistoinitializethenetworkusingtheSequentialmodelfrom Keras.
wearerequiredtospecifyoptimizers.

Optimizerisusedtoreducethecostcalculatedbycross-entropy The

loss function is used to calculate the error

Themetricstermisused torepresentthe efficiencyofthemodel

Inthisstep,wewillseehowtosetthedatadirectoryandgenerateimage data.

PROGRAM

importnumpyasnpimpor

trandom

importmatplotlib.pyplotasplt

fromtensorflow.keras.modelsimportSequential

fromtensorflow.keras.layersimportConv2D,Activation,MaxPooling2D,Dense,Flattenfrom

tensorflow.keras.preprocessing.imageimportImageDataGenerator

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=',')

7
IF4071-DEEPLEARNINGLABMANUAL
Y_test=np.loadtxt("C:/python/

labels_test.csv",delimiter=',')X_train=X_train.reshape(len(X_tr

ain),100,100,3)

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

X_train=X_train/

255.0X_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("ShapeofX_train:",X_train.shape)print("Sh

apeofY_train:",Y_train.shape)print("ShapeofX_t

est:",X_test.shape)print("ShapeofY_test:",Y_tes

t.shape)

idx=random.randint(0,len(X_train))p

lt.imshow(X_train[idx,:])plt.show()

8
IF4071-DEEPLEARNINGLABMANUAL

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

9
IF4071-DEEPLEARNINGLABMANUAL

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,acti

vation='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.5i

f(Y_pred==0):

pred='dog'e

lse:

pred='cat'

10
IF4071-DEEPLEARNINGLABMANUAL
print("Ourmodelsaysitisa:",pred)

RESULT:
Thustheprogramforimageclassificationwasexecutedsuccessfully

11
IF4071-DEEPLEARNINGLABMANUAL

Ex.No.4PROGRAMFORIMAGERECOGNITION
AIM
Towriteaprogramforimagerecognition
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

Conv2Disthelayertoconvolvetheimageintomultipleimages
Activationistheactivationfunction.
MaxPooling2D is used to max pool the value from the given size matrix and same is
usedfor the next 2 layers. then,
Flattenisusedtoflattenthedimensionsoftheimageobtainedafterconvolvingit.
Denseisusedtomakethisafullyconnectedmodelandisthehiddenlayer.
Dropoutisusedtoavoidoverfittingonthedataset.
Dense is the output layer contains only one neuron which decide to which category
imagebelongs.
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
theimage and does horizontal flipping with the image. This ImageDataGenerator includes
allpossible orientation of the image.
train_datagen.flow_from_directory is the function that is used to prepare data from
thetrain_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
issimilar as above.
fit_generator is used to fit the data into the model made above, other factors used
aresteps_per_epochs tells us about the number of times the model will execute for the
trainingdata.
epochs tells us the number of times model will be trained in forward and backward pass.
validation_dataisusedtofeedthevalidation/testdataintothemodel.validation_steps denotes
the number of validation/test samples.

12
IF4071-DEEPLEARNINGLABMANUAL

PROGRAM

#Importingallnecessarylibraries

fromkeras.preprocessing.imageimportImageDataGeneratorfro

mkeras.modelsimportSequential

fromkeras.layersimportConv2D,MaxPooling2D

fromkeras.layersimportActivation,Dropout,Flatten,Densefrom

kerasimportbackendasK

img_width,img_height=224,224train_data_dir=

'v_data/train'validation_data_dir='v_data/

test'nb_train_samples=400

nb_validation_samples=100

epochs=10

batch_size=16

ifK.image_data_format()=='channels_first':input_shap

e=(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.ad

d(MaxPooling2D(pool_size=(2,2)))

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

13
IF4071-DEEPLEARNINGLABMANUAL
model.add(Activation('relu'))model.add(Max

Pooling2D(pool_size=(2,2)))

model.add(Flatten())model.add(Dense(64))

model.add(Activation('relu'))model.add(Dr

opout(0.5))model.add(Dense(1))model.ad

d(Activation('sigmoid'))model.compile(loss

='binary_crossentropy',

optimizer='rmsprop',metrics=['

accuracy'])

train_datagen=ImageDataGenerator(re

scale=1./255,shear_range=0.2,z

oom_range=0.2,horizontal_flip=

True)

test_datagen=ImageDataGenerator(rescale=1./255)

train_generator=train_datagen.flow_from_directory(t

rain_data_dir,

target_size=(img_width,img_height),

batch_size=batch_size,class_mode='b

inary')

validation_generator=test_datagen.flow_from_directory(validation_

data_dir,

target_size=(img_width,img_height),

batch_size=batch_size,class_mode='b

inary')

14
IF4071-DEEPLEARNINGLABMANUAL

model.fit_generator(

train_generator,steps_per_epoch=nb_train_sample

s//

batch_size,epochs=epochs,validation_data=validatio

n_generator,

validation_steps=nb_validation_samples//

batch_size)model.save_weights('model_saved.h5')

fromkeras.modelsimportload_model

fromkeras.preprocessing.imageimportload_imgfromk

eras.preprocessing.imageimportimg_to_array

fromkeras.applications.vgg16importpreprocess_inputfromk

eras.applications.vgg16importdecode_predictionsfromkera

s.applications.vgg16importVGG16

importnumpyasnp

fromkeras.modelsimportload_model

model=load_model('model_saved.h5')

image=load_img('v_data/test/planes/5.jpg',target_size=(224,224))

15
IF4071-DEEPLEARNINGLABMANUAL
img=np.array(image)img

=img/255.0

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

bel=model.predict(img)

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

RESULT:
Thustheprogram forimagerecognition wasexecuted successfully

16
IF4071-DEEPLEARNINGLABMANUAL

Ex.No.5PROGRAMFORVIDEORECOGNITION
AIM
Towriteaprogramforvideorecognition
ALGORITHM
1. Loopoverallframes inthevideofile

2. Foreachframe,passtheframethroughtheCNN

3. Classifyeach frameindividuallyandindependentlyofeachother

4. Choosethelabel withthelargestcorrespondingprobability

5. Labeltheframeandwritetheoutputframetodisk

6. Trainingdata is in thesame directorycurrentlyworking.

7. Classifierfilesareinthe model/directory.

8. Anemptyoutput/ folderisthe locationwherewe’llstorevideoclassificationresults.

PROGRAM

importcv2

importface_recognition

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

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

ge=face_recognition.load_image_file("sample_image.jpeg")face_

encoding=face_recognition.face_encodings(image)[0]

known_faces=[face_enc

oding,

#Initializevariables

17
IF4071-DEEPLEARNINGLABMANUAL
face_locations=[]face_e

ncodings=[]face_names=

[]frame_number=0

whileTrue:

#Grabasingleframeofvideoret,fra

me=input_movie.read()frame_n

umber+=1

#Quitwhentheinputvideofileendsifno

tret:

break

#ConverttheimagefromBGRcolor(whichOpenCVuses)toRGBcolor(whichface_recognitionuses)

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

#Findallthefacesandfaceencodingsinthecurrentframeofvideoface_locations=fac

e_recognition.face_locations(rgb_frame,model="cnn")face_encodings=face_re

cognition.face_encodings(rgb_frame,face_locations)

face_names=[]

forface_encodinginface_encodings:

#Seeifthefaceisamatchfortheknownface(s)

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

name=Noneif

match[0]:

name="PhaniSrikant"

18
IF4071-DEEPLEARNINGLABMANUAL
face_names.append(name)

#Labeltheresults

for(top,right,bottom,left),nameinzip(face_locations,face_names):ifnot

name:

continue

#Drawaboxaroundtheface

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

#Drawalabelwithanamebelowtheface

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)

#Writetheresultingimagetotheoutputvideofileprint("Writing

frame{}/

{}".format(frame_number,length))output_movie.write(fram

e)

#Alldone!

input_movie.release()cv2

.destroyAllWindows()

19
IF4071-DEEPLEARNINGLABMANUAL

RESULT:
Thustheprogramforvideorecognitionwasexecuted successfully

20
IF4071-DEEPLEARNINGLABMANUAL

Ex.No.6PROGRAMFORTRANSFERLEARNING

AIM
Towriteaprogramfortransfer learning
ALGORITHM

Inputdatafilesareavailableinthe"../input/"directory. import os
fordirname,_, filenamesinos.walk('/kaggle/input'):
forfilenamein filenames:
print(os.path.join(dirname,filename))
Anyresultsyouwritetothecurrentdirectoryaresavedasoutput.
Instantiate convolutional base
Extractfeatures andpassthroughconvolutionalbase
Testthefearturestest_features,test_labels=extract_features(test_dir,test_size) Compile
and train the model

PROGRAM

importnumpyasnpimpor

tcv2

importPIL.ImageasImageimport

os

importmatplotlib.pylabasplt

importtensorflowastf

importtensorflow_hubashub

fromtensorflowimportkeras

fromtensorflow.kerasimportlayers

21
IF4071-DEEPLEARNINGLABMANUAL
fromtensorflow.keras.modelsimportSequentialIMAGE

_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.0gold_fish.shape

gold_fish[np.newaxis,...]

22
IF4071-DEEPLEARNINGLABMANUAL

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_la

bel_index

image_labels=[]

withopen("C:/python/

ImageNetLabels.txt","r")asf:image_labels=f.read().s

plitlines()

image_labels[:5]image_labels[predicted_label_i

ndex]

RESULT:

Thustheprogramfortransferlearningwasexecutedsuccessfully

23
IF4071-DEEPLEARNINGLABMANUAL

Ex.No.7FEATUREEXTRACTIONFROMIMAGEDATA

AIM
Towriteaprogramforfeatureextractionfromimagedata
ALGORITHM

1. Importingtherequired libraries

Wewilllookatalltheaspectsoftheimagesoweneedtoimportdifferentlibrariesincluding NumPy,
pandas, etc.

2. Loadingthe image

Youcanuse anyimagefrom yoursystem.Ihave animagenamed’image.jpg’ forwhichIwill


beperformingfeatureextraction.

3. Analyzingboththeimages

Here we can see that the colored image contains rows, columns, and channels as it is a
coloredimagetherearethreechannelsRGBwhilegrayscalepictureshaveonlyonechannel. So we
can clearly identify the colored and grayscale images by their shapes.

4. FeatureExtraction

Pixel Features
Thenumberofpixelsinanimageisthesameasthesizeoftheimageforgrayscaleimageswe
canfindthepixelfeaturesbyreshapingtheshapeoftheimageandreturningthearrayformof the
image.

PROGRAM

importpandasaspdimpor

tnumpyasnp

importmatplotlib.pyplotasplt

%matplotlibinline

fromskimage.ioimportimread,imshow

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

24
IF4071-DEEPLEARNINGLABMANUAL
imshow(image)

image.shape,image

importnumpyasnp

fromskimage.ioimportimread,imshow

fromskimage.filtersimportprewitt_h,prewitt_vi

mportmatplotlib.pyplotasplt

%matplotlibinline

25
IF4071-DEEPLEARNINGLABMANUAL
#readingtheimage

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

#calculatinghorizontaledgesusingprewittkerneledges_

prewitt_horizontal=prewitt_h(image)#calculatingverti

caledgesusingprewittkerneledges_prewitt_vertical=pr

ewitt_v(image)

imshow(edges_prewitt_vertical,cmap='gray')

RESULT:
Thustheprogramforfeatureextractionfromimage wasexecuted successfully

26
IF4071-DEEPLEARNINGLABMANUAL

Ex.No.8PROGRAMFORIMAGECOLORIZATION
AIM
Towriteaprogramforimagecolorization
ALGORITHM
1. Loadthemodelandtheconvolution/kernelpoints
2. Readandpreprocesstheimage
3. GeneratemodelpredictionsusingtheLchannelfromourinputimage
4. Usetheoutput->abchanneltocreatearesultingimage
Lab colour has 3 channels L, a, and b. But here instead of pixel
values, these have different significances i.e :
 L-channel:lightintensity
 achannel:green-redencoding
 bchannel:blue-redencoding

PROGRAM

importnumpyasnpimpor

tcv2

fromcv2importdnn

#−−−−−−−−Modelfilepaths−−−−−−−−#

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'#−−

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

#−−−−−−−−Readingthemodelparams−−−−−−−−#

net=dnn.readNetFromCaffe(proto_file,model_file)k

ernel=np.load(hull_pts)#−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−

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

27
IF4071-DEEPLEARNINGLABMANUAL
#−−−−−Readingandpreprocessingimage−−−−−−−

−#img=cv2.imread(img_path)

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

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

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

#addtheclustercentersas1x1convolutionstothemodelclass8=ne

t.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.6

06,dtype="float32")]#−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−#−−−−−−−−−−−−−−−−−−−−−#

#we'llresizetheimageforthenetworkresi

zed=cv2.resize(lab_img,

(224,224))#splittheLchannel

L=cv2.split(resized)

[0]#meansubtraction

L−=50#−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−#−−−−−−−−−−−−−−−−−−−

−−#

#predictingtheabchannelsfromtheinputLchannel

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

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

#resizethepredicted'ab'volumetothesamedimensionsasour#inpu

timage

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

28
IF4071-DEEPLEARNINGLABMANUAL
#TaketheLchannelfromtheimageL=c

v2.split(lab_img)[0]

#JointheLchannelwithpredictedabchannel

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

#ThenconverttheimagefromLabtoBGR

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

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

#changetheimageto0−255rangeandconvertitfromfloat32tointcoloriz

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

#Let'sresizetheimagesandshowthemtogetherimg=cv

2.resize(img,(640,640))

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

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

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

cv2.waitKey(0)

29
IF4071-DEEPLEARNINGLABMANUAL

OUTPUT:

RESULT:
Thustheprogram forimagecolorization wasexecuted successfully.

30
IF4071-DEEPLEARNINGLABMANUAL

Ex.No.9PROGRAMFORTWITTERSENTIMENTANALYSIS

AIM
Towriteaprogramfortwittersentimentanalysis
ALGORITHM
1. Sentimentanalysisistheprocessofdeterminingwhetherapieceof writingis
positive, negative or neutral.
2. InstallTweepy:isthepythonclientforthe official
3. TwitterAPI

Install it bypip installtweepy

Textblob:isthepythonlibraryforprocessingtextual data.

PROGRAM

fromtransformersimportAutoTokenizer,AutoModelForSequenceClassificationfro

mscipy.specialimportsoftmax

#tweet="@MehranShakaramitoday'scold@home😒https://
mehranshakarami.com"tweet='Greatcontent!subscribed😉'

#precprcesstweettweet_

words=[]

forwordintweet.split(''):

ifword.startswith('@')andlen(word)>1:word='

@user'

elifword.startswith('http'):

word="http"

tweet_words.append(word)
IF4071-DEEPLEARNINGLABMANUAL
31
IF4071-DEEPLEARNINGLABMANUAL
tweet_proc="".join(tweet_words)

#loadmodelandtokenizer

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

model=AutoModelForSequenceClassification.from_pretrained(roberta)tokenizer

=AutoTokenizer.from_pretrained(roberta)

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

#sentimentanalysis

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(scor

es)

foriinrange(len(scores)):

l=labels[i]s

=scores[i]p

rint(l,s)

32
IF4071-DEEPLEARNINGLABMANUAL

OUTPUT:

RESULT:
Thustheprogramfortwittersentimentanalysiswasexecutedsuccessfully

33
IF4071-DEEPLEARNINGLABMANUAL

Ex.No.10PROGRAMFORBACKPROPAGATION
AIM
Towriteaprogram forbackpropagation with CNN.
ALGORITHM

1. Visualizingtheinputdata
2. DecidingtheshapesofWeightandbiasmatrix
3. Initializingmatrix,functiontobeused
4. Implementingthebackpropagationmethod
5. Implementingthecostcalculation
6. Backpropagationandoptimizing
7. predictionandvisualizingtheoutput

PROGRAM

importnumpyasnp

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)#maximumofXarraylongitudinallyy=y/100

defsigmoid(x):

return1/

(1+np.exp(−x))defderivativ

es_sigmoid(x):

returnx*(1−x)epoch=5

lr=0.1inputlayer_neuron

s=2

hiddenlayer_neurons=3

output_neurons=1

34
IF4071-DEEPLEARNINGLABMANUAL
bh=np.random.uniform(size=(1,hiddenlayer_neurons))wout=np.random.uniform(size=(hiddenl

ayer_neurons,output_neurons))

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

EO=y−output

outgrad=derivatives_sigmoid(output)d_o

utput=EO*outgrad

EH=d_output.dot(wout.T)

hiddengrad=derivatives_sigmoid(hlayer_act)d_hiddenl

ayer=EH*hiddengrad

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

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

print("−−−−−−−−−−−Epoch−",i+1,"Starts−−−−−−−−−

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

print("ActualOutput:\

n"+str(y))print("PredictedOutput:\n",output)

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

print("Input:\

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

n"+str(y))print("PredictedOutput:\

n",output)

35
IF4071-DEEPLEARNINGLABMANUAL

RESULT:
ThustheprogramforbackpropagationwithCNNwasexecutedsuccessfully

36
IF4071-DEEPLEARNINGLABMANUAL

Ex.No.11PROGRAMFORDENOISINGIMAGEUSINGAUTOENCODERS

AIM
Towriteaprogram fordenoisingimageusingautoencoders
ALGORITHM

1. Implementadeepconvolutionalautoencoderforimagedenoising,mappingnoisy
digits images from the MNIST dataset to clean digits images.
2. ThisimplementationisbasedonanoriginalblogposttitledBuildingAutoencodersinKeras
3. Setupthenecessarylibraryfiles.
4. Buildtheautoencoder
5. Wecantrainour autoencoderusingtrain_data asbothourinputdataand target
6. Predictonourtestdatasetanddisplaytheoriginalimagetogetherwitht
he prediction from our autoencoder.
7. Usingthenoisydataas ourinputandthecleandataasourtarget,
wewantour autoencoder to learn how to denoise the images.
8. Nowpredict onthenoisydataanddisplaytheresultsofourautoencoder.
9. Theautoencoderfinallyremovesthenoisefromtheinputimages.

PROGRAM

importnumpyasnpimpor

ttensorflowastf

importmatplotlib.pyplotasplt

fromtensorflow.kerasimportlayers

fromtensorflow.keras.datasetsimportmnistfro

mtensorflow.keras.modelsimportModel

defpreprocess(array):

37
IF4071-DEEPLEARNINGLABMANUAL
array=array.astype("float32")/255.0

array=np.reshape(array,

(len(array),28,28,1))returnarray

defnoise(array):

noise_factor=0.4

noisy_array=array+noise_factor*np.random.normal(loc=0.0,sc

ale=1.0,size=array.shape

returnnp.clip(noisy_array,0.0,1.0)

defdisplay(array1,array2):

n=10

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

es1=array1[indices,:]

images2=array2[indices,:]

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

fori,

(image1,image2)inenumerate(zip(images1,images2)):ax=plt

.subplot(2,n,i+1)

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

plt.gray()ax.get_xaxis().set_visible(Fa

lse)ax.get_yaxis().set_visible(False)

38
IF4071-DEEPLEARNINGLABMANUAL
ax=plt.subplot(2,n,i+1+n)plt.imshow

(image2.reshape(28,28))plt.gray()ax.

get_xaxis().set_visible(False)ax.get_y

axis().set_visible(False)

plt.show()

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

#Normalizeandreshapethedatatrain

_data=preprocess(train_data)test_d

ata=preprocess(test_data)

#Createacopyofthedatawithaddednoisenoisy_t

rain_data=noise(train_data)noisy_test_data=n

oise(test_data)

#Displaythetraindataandaversionofitwithaddednoisedisplay(train_da

ta,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)

39
IF4071-DEEPLEARNINGLABMANUAL
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

40
IF4071-DEEPLEARNINGLABMANUAL
y=train_data,epochs=5

0,batch_size=128,shuff

le=True,

validation_data=(test_data,test_data),

predictions=autoencoder.predict(noisy_test_data)disp

lay(noisy_test_data,predictions)

41
IF4071-DEEPLEARNINGLABMANUAL

RESULT:

Thustheprogramfordenoisingimageusingautoencoderswasexecutedsuccessfully.

42

You might also like