前言:
众所周知,在2017年论文《Xception:Deep Learning with Depthwise Separable Convolutions》中首次提到了深度可分离卷积.
深度可分离卷积:
首先对每个通道上的特征独立执行卷积操作,其次对整体进行1*1卷积操作.
可参考博文:https://zhuanlan.zhihu.com/p/92134485
代码实现:
这里以mobilenetv1为例:
import tensorflow as tf
"""
MobileNetv1 模型参数设置
"""
num_filters_1st = 32
num_filters_2nd = 64
num_filters_3rd = 128
num_filters_4th = 256
num_filters_5th = 512
num_filters_6th = 1024
num_calss = 1000
tf.keras.layers.Conv2D(input_shape=(224, 224, 3), filters=num_filters_1st, kernel_size=(3, 3), strides=2, padding="same"),
tf.keras.layers.SeparableConv2D(filters=num_filters_2nd,kernel_size=(3, 3),strides=1,padding="same"),
tf.keras.layers.SeparableConv2D(filters=num_filters_3rd,kernel_size=(3, 3),strides=2,padding="same"),
tf.keras.layers.SeparableConv2D(filters=num_filters_3rd,kernel_size=(3, 3),strides=1,padding="same"),
tf.keras.layers.SeparableConv2D(filters=num_filters_4th,kernel_size=(3, 3),strides=2,padding="same"),
tf.keras.layers.SeparableConv2D(filters=num_filters_4th,kernel_size=(3, 3),strides=1,padding="same"),
tf.keras.layers.SeparableConv2D(filters=num_filters_5th,kernel_size=(3, 3),strides=2,padding="same"),
# 5个深度可分离卷积
tf.keras.layers.SeparableConv2D(filters=num_filters_5th,kernel_size=(3, 3),strides=1,padding="same"),
tf.keras.layers.SeparableConv2D(filters=num_filters_5th,kernel_size=(3, 3),strides=1,padding="same"),
tf.keras.layers.SeparableConv2D(filters=num_filters_5th,kernel_size=(3, 3),strides=1,padding="same"),
tf.keras.layers.SeparableConv2D(filters=num_filters_5th,kernel_size=(3, 3),strides=1,padding="same"),
tf.keras.layers.SeparableConv2D(filters=num_filters_5th,kernel_size=(3, 3),strides=1,padding="same"),
tf.keras.layers.SeparableConv2D(filters=num_filters_6th,kernel_size=(3, 3),strides=2,padding="same"),
tf.keras.layers.SeparableConv2D(filters=num_filters_6th,kernel_size=(3, 3),strides=1,padding="same"),
tf.keras.layers.AveragePooling2D(pool_size=(7, 7),strides=1),
tf.keras.layers.Dense(units=num_calss,activation=tf.keras.activations.softmax)
说明:
在tf官网上有两个函数来实现深度可分离卷积的操作:
1)使用函数tf.keras.layers.DepthwiseConv2D和tf.keras.layers.Conv2D组合使用实现:
tf.keras.layers.DepthwiseConv2D(kernel_size=(3, 3), strides=1, padding='same', depth_multiplier=1),
tf.keras.layers.Conv2D(filters=16, kernel_size=(1, 1), strides=1, padding='same')
2)直接使用函数tf.keras.layers.SeparableConv2D实现:
tf.keras.layers.SeparableConv2D(filters=num_filters_2nd,kernel_size=(3, 3),strides=1,padding="same")
显然,直接使用2)代码量会少很多.
参考文档:
https://tensorflow.google.cn/api_docs/python/tf/keras/layers/SeparableConv2D?hl=en
https://tensorflow.google.cn/api_docs/python/tf/keras/layers/DepthwiseConv2D?hl=en