MobileNet V2网络结构

Mark as follows:
这里写图片描述

MatConvNet实现该网络架构如下:

function output = MobileNetV2()
net = dagnn.DagNN() ;

convBlock = dagnn.Conv('size', [3 3 3 32], 'hasBias', false, 'pad',[1 1], 'stride', [2 2]) ;
net.addLayer('conv1', convBlock, {'data'}, {'conv1'}, {'f1'}) ;
normBlock = dagnn.BatchNorm('numChannels', 32, 'epsilon', 1e-5);
net.addLayer('norm1', normBlock, 'conv1', 'bnorm1', {'bn1_w', 'bn1_b', 'bn1_m'}) ;
reluBlock = dagnn.ReLU() ;
net.addLayer('relu1', reluBlock, {'bnorm1'}, {'relu1'}, {}) ;

net = BottleNeck(net, 'bneck1', 32, 1, 16, 'relu1', 'bnorm2', 1);

net = BottleNeck(net, 'bneck2', 16, 6, 24, 'bnorm2', 'bnorm3', 2);
net = BottleNeck(net, 'bneck3', 24, 6, 24, 'bnorm3', 'bnorm4', 1);
net.addLayer('sum1', dagnn.Sum(), {'bnorm3', 'bnorm4'}, 'sum1') ;

net = BottleNeck(net, 'bneck4', 24, 6, 32, 'sum1', 'bnorm5', 2);
net = BottleNeck(net, 'bneck5', 32, 6, 32, 'bnorm5', 'bnorm6', 1);
net.addLayer('sum2', dagnn.Sum(), {'bnorm5', 'bnorm6'}, 'sum2') ;
net = BottleNeck(net, 'bneck6', 32, 6, 32, 'sum2', 'bnorm7', 1);
net.addLayer('sum3', dagnn.Sum(), {'sum2', 'bnorm7'}, 'sum3') ;

net = BottleNeck(net, 'bneck7', 32, 6, 64, 'sum3', 'bnorm8', 1);
net = BottleNeck(net, 'bneck8', 64, 6, 64, 'bnorm8', 'bnorm9', 1);
net.addLayer('sum4', dagnn.Sum(), {'bnorm8', 'bnorm9'}, 'sum4') ;
net = BottleNeck(net, 'bneck9', 64, 6, 64, 'sum4', 'bnorm10', 1);
net.addLayer('sum5', dagnn.Sum(), {'sum4', 'bnorm10'}, 'sum5') ;
net = BottleNeck(net, 'bneck10', 64, 6, 64, 'sum5', 'bnorm11', 1);
net.addLayer('sum6', dagnn.Sum(), {'sum5', 'bnorm11'}, 'sum6') ;

net = BottleNeck(net, 'bneck11', 64, 6, 96, 'sum6', 'bnorm12', 2);
net = BottleNeck(net, 'bneck12', 96, 6, 96, 'bnorm12', 'bnorm13', 1);
net.addLayer('sum7', dagnn.Sum(), {'bnorm12', 'bnorm13'}, 'sum7') ;
net = BottleNeck(net, 'bneck13', 96, 6, 96, 'sum7', 'bnorm14', 1);
net.addLayer('sum8', dagnn.Sum(), {'sum7', 'bnorm14'}, 'sum8') ;

net = BottleNeck(net, 'bneck14', 96, 6, 160, 'sum8', 'bnorm15', 2);
net = BottleNeck(net, 'bneck15', 160, 6, 160, 'bnorm15', 'bnorm16', 1);
net.addLayer('sum9', dagnn.Sum(), {'bnorm15', 'bnorm16'}, 'sum9') ;
net = BottleNeck(net, 'bneck16', 160, 6, 160, 'sum9', 'bnorm17', 1);
net.addLayer('sum10', dagnn.Sum(), {'sum9', 'bnorm17'}, 'sum10') ;

net = BottleNeck(net, 'bneck17', 160, 6, 320, 'sum10', 'bnorm18', 1);

