




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
第python神經網絡MobileNetV2模型的復現詳解目錄什么是MobileNetV2模型MobileNetV2網絡部分實現代碼圖片預測
什么是MobileNetV2模型
MobileNet它哥MobileNetV2也是很不錯的呢
MobileNet模型是Google針對手機等嵌入式設備提出的一種輕量級的深層神經網絡,其使用的核心思想便是depthwiseseparableconvolution。
MobileNetV2是MobileNet的升級版,它具有兩個特征點:
1、Invertedresiduals,在ResNet50里我們認識到一個結構,bottleneckdesign結構,在3x3網絡結構前利用1x1卷積降維,在3x3網絡結構后,利用1x1卷積升維,相比直接使用3x3網絡卷積效果更好,參數更少,先進行壓縮,再進行擴張。而在MobileNetV2網絡部分,其采用Invertedresiduals結構,在3x3網絡結構前利用1x1卷積升維,在3x3網絡結構后,利用1x1卷積降維,先進行擴張,再進行壓縮。
2、Linearbottlenecks,為了避免Relu對特征的破壞,在在3x3網絡結構前利用1x1卷積升維,在3x3網絡結構后,再利用1x1卷積降維后,不再進行Relu6層,直接進行殘差網絡的加法。
整體網絡結構如下:(其中bottleneck進行的操作就是上述的創新操作)
MobileNetV2網絡部分實現代碼
#-------------------------------------------------------------#
#MobileNetV2的網絡部分
#-------------------------------------------------------------#
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)
#用于計算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]))
#使其結果可以被8整除,因為使用到了膨脹系數α
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數據擴張
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函數,保證特征不被破壞
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
圖片預測
建立網絡后,可以用以下的代碼進行預測。
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等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年中國男性護理液行業市場全景分析及前景機遇研判報告
- 中班美術活動變臉
- 無創呼吸機應用和護理
- 智慧教育發展
- 煤礦機電運輸事故原因及控制對策探究
- 物業品質管理與培訓
- 車用尿素研發生產與銷售合作協議書
- 房地產租賃合同補充協議書
- 員工培訓計劃表
- 知識產權侵權代理授權協議
- 陜西2025中考試題及答案
- 供應風險管理制度
- 直播間貨盤管理制度
- 2025至2030中國心臟電生理標測、導航和記錄設備行業發展趨勢分析與未來投資戰略咨詢研究報告
- 2025泰山護理職業學院教師招聘考試試題
- 2025年重慶市中考歷史真題(原卷版)
- 吉林省國資委監管企業招聘筆試真題2024
- 項目管理中的資源優化配置
- 2025年重慶市中考道德與法治試卷真題(含標準答案)
- 2025年北京昌平區東小口鎮城市協管員招聘題庫帶答案分析
- 婦女兒童之家管理制度
評論
0/150
提交評論