简介
该论文提出了一种新的模型缩放方法,它使用一个简单而高效的复合系数来以更结构化的方式放大 CNNs。 不像传统的方法那样任意缩放网络维度,如宽度,深度和分辨率,该论文的方法用一系列固定的尺度缩放系数来统一缩放网络维度。 通过使用这种新颖的缩放方法和 AutoML[5] 技术,作者将这种模型称为 EfficientNets ,它具有最高达10倍的效率(更小、更快)。
区别
第一处区别是在最开始降采样没有采用maxpooling,而是换成了stride为2的conv。。猜测是为了减少信息丢失,尤其是对小模型来说,前期的底层特征提取更重要。
第二处区别是第一次降采样后的channel反而减少了,这个我没搞懂。。。。
第三处区别是有很多stage都采用了5x5的conv。。。这是因为对于depthwise separable conv来说,5x5的计算量要比两个3x3的计算量要小。。(坊间传闻large kernel is all your need. 233333)
其中输入特征图尺寸为(H, W, M),输出特征图尺寸为(H, W, N)。
第四处区别是降采样后的特征图尺寸减半,但是channel没有扩大两倍。第6个stage特征图尺寸没变,但是channel也扩大了。。这些可能都是手工设计很难搞定的。。。我能想到的解释是,MnasNet在搜网络结构的时候带上了运算量的约束,可以理解成网络在训练的时候就考虑了pruing(裁枝),因此才会出现一些不规则的channel数,同时这个带来另外的一个好处就是,网络可以更好的训练更有意义的权重,因此这些搜出来的网络结构的上限更高。
通过对模型效率的显著改进,预计 EfficientNets 可能成为未来计算机视觉任务的新基础。
import tensorflow as tf
import math
NUM_CLASSES = 10
def swish(x):
return x * tf.nn.sigmoid(x)
def round_filters(filters, multiplier):
depth_divisor = 8
min_depth = None
min_depth = min_depth or depth_divisor
filters = filters * multiplier
new_filters = max(min_depth, int(filters + depth_divisor / 2) // depth_divisor * depth_divisor)
if new_filters < 0.9 * filters:
new_filters += depth_divisor
return int(new_filters)
def round_repeats(repeats, multiplier):
if not multiplier:
return repeats
return int(math.ceil(multiplier * repeats))
class SEBlock(tf.keras.layers.Layer):
def __init__(self, input_channels, ratio=0.25):
super(SEBlock, self).__init__()
self.num_reduced_filters = max(1, int(input_channels * ratio))
self.pool = tf.keras.layers.GlobalAveragePooling2D()
self.reduce_conv = tf.keras.layers.Conv2D(filters=self.num_reduced_filters,
kernel_size=(1, 1),
strides=1,
padding="same")
self.expand_conv = tf.keras.layers.Conv2D(filters=input_channels,
kernel_size=(1, 1),
strides=1,
padding="same")
def call(self, inputs, **kwargs):
branch = self.pool(inputs)
branch = tf.expand_dims(input=branch, axis=1)
branch = tf.expand_dims(input=branch, axis=1)
branch = self.reduce_conv(branch)
branch = swish(branch)
branch = self.expand_conv(branch)
branch = tf.nn.sigmoid(branch)
output = inputs * branch
return output
class MBConv(tf.keras.layers.Layer):
def __init__(self, in_channels, out_channels, expansion_factor, stride, k, drop_connect_rate):
super(MBConv, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.stride = stride
self.drop_connect_rate = drop_connect_rate
self.conv1 = tf.keras.layers.Conv2D(filters=in_channels * expansion_factor,
kernel_size=(1, 1),
strides=1,
padding="same")
self.bn1 = tf.keras.layers.BatchNormalization()
self.dwconv = tf.keras.layers.DepthwiseConv2D(kernel_size=(k, k),
strides=stride,
padding="same")
self.bn2 = tf.keras.layers.BatchNormalization()
self.se = SEBlock(input_channels=in_channels * expansion_factor)
self.conv2 = tf.keras.layers.Conv2D(filters=out_channels,
kernel_size=(1, 1),
strides=1,
padding="same")
self.bn3 = tf.keras.layers.BatchNormalization()
self.dropout = tf.keras.layers.Dropout(rate=drop_connect_rate)
def call(self, inputs, training=None, **kwargs):
x = self.conv1(inputs)
x = self.bn1(x, training=training)
x = swish(x)
x = self.dwconv(x)
x = self.bn2(x, training=training)
x = self.se(x)
x = swish(x)
x = self.conv2(x)
x = self.bn3(x, training=training)
if self.stride == 1 and self.in_channels == self.out_channels:
if self.drop_connect_rate:
x = self.dropout(x, training=training)
x = tf.keras.layers.add([x, inputs])
return x
def build_mbconv_block(in_channels, out_channels, layers, stride, expansion_factor, k, drop_connect_rate):
block = tf.keras.Sequential()
for i in range(layers):
if i == 0:
block.add(MBConv(in_channels=in_channels,
out_channels=out_channels,
expansion_factor=expansion_factor,
stride=stride,
k=k,
drop_connect_rate=drop_connect_rate))
else:
block.add(MBConv(in_channels=out_channels,
out_channels=out_channels,
expansion_factor=expansion_factor,
stride=1,
k=k,
drop_connect_rate=drop_connect_rate))
return block
class EfficientNet(tf.keras.Model):
def __init__(self, width_coefficient, depth_coefficient, dropout_rate, drop_connect_rate=0.2):
super(EfficientNet, self).__init__()
self.conv1 = tf.keras.layers.Conv2D(filters=round_filters(32, width_coefficient),
kernel_size=(3, 3),
strides=2,
padding="same")
self.bn1 = tf.keras.layers.BatchNormalization()
self.block1 = build_mbconv_block(in_channels=round_filters(32, width_coefficient),
out_channels=round_filters(16, width_coefficient),
layers=round_repeats(1, depth_coefficient),
stride=1,
expansion_factor=1, k=3, drop_connect_rate=drop_connect_rate)
self.block2 = build_mbconv_block(in_channels=round_filters(16, width_coefficient),
out_channels=round_filters(24, width_coefficient),
layers=round_repeats(2, depth_coefficient),
stride=2,
expansion_factor=6, k=3, drop_connect_rate=drop_connect_rate)
self.block3 = build_mbconv_block(in_channels=round_filters(24, width_coefficient),
out_channels=round_filters(40, width_coefficient),
layers=round_repeats(2, depth_coefficient),
stride=2,
expansion_factor=6, k=5, drop_connect_rate=drop_connect_rate)
self.block4 = build_mbconv_block(in_channels=round_filters(40, width_coefficient),
out_channels=round_filters(80, width_coefficient),
layers=round_repeats(3, depth_coefficient),
stride=2,
expansion_factor=6, k=3, drop_connect_rate=drop_connect_rate)
self.block5 = build_mbconv_block(in_channels=round_filters(80, width_coefficient),
out_channels=round_filters(112, width_coefficient),
layers=round_repeats(3, depth_coefficient),
stride=1,
expansion_factor=6, k=5, drop_connect_rate=drop_connect_rate)
self.block6 = build_mbconv_block(in_channels=round_filters(112, width_coefficient),
out_channels=round_filters(192, width_coefficient),
layers=round_repeats(4, depth_coefficient),
stride=2,
expansion_factor=6, k=5, drop_connect_rate=drop_connect_rate)
self.block7 = build_mbconv_block(in_channels=round_filters(192, width_coefficient),
out_channels=round_filters(320, width_coefficient),
layers=round_repeats(1, depth_coefficient),
stride=1,
expansion_factor=6, k=3, drop_connect_rate=drop_connect_rate)
self.conv2 = tf.keras.layers.Conv2D(filters=round_filters(1280, width_coefficient),
kernel_size=(1, 1),
strides=1,
padding="same")
self.bn2 = tf.keras.layers.BatchNormalization()
self.pool = tf.keras.layers.GlobalAveragePooling2D()
self.dropout = tf.keras.layers.Dropout(rate=dropout_rate)
self.fc = tf.keras.layers.Dense(units=NUM_CLASSES,
activation=tf.keras.activations.softmax)
def call(self, inputs, training=None, mask=None):
x = self.conv1(inputs)
x = self.bn1(x, training=training)
x = swish(x)
x = self.block1(x)
x = self.block2(x)
x = self.block3(x)
x = self.block4(x)
x = self.block5(x)
x = self.block6(x)
x = self.block7(x)
x = self.conv2(x)
x = self.bn2(x, training=training)
x = swish(x)
x = self.pool(x)
x = self.dropout(x, training=training)
x = self.fc(x)
return x
def get_efficient_net(width_coefficient, depth_coefficient, resolution, dropout_rate):
net = EfficientNet(width_coefficient=width_coefficient,
depth_coefficient=depth_coefficient,
dropout_rate=dropout_rate)
net.build(input_shape=(None, resolution, resolution, 3))
net.summary()
return net
def efficient_net_b0():
return get_efficient_net(1.0, 1.0, 224, 0.2)
def efficient_net_b1():
return get_efficient_net(1.0, 1.1, 240, 0.2)
def efficient_net_b2():
return get_efficient_net(1.1, 1.2, 260, 0.3)
def efficient_net_b3():
return get_efficient_net(1.2, 1.4, 300, 0.3)
def efficient_net_b4():
return get_efficient_net(1.4, 1.8, 380, 0.4)
def efficient_net_b5():
return get_efficient_net(1.6, 2.2, 456, 0.4)
def efficient_net_b6():
return get_efficient_net(1.8, 2.6, 528, 0.5)
def efficient_net_b7():
return get_efficient_net(2.0, 3.1, 600, 0.5)