Cifar10 Model 1 Resnet-32 with stack_n = 5
Cifar10 Model 2 Resnet-50 with stack_n = 8
Cifar10 Model 4 Resnet-20 with stack_n = 3
def residual_network(img_input, classes_num=10, stack_n=5, weight_decay=1e-4):
def residual_block(x, o_filters, increase=False):
stride = (1, 1)
if increase:
stride = (2, 2)
o1 = Activation('relu')(BatchNormalization(momentum=0.9, epsilon=1e-5)(x))
conv_1 = Conv2D(o_filters, kernel_size=(3, 3), strides=stride, padding='same',
kernel_initializer="he_normal",
kernel_regularizer=regularizers.l2(weight_decay))(o1)
o2 = Activation('relu')(BatchNormalization(momentum=0.9, epsilon=1e-5)(conv_1))
conv_2 = Conv2D(o_filters, kernel_size=(3, 3), strides=(1, 1), padding='same',
kernel_initializer="he_normal",
kernel_regularizer=regularizers.l2(weight_decay))(o2)
if increase:
projection = Conv2D(o_filters, kernel_size=(1, 1), strides=(2, 2), padding='same',
kernel_initializer="he_normal",
kernel_regularizer=regularizers.l2(weight_decay))(o1)
block = add([conv_2, projection])
else:
block = add([conv_2, x])
return block
# build model ( total layers = stack_n * 3 * 2 + 2 )
# stack_n = 5 by default, total layers = 32
# input: 32x32x3 output: 32x32x16
x = Conv2D(filters=16, kernel_size=(3, 3), strides=(1, 1), padding='same',
kernel_initializer="he_normal",
kernel_regularizer=regularizers.l2(weight_decay))(img_input)
# input: 32x32x16 output: 32x32x16
for _ in range(stack_n):
x = residual_block(x, 16, False)
# input: 32x32x16 output: 16x16x32
x = residual_block(x, 32, True)
for _ in range(1, stack_n):
x = residual_block(x, 32, False)
# input: 16x16x32 output: 8x8x64
x = residual_block(x, 64, True)
for _ in range(1, stack_n):
x = residual_block(x, 64, False)
x = BatchNormalization(momentum=0.9, epsilon=1e-5)(x)
x = Activation('relu')(x)
x = GlobalAveragePooling2D()(x)
# input: 64 output: 10
x = Dense(classes_num, activation='softmax', kernel_initializer="he_normal",
kernel_regularizer=regularizers.l2(weight_decay))(x)
model = Model(img_input, x)
sgd = optimizers.SGD(lr=.1, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
return model