python神經(jīng)網(wǎng)絡(luò)ResNet50模型的復(fù)現(xiàn)詳解_第1頁
python神經(jīng)網(wǎng)絡(luò)ResNet50模型的復(fù)現(xiàn)詳解_第2頁
python神經(jīng)網(wǎng)絡(luò)ResNet50模型的復(fù)現(xiàn)詳解_第3頁
python神經(jīng)網(wǎng)絡(luò)ResNet50模型的復(fù)現(xiàn)詳解_第4頁
python神經(jīng)網(wǎng)絡(luò)ResNet50模型的復(fù)現(xiàn)詳解_第5頁
已閱讀5頁,還剩1頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

第python神經(jīng)網(wǎng)絡(luò)ResNet50模型的復(fù)現(xiàn)詳解目錄什么是殘差網(wǎng)絡(luò)什么是ResNet50模型ResNet50網(wǎng)絡(luò)部分實(shí)現(xiàn)代碼圖片預(yù)測

什么是殘差網(wǎng)絡(luò)

最近看yolo3里面講到了殘差網(wǎng)絡(luò),對(duì)這個(gè)網(wǎng)絡(luò)結(jié)構(gòu)很感興趣,于是了解到這個(gè)網(wǎng)絡(luò)結(jié)構(gòu)最初的使用是在ResNet網(wǎng)絡(luò)里。

Residualnet(殘差網(wǎng)絡(luò)):

將靠前若干層的某一層數(shù)據(jù)輸出直接跳過多層引入到后面數(shù)據(jù)層的輸入部分。

意味著后面的特征層的內(nèi)容會(huì)有一部分由其前面的某一層線性貢獻(xiàn)。

其結(jié)構(gòu)如下:

深度殘差網(wǎng)絡(luò)的設(shè)計(jì)是為了克服由于網(wǎng)絡(luò)深度加深而產(chǎn)生的學(xué)習(xí)效率變低與準(zhǔn)確率無法有效提升的問題。

什么是ResNet50模型

ResNet50有兩個(gè)基本的塊,分別名為ConvBlock和IdentityBlock,其中ConvBlock輸入和輸出的維度是不一樣的,所以不能連續(xù)串聯(lián),它的作用是改變網(wǎng)絡(luò)的維度;

IdentityBlock輸入維度和輸出維度相同,可以串聯(lián),用于加深網(wǎng)絡(luò)的。

ConvBlock的結(jié)構(gòu)如下:

IdentityBlock的結(jié)構(gòu)如下:

這兩個(gè)都是殘差網(wǎng)絡(luò)結(jié)構(gòu)。

總的網(wǎng)絡(luò)結(jié)構(gòu)如下:

這樣看起來可能比較抽象,還有一副很好的我從網(wǎng)上找的圖,可以拉到最后面去看哈,放前面太占位置了。

ResNet50網(wǎng)絡(luò)部分實(shí)現(xiàn)代碼

#-------------------------------------------------------------#

#ResNet50的網(wǎng)絡(luò)部分

#-------------------------------------------------------------#

from__future__importprint_function

importnumpyasnp

fromkerasimportlayers

fromkeras.layersimportInput

fromkeras.layersimportDense,Conv2D,MaxPooling2D,ZeroPadding2D,AveragePooling2D

fromkeras.layersimportActivation,BatchNormalization,Flatten

fromkeras.modelsimportModel

fromkeras.preprocessingimportimage

importkeras.backendasK

fromkeras.utils.data_utilsimportget_file

fromkeras.applications.imagenet_utilsimportdecode_predictions

fromkeras.applications.imagenet_utilsimportpreprocess_input

defidentity_block(input_tensor,kernel_size,filters,stage,block):

filters1,filters2,filters3=filters

conv_name_base='res'+str(stage)+block+'_branch'

bn_name_base='bn'+str(stage)+block+'_branch'

x=Conv2D(filters1,(1,1),name=conv_name_base+'2a')(input_tensor)

x=BatchNormalization(name=bn_name_base+'2a')(x)

x=Activation('relu')(x)

x=Conv2D(filters2,kernel_size,padding='same',name=conv_name_base+'2b')(x)

