




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
第python神經(jīng)網(wǎng)絡(luò)MobileNetV2模型的復(fù)現(xiàn)詳解目錄什么是MobileNetV2模型MobileNetV2網(wǎng)絡(luò)部分實(shí)現(xiàn)代碼圖片預(yù)測(cè)
什么是MobileNetV2模型
MobileNet它哥MobileNetV2也是很不錯(cuò)的呢
MobileNet模型是Google針對(duì)手機(jī)等嵌入式設(shè)備提出的一種輕量級(jí)的深層神經(jīng)網(wǎng)絡(luò),其使用的核心思想便是depthwiseseparableconvolution。
MobileNetV2是MobileNet的升級(jí)版,它具有兩個(gè)特征點(diǎn):
1、Invertedresiduals,在ResNet50里我們認(rèn)識(shí)到一個(gè)結(jié)構(gòu),bottleneckdesign結(jié)構(gòu),在3x3網(wǎng)絡(luò)結(jié)構(gòu)前利用1x1卷積降維,在3x3網(wǎng)絡(luò)結(jié)構(gòu)后,利用1x1卷積升維,相比直接使用3x3網(wǎng)絡(luò)卷積效果更好,參數(shù)更少,先進(jìn)行壓縮,再進(jìn)行擴(kuò)張。而在MobileNetV2網(wǎng)絡(luò)部分,其采用Invertedresiduals結(jié)構(gòu),在3x3網(wǎng)絡(luò)結(jié)構(gòu)前利用1x1卷積升維,在3x3網(wǎng)絡(luò)結(jié)構(gòu)后,利用1x1卷積降維,先進(jìn)行擴(kuò)張,再進(jìn)行壓縮。
2、Linearbottlenecks,為了避免Relu對(duì)特征的破壞,在在3x3網(wǎng)絡(luò)結(jié)構(gòu)前利用1x1卷積升維,在3x3網(wǎng)絡(luò)結(jié)構(gòu)后,再利用1x1卷積降維后,不再進(jìn)行Relu6層,直接進(jìn)行殘差網(wǎng)絡(luò)的加法。
整體網(wǎng)絡(luò)結(jié)構(gòu)如下:(其中bottleneck進(jìn)行的操作就是上述的創(chuàng)新操作)
MobileNetV2網(wǎng)絡(luò)部分實(shí)現(xiàn)代碼
#-------------------------------------------------------------#
#MobileNetV2的網(wǎng)絡(luò)部分
#-------------------------------------------------------------#
importmath
importnumpyasnp
importtensorflowastf
fromtensorflow.kerasimportbackend
fromkerasimportbackendasK
fromkeras.preprocessingimportimage
fromkeras.modelsimportModel
fromkeras.layers.normalizationimportBatchNormalization
fromkeras.layersimportConv2D,Add,ZeroPadding2D,GlobalAveragePooling2D,Dropout,Dense
fromkeras.layersimportMaxPooling2D,Activation,DepthwiseConv2D,Input,GlobalMaxPooling2D
fromkeras.applicationsimportimagenet_utils
fromkeras.applications.imagenet_utilsimportdecode_predictions
fromkeras.utils.data_utilsimportget_file
#TODOChangepathtov1.1
BASE_WEIGHT_PATH=('/JonathanCMitchell/mobilenet_v2_keras/'
'releases/download/v1.1/')
#relu6!
defrelu6(x):
returnK.relu(x,max_value=6)
#用于計(jì)算padding的大小
defcorrect_pad(inputs,kernel_size):
img_dim=1
input_size=_shape(inputs)[img_dim:(img_dim+2)]
ifisinstance(kernel_size,int):
kernel_size=(kernel_size,kernel_size)
ifinput_size[0]isNone:
adjust=(1,1)
else:
adjust=(1-input_size[0]%2,1-input_size[1]%2)
correct=(kernel_size[0]//2,kernel_size[1]//2)
return((correct[0]-adjust[0],correct[0]),
(correct[1]-adjust[1],correct[1]))
#使其結(jié)果可以被8整除,因?yàn)槭褂玫搅伺蛎浵禂?shù)α
def_make_divisible(v,divisor,min_value=None):
ifmin_valueisNone:
min_value=divisor
new_v=max(min_value,int(v+divisor/2)//divisor*divisor)
ifnew_v0.9*v:
new_v+=divisor
returnnew_v
defMobileNetV2(input_shape=[224,224,3],
alpha=1.0,
include_top=True,
weights='imagenet',
classes=1000):
rows=input_shape[0]
img_input=Input(shape=input_shape)
#stem部分
#224,224,3-112,112,32
first_block_filters=_make_divisible(32*alpha,8)
x=ZeroPadding2D(padding=correct_pad(img_input,3),
name='Conv1_pad')(img_input)
x=Conv2D(first_block_filters,
kernel_size=3,
strides=(2,2),
padding='valid',
use_bias=False,
name='Conv1')(x)
x=BatchNormalization(epsilon=1e-3,
momentum=0.999,
name='bn_Conv1')(x)
x=Activation(relu6,name='Conv1_relu')(x)
#112,112,32-112,112,16
x=_inverted_res_block(x,filters=16,alpha=alpha,stride=1,
expansion=1,block_id=0)
#112,112,16-56,56,24
x=_inverted_res_block(x,filters=24,alpha=alpha,stride=2,
expansion=6,block_id=1)
x=_inverted_res_block(x,filters=24,alpha=alpha,stride=1,
expansion=6,block_id=2)
#56,56,24-28,28,32
x=_inverted_res_block(x,filters=32,alpha=alpha,stride=2,
expansion=6,block_id=3)
x=_inverted_res_block(x,filters=32,alpha=alpha,stride=1,
expansion=6,block_id=4)
x=_inverted_res_block(x,filters=32,alpha=alpha,stride=1,
expansion=6,block_id=5)
#28,28,32-14,14,64
x=_inverted_res_block(x,filters=64,alpha=alpha,stride=2,
expansion=6,block_id=6)
x=_inverted_res_block(x,filters=64,alpha=alpha,stride=1,
expansion=6,block_id=7)
x=_inverted_res_block(x,filters=64,alpha=alpha,stride=1,
expansion=6,block_id=8)
x=_inverted_res_block(x,filters=64,alpha=alpha,stride=1,
expansion=6,block_id=9)
#14,14,64-14,14,96
x=_inverted_res_block(x,filters=96,alpha=alpha,stride=1,
expansion=6,block_id=10)
x=_inverted_res_block(x,filters=96,alpha=alpha,stride=1,
expansion=6,block_id=11)
x=_inverted_res_block(x,filters=96,alpha=alpha,stride=1,
expansion=6,block_id=12)
#14,14,96-7,7,160
x=_inverted_res_block(x,filters=160,alpha=alpha,stride=2,
expansion=6,block_id=13)
x=_inverted_res_block(x,filters=160,alpha=alpha,stride=1,
expansion=6,block_id=14)
x=_inverted_res_block(x,filters=160,alpha=alpha,stride=1,
expansion=6,block_id=15)
#7,7,160-7,7,320
x=_inverted_res_block(x,filters=320,alpha=alpha,stride=1,
expansion=6,block_id=16)
ifalpha1.0:
last_block_filters=_make_divisible(1280*alpha,8)
else:
last_block_filters=1280
#7,7,320-7,7,1280
x=Conv2D(last_block_filters,
kernel_size=1,
use_bias=False,
name='Conv_1')(x)
x=BatchNormalization(epsilon=1e-3,
momentum=0.999,
name='Conv_1_bn')(x)
x=Activation(relu6,name='out_relu')(x)
x=GlobalAveragePooling2D()(x)
x=Dense(classes,activation='softmax',
use_bias=True,name='Logits')(x)
inputs=img_input
model=Model(inputs,x,name='mobilenetv2_%0.2f_%s'%(alpha,rows))
#Loadweights.
ifweights=='imagenet':
ifinclude_top:
model_name=('mobilenet_v2_weights_tf_dim_ordering_tf_kernels_'+
str(alpha)+'_'+str(rows)+'.h5')
weight_path=BASE_WEIGHT_PATH+model_name
weights_path=get_file(
model_name,weight_path,cache_subdir='models')
else:
model_name=('mobilenet_v2_weights_tf_dim_ordering_tf_kernels_'+
str(alpha)+'_'+str(rows)+'_no_top'+'.h5')
weight_path=BASE_WEIGHT_PATH+model_name
weights_path=get_file(
model_name,weight_path,cache_subdir='models')
model.load_weights(weights_path)
elifweightsisnotNone:
model.load_weights(weights)
returnmodel
def_inverted_res_block(inputs,expansion,stride,alpha,filters,block_id):
in_channels=_shape(inputs)[-1]
pointwise_conv_filters=int(filters*alpha)
pointwise_filters=_make_divisible(pointwise_conv_filters,8)
x=inputs
prefix='block_{}_'.format(block_id)
#part1數(shù)據(jù)擴(kuò)張
ifblock_id:
#Expand
x=Conv2D(expansion*in_channels,
kernel_size=1,
padding='same',
use_bias=False,
activation=None,
name=prefix+'expand')(x)
x=BatchNormalization(epsilon=1e-3,
momentum=0.999,
name=prefix+'expand_BN')(x)
x=Activation(relu6,name=prefix+'expand_relu')(x)
else:
prefix='expanded_conv_'
ifstride==2:
x=ZeroPadding2D(padding=correct_pad(x,3),
name=prefix+'pad')(x)
#part2可分離卷積
x=DepthwiseConv2D(kernel_size=3,
strides=stride,
activation=None,
use_bias=False,
padding='same'ifstride==1else'valid',
name=prefix+'depthwise')(x)
x=BatchNormalization(epsilon=1e-3,
momentum=0.999,
name=prefix+'depthwise_BN')(x)
x=Activation(relu6,name=prefix+'depthwise_relu')(x)
#part3壓縮特征,而且不使用relu函數(shù),保證特征不被破壞
x=Conv2D(pointwise_filters,
kernel_size=1,
padding='same',
use_bias=False,
activation=None,
name=prefix+'project')(x)
x=BatchNormalization(epsilon=1e-3,
momentum=0.999,
name=prefix+'project_BN')(x)
ifin_channels==pointwise_filtersandstride==1:
returnAdd(name=prefix+'add')([inputs,x])
returnx
圖片預(yù)測(cè)
建立網(wǎng)絡(luò)后,可以用以下的代碼進(jìn)行預(yù)測(cè)。
defpreprocess_input(x):
x/=255.
x-=0.5
x*=2.
returnx
if__name__=='__main__':
model=MobileNetV2(input_shape=(224,
溫馨提示
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 買賣合同房屋買賣協(xié)議
- 小區(qū)綠化環(huán)保工程施工協(xié)議
- 2025短期用工合同范本
- 2025項(xiàng)目經(jīng)理勞動(dòng)合同勞動(dòng)合同范本
- 現(xiàn)代管理學(xué)重要題型試題及答案
- 2025計(jì)算機(jī)設(shè)備采購合同范本 計(jì)算機(jī)設(shè)備采購合同(年度)
- 2025竹林經(jīng)營合同
- 行政預(yù)算與控制分析試題及答案
- 2025建筑工程監(jiān)理合同范本
- 公文處理中的文化適宜性分析試題及答案
- 思政課社會(huì)實(shí)踐報(bào)告1500字6篇
- 常暗之廂(7規(guī)則-簡體修正)
- GB∕T 25119-2021 軌道交通 機(jī)車車輛電子裝置
- 電池PCBA規(guī)格書
- 機(jī)械零件加工驗(yàn)收檢驗(yàn)記錄(共2頁)
- 機(jī)械加工切削全參數(shù)推薦表
- 終端塔基礎(chǔ)預(yù)偏值(抬高值)計(jì)算表格
- 海外醫(yī)療服務(wù)委托合同協(xié)議書范本模板
- (完整版)研究者手冊(cè)模板
- 菲林檢驗(yàn)及管理辦法
- 磁芯參數(shù)對(duì)照表
評(píng)論
0/150
提交評(píng)論