写在前面
本篇主要介绍CVPR 2017最佳论文:《Densely Connected Convolutional Networks》,主要整理自参考文献[1-3]。
随着网络结构的加深,梯度与信息消失现象也就越明显。为了解决这一问题,人们设计出ResNet,Highway Network,FractalNets等。这些网络结构都有一个共同的特点:在每一层与之后的层之间建立捷径来缓解梯度消失这种现象。但是这样也会带来一些问题:巨量的网络参数,网络结构的利用率不高(一些层被有选择地Drop掉了)。鉴于此,DenseNet作者提出了这种新的网络结构,利用当前层与后面层的连结(Concatenate)使得每一层的信息得到充分的利用,缓解了梯度消失的同时,极大地减少了参数。
技术要点
一、让网络中的每一层都直接与其前面层相连,实现特征的重复利用;
二、同时把网络的每一层设计得特别「窄」,即只学习非常少的特征图(最极端情况就是每一层只学习一个特征图),达到降低冗余性的目的。
网络结构
一、网络整体结构图表
网络整体结构图示如下。其中第一幅图是一个5层的Dense Block示意图,生长率(Grow Rate)k=4;第二幅图是含有是三个Dense Blocks的DenseNet示意图;第三个表展示了ImageNet上DenseNet的结构,表中素有网络的生长率都为k=32,每一个Conv层都对应着BN-ReLU-Conv。
二、结构中的一些概念细节
1 紧密连接(Dense Connectivity)
在DenseNet结构中,讲每一层的输出都导入后面的所有层,与ResNet的相加不同的是,DenseNet结构使用的是连结结构(Concatenate)。这样的结构可以减少网络参数,避免ResNet中可能出现的缺点(例如某些层被选择性丢弃,信息阻塞等)。
2 组成函数(Composite Function)
使用Batch Normalization + ReLU + 3*3 Conv层作为组成函数。
3 过渡层(Transition Layer)
过渡层包含瓶颈层(Bottleneck layer,即1*1卷积层)和池化层。
1)瓶颈层
1*1的卷积层用于压缩参数。每一层输出k个Feature Maps,理论上将每个Dense Block输出为4k个Feature Maps,然而实际情况中会大于这个数字。卷积层的作用是将一个Dense Block的参数压缩到4k个。
2)池化层
由于采用了Dense Connectivity结构,直接在各个层之间加入池化层是不可行的,因此采用的是Dense Block组合的方式,在各个Dense Block之间加入卷积层和池化层。
4 增长率(Growth Rate)
这里的增长率代表的是每一层输出的Feature Maps的厚度。ResNet,GoogleNet等网络结构中经常能见到输出厚度为上百个,其目的主要是为了提取不同的特征。但是由于DenseNet中每一层都能直接为后面网络所用,所以k被限制在一个很小的数值。
5 压缩(Compression)
跟1*1卷积层作用类似,压缩参数。作者选择压缩率(theta)为0.5。包含Bottleneck Layer的叫DenseNet-B,包含压缩层的叫DenseNet-C,两者都包含的叫DenseNet-BC。
代码
#-*- coding: UTF-8 -*-
"""
Environment: Keras2.0.5,Python2.7
Model: DenseNet
"""
from __future__ import absolute_import
from __future__ import division
import os
from keras import backend
from keras import layers
from keras import models
from keras.utils import plot_model
def _obtain_input_shape(input_shape,
default_size,
min_size,
data_format,
require_flatten,
weights=None):
"""Internal utility to compute/validate a model's input shape.
# Arguments
input_shape: Either None (will return the default network input shape),
or a user-provided shape to be validated.
default_size: Default input width/height for the model.
min_size: Minimum input width/height accepted by the model.
data_format: Image data format to use.
require_flatten: Whether the model is expected to
be linked to a classifier via a Flatten layer.
# Returns
An integer shape tuple (may include None entries).
# Raises
ValueError: In case of invalid argument values.
"""
if input_shape and len(input_shape) == 3:
if data_format == 'channels_first':
if input_shape[0] not in {1, 3}:
warnings.warn(
'This model usually expects 1 or 3 input channels. '
'However, it was passed an input_shape with ' +
str(input_shape[0]) + ' input channels.')
default_shape = (input_shape[0], default_size, default_size)
else:
if input_shape[-1] not in {1, 3}:
warnings.warn(
'This model usually expects 1 or 3 input channels. '
'However, it was passed an input_shape with ' +
str(input_shape[-1]) + ' input channels.')
default_shape = (default_size, default_size, input_shape[-1])
else:
if data_format == 'channels_first':
default_shape = (3, default_size, default_size)
else:
default_shape = (default_size, default_size, 3)
if input_shape:
if data_format == 'channels_first':
if input_shape is not None:
if len(input_shape) != 3:
raise ValueError(
'`input_shape` must be a tuple of three integers.')
if input_shape[0] != 3 and weights == 'imagenet':
raise ValueError('The input must have 3 channels; got '
'`input_shape=' + str(input_shape) + '`')
if ((input_shape[1] is not None and input_shape[1] < min_size) or
(input_shape[2] is not None and input_shape[2] < min_size)):
raise ValueError('Input size must be at least ' +
str(min_size) + 'x' + str(min_size) +
'; got `input_shape=' +
str(input_shape) + '`')
else:
if input_shape is not None:
if len(input_shape) != 3:
raise ValueError(
'`input_shape` must be a tuple of three integers.')
if input_shape[-1] != 3 and weights == 'imagenet':
raise ValueError('The input must have 3 channels; got '
'`input_shape=' + str(input_shape) + '`')
if ((input_shape[0] is not None and input_shape[0] < min_size) or
(input_shape[1] is not None and input_shape[1] < min_size)):
raise ValueError('Input size must be at least ' +
str(min_size) + 'x' + str(min_size) +
'; got `input_shape=' +
str(input_shape) + '`')
else:
if require_flatten:
input_shape = default_shape
else:
if data_format == 'channels_first':
input_shape = (3, None, None)
else:
input_shape = (None, None, 3)
if require_flatten:
if None in input_shape:
raise ValueError('If `include_top` is True, '
'you should specify a static `input_shape`. '
'Got `input_shape=' + str(input_shape) + '`')
return input_shape
def dense_block(x, blocks, name):
"""A dense block.
# Arguments
x: input tensor.
blocks: integer, the number of building blocks.
name: string, block label.
# Returns
output tensor for the block.
"""
for i in range(blocks):
x = conv_block(x, 32, name=name + '_block' + str(i + 1))
return x
def transition_block(x, reduction, name):
"""A transition block.
# Arguments
x: input tensor.
reduction: float, compression rate at transition layers.
name: string, block label.
# Returns
output tensor for the block.
"""
bn_axis = 3 if backend.image_data_format() == 'channels_last' else 1
x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5,
name=name + '_bn')(x)
x = layers.Activation('relu', name=name + '_relu')(x)
x = layers.Conv2D(int(backend.int_shape(x)[bn_axis] * reduction), 1,
use_bias=False,
name=name + '_conv')(x)
x = layers.AveragePooling2D(2, strides=2, name=name + '_pool')(x)
return x
def conv_block(x, growth_rate, name):
"""A building block for a dense block.
# Arguments
x: input tensor.
growth_rate: float, growth rate at dense layers.
name: string, block label.
# Returns
Output tensor for the block.
"""
bn_axis = 3 if backend.image_data_format() == 'channels_last' else 1
x1 = layers.BatchNormalization(axis=bn_axis,
epsilon=1.001e-5,
name=name + '_0_bn')(x)
x1 = layers.Activation('relu', name=name + '_0_relu')(x1)
x1 = layers.Conv2D(4 * growth_rate, 1,
use_bias=False,
name=name + '_1_conv')(x1)
x1 = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5,
name=name + '_1_bn')(x1)
x1 = layers.Activation('relu', name=name + '_1_relu')(x1)
x1 = layers.Conv2D(growth_rate, 3,
padding='same',
use_bias=False,
name=name + '_2_conv')(x1)
x = layers.Concatenate(axis=bn_axis, name=name + '_concat')([x, x1])
return x
def DenseNet(blocks,
include_top=True,
input_tensor=None,
input_shape=None,
pooling=None,
classes=1000):
"""Instantiates the DenseNet architecture.
Optionally loads weights pre-trained on ImageNet.
Note that the data format convention used by the model is
the one specified in your Keras config at `~/.keras/keras.json`.
# Arguments
blocks: numbers of building blocks for the four dense layers.
include_top: whether to include the fully-connected
layer at the top of the network.
input_tensor: optional Keras tensor
(i.e. output of `layers.Input()`)
to use as image input for the model.
input_shape: optional shape tuple, only to be specified
if `include_top` is False (otherwise the input shape
has to be `(224, 224, 3)` (with `channels_last` data format)
or `(3, 224, 224)` (with `channels_first` data format).
It should have exactly 3 inputs channels.
pooling: optional pooling mode for feature extraction
when `include_top` is `False`.
- `None` means that the output of the model will be
the 4D tensor output of the
last convolutional layer.
- `avg` means that global average pooling
will be applied to the output of the
last convolutional layer, and thus
the output of the model will be a 2D tensor.
- `max` means that global max pooling will
be applied.
classes: optional number of classes to classify images
into, only to be specified if `include_top` is True, and
if no `weights` argument is specified.
# Returns
A Keras model instance.
# Raises
ValueError: in case of invalid input shape.
"""
# Determine proper input shape
input_shape = _obtain_input_shape(input_shape,
default_size=224,
min_size=32,
data_format=backend.image_data_format(),
require_flatten=include_top)
if input_tensor is None:
img_input = layers.Input(shape=input_shape)
else:
if not backend.is_keras_tensor(input_tensor):
img_input = layers.Input(tensor=input_tensor, shape=input_shape)
else:
img_input = input_tensor
bn_axis = 3 if backend.image_data_format() == 'channels_last' else 1
x = layers.ZeroPadding2D(padding=((3, 3), (3, 3)))(img_input)
x = layers.Conv2D(64, 7, strides=2, use_bias=False, name='conv1/conv')(x)
x = layers.BatchNormalization(
axis=bn_axis, epsilon=1.001e-5, name='conv1/bn')(x)
x = layers.Activation('relu', name='conv1/relu')(x)
x = layers.ZeroPadding2D(padding=((1, 1), (1, 1)))(x)
x = layers.MaxPooling2D(3, strides=2, name='pool1')(x)
x = dense_block(x, blocks[0], name='conv2')
x = transition_block(x, 0.5, name='pool2')
x = dense_block(x, blocks[1], name='conv3')
x = transition_block(x, 0.5, name='pool3')
x = dense_block(x, blocks[2], name='conv4')
x = transition_block(x, 0.5, name='pool4')
x = dense_block(x, blocks[3], name='conv5')
x = layers.BatchNormalization(
axis=bn_axis, epsilon=1.001e-5, name='bn')(x)
if include_top:
x = layers.GlobalAveragePooling2D(name='avg_pool')(x)
x = layers.Dense(classes, activation='softmax', name='fc1000')(x)
else:
if pooling == 'avg':
x = layers.GlobalAveragePooling2D(name='avg_pool')(x)
elif pooling == 'max':
x = layers.GlobalMaxPooling2D(name='max_pool')(x)
# Ensure that the model takes into account
# any potential predecessors of `input_tensor`.
if input_tensor is not None:
inputs = keras_utils.get_source_inputs(input_tensor)
else:
inputs = img_input
# Create model.
if blocks == [6, 12, 24, 16]:
model = models.Model(inputs, x, name='densenet121')
elif blocks == [6, 12, 32, 32]:
model = models.Model(inputs, x, name='densenet169')
elif blocks == [6, 12, 48, 32]:
model = models.Model(inputs, x, name='densenet201')
else:
model = models.Model(inputs, x, name='densenet')
return model
def DenseNet121(include_top=True,
input_tensor=None,
input_shape=None,
pooling=None,
classes=1000):
return DenseNet([6, 12, 24, 16],
include_top,
input_tensor, input_shape,
pooling, classes)
def DenseNet169(include_top=True,
input_tensor=None,
input_shape=None,
pooling=None,
classes=1000):
return DenseNet([6, 12, 32, 32],
include_top,
input_tensor, input_shape,
pooling, classes)
def DenseNet201(include_top=True,
input_tensor=None,
input_shape=None,
pooling=None,
classes=1000):
return DenseNet([6, 12, 48, 32],
include_top,
input_tensor, input_shape,
pooling, classes)
def preprocess_input(x, data_format=None):
"""Preprocesses a numpy array encoding a batch of images.
# Arguments
x: a 3D or 4D numpy array consists of RGB values within [0, 255].
data_format: data format of the image tensor.
# Returns
Preprocessed array.
"""
return imagenet_utils.preprocess_input(x, data_format, mode='torch')
def check_print():
# Create a Keras Model
model=DenseNet121()
model.summary()
# Save a PNG of the Model Build
plot_model(model,to_file='DenseNet.png')
model.compile(optimizer='sgd',loss='categorical_crossentropy')
print 'Model Compiled'
if __name__=='__main__':
check_print()
另:作者公开的源码和训练的模型在这里,整理了PyTorch,Caffe,Tensorflow,Keras,MXNet等各种深度学习开源平台下的实现。
参考文献
[1] DenseNet, Densely Connected Convolutional Networks.
[2] CVPR 2017最佳论文作者解读:DenseNet 的“What”、“Why”和“How”|CVPR 2017.