x=BatchNormalization(name=bn_name_base+'2b')(x)

x=Activation('relu')(x)

x=Conv2D(filters3,(1,1),name=conv_name_base+'2c')(x)

x=BatchNormalization(name=bn_name_base+'2c')(x)

x=layers.add([x,input_tensor])

x=Activation('relu')(x)

returnx

defconv_block(input_tensor,kernel_size,filters,stage,block,strides=(2,2)):

filters1,filters2,filters3=filters

conv_name_base='res'+str(stage)+block+'_branch'

bn_name_base='bn'+str(stage)+block+'_branch'

x=Conv2D(filters1,(1,1),strides=strides,

name=conv_name_base+'2a')(input_tensor)

x=BatchNormalization(name=bn_name_base+'2a')(x)

x=Activation('relu')(x)

x=Conv2D(filters2,kernel_size,padding='same',

name=conv_name_base+'2b')(x)

x=BatchNormalization(name=bn_name_base+'2b')(x)

x=Activation('relu')(x)

x=Conv2D(filters3,(1,1),name=conv_name_base+'2c')(x)

x=BatchNormalization(name=bn_name_base+'2c')(x)

shortcut=Conv2D(filters3,(1,1),strides=strides,

name=conv_name_base+'1')(input_tensor)

shortcut=BatchNormalization(name=bn_name_base+'1')(shortcut)

x=layers.add([x,shortcut])

x=Activation('relu')(x)

returnx

defResNet50(input_shape=[224,224,3],classes=1000):

img_input=Input(shape=input_shape)

x=ZeroPadding2D((3,3))(img_input)

x=Conv2D(64,(7,7),strides=(2,2),name='conv1')(x)

x=BatchNormalization(name='bn_conv1')(x)

x=Activation('relu')(x)

x=MaxPooling2D((3,3),strides=(2,2))(x)

x=conv_block(x,3,[64,64,256],stage=2,block='a',strides=(1,1))

x=identity_block(x,3,[64,64,256],stage=2,block='b')

x=identity_block(x,3,[64,64,256],stage=2,block='c')

x=conv_block(x,3,[128,128,512],stage=3,block='a')

x=identity_block(x,3,[128,128,512],stage=3,block='b')

x=identity_block(x,3,[128,128,512],stage=3,block='c')

x=identity_block(x,3,[128,128,512],stage=3,block='d')

x=conv_block(x,3,[256,256,1024],stage=4,block='a')

x=identity_block(x,3,[256,256,1024],stage=4,block='b')

x=identity_block(x,3,[256,256,1024],stage=4,block='c')

x=identity_block(x,3,[256,256,1024],stage=4,block='d')

x=identity_block(x,3,[256,256,1024],stage=4,block='e')

x=identity_block(x,3,[256,256,1024],stage=4,block='f')

x=conv_block(x,3,[512,512,2048],stage=5,block='a')

x=identity_block(x,3,[512,512,2048],stage=5,block='b')

x=identity_block(x,3,[512,512,2048],stage=5,block='c')

x=AveragePooling2D((7,7),name='avg_pool')(x)

x=Flatten()(x)

x=Dense(classes,activation='softmax',name='fc1000')(x)

model=Model(img_input,x,name='resnet50')

model.load_weights("resnet50_weights_tf_dim_ordering_tf_kernels.h5")

returnmodel

圖片預(yù)測

建立網(wǎng)絡(luò)后,可以用以下的代碼進(jìn)行預(yù)測。

if__name__=='__main__':

model=ResNet50()

model.summary()

img_path='elephant.jpg'

img=image.load_img(img_path,target_size=(224,224))

x=image.img_to_array(img)

x=np.expand_dims(x,axis=0)

x=preprocess_input(x)

print('Inputimageshape:',x.shape)

preds=model.predict(x)

print('Predicted:',decode_predictions(preds))

預(yù)測所需的已經(jīng)訓(xùn)練好的ResNet50模型可以在/fchollet/deep-learning-models/releases下載。非常方便。

預(yù)測結(jié)果為:

Predicted:[[('n01871265','tusker',0.41107917)

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論