您的位置:首页 > 理论基础 > 计算机网络

机器学习—第四周—作业2—用深度神经网络分类图像

2017-12-01 15:45 525 查看

DeepNeuralNetworkforImageClassification:Application

Whenyoufinishthis,youwillhavefinishedthelastprogrammingassignmentofWeek4,andalsothelastprogrammingassignmentofthiscourse!

Youwilluseusethefunctionsyou'dimplementedinthepreviousassignmenttobuildadeepnetwork,andapplyittocatvsnon-catclassification.Hopefully,youwillseeanimprovementinaccuracyrelativetoyourpreviouslogisticregressionimplementation.

Afterthisassignmentyouwillbeableto:

Buildandapplyadeepneuralnetworktosupervisedlearning.
Let'sgetstarted!

1-Packages

Let'sfirstimportallthepackagesthatyouwillneedduringthisassignment.

numpyisthefundamentalpackageforscientific
computingwithPython.
matplotlibisalibrarytoplotgraphsinPython.
h5pyisacommonpackagetointeractwithadatasetthatisstoredonanH5file.
PILand
scipyareusedheretotestyourmodelwithyourownpictureattheend.
dnn_app_utilsprovidesthefunctionsimplementedinthe"BuildingyourDeepNeuralNetwork:StepbyStep"assignmenttothisnotebook.
np.random.seed(1)isusedtokeepalltherandomfunctioncallsconsistent.Itwillhelpusgradeyourwork.

importtime
importnumpyasnp
importh5py
importmatplotlib.pyplotasplt
importscipy
fromPILimportImage
fromscipyimportndimage
fromdnn_app_utils_v2import*
%matplotlibinline
plt.rcParams['figure.figsize']=(5.0,4.0)#setdefaultsizeofplots
plt.rcParams['image.interpolation']='nearest'
plt.rcParams['image.cmap']='gray'
%load_extautoreload
%autoreload2
np.random.seed(1)

2-Dataset

Youwillusethesame"Catvsnon-Cat"datasetasin"LogisticRegressionasaNeuralNetwork"(Assignment2).Themodelyouhadbuilthad70%testaccuracyonclassifyingcatsvsnon-catsimages.Hopefully,yournewmodelwillperformabetter!
ProblemStatement:Youaregivenadataset("data.h5")containing:
-atrainingsetofm_trainimageslabelledascat(1)ornon-cat(0)
-atestsetofm_testimageslabelledascatandnon-cat
-eachimageisofshape(num_px,num_px,3)where3isforthe3channels(RGB).
Let'sgetmorefamiliarwiththedataset.Loadthedatabyrunningthecellbelow.

train_x_orig,train_y,test_x_orig,test_y,classes=load_data()


Thefollowingcodewillshowyouanimageinthedataset.Feelfreetochangetheindexandre-runthecellmultipletimestoseeotherimages.

#Exampleofapicture
index=17
plt.imshow(train_x_orig[index])
print("y="+str(train_y[0,index])+".It'sa"+classes[train_y[0,index]].decode("utf-8")+"picture.")
y=0.It'sanon-catpicture.


#Exploreyourdataset

m_train=train_x_orig.shape[0]

num_px=train_x_orig.shape[1]

m_test=test_x_orig.shape[0]


print("Numberoftrainingexamples:"+str(m_train))

print("Numberoftestingexamples:"+str(m_test))

print("Eachimageisofsize:("+str(num_px)+","+str(num_px)+",3)")

print("train_x_origshape:"+str(train_x_orig.shape))

print("train_yshape:"+str(train_y.shape))

print("test_x_origshape:"+str(test_x_orig.shape))

print("test_yshape:"+str(test_y.shape))


Numberoftrainingexamples:209
Numberoftestingexamples:50
Eachimageisofsize:(64,64,3)
train_x_origshape:(209,64,64,3)
train_yshape:(1,209)
test_x_origshape:(50,64,64,3)
test_yshape:(1,50)


Asusual,youreshapeandstandardizetheimagesbeforefeedingthemtothenetwork.Thecodeisgiveninthecellbelow.



Figure1:Imagetovectorconversion.