convBlock = dagnn.Conv('size', [1 1 320 1280], 'hasBias', false) ;
net.addLayer('conv2', convBlock, {'bnorm18'}, {'conv2'}, {'f2'}) ;

poolBlock = dagnn.Pooling('poolSize', [7 7], 'method', 'avg') ;
net.addLayer('pool_global', poolBlock, 'conv2', 'pool_global') ;

convBlock = dagnn.Conv('size', [1 1 1280 1000], 'hasBias', false) ;
net.addLayer('prediction' , convBlock, 'pool_global', 'prediction', {'prediction_f'}) ;

net.addLayer('loss', dagnn.Loss('loss', 'softmaxlog'), {'prediction', 'label'}, 'objective') ;
net.addLayer('top1error', dagnn.Loss('loss', 'classerror'), {'prediction', 'label'}, 'top1error') ;
net.addLayer('top5error', dagnn.Loss('loss', 'topkerror', 'opts', {'topK', 5}), {'prediction', 'label'}, 'top5error') ;

net.initParams() ;

%% 以下为测试过程
input = randn(224,224,3,8,'single') ;
tic
net.eval({'data', input}) ;
toc
i = net.getVarIndex('prediction') ;
output = net.vars(i).value ;

function net = BottleNeck(net, name, depth, dscale, outdepth, input, output, stride)
% PW1
convBlock = dagnn.Conv('size', [1 1 depth depth*dscale], 'hasBias', false) ;
net.addLayer([name '_expand_conv'], convBlock, {input}, {[name '_expand_conv']}, ...
    {[name '_expand_conv_filter']}) ;
normBlock = dagnn.BatchNorm('numChannels', depth*dscale, 'epsilon', 1e-5);
net.addLayer([name '_expand_bnorm'], normBlock, {[name '_expand_conv']}, {[name '_expand_bnorm']}, ...
    {[name '_expand_bn_w'], [name '_expand_bn_b'], [name '_expand_bn_m']}) ;
reluBlock = dagnn.ReLU() ;
net.addLayer([name '_expand_relu'], reluBlock, {[name '_expand_bnorm']}, {[name '_expand_relu']}, {}) ;

% DW1
convBlock = dagnn.Conv('size', [3 3 1 depth*dscale], 'hasBias', false, 'pad',[1 1], 'stride', stride) ;
net.addLayer([name '_dwise_conv'], convBlock, {[name '_expand_relu']}, {[name '_dwise_conv']}, ...
    {[name '_dwise_conv_filter']}) ;
normBlock = dagnn.BatchNorm('numChannels', depth*dscale, 'epsilon', 1e-5);
net.addLayer([name '_dwise_bnorm'], normBlock, {[name '_dwise_conv']}, [name '_dwise_bnorm'], ...
    {[name '_dwise_bn_w'], [name '_dwise_bn_b'], [name '_dwise_bn_m']}) ;
reluBlock = dagnn.ReLU() ;
net.addLayer([name '_dwise_relu'], reluBlock, {[name '_dwise_bnorm']}, {[name '_dwise_relu']}, {}) ;

% PW1
convBlock = dagnn.Conv('size', [1 1 depth*dscale outdepth], 'hasBias', false) ;
net.addLayer([name '_linear_conv'], convBlock, {[name '_dwise_relu']}, {[name '_linear_conv']}, ...
    {[name '_linear_conv_filter']}) ;
normBlock = dagnn.BatchNorm('numChannels', outdepth, 'epsilon', 1e-5);
net.addLayer([name '_linear_bnorm'], normBlock, {[name '_linear_conv']}, {output}, ...
    {[name '_linear_bn_w'], [name '_linear_bn_b'], [name '_linear_bn_m']}) ;

值得一提的是,在Caffe和MatConvNet实现的该网络,使用GPU的速度并没有提供速度,主要原因可能是这两个库对Depthwise Separable Convolution的支持不好!!!在没有大神写出更搞笑的CUDA底层支持,估计还要回归tensorflow。

你可能感兴趣的:(计算机视觉,MobileNet,V2,网络结构)