以DenseNet121为例,具体看以下代码
def dense_block(x, blocks, name): for i in range(blocks): x = conv_block(x, 32, name=name + '_block' + str(i + 1)) return x def conv_block(x, growth_rate, name): bn_axis = 3 x1 = BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name=name + '_0_bn')(x) x1 = Activation('relu', name=name + '_0_relu')(x1) x1 = Conv2D(4 * growth_rate, 1, use_bias=False, name=name + '_1_conv')(x1) x1 = BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name=name + '_1_bn')(x1) x1 = Activation('relu', name=name + '_1_relu')(x1) x1 = Conv2D(growth_rate, 3, padding='same', use_bias=False, name=name + '_2_conv')(x1) x = Concatenate(axis=bn_axis, name=name + '_concat')([x, x1]) return x def transition_block(x, reduction, name): bn_axis = 3 x = BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name=name + '_bn')(x) x = Activation('relu', name=name + '_relu')(x) x = Conv2D(int(K.int_shape(x)[bn_axis] * reduction), 1, use_bias=False, name=name + '_conv')(x) x = AveragePooling2D(2, strides=2, name=name + '_pool')(x) return x def DenseNet121(input_shape=(224, 224, 3), classes=1000): blocks = [6, 12, 24, 16] img_input = Input(shape=input_shape) bn_axis = 3 x = ZeroPadding2D(padding=((3, 3), (3, 3)))(img_input) x = Conv2D(64, 7, strides=2, use_bias=False, name='conv1/conv')(x) x = BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name='conv1/bn')(x) x = Activation('relu', name='conv1/relu')(x) x = ZeroPadding2D(padding=((1, 1), (1, 1)))(x) x = 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 = BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name='bn')(x) x = GlobalAveragePooling2D(name='avg_pool')(x) x = Dense(classes, activation='softmax', name='fc1000')(x) model = Model(img_input, x, name='densenet121') return model