## 深度可分离卷积结构（depthwise separable convolution）计算复杂度分析

https://zhuanlan.zhihu.com/p/28186857

## 16*3*3                       16*32*1*1

MobileNet-v1:

MobileNet主要用于移动端计算模型,是将传统的卷积操作改为两层的卷积操作,在保证准确率的条件下,计算时间减少为原来的1/9,计算参数减少为原来的1/7.

MobileNet模型的核心就是将原本标准的卷积操作因式分解成一个depthwise convolution和一个1*1的pointwise convolution操作。简单讲就是将原来一个卷积层分成两个卷积层，其中前面一个卷积层的每个filter都只跟input的每个channel进行卷积，然后后面一个卷积层则负责combining，即将上一层卷积的结果进行合并。

depthwise convolution:

pointwise convolution:

https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet_v1.py

# Tensorflow mandates these.
from collections import namedtuple
import functools

import tensorflow as tf

slim = tf.contrib.slim

# Conv and DepthSepConv namedtuple define layers of the MobileNet architecture
# Conv defines 3x3 convolution layers
# DepthSepConv defines 3x3 depthwise convolution followed by 1x1 convolution.
# stride is the stride of the convolution
# depth is the number of channels or filters in a layer
Conv = namedtuple('Conv', ['kernel', 'stride', 'depth'])
DepthSepConv = namedtuple('DepthSepConv', ['kernel', 'stride', 'depth'])

# _CONV_DEFS specifies the MobileNet body
_CONV_DEFS = [
Conv(kernel=[3, 3], stride=2, depth=32),
DepthSepConv(kernel=[3, 3], stride=1, depth=64),
DepthSepConv(kernel=[3, 3], stride=2, depth=128),
DepthSepConv(kernel=[3, 3], stride=1, depth=128),
DepthSepConv(kernel=[3, 3], stride=2, depth=256),
DepthSepConv(kernel=[3, 3], stride=1, depth=256),
DepthSepConv(kernel=[3, 3], stride=2, depth=512),
DepthSepConv(kernel=[3, 3], stride=1, depth=512),
DepthSepConv(kernel=[3, 3], stride=1, depth=512),
DepthSepConv(kernel=[3, 3], stride=1, depth=512),
DepthSepConv(kernel=[3, 3], stride=1, depth=512),
DepthSepConv(kernel=[3, 3], stride=1, depth=512),
DepthSepConv(kernel=[3, 3], stride=2, depth=1024),
DepthSepConv(kernel=[3, 3], stride=1, depth=1024)
]

input_size = 160
inputdepth = 3
conv_defs = _CONV_DEFS
sumcost = 0
for i, conv_def in enumerate(conv_defs):
stride = conv_def.stride
kernel = conv_def.kernel
outdepth = conv_def.depth
output_size = round((input_size - int(kernel[0] / 2) * 2) / stride)
if isinstance(conv_def, Conv):
sumcost += output_size * output_size * kernel[0] * kernel[0] * inputdepth * outdepth
if isinstance(conv_def, DepthSepConv):
sumcost += output_size * output_size * kernel[0] * kernel[0] * inputdepth * outdepth
inputdepth = outdepth
input_size = output_size
print("src conv:    ", sumcost)

input_size = 160
inputdepth = 3
conv_defs = _CONV_DEFS
sumcost1 = 0
for i, conv_def in enumerate(conv_defs):
stride = conv_def.stride
kernel = conv_def.kernel
outdepth = conv_def.depth
output_size = round((input_size - int(kernel[0] / 2) * 2) / stride)
if isinstance(conv_def, Conv):
sumcost1 += output_size * output_size * kernel[0] * kernel[0] * inputdepth * outdepth
if isinstance(conv_def, DepthSepConv):
#sumcost += output_size * output_size * kernel[0] * kernel[0] * inputdepth * outdepth
sumcost1 += output_size * output_size *(inputdepth * kernel[0] * kernel[0]  + inputdepth * outdepth * 1 * 1)
inputdepth = outdepth
input_size = output_size
print("DepthSepConv:", sumcost1)
print("compare:", sumcost1 / sumcost)

src conv:            1045417824
DepthSepConv:   126373376
compare: 0.12088312739538674

mobilenet V1介绍

https://www.cnblogs.com/darkknightzh/p/9410540.html

posted on 2017-11-29 09:16 Maddock 阅读(...) 评论(...) 编辑 收藏