#Reshapethetrainingandtestexamples
train_x_flatten=train_x_orig.reshape(train_x_orig.shape[0],-1).T#The"-1"makesreshapeflattentheremainingdimensions
test_x_flatten=test_x_orig.reshape(test_x_orig.shape[0],-1).T
#Standardizedatatohavefeaturevaluesbetween0and1.
train_x=train_x_flatten/255.
test_x=test_x_flatten/255.
print("train_x'sshape:"+str(train_x.shape))
print("test_x'sshape:"+str(test_x.shape))
train_x'sshape:(12288,209)
test_x'sshape:(12288,50)
12,28812,288equals64×64×364×64×3whichisthesizeofonereshapedimagevector.

3-Architectureofyourmodel

Nowthatyouarefamiliarwiththedataset,itistimetobuildadeepneuralnetworktodistinguishcatimagesfromnon-catimages.
Youwill
19954
buildtwodifferentmodels:
A2-layerneuralnetwork
AnL-layerdeepneuralnetwork
Youwillthencomparetheperformanceofthesemodels,andalsotryoutdifferentvaluesforLL.
Let'slookatthetwoarchitectures.

3.1-2-layerneuralnetwork



Figure2:2-layerneuralnetwork.
Themodelcanbesummarizedas:INPUT->LINEAR->RELU->LINEAR->SIGMOID->OUTPUT.DetailedArchitectureoffigure2:
Theinputisa(64,64,3)imagewhichisflattenedtoavectorofsize(12288,1)(12288,1).
Thecorrespondingvector:[x0,x1,...,x12287]T[x0,x1,...,x12287]TisthenmultipliedbytheweightmatrixW[1]W[1]ofsize(n[1],12288)(n[1],12288).
Youthenaddabiastermandtakeitsrelutogetthefollowingvector:[a[1]0,a[1]1,...,a[1]n[1]−1]T[a0[1],a1[1],...,an[1]−1[1]]T.
Youthenrepeatthesameprocess.
YoumultiplytheresultingvectorbyW[2]W[2]andaddyourintercept(bias).
Finally,youtakethesigmoidoftheresult.Ifitisgreaterthan0.5,youclassifyittobeacat.

3.2-L-layerdeepneuralnetwork

ItishardtorepresentanL-layerdeepneuralnetworkwiththeaboverepresentation.However,hereisasimplifiednetworkrepresentation:



Figure3:L-layerneuralnetwork.
Themodelcanbesummarizedas:[LINEAR->RELU]××(L-1)->LINEAR->SIGMOIDDetailedArchitectureoffigure3:
Theinputisa(64,64,3)imagewhichisflattenedtoavectorofsize(12288,1).
Thecorrespondingvector:[x0,x1,...,x12287]T[x0,x1,...,x12287]TisthenmultipliedbytheweightmatrixW[1]W[1]andthenyouaddtheinterceptb[1]b[1].Theresultiscalledthelinearunit.
Next,youtakethereluofthelinearunit.Thisprocesscouldberepeatedseveraltimesforeach(W[l],b[l])(W[l],b[l])dependingonthemodelarchitecture.
Finally,youtakethesigmoidofthefinallinearunit.Ifitisgreaterthan0.5,youclassifyittobeacat.

3.3-Generalmethodology

AsusualyouwillfollowtheDeepLearningmethodologytobuildthemodel:
1.Initializeparameters/Definehyperparameters
2.Loopfornum_iterations:
a.Forwardpropagation
b.Computecostfunction
c.Backwardpropagation
d.Updateparameters(usingparameters,andgradsfrombackprop)
4.Usetrainedparameterstopredictlabels
Let'snowimplementthosetwomodels!

4-Two-layerneuralnetwork

