def block_inception_a(input):
if K.image_dim_ordering() == "th":
channel_axis = 1
else:
channel_axis = -1
branch_0 = conv2d_bn(input, 96, 1, 1)
branch_1 = conv2d_bn(input, 64, 1, 1)
branch_1 = conv2d_bn(branch_1, 96, 3, 3)
branch_2 = conv2d_bn(input, 64, 1, 1)
branch_2 = conv2d_bn(branch_2, 96, 3, 3)
branch_2 = conv2d_bn(branch_2, 96, 3, 3)
branch_3 = AveragePooling2D((3,3), strides=(1,1), border_mode='same')(input)
branch_3 = conv2d_bn(branch_3, 96, 1, 1)
x = merge([branch_0, branch_1, branch_2, branch_3], mode='concat', concat_axis=channel_axis)
return x
python类AveragePooling2D()的实例源码
def block_inception_b(input):
if K.image_dim_ordering() == "th":
channel_axis = 1
else:
channel_axis = -1
branch_0 = conv2d_bn(input, 384, 1, 1)
branch_1 = conv2d_bn(input, 192, 1, 1)
branch_1 = conv2d_bn(branch_1, 224, 1, 7)
branch_1 = conv2d_bn(branch_1, 256, 7, 1)
branch_2 = conv2d_bn(input, 192, 1, 1)
branch_2 = conv2d_bn(branch_2, 192, 7, 1)
branch_2 = conv2d_bn(branch_2, 224, 1, 7)
branch_2 = conv2d_bn(branch_2, 224, 7, 1)
branch_2 = conv2d_bn(branch_2, 256, 1, 7)
branch_3 = AveragePooling2D((3,3), strides=(1,1), border_mode='same')(input)
branch_3 = conv2d_bn(branch_3, 128, 1, 1)
x = merge([branch_0, branch_1, branch_2, branch_3], mode='concat', concat_axis=channel_axis)
return x
def build_model(self):
img_input = Input(shape=(img_channels, img_rows, img_cols))
# one conv at the beginning (spatial size: 32x32)
x = ZeroPadding2D((1, 1))(img_input)
x = Convolution2D(16, nb_row=3, nb_col=3)(x)
# Stage 1 (spatial size: 32x32)
x = bottleneck(x, n, 16, 16 * k, dropout=0.3, subsample=(1, 1))
# Stage 2 (spatial size: 16x16)
x = bottleneck(x, n, 16 * k, 32 * k, dropout=0.3, subsample=(2, 2))
# Stage 3 (spatial size: 8x8)
x = bottleneck(x, n, 32 * k, 64 * k, dropout=0.3, subsample=(2, 2))
x = BatchNormalization(mode=0, axis=1)(x)
x = Activation('relu')(x)
x = AveragePooling2D((8, 8), strides=(1, 1))(x)
x = Flatten()(x)
preds = Dense(nb_classes, activation='softmax')(x)
self.model = Model(input=img_input, output=preds)
self.keras_get_params()
def block_inception_a(input):
if K.image_data_format() == 'channels_first':
channel_axis = 1
else:
channel_axis = -1
branch_0 = conv2d_bn(input, 96, 1, 1)
branch_1 = conv2d_bn(input, 64, 1, 1)
branch_1 = conv2d_bn(branch_1, 96, 3, 3)
branch_2 = conv2d_bn(input, 64, 1, 1)
branch_2 = conv2d_bn(branch_2, 96, 3, 3)
branch_2 = conv2d_bn(branch_2, 96, 3, 3)
branch_3 = AveragePooling2D((3,3), strides=(1,1), padding='same')(input)
branch_3 = conv2d_bn(branch_3, 96, 1, 1)
x = concatenate([branch_0, branch_1, branch_2, branch_3], axis=channel_axis)
return x
def block_inception_b(input):
if K.image_data_format() == 'channels_first':
channel_axis = 1
else:
channel_axis = -1
branch_0 = conv2d_bn(input, 384, 1, 1)
branch_1 = conv2d_bn(input, 192, 1, 1)
branch_1 = conv2d_bn(branch_1, 224, 1, 7)
branch_1 = conv2d_bn(branch_1, 256, 7, 1)
branch_2 = conv2d_bn(input, 192, 1, 1)
branch_2 = conv2d_bn(branch_2, 192, 7, 1)
branch_2 = conv2d_bn(branch_2, 224, 1, 7)
branch_2 = conv2d_bn(branch_2, 224, 7, 1)
branch_2 = conv2d_bn(branch_2, 256, 1, 7)
branch_3 = AveragePooling2D((3,3), strides=(1,1), padding='same')(input)
branch_3 = conv2d_bn(branch_3, 128, 1, 1)
x = concatenate([branch_0, branch_1, branch_2, branch_3], axis=channel_axis)
return x
def inception_A(input):
if K.image_dim_ordering() == "th":
channel_axis = 1
else:
channel_axis = -1
a1 = conv_block(input, 96, 1, 1)
a2 = conv_block(input, 64, 1, 1)
a2 = conv_block(a2, 96, 3, 3)
a3 = conv_block(input, 64, 1, 1)
a3 = conv_block(a3, 96, 3, 3)
a3 = conv_block(a3, 96, 3, 3)
a4 = AveragePooling2D((3, 3), strides=(1, 1), border_mode='same')(input)
a4 = conv_block(a4, 96, 1, 1)
m = merge([a1, a2, a3, a4], mode='concat', concat_axis=channel_axis)
return m
def inception_B(input):
if K.image_dim_ordering() == "th":
channel_axis = 1
else:
channel_axis = -1
b1 = conv_block(input, 384, 1, 1)
b2 = conv_block(input, 192, 1, 1)
b2 = conv_block(b2, 224, 1, 7)
b2 = conv_block(b2, 256, 7, 1)
b3 = conv_block(input, 192, 1, 1)
b3 = conv_block(b3, 192, 7, 1)
b3 = conv_block(b3, 224, 1, 7)
b3 = conv_block(b3, 224, 7, 1)
b3 = conv_block(b3, 256, 1, 7)
b4 = AveragePooling2D((3, 3), strides=(1, 1), border_mode='same')(input)
b4 = conv_block(b4, 128, 1, 1)
m = merge([b1, b2, b3, b4], mode='concat', concat_axis=channel_axis)
return m
def inception_C(input):
if K.image_dim_ordering() == "th":
channel_axis = 1
else:
channel_axis = -1
c1 = conv_block(input, 256, 1, 1)
c2 = conv_block(input, 384, 1, 1)
c2_1 = conv_block(c2, 256, 1, 3)
c2_2 = conv_block(c2, 256, 3, 1)
c2 = merge([c2_1, c2_2], mode='concat', concat_axis=channel_axis)
c3 = conv_block(input, 384, 1, 1)
c3 = conv_block(c3, 448, 3, 1)
c3 = conv_block(c3, 512, 1, 3)
c3_1 = conv_block(c3, 256, 1, 3)
c3_2 = conv_block(c3, 256, 3, 1)
c3 = merge([c3_1, c3_2], mode='concat', concat_axis=channel_axis)
c4 = AveragePooling2D((3, 3), strides=(1, 1), border_mode='same')(input)
c4 = conv_block(c4, 256, 1, 1)
m = merge([c1, c2, c3, c4], mode='concat', concat_axis=channel_axis)
return m
def test_averagepooling_2d():
for border_mode in ['valid', 'same']:
for pool_size in [(2, 2), (3, 3), (4, 4), (5, 5)]:
for strides in [(1, 1), (2, 2)]:
layer_test(convolutional.AveragePooling2D,
kwargs={'strides': strides,
'border_mode': border_mode,
'pool_size': pool_size},
input_shape=(3, 11, 12, 4))
def block_inception_c(input):
if K.image_dim_ordering() == "th":
channel_axis = 1
else:
channel_axis = -1
branch_0 = conv2d_bn(input, 256, 1, 1)
branch_1 = conv2d_bn(input, 384, 1, 1)
branch_10 = conv2d_bn(branch_1, 256, 1, 3)
branch_11 = conv2d_bn(branch_1, 256, 3, 1)
branch_1 = merge([branch_10, branch_11], mode='concat', concat_axis=channel_axis)
branch_2 = conv2d_bn(input, 384, 1, 1)
branch_2 = conv2d_bn(branch_2, 448, 3, 1)
branch_2 = conv2d_bn(branch_2, 512, 1, 3)
branch_20 = conv2d_bn(branch_2, 256, 1, 3)
branch_21 = conv2d_bn(branch_2, 256, 3, 1)
branch_2 = merge([branch_20, branch_21], mode='concat', concat_axis=channel_axis)
branch_3 = AveragePooling2D((3, 3), strides=(1, 1), border_mode='same')(input)
branch_3 = conv2d_bn(branch_3, 256, 1, 1)
x = merge([branch_0, branch_1, branch_2, branch_3], mode='concat', concat_axis=channel_axis)
return x
def pooling_func(x):
if pooltype == 1:
return AveragePooling2D((2, 2), strides=(2, 2))(x)
else:
return MaxPooling2D((2, 2), strides=(2, 2))(x)
# get tensor representations of our images
def pooling_func(x):
if pooltype == 1:
return AveragePooling2D((2, 2), strides=(2, 2))(x)
else:
return MaxPooling2D((2, 2), strides=(2, 2))(x)
# get tensor representations of our images
def test_averagepooling_2d():
for border_mode in ['valid', 'same']:
for pool_size in [(2, 2), (3, 3), (4, 4), (5, 5)]:
for strides in [(1, 1), (2, 2)]:
layer_test(convolutional.AveragePooling2D,
kwargs={'strides': strides,
'border_mode': border_mode,
'pool_size': pool_size},
input_shape=(3, 11, 12, 4))
def pooling_func(x):
if pooltype == 1:
return AveragePooling2D((2, 2), strides=(2, 2))(x)
else:
return MaxPooling2D((2, 2), strides=(2, 2))(x)
# get tensor representations of our images
def block_inception_c(input):
if K.image_data_format() == 'channels_first':
channel_axis = 1
else:
channel_axis = -1
branch_0 = conv2d_bn(input, 256, 1, 1)
branch_1 = conv2d_bn(input, 384, 1, 1)
branch_10 = conv2d_bn(branch_1, 256, 1, 3)
branch_11 = conv2d_bn(branch_1, 256, 3, 1)
branch_1 = concatenate([branch_10, branch_11], axis=channel_axis)
branch_2 = conv2d_bn(input, 384, 1, 1)
branch_2 = conv2d_bn(branch_2, 448, 3, 1)
branch_2 = conv2d_bn(branch_2, 512, 1, 3)
branch_20 = conv2d_bn(branch_2, 256, 1, 3)
branch_21 = conv2d_bn(branch_2, 256, 3, 1)
branch_2 = concatenate([branch_20, branch_21], axis=channel_axis)
branch_3 = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(input)
branch_3 = conv2d_bn(branch_3, 256, 1, 1)
x = concatenate([branch_0, branch_1, branch_2, branch_3], axis=channel_axis)
return x
def test_averagepooling_2d():
for border_mode in ['valid']:
for pool_size in [(2, 2), (3, 3), (4, 4), (5, 5)]:
for strides in [(1, 1), (2, 2)]:
layer_test(convolutional.AveragePooling2D,
kwargs={'strides': strides,
'border_mode': border_mode,
'pool_size': pool_size},
input_shape=(3, 11, 12, 4))
def pooling_func(x):
if pooltype == 1:
return AveragePooling2D((2, 2), strides=(2, 2))(x)
else:
return MaxPooling2D((2, 2), strides=(2, 2))(x)
# get tensor representations of our images
def inception_v4():
'''
Creates the inception v4 network
Args:
num_classes: number of classes
dropout_keep_prob: float, the fraction to keep before final layer.
Returns:
logits: the logits outputs of the model.
'''
# Input Shape is 299 x 299 x 3 (tf) or 3 x 299 x 299 (th)
if K.image_dim_ordering() == 'th':
inputs = Input((3, 299, 299))
else:
inputs = Input((299, 299, 3))
# Make inception base
net = inception_v4_base(inputs)
# Final pooling and prediction
# 8 x 8 x 1536
net = AveragePooling2D((8,8), border_mode='valid')(net)
# 1 x 1 x 1536
net = Flatten()(net)
# 1536
predictions = Dense(output_dim=1001, activation='softmax')(net)
model = Model(inputs, predictions, name='inception_v4')
model.load_weights(TF_WEIGHTS_PATH, by_name=True)
model = pop_layer(model)
# batchnormed = BatchNormalization(axis=3) ()
# dense = Dense(128) (model.layers[-1].output)
# batchnormed = BatchNormalization() (model.layers[-1].output)
# relu = Activation('relu') (batchnormed)
# dropout = Dropout(0.5) (relu)
predictions = Dense(output_dim=1, activation='sigmoid')(model.layers[-1].output)
model = Model(inputs, predictions, name='inception_v4')
for layer in model.layers:
layer.trainable = False
if layer.name == 'merge_25': break
return model
def get_model(input_shape, output_shape, params):
print('compiling model...')
# Dimension of The last Convolutional Feature Map (eg. if input 32x32 and there are 5 conv layers 2x2 fm_size = 27)
fm_size = input_shape[-1] - params['cl']
# Tuple with the pooling size for the last convolutional layer using the params['pf']
pool_siz = (np.round(fm_size*params['pf']).astype(int), np.round(fm_size*params['pf']).astype(int))
# Initialization of the model
model = Sequential()
# Add convolutional layers to model
model.add(Convolution2D(params['k']*get_FeatureMaps(1, params['fp']), 2, 2, init='orthogonal', input_shape=input_shape[1:]))
model.add(LeakyReLU(params['a']))
for i in range(2, params['cl']+1):
model.add(Convolution2D(params['k']*get_FeatureMaps(i, params['fp']), 2, 2, init='orthogonal'))
model.add(LeakyReLU(params['a']))
# Add Pooling and Flatten layers to model
if params['pt'] == 'Avg':
model.add(AveragePooling2D(pool_size=pool_siz))
elif params['pt'] == 'Max':
model.add(MaxPooling2D(pool_size=pool_siz))
else:
sys.exit("Wrong type of Pooling layer")
model.add(Flatten())
model.add(Dropout(params['do']))
# Add Dense layers and Output to model
model.add(Dense(int(params['k']*get_FeatureMaps(params['cl'], params['fp']))/params['pf']*6, init='he_uniform'))
model.add(LeakyReLU(0))
model.add(Dropout(params['do']))
model.add(Dense(int(params['k']*get_FeatureMaps(params['cl'], params['fp']))/params['pf']*2, init='he_uniform'))
model.add(LeakyReLU(0))
model.add(Dropout(params['do']))
model.add(Dense(output_shape[1], init='he_uniform', activation='softmax'))
# Compile model and select optimizer and objective function
if params['opt'] not in ['Adam', 'Adagrad', 'SGD']:
sys.exit('Wrong optimizer: Please select one of the following. Adam, Adagrad, SGD')
if get_Obj(params['obj']) not in ['MSE', 'categorical_crossentropy']:
sys.exit('Wrong Objective: Please select one of the following. MSE, categorical_crossentropy')
model.compile(optimizer=params['opt'], loss=get_Obj(params['obj']))
return model
def inception_v4(num_classes, dropout_keep_prob, weights, include_top):
'''
Creates the inception v4 network
Args:
num_classes: number of classes
dropout_keep_prob: float, the fraction to keep before final layer.
Returns:
logits: the logits outputs of the model.
'''
# Input Shape is 299 x 299 x 3 (tf) or 3 x 299 x 299 (th)
if K.image_data_format() == 'channels_first':
inputs = Input((3, 299, 299))
else:
inputs = Input((299, 299, 3))
# Make inception base
x = inception_v4_base(inputs)
# Final pooling and prediction
if include_top:
# 1 x 1 x 1536
x = AveragePooling2D((8,8), padding='valid')(x)
x = Dropout(dropout_keep_prob)(x)
x = Flatten()(x)
# 1536
x = Dense(units=num_classes, activation='softmax')(x)
model = Model(inputs, x, name='inception_v4')
# load weights
if weights == 'imagenet':
if K.image_data_format() == 'channels_first':
if K.backend() == 'tensorflow':
warnings.warn('You are using the TensorFlow backend, yet you '
'are using the Theano '
'image data format convention '
'(`image_data_format="channels_first"`). '
'For best performance, set '
'`image_data_format="channels_last"` in '
'your Keras config '
'at ~/.keras/keras.json.')
if include_top:
weights_path = get_file(
'inception-v4_weights_tf_dim_ordering_tf_kernels.h5',
WEIGHTS_PATH,
cache_subdir='models',
md5_hash='9fe79d77f793fe874470d84ca6ba4a3b')
else:
weights_path = get_file(
'inception-v4_weights_tf_dim_ordering_tf_kernels_notop.h5',
WEIGHTS_PATH_NO_TOP,
cache_subdir='models',
md5_hash='9296b46b5971573064d12e4669110969')
model.load_weights(weights_path, by_name=True)
return model
def get_deep_anime_model(n_outputs=1000, input_size=128):
'''The deep neural network used for deep anime bot'''
conv = Sequential()
conv.add(Convolution2D(64, 3, 3, activation='relu', input_shape=(3, input_size, input_size)))
conv.add(ZeroPadding2D((1, 1)))
conv.add(Convolution2D(64, 3, 3, activation='relu'))
conv.add(MaxPooling2D((2, 2), strides=(2, 2)))
conv.add(BatchNormalization())
# conv.add(Dropout(0.5))
conv.add(ZeroPadding2D((1, 1)))
conv.add(Convolution2D(128, 3, 3, activation='relu'))
# conv.add(ZeroPadding2D((1, 1)))
conv.add(Convolution2D(128, 1, 1, activation='relu'))
conv.add(MaxPooling2D((2, 2), strides=(2, 2)))
conv.add(BatchNormalization())
# conv.add(Dropout(0.5))
conv.add(ZeroPadding2D((1, 1)))
conv.add(Convolution2D(256, 3, 3, activation='relu'))
conv.add(ZeroPadding2D((1, 1)))
conv.add(Convolution2D(256, 3, 3, activation='relu'))
# conv.add(ZeroPadding2D((1, 1)))
conv.add(Convolution2D(256, 1, 1, activation='relu'))
conv.add(MaxPooling2D((2, 2), strides=(2, 2)))
conv.add(BatchNormalization())
# conv.add(Dropout(0.5))
conv.add(ZeroPadding2D((1, 1)))
conv.add(Convolution2D(512, 3, 3, activation='relu'))
conv.add(ZeroPadding2D((1, 1)))
conv.add(Convolution2D(512, 3, 3, activation='relu'))
# conv.add(ZeroPadding2D((1, 1)))
conv.add(Convolution2D(512, 1, 1, activation='relu'))
conv.add(AveragePooling2D((8, 8), strides=(2, 2)))
conv.add(BatchNormalization())
# conv.add(Dropout(0.5))
# conv.add(ZeroPadding2D((1, 1)))
# conv.add(Convolution2D(512, 3, 3, activation='relu'))
# conv.add(ZeroPadding2D((1, 1)))
# conv.add(Convolution2D(512, 3, 3, activation='relu'))
# #conv.add(ZeroPadding2D((1, 1)))
# conv.add(Convolution2D(512, 1, 1, activation='relu'))
# conv.add(AveragePooling2D((4, 4)))
# conv.add(BatchNormalization())
conv.add(Flatten())
conv.add(Dropout(0.5))
conv.add(Dense(2048))
conv.add(BatchNormalization())
conv.add(Dropout(0.7))
conv.add(Dense(2048))
conv.add(BatchNormalization())
conv.add(Dropout(0.7))
conv.add(Dense(n_outputs))
conv.add(Activation('softmax'))
print(conv.summary())
return conv
def create_inception_resnet_v2(nb_classes=1001, scale=True):
'''
Creates a inception resnet v2 network
:param nb_classes: number of classes.txt
:param scale: flag to add scaling of activations
:return: Keras Model with 1 input (299x299x3) input shape and 2 outputs (final_output, auxiliary_output)
'''
if K.image_dim_ordering() == 'th':
init = Input((3, 299, 299))
else:
init = Input((299, 299, 3))
# Input Shape is 299 x 299 x 3 (tf) or 3 x 299 x 299 (th)
x = inception_resnet_stem(init)
# 10 x Inception Resnet A
for i in range(10):
x = inception_resnet_v2_A(x, scale_residual=scale)
# Reduction A
x = reduction_A(x, k=256, l=256, m=384, n=384)
# 20 x Inception Resnet B
for i in range(20):
x = inception_resnet_v2_B(x, scale_residual=scale)
# Auxiliary tower
aux_out = AveragePooling2D((5, 5), strides=(3, 3))(x)
aux_out = Convolution2D(128, 1, 1, border_mode='same', activation='relu')(aux_out)
aux_out = Convolution2D(768, 5, 5, activation='relu')(aux_out)
aux_out = Flatten()(aux_out)
aux_out = Dense(nb_classes, activation='softmax')(aux_out)
# Reduction Resnet B
x = reduction_resnet_v2_B(x)
# 10 x Inception Resnet C
for i in range(10):
x = inception_resnet_v2_C(x, scale_residual=scale)
# Average Pooling
x = AveragePooling2D((8,8))(x)
# Dropout
x = Dropout(0.8)(x)
x = Flatten()(x)
# Output
out = Dense(output_dim=nb_classes, activation='softmax')(x)
model = Model(init, output=[out, aux_out], name='Inception-Resnet-v2')
return model
def create_inception_resnet_v1(nb_classes=1001, scale=True):
'''
Creates a inception resnet v1 network
:param nb_classes: number of classes.txt
:param scale: flag to add scaling of activations
:return: Keras Model with 1 input (299x299x3) input shape and 2 outputs (final_output, auxiliary_output)
'''
if K.image_dim_ordering() == 'th':
init = Input((3, 299, 299))
else:
init = Input((299, 299, 3))
# Input Shape is 299 x 299 x 3 (tf) or 3 x 299 x 299 (th)
x = inception_resnet_stem(init)
# 5 x Inception Resnet A
for i in range(5):
x = inception_resnet_A(x, scale_residual=scale)
# Reduction A - From Inception v4
x = reduction_A(x, k=192, l=192, m=256, n=384)
# 10 x Inception Resnet B
for i in range(10):
x = inception_resnet_B(x, scale_residual=scale)
# Auxiliary tower
aux_out = AveragePooling2D((5, 5), strides=(3, 3))(x)
aux_out = Convolution2D(128, 1, 1, border_mode='same', activation='relu')(aux_out)
aux_out = Convolution2D(768, 5, 5, activation='relu')(aux_out)
aux_out = Flatten()(aux_out)
aux_out = Dense(nb_classes, activation='softmax')(aux_out)
# Reduction Resnet B
x = reduction_resnet_B(x)
# 5 x Inception Resnet C
for i in range(5):
x = inception_resnet_C(x, scale_residual=scale)
# Average Pooling
x = AveragePooling2D((8,8))(x)
# Dropout
x = Dropout(0.8)(x)
x = Flatten()(x)
# Output
out = Dense(output_dim=nb_classes, activation='softmax')(x)
model = Model(init, output=[out, aux_out], name='Inception-Resnet-v1')
return model
def cifar10_resnet(depth, cifar10model, decay, loss):
# how many layers this is going to create?
# 2 + 6 * depth
model = cifar10model
input = Input(shape=(model.img_rows, model.img_cols, model.img_channels))
# 1 conv + BN + relu
b = Conv2D(filters=32, kernel_size=(model.num_conv, model.num_conv),
kernel_initializer="he_normal", padding="same",
kernel_regularizer=l2(decay), bias_regularizer=l2(0))(input)
b = BatchNormalization(axis=BN_AXIS)(b)
b = Activation("relu")(b)
# 1 res, no striding
b = residual(model, decay, first=True)(b) # 2 layers inside
for _ in np.arange(1, depth): # start from 1 => 2 * depth in total
b = residual(model, decay)(b)
# 2 res, with striding
b = residual(model, decay, more_filters=True)(b)
for _ in np.arange(1, depth):
b = residual(model, decay)(b)
# 3 res, with striding
b = residual(model, decay, more_filters=True)(b)
for _ in np.arange(1, depth):
b = residual(model, decay)(b)
b = BatchNormalization(axis=BN_AXIS)(b)
b = Activation("relu")(b)
b = AveragePooling2D(pool_size=(8, 8), strides=(1, 1),
padding="valid")(b)
out = Flatten()(b)
if loss in yes_softmax:
dense = Dense(units=model.classes, kernel_initializer="he_normal",
activation="softmax",
kernel_regularizer=l2(decay), bias_regularizer=l2(0))(out)
elif loss in yes_bound:
dense = Dense(units=model.classes, kernel_initializer="he_normal",
kernel_regularizer=l2(decay), bias_regularizer=l2(0))(out)
dense = BatchNormalization(axis=BN_AXIS)(dense)
else:
dense = Dense(units=model.classes, kernel_initializer="he_normal",
kernel_regularizer=l2(decay), bias_regularizer=l2(0))(out)
return Model(inputs=input, outputs=dense)
def design_for_residual_blocks(num_channel_input=1):
''''''
model = Sequential() # it's a CONTAINER, not MODEL
# set numbers
num_big_blocks = 3
image_patch_sizes = [[3,3]]*num_big_blocks
pool_sizes = [(2,2)]*num_big_blocks
n_features = [128, 256, 512, 512, 1024]
n_features_next = [256, 512, 512, 512, 1024]
height_input = 32
width_input = 32
for conv_idx in range(num_big_blocks):
n_feat_here = n_features[conv_idx]
# residual block 0
model.add(residual_blocks.building_residual_block( (num_channel_input, height_input, width_input),
n_feat_here,
kernel_sizes=image_patch_sizes[conv_idx]
))
# residual block 1 (you can add it as you want (and your resources allow..))
if False:
model.add(residual_blocks.building_residual_block( (n_feat_here, height_input, width_input),
n_feat_here,
kernel_sizes=image_patch_sizes[conv_idx]
))
# the last residual block N-1
# the last one : pad zeros, subsamples, and increase #channels
pad_height = compute_padding_length(height_input, pool_sizes[conv_idx][0], image_patch_sizes[conv_idx][0])
pad_width = compute_padding_length(width_input, pool_sizes[conv_idx][1], image_patch_sizes[conv_idx][1])
model.add(ZeroPadding2D(padding=(pad_height,pad_width)))
height_input += 2*pad_height
width_input += 2*pad_width
n_feat_next = n_features_next[conv_idx]
model.add(residual_blocks.building_residual_block( (n_feat_here, height_input, width_input),
n_feat_next,
kernel_sizes=image_patch_sizes[conv_idx],
is_subsample=True,
subsample=pool_sizes[conv_idx]
))
height_input, width_input = model.output_shape[2:]
# width_input = int(width_input/pool_sizes[conv_idx][1])
num_channel_input = n_feat_next
# Add average pooling at the end:
print('Average pooling, from (%d,%d) to (1,1)' % (height_input, width_input))
model.add(AveragePooling2D(pool_size=(height_input, width_input)))
return model