MobileNet V2 Trained on ImageNet Competition Data
MobileNet V2 Trained on ImageNet Competition Data
NetModel Access
NetModel Access
This Notebook
This Notebook
NetModel["MobileNet V2 Trained on ImageNet Competition Data","ConstructionNotebook"]
Untrained Net
Untrained Net
NetModel["MobileNet V2 Trained on ImageNet Competition Data","UninitializedEvaluationNet"]
Trained Net
Trained Net
NetModel["MobileNet V2 Trained on ImageNet Competition Data"]
Notes
Notes
These models are based on the original Tensorflow implementation, and have asymmetric padding in ConvolutionLayer to replicate the Tensorflow “SAME” Padding behaviour.
Parameter Settings
Parameter Settings
In[]:=
mean={0.485,0.456,0.406};variance={0.052441,0.050176,0.050625};
imageNetClasses=NetExtract[NetModel["MobileNet V2 Trained on ImageNet Competition Data","UninitializedEvaluationNet"],"Output"][["Labels"]];
Internal Functions
Internal Functions
In[]:=
mobileunit[prefix_,nchannels_,kernel_,stride_,pad_,ngroup_,type_]:=Which[type1,NetGraph[ <| "conv"<>prefixConvolutionLayer[nchannels,kernel,"Stride"->stride,"PaddingSize"->pad,"ChannelGroups"->ngroup], "conv"<>prefix<>"_bn"->BatchNormalizationLayer[], "relu"<>prefix->ElementwiseLayer[(Min[Max[0,#],6]&)] |>, {NetPort["Input"]->1->2->3} ],type2,NetGraph[ <| "conv"<>prefix->ConvolutionLayer[nchannels,kernel,"Stride"->stride,"PaddingSize"->pad,"ChannelGroups"->ngroup], "conv"<>prefix<>"_bn"->BatchNormalizationLayer[] |>, {NetPort["Input"]->1->2} ],type3,NetGraph[<|"conv"<>prefixConvolutionLayer[nchannels,kernel,"Stride"stride,"PaddingSize"{{Ceiling[kernel[[1]]/2]-2,Ceiling[kernel[[2]]/2]},{Ceiling[kernel[[1]]/2]-2,Ceiling[kernel[[2]]/2]}},"ChannelGroups"ngroup],"conv"<>prefix<>"_bn"BatchNormalizationLayer[],"relu"<>prefixElementwiseLayer[(Min[Max[0,#],6]&)]|>,{NetPort["Input"]123}]]
In[]:=
invresunit[prefix_,nchannels_,kernel_,stride_,pad_,ngroup_,type_]:=Which[type1,NetChain[mobileunit[prefix<>"_expand",ngroup,1,1,0,1,1], mobileunit[prefix<>"_dwise",ngroup,kernel,stride,pad,ngroup,1], mobileunit[prefix<>"_linear",nchannels,1,1,0,1,2]],type2,NetChain[mobileunit[prefix<>"_dwise",ngroup,kernel,stride,pad,ngroup,1], mobileunit[prefix<>"_linear",nchannels,1,1,0,1,2]],type3,NetChain[mobileunit[prefix<>"_expand",ngroup,1,1,0,1,1], mobileunit[prefix<>"_dwise",ngroup,kernel,stride,pad,ngroup,3], mobileunit[prefix<>"_linear",nchannels,1,1,0,1,2]]]
In[]:=
mobilenetblock[prefix_,nchannels_,kernel_,stride_,pad_,ngroup_]:=NetGraph[{mobileunit[prefix<>"_expand",ngroup,1,1,0,1,1],mobileunit[prefix<>"_dwise",ngroup,kernel,stride,pad,ngroup,1],mobileunit[prefix<>"_linear",nchannels,1,1,0,1,2],ThreadingLayer[Plus]},{1->2->3->4,NetPort["Input"]->4}]
In[]:=
genmobilenet[c1_,c2_,c3_,c4_,c5_,c6_,c7_,c8_,c9_,g1_,g2_,g3_,g4_,g5_,g6_,g7_,p_,dim_]:=NetChain[{mobileunit["1",c1,{3,3},{2,2},{0,0},1,3],invresunit["2_1",c2,{3,3},{1,1},{1,1},g1,2],invresunit["2_2",c3,{3,3},{2,2},{0,0},g2,3],mobilenetblock["3_1",c3,{3,3},{1,1},{1,1},g3],invresunit["3_2",c4,{3,3},{2,2},{0,0},g3,3],mobilenetblock["4_1",c4,{3,3},{1,1},{1,1},g4],mobilenetblock["4_2",c4,{3,3},{1,1},{1,1},g4],invresunit["4_3",c5,{3,3},{2,2},{0,0},g4,3],mobilenetblock["4_4",c5,{3,3},{1,1},{1,1},g5],mobilenetblock["4_5",c5,{3,3},{1,1},{1,1},g5],mobilenetblock["4_6",c5,{3,3},{1,1},{1,1},g5],invresunit["4_7",c6,{3,3},{1,1},{1,1},g5,1],mobilenetblock["5_1",c6,{3,3},{1,1},{1,1},g6],mobilenetblock["5_2",c6,{3,3},{1,1},{1,1},g6],invresunit["5_3",c7,{3,3},{2,2},{0,0},g6,3],mobilenetblock["6_1",c7,{3,3},{1,1},{1,1},g7],mobilenetblock["6_2",c7,{3,3},{1,1},{1,1},g7],invresunit["6_3",c8,{3,3},{1,1},{1,1},g7,1],mobileunit["6_4",c9,{1,1},{1,1},{0,0},1,1],NetChain[<|"pool6"->PoolingLayer[{p,p},{1,1},"Function"->Mean],"fc7"->ConvolutionLayer[1001,{1,1}],"reshape"->ReshapeLayer[{1001}],"prob_softmax"->SoftmaxLayer[]|>]},"Input"->{3,dim,dim}]
Final Nets
Final Nets
In[]:=
createmobilenet[depth_,inp_]:= Block[{mobilenet,c1,c2,c3,c4,c5,c6,c7,c8,c9,p}, Which[ depth==1.4, {c1,c2,c3,c4,c5,c6,c7,c8,c9}= {48,24,32,48,88,136,224,448,1792}, depth==1.3, {c1,c2,c3,c4,c5,c6,c7,c8,c9}= {40,24,32,40,80,128,208,416,1664}, depth==1.0, {c1,c2,c3,c4,c5,c6,c7,c8,c9}= {32,16,24,32,64,96,160,320,1280}, depth==0.75, {c1,c2,c3,c4,c5,c6,c7,c8,c9}= {24,16,24,24,48,72,120,240,1280}, depth==0.5, {c1,c2,c3,c4,c5,c6,c7,c8,c9}= {16,8,16,16,32,48,80,160,1280}, depth==0.35, {c1,c2,c3,c4,c5,c6,c7,c8,c9}= {16,8,8,16,24,32,56,112,1280} ]; Which[ inp==224,{p=7}, inp==192,{p=6}, inp==160,{p=5}, inp==128,{p=4}, inp==96,{p=3} ]; mobilenet=NetInitialize@genmobilenet[c1,c2,c3,c4,c5,c6,c7,c8,c9,c1,6*c2,6*c3,6*c4,6*c5,6*c6,6*c7,p,inp]]