Question:Usethehelperfunctionsyouhaveimplementedinthepreviousassignmenttobuilda2-layerneuralnetworkwiththefollowingstructure:LINEAR->RELU->LINEAR->SIGMOID.Thefunctionsyoumayneedandtheirinputsare:```pythondefinitialize_parameters(n_x,n_h,n_y):np.random.seed(1)
###STARTCODEHERE###(≈4linesofcode)
W1=np.random.randn(n_h,n_x)*0.01
b1=np.zeros((n_h,1))
W2=np.random.randn(n_y,n_h)*0.01
b2=np.zeros((n_y,1))
###ENDCODEHERE###

assert(W1.shape==(n_h,n_x))
assert(b1.shape==(n_h,1))
assert(W2.shape==(n_y,n_h))
assert(b2.shape==(n_y,1))

parameters={"W1":W1,
"b1":b1,
"W2":W2,
"b2":b2}

returnparameters
deflinear_activation_forward(A_prev,W,b,activation):ifactivation=="sigmoid":
#Inputs:"A_prev,W,b".Outputs:"A,activation_cache".
###STARTCODEHERE###(≈2linesofcode)
Z,linear_cache=linear_forward(A_prev,W,b)
A,activation_cache=sigmoid(Z)
###ENDCODEHERE###

elifactivation=="relu":
#Inputs:"A_prev,W,b".Outputs:"A,activation_cache".
###STARTCODEHERE###(≈2linesofcode)
Z,linear_cache=linear_forward(A_prev,W,b)
A,activation_cache=relu(Z)
###ENDCODEHERE###

assert(A.shape==(W.shape[0],A_prev.shape[1]))
cache=(linear_cache,activation_cache)
returnA,cache
defcompute_cost(AL,Y):m=Y.shape[1]
#ComputelossfromaLandy.
###STARTCODEHERE###(≈1linesofcode)
cost=-1/m*np.sum((np.multiply(Y,np.log(AL))+np.multiply((1-Y),np.log(1-AL))))
###ENDCODEHERE###

cost=np.squeeze(cost)#Tomakesureyourcost'sshapeiswhatweexpect(e.g.thisturns[[17]]into17).
assert(cost.shape==())
returncost
deflinear_activation_backward(dA,cache,activation):linear_cache,activation_cache=cache
ifactivation=="relu":
###STARTCODEHERE###(≈2linesofcode)
dZ=relu_backward(dA,activation_cache)
dA_prev,dW,db=linear_backward(dZ,linear_cache)
###ENDCODEHERE###

elifactivation=="sigmoid":
###STARTCODEHERE###(≈2linesofcode)
dZ=sigmoid_backward(dA,activation_cache)
dA_prev,dW,db=linear_backward(dZ,linear_cache)
###ENDCODEHERE###
returndA_prev,dW,db
defupdate_parameters(parameters,grads,learning_rate):"""Updateparametersusinggradientdescent
Arguments:
parameters--pythondictionarycontainingyourparameters
grads--pythondictionarycontainingyourgradients,outputofL_model_backward

Returns:
parameters--pythondictionarycontainingyourupdatedparameters
parameters["W"+str(l)]=...
parameters["b"+str(l)]=...
"""

L=len(parameters)//2#numberoflayersintheneuralnetwork

#Updateruleforeachparameter.Useaforloop.
###STARTCODEHERE###(≈3linesofcode)
forlinrange(L):
parameters["W"+str(l+1)]=parameters["W"+str(l+1)]-learning_rate*grads["dW"+str(l+1)]
parameters["b"+str(l+1)]=parameters["b"+str(l+1)]-learning_rate*grads["db"+str(l+1)]

###ENDCODEHERE###
returnparameters


###CONSTANTSDEFININGTHEMODEL####

n_x=12288#num_px*num_px*3

n_h=7

n_y=1

layers_dims=(n_x,n_h,n_y)


#GRADEDFUNCTION:two_layer_model
deftwo_layer_model(X,Y,layers_dims,learning_rate=0.0075,num_iterations=3000,print_cost=False):
"""
Implementsatwo-layerneuralnetwork:LINEAR->RELU->LINEAR->SIGMOID.
Arguments:
X--inputdata,ofshape(n_x,numberofexamples)
Y--true"label"vector(containing0ifcat,1ifnon-cat),ofshape(1,numberofexamples)
layers_dims--dimensionsofthelayers(n_x,n_h,n_y)
num_iterations--numberofiterationsoftheoptimizationloop
learning_rate--learningrateofthegradientdescentupdaterule
print_cost--IfsettoTrue,thiswillprintthecostevery100iterations
Returns:
parameters--adictionarycontainingW1,W2,b1,andb2
"""
np.random.seed(1)
grads={}
costs=[]#tokeeptrackofthecost
m=X.shape[1]#numberofexamples
(n_x,n_h,n_y)=layers_dims
#Initializeparametersdictionary,bycallingoneofthefunctionsyou'dpreviouslyimplemented
###STARTCODEHERE###(≈1lineofcode)
parameters=initialize_parameters(n_x,n_h,n_y)
###ENDCODEHERE###
#GetW1,b1,W2andb2fromthedictionaryparameters.
W1=parameters["W1"]
b1=parameters["b1"]
W2=parameters["W2"]
b2=parameters["b2"]
#Loop(gradientdescent)
foriinrange(0,num_iterations):
#Forwardpropagation:LINEAR->RELU->LINEAR->SIGMOID.Inputs:"X,W1,b1".Output:"A1,cache1,A2,cache2".
###STARTCODEHERE###(≈2linesofcode)
A1,cache1=linear_activation_forward(X,W1,b1,"relu")
A2,cache2=linear_activation_forward(A1,W2,b2,"sigmoid")
###ENDCODEHERE###
#Computecost
###STARTCODEHERE###(≈1lineofcode)
cost=compute_cost(A2,Y)
###ENDCODEHERE###
#Initializingbackwardpropagation
dA2=-(np.divide(Y,A2)-np.divide(1-Y,1-A2))
#Backwardpropagation.Inputs:"dA2,cache2,cache1".Outputs:"dA1,dW2,db2;alsodA0(notused),dW1,db1".
###STARTCODEHERE###(≈2linesofcode)
dA1,dW2,db2=linear_activation_backward(dA2,cache2,"sigmoid")
dA0,dW1,db1=linear_activation_backward(dA1,cache1,"relu")
###ENDCODEHERE###
#Setgrads['dWl']todW1,grads['db1']todb1,grads['dW2']todW2,grads['db2']todb2
grads['dW1']=dW1
grads['db1']=db1
grads['dW2']=dW2
grads['db2']=db2
#Updateparameters.
###STARTCODEHERE###(approx.1lineofcode)
parameters=update_parameters(parameters,grads,learning_rate)
###ENDCODEHERE###
#RetrieveW1,b1,W2,b2fromparameters
W1=parameters["W1"]
b1=parameters["b1"]
W2=parameters["W2"]
b2=parameters["b2"]
#Printthecostevery100trainingexample
ifprint_costandi%100==0:
print("Costafteriteration{}:{}".format(i,np.squeeze(cost)))
ifprint_costandi%100==0:
costs.append(cost)
#plotthecost
plt.plot(np.squeeze(costs))
plt.ylabel('cost')
plt.xlabel('iterations(pertens)')
plt.title("Learningrate="+str(learning_rate))
plt.show()
returnparameters
Runthecellbelowtotrainyourparameters.Seeifyourmodelruns.Thecostshouldbedecreasing.Itmaytakeupto5minutestorun2500iterations.Checkifthe"Costafteriteration0"matchestheexpectedoutputbelow,ifnotclickonthesquare(⬛)ontheupperbarofthenotebooktostopthecellandtrytofindyourerror.

parameters=two_layer_model(train_x,train_y,layers_dims=(n_x,n_h,n_y),num_iterations=2500,print_cost=True)


Costafteriteration0:0.693049735659989
Costafteriteration100:0.6464320953428849
Costafteriteration200:0.6325140647912678
Costafteriteration300:0.6015024920354665
Costafteriteration400:0.5601966311605748
Costafteriteration500:0.515830477276473
Costafteriteration600:0.4754901313943325
Costafteriteration700:0.43391631512257495
Costafteriteration800:0.4007977536203886
Costafteriteration900:0.35807050113237987
Costafteriteration1000:0.3394281538366413
Costafteriteration1100:0.30527536361962654
Costafteriteration1200:0.2749137728213015
Costafteriteration1300:0.24681768210614827
Costafteriteration1400:0.1985073503746611
Costafteriteration1500:0.17448318112556593
Costafteriteration1600:0.1708076297809661
Costafteriteration1700:0.11306524562164737
Costafteriteration1800:0.09629426845937163
Costafteriteration1900:0.08342617959726878
Costafteriteration2000:0.0743907870431909
Costafteriteration2100:0.06630748132267938
Costafteriteration2200:0.05919329501038176
Costafteriteration2300:0.05336140348560564
Costafteriteration2400:0.048554785628770226


ExpectedOutput:

Costafteriteration00.6930497356599888
Costafteriteration1000.6464320953428849
......
Costafteriteration24000.048554785628770206
Goodthingyoubuiltavectorizedimplementation!Otherwiseitmighthavetaken10timeslongertotrainthis.

Now,youcanusethetrainedparameterstoclassifyimagesfromthedataset.Toseeyourpredictionsonthetrainingandtestsets,runthecellbelow.

predictions_train=predict(train_x,train_y,parameters)
Accuracy:1.0
ExpectedOutput:
Accuracy1.0
predictions_test=predict(test_x,test_y,parameters)


Accuracy:0.72


ExpectedOutput:

Accuracy0.72
Note:Youmaynoticethatrunningthemodelonfeweriterations(say1500)givesbetteraccuracyonthetestset.Thisiscalled"earlystopping"andwewilltalkaboutitinthenextcourse.Earlystoppingisawaytopreventoverfitting.

Congratulations!Itseemsthatyour2-layerneuralnetworkhasbetterperformance(72%)thanthelogisticregressionimplementation(70%,assignmentweek2).Let'sseeifyoucandoevenbetterwithan
LL-layer
model.

5-L-layerNeuralNetwork

Question:Usethehelperfunctionsyouhaveimplementedpreviouslytobuildan
LL-layer
neuralnetworkwiththefollowingstructure:[LINEAR->RELU]××(L-1)
->LINEAR->SIGMOID.Thefunctionsyoumayneedandtheirinputsare:

definitialize_parameters_deep(layer_dims):
...
returnparameters
defL_model_forward(X,parameters):
...
returnAL,caches
defcompute_cost(AL,Y):
...
returncost
defL_model_backward(AL,Y,caches):
...
returngrads
defupdate_parameters(parameters,grads,learning_rate):
...
returnparameters


###CONSTANTS###
layers_dims=[12288,20,7,5,1]#5-layermodel


#GRADEDFUNCTION:L_layer_model


defL_layer_model(X,Y,layers_dims,learning_rate=0.0075,num_iterations=3000,print_cost=False):#lrwas0.009

"""

ImplementsaL-layerneuralnetwork:[LINEAR->RELU]*(L-1)->LINEAR->SIGMOID.


Arguments:

X--data,numpyarrayofshape(numberofexamples,num_px*num_px*3)

Y--true"label"vector(containing0ifcat,1ifnon-cat),ofshape(1,numberofexamples)

layers_dims--listcontainingtheinputsizeandeachlayersize,oflength(numberoflayers+1).

learning_rate--learningrateofthegradientdescentupdaterule

num_iterations--numberofiterationsoftheoptimizationloop

print_cost--ifTrue,itprintsthecostevery100steps


Returns:

parameters--parameterslearntbythemodel.Theycanthenbeusedtopredict.

"""


np.random.seed(1)

costs=[]#keeptrackofcost


#Parametersinitialization.

###STARTCODEHERE###

parameters=initialize_parameters_deep(layers_dims)

###ENDCODEHERE###


#Loop(gradientdescent)

foriinrange(0,num_iterations):


#Forwardpropagation:[LINEAR->RELU]*(L-1)->LINEAR->SIGMOID.

###STARTCODEHERE###(≈1lineofcode)

AL,caches=L_model_forward(X,parameters)

###ENDCODEHERE###


#Computecost.

###STARTCODEHERE###(≈1lineofcode)

cost=compute_cost(AL,Y)

###ENDCODEHERE###


#Backwardpropagation.

###STARTCODEHERE###(≈1lineofcode)

grads=L_model_backward(AL,Y,caches)

###ENDCODEHERE###


#Updateparameters.

###STARTCODEHERE###(≈1lineofcode)

parameters=update_parameters(parameters,grads,learning_rate)

###ENDCODEHERE###


#Printthecostevery100trainingexample

ifprint_costandi%100==0:

print("Costafteriteration%i:%f"%(i,cost))

ifprint_costandi%100==0:

costs.append(cost)


#plotthecost

plt.plot(np.squeeze(costs))

plt.ylabel('cost')

plt.xlabel('iterations(pertens)')

plt.title("Learningrate="+str(learning_rate))

plt.show()


returnparameters


Youwillnowtrainthemodelasa5-layerneuralnetwork.

Runthecellbelowtotrainyourmodel.Thecostshoulddecreaseoneveryiteration.Itmaytakeupto5minutestorun2500iterations.Checkifthe"Costafteriteration0"matchestheexpectedoutputbelow,ifnotclickonthesquare(⬛)ontheupper
barofthenotebooktostopthecellandtrytofindyourerror.

parameters=L_layer_model(train_x,train_y,layers_dims,num_iterations=2500,print_cost=True)
Costafteriteration0:0.771749
Costafteriteration100:0.672053
Costafteriteration200:0.648263
Costafteriteration300:0.611507
Costafteriteration400:0.567047
Costafteriteration500:0.540138
Costafteriteration600:0.527930
Costafteriteration700:0.465477
Costafteriteration800:0.369126
Costafteriteration900:0.391747
Costafteriteration1000:0.315187
Costafteriteration1100:0.272700
Costafteriteration1200:0.237419
Costafteriteration1300:0.199601
Costafteriteration1400:0.189263
Costafteriteration1500:0.161189
Costafteriteration1600:0.148214
Costafteriteration1700:0.137775
Costafteriteration1800:0.129740
Costafteriteration1900:0.121225
Costafteriteration2000:0.113821
Costafteriteration2100:0.107839
Costafteriteration2200:0.102855
Costafteriteration2300:0.100897
Costafteriteration2400:0.092878
ExpectedOutput:
Costafteriteration00.771749
Costafteriteration1000.672053
......
Costafteriteration24000.092878
pred_train=predict(train_x,train_y,parameters)


Accuracy:0.985645933014


TrainAccuracy0.985645933014
pred_test=predict(test_x,test_y,parameters)
Accuracy:0.8
ExpectedOutput:
TestAccuracy0.8
Congrats!Itseemsthatyour5-layerneuralnetworkhasbetterperformance(80%)thanyour2-layerneuralnetwork(72%)onthesametestset.
Thisisgoodperformanceforthistask.Nicejob!
Thoughinthenextcourseon"Improvingdeepneuralnetworks"youwilllearnhowtoobtainevenhigheraccuracybysystematicallysearchingforbetterhyperparameters(learning_rate,layers_dims,num_iterations,andothersyou'llalsolearninthenextcourse).

6)ResultsAnalysis

First,let'stakealookatsomeimagestheL-layermodellabeledincorrectly.Thiswillshowafewmislabeledimages.

print_mislabeled_images(classes,test_x,test_y,pred_test)


Afewtypeofimagesthemodeltendstodopoorlyoninclude:

Catbodyinanunusualposition
Catappearsagainstabackgroundofasimilarcolor
Unusualcatcolorandspecies
CameraAngle
Brightnessofthepicture
Scalevariation(catisverylargeorsmallinimage)

7)Testwithyourownimage(optional/ungradedexercise)

Congratulationsonfinishingthisassignment.Youcanuseyourownimageandseetheoutputofyourmodel.Todothat:

1.Clickon"File"intheupperbarofthisnotebook,thenclick"Open"togoonyourCourseraHub.
2.AddyourimagetothisJupyterNotebook'sdirectory,inthe"images"folder
3.Changeyourimage'snameinthefollowingcode
4.Runthecodeandcheckifthealgorithmisright(1=cat,0=non-cat)!


##STARTCODEHERE##

my_image="my_image.jpg"#changethistothenameofyourimagefile

my_label_y=[1]#thetrueclassofyourimage(1->cat,0->non-cat)

##ENDCODEHERE##


fname="images/"+my_image

image=np.array(ndimage.imread(fname,flatten=False))

my_image=scipy.misc.imresize(image,size=(num_px,num_px)).reshape((num_px*num_px*3,1))

my_predicted_image=predict(my_image,my_label_y,parameters)


plt.imshow(image)

print("y="+str(np.squeeze(my_predicted_image))+",yourL-layermodelpredictsa\""+classes[int(np.squeeze(my_predicted_image)),].decode("utf-8")+"\"picture.")


References:

forauto-reloadingexternalmodule:http://stackoverflow.com/questions/1907993/autoreload-of-modules-in-ipython
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