def test_tiny_mcrnn_recurrent(self):
model = Sequential()
model.add(Conv2D(3,(1,1), input_shape=(2,4,4), padding='same'))
model.add(AveragePooling2D(pool_size=(2,2)))
model.add(Reshape((2,3)))
model.add(LSTM(5, recurrent_activation = 'sigmoid'))
self._test_keras_model(model)
python类AveragePooling2D()的实例源码
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 make_model(x_shape, batch_size=128, num_classes=10):
y = tf.placeholder(dtype=tf.int32,shape=(batch_size,))# Input(dtype=tf.int32, shape=y_shape)
K.set_learning_phase(1)
img_input = Input(batch_shape= tuple( [batch_size] + list(x_shape)))
bn_axis = 3
x = Conv2D(64, (3, 3), strides=(1, 1), padding='same',name='conv1')(img_input)
x = BatchNormalization(axis=bn_axis, name='bn_conv1')(x)
x = Activation('relu')(x)
x = conv_block(x, 3, [16,16,16], stage=2, block='a', strides=(1, 1))
x = identity_block(x, 3, [16,16,16], stage=2, block='b')
x = identity_block(x, 3, [16,16,16], stage=2, block='c')
x = identity_block(x, 3, [16,16,16], stage=3, block='d')
x = conv_block(x, 3, [32,32,32], stage=3, block='a', strides=(2,2))
x = identity_block(x, 3, [32,32,32], stage=3, block='b')
x = identity_block(x, 3, [32,32,32], stage=3, block='c')
x = identity_block(x, 3, [32,32,32], stage=3, block='d')
x = conv_block(x, 3, [64,64,64], stage=3, block='a', strides=(2,2))
x = identity_block(x, 3, [64,64,64], stage=3, block='b')
x = identity_block(x, 3, [64,64,64], stage=3, block='c')
x = identity_block(x, 3, [64,64,64], stage=3, block='d')
x = AveragePooling2D((8, 8), name='avg_pool')(x)
x = Flatten()(x)
x = Dense(num_classes, activation='softmax', name='fc1000')(x)
loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=x,labels=y))
return img_input, y, loss
def __transition_block(ip, nb_filter, compression=1.0, weight_decay=1e-4, block_prefix=None):
'''
Adds a pointwise convolution layer (with batch normalization and relu),
and an average pooling layer. The number of output convolution filters
can be reduced by appropriately reducing the compression parameter.
# Arguments
ip: input keras tensor
nb_filter: integer, the dimensionality of the output space
(i.e. the number output of filters in the convolution)
compression: calculated as 1 - reduction. Reduces the number
of feature maps in the transition block.
weight_decay: weight decay factor
block_prefix: str, for block unique naming
# Input shape
4D tensor with shape:
`(samples, channels, rows, cols)` if data_format='channels_first'
or 4D tensor with shape:
`(samples, rows, cols, channels)` if data_format='channels_last'.
# Output shape
4D tensor with shape:
`(samples, nb_filter * compression, rows / 2, cols / 2)`
if data_format='channels_first'
or 4D tensor with shape:
`(samples, rows / 2, cols / 2, nb_filter * compression)`
if data_format='channels_last'.
# Returns
a keras tensor
'''
with K.name_scope('Transition'):
concat_axis = 1 if K.image_data_format() == 'channels_first' else -1
x = BatchNormalization(axis=concat_axis, epsilon=1.1e-5, name=name_or_none(block_prefix, '_bn'))(ip)
x = Activation('relu')(x)
x = Conv2D(int(nb_filter * compression), (1, 1), kernel_initializer='he_normal', padding='same',
use_bias=False, kernel_regularizer=l2(weight_decay), name=name_or_none(block_prefix, '_conv2D'))(x)
x = AveragePooling2D((2, 2), strides=(2, 2))(x)
return x
def _adjust_block(p, ip, filters, weight_decay=5e-5, id=None):
'''
Adjusts the input `p` to match the shape of the `input`
or situations where the output number of filters needs to
be changed
# Arguments:
p: input tensor which needs to be modified
ip: input tensor whose shape needs to be matched
filters: number of output filters to be matched
weight_decay: l2 regularization weight
id: string id
# Returns:
an adjusted Keras tensor
'''
channel_dim = 1 if K.image_data_format() == 'channels_first' else -1
img_dim = 2 if K.image_data_format() == 'channels_first' else -2
with K.name_scope('adjust_block'):
if p is None:
p = ip
elif p._keras_shape[img_dim] != ip._keras_shape[img_dim]:
with K.name_scope('adjust_reduction_block_%s' % id):
p = Activation('relu', name='adjust_relu_1_%s' % id)(p)
p1 = AveragePooling2D((1, 1), strides=(2, 2), padding='valid', name='adjust_avg_pool_1_%s' % id)(p)
p1 = Conv2D(filters // 2, (1, 1), padding='same', use_bias=False, kernel_regularizer=l2(weight_decay),
name='adjust_conv_1_%s' % id, kernel_initializer='he_normal')(p1)
p2 = ZeroPadding2D(padding=((0, 1), (0, 1)))(p)
p2 = Cropping2D(cropping=((1, 0), (1, 0)))(p2)
p2 = AveragePooling2D((1, 1), strides=(2, 2), padding='valid', name='adjust_avg_pool_2_%s' % id)(p2)
p2 = Conv2D(filters // 2, (1, 1), padding='same', use_bias=False, kernel_regularizer=l2(weight_decay),
name='adjust_conv_2_%s' % id, kernel_initializer='he_normal')(p2)
p = concatenate([p1, p2], axis=channel_dim)
p = BatchNormalization(axis=channel_dim, momentum=_BN_DECAY, epsilon=_BN_EPSILON,
name='adjust_bn_%s' % id)(p)
elif p._keras_shape[channel_dim] != filters:
with K.name_scope('adjust_projection_block_%s' % id):
p = Activation('relu')(p)
p = Conv2D(filters, (1, 1), strides=(1, 1), padding='same', name='adjust_conv_projection_%s' % id,
use_bias=False, kernel_regularizer=l2(weight_decay), kernel_initializer='he_normal')(p)
p = BatchNormalization(axis=channel_dim, momentum=_BN_DECAY, epsilon=_BN_EPSILON,
name='adjust_bn_%s' % id)(p)
return p
def _normal_A(ip, p, filters, weight_decay=5e-5, id=None):
'''Adds a Normal cell for NASNet-A (Fig. 4 in the paper)
# Arguments:
ip: input tensor `x`
p: input tensor `p`
filters: number of output filters
weight_decay: l2 regularization weight
id: string id
# Returns:
a Keras tensor
'''
channel_dim = 1 if K.image_data_format() == 'channels_first' else -1
with K.name_scope('normal_A_block_%s' % id):
p = _adjust_block(p, ip, filters, weight_decay, id)
h = Activation('relu')(ip)
h = Conv2D(filters, (1, 1), strides=(1, 1), padding='same', name='normal_conv_1_%s' % id,
use_bias=False, kernel_initializer='he_normal', kernel_regularizer=l2(weight_decay))(h)
h = BatchNormalization(axis=channel_dim, momentum=_BN_DECAY, epsilon=_BN_EPSILON,
name='normal_bn_1_%s' % id)(h)
with K.name_scope('block_1'):
x1_1 = _separable_conv_block(h, filters, kernel_size=(5, 5), weight_decay=weight_decay,
id='normal_left1_%s' % id)
x1_2 = _separable_conv_block(p, filters, weight_decay=weight_decay, id='normal_right1_%s' % id)
x1 = add([x1_1, x1_2], name='normal_add_1_%s' % id)
with K.name_scope('block_2'):
x2_1 = _separable_conv_block(p, filters, (5, 5), weight_decay=weight_decay, id='normal_left2_%s' % id)
x2_2 = _separable_conv_block(p, filters, (3, 3), weight_decay=weight_decay, id='normal_right2_%s' % id)
x2 = add([x2_1, x2_2], name='normal_add_2_%s' % id)
with K.name_scope('block_3'):
x3 = AveragePooling2D((3, 3), strides=(1, 1), padding='same', name='normal_left3_%s' % (id))(h)
x3 = add([x3, p], name='normal_add_3_%s' % id)
with K.name_scope('block_4'):
x4_1 = AveragePooling2D((3, 3), strides=(1, 1), padding='same', name='normal_left4_%s' % (id))(p)
x4_2 = AveragePooling2D((3, 3), strides=(1, 1), padding='same', name='normal_right4_%s' % (id))(p)
x4 = add([x4_1, x4_2], name='normal_add_4_%s' % id)
with K.name_scope('block_5'):
x5 = _separable_conv_block(h, filters, weight_decay=weight_decay, id='normal_left5_%s' % id)
x5 = add([x5, h], name='normal_add_5_%s' % id)
x = concatenate([p, x1, x2, x3, x4, x5], axis=channel_dim, name='normal_concat_%s' % id)
return x, ip
def _reduction_A(ip, p, filters, weight_decay=5e-5, id=None):
'''Adds a Reduction cell for NASNet-A (Fig. 4 in the paper)
# Arguments:
ip: input tensor `x`
p: input tensor `p`
filters: number of output filters
weight_decay: l2 regularization weight
id: string id
# Returns:
a Keras tensor
'''
""""""
channel_dim = 1 if K.image_data_format() == 'channels_first' else -1
with K.name_scope('reduction_A_block_%s' % id):
p = _adjust_block(p, ip, filters, weight_decay, id)
h = Activation('relu')(ip)
h = Conv2D(filters, (1, 1), strides=(1, 1), padding='same', name='reduction_conv_1_%s' % id,
use_bias=False, kernel_initializer='he_normal', kernel_regularizer=l2(weight_decay))(h)
h = BatchNormalization(axis=channel_dim, momentum=_BN_DECAY, epsilon=_BN_EPSILON,
name='reduction_bn_1_%s' % id)(h)
with K.name_scope('block_1'):
x1_1 = _separable_conv_block(h, filters, (5, 5), strides=(2, 2), weight_decay=weight_decay,
id='reduction_left1_%s' % id)
x1_2 = _separable_conv_block(p, filters, (7, 7), strides=(2, 2), weight_decay=weight_decay,
id='reduction_1_%s' % id)
x1 = add([x1_1, x1_2], name='reduction_add_1_%s' % id)
with K.name_scope('block_2'):
x2_1 = MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='reduction_left2_%s' % id)(h)
x2_2 = _separable_conv_block(p, filters, (7, 7), strides=(2, 2), weight_decay=weight_decay,
id='reduction_right2_%s' % id)
x2 = add([x2_1, x2_2], name='reduction_add_2_%s' % id)
with K.name_scope('block_3'):
x3_1 = AveragePooling2D((3, 3), strides=(2, 2), padding='same', name='reduction_left3_%s' % id)(h)
x3_2 = _separable_conv_block(p, filters, (5, 5), strides=(2, 2), weight_decay=weight_decay,
id='reduction_right3_%s' % id)
x3 = add([x3_1, x3_2], name='reduction_add3_%s' % id)
with K.name_scope('block_4'):
x4 = AveragePooling2D((3, 3), strides=(1, 1), padding='same', name='reduction_left4_%s' % id)(x1)
x4 = add([x2, x4])
with K.name_scope('block_5'):
x5_1 = _separable_conv_block(x1, filters, (3, 3), weight_decay=weight_decay, id='reduction_left4_%s' % id)
x5_2 = MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='reduction_right5_%s' % id)(h)
x5 = add([x5_1, x5_2], name='reduction_add4_%s' % id)
x = concatenate([x2, x3, x4, x5], axis=channel_dim, name='reduction_concat_%s' % id)
return x, ip
def keras_nn(X_train, y_train):
"""
Constructs a neural network using keras and trains it.
The best final networks is saved in model.json and model.h5
Parameters
----------
X_train : numpy array
The images
y_train : numpy array
The angles
"""
model = Sequential()
# Further reduces the dimension of the image to 8x16
model.add(AveragePooling2D((2, 2), border_mode='valid', input_shape=(16, 32, 1)))
# Applies 2x2 convolution
model.add(Convolution2D(1, 2, 2, subsample=(1, 1)))
model.add(ELU())
# Max Pooling to reduce the dimensions. 2X4 used because it matches the aspect ratio of the input
model.add(MaxPooling2D((2, 4), border_mode='valid'))
# Droput - We only have 10 connections at this point, but it still improves performance. However it should be kept low, e.g. 0.5 doesn't work
model.add(Dropout(0.25))
model.add(Flatten())
# The final layer - outputs a float number (the steering angle)
model.add(Dense(1)) #
# Show a summary of the neural network
model.summary()
# Save the best model by validation mean squared error
checkpoint = ModelCheckpoint("model.h5", monitor='val_mean_squared_error', verbose=1, save_best_only=True, mode='min')
# Stop training when there is no improvment.
# This is to speed up training, the accuracy is not affected, because the checkpoint will pick-up the best model anyway
early_stop = EarlyStopping(monitor='val_mean_squared_error', min_delta=0.0001, patience=4, verbose=1, mode='min')
# Compile the model with Adam optimizer and monitor mean squared error
model.compile('adam', 'mean_squared_error', ['mean_squared_error'])
# Save the model to JSON
model_json = model.to_json()
with open("model.json", "w") as model_file:
model_file.write(model_json)
# Start training.
# nb_epoch should be a big number, there is early stopping callback anyway
# Data is split by keras to training and validation
history = model.fit(X_train, y_train, batch_size=32, nb_epoch=150, verbose=1, callbacks=[checkpoint, early_stop], validation_split=0.2, shuffle=True)
def densenet(img_input,classes_num):
def bn_relu(x):
x = BatchNormalization()(x)
x = Activation('relu')(x)
return x
def bottleneck(x):
channels = growth_rate * 4
x = bn_relu(x)
x = Conv2D(channels,kernel_size=(1,1),strides=(1,1),padding='same',kernel_initializer=he_normal(),kernel_regularizer=regularizers.l2(weight_decay),use_bias=False)(x)
x = bn_relu(x)
x = Conv2D(growth_rate,kernel_size=(3,3),strides=(1,1),padding='same',kernel_initializer=he_normal(),kernel_regularizer=regularizers.l2(weight_decay),use_bias=False)(x)
return x
def single(x):
x = bn_relu(x)
x = Conv2D(growth_rate,kernel_size=(3,3),strides=(1,1),padding='same',kernel_initializer=he_normal(),kernel_regularizer=regularizers.l2(weight_decay),use_bias=False)(x)
return x
def transition(x, inchannels):
outchannels = int(inchannels * compression)
x = bn_relu(x)
x = Conv2D(outchannels,kernel_size=(1,1),strides=(1,1),padding='same',kernel_initializer=he_normal(),kernel_regularizer=regularizers.l2(weight_decay),use_bias=False)(x)
x = AveragePooling2D((2,2), strides=(2, 2))(x)
return x, outchannels
def dense_block(x,blocks,nchannels):
concat = x
for i in range(blocks):
x = bottleneck(concat)
concat = concatenate([x,concat], axis=-1)
nchannels += growth_rate
return concat, nchannels
def dense_layer(x):
return Dense(classes_num,activation='softmax',kernel_initializer=he_normal(),kernel_regularizer=regularizers.l2(weight_decay))(x)
nblocks = (depth - 4) // 6
nchannels = growth_rate * 2
x = Conv2D(nchannels,kernel_size=(3,3),strides=(1,1),padding='same',kernel_initializer=he_normal(),kernel_regularizer=regularizers.l2(weight_decay),use_bias=False)(img_input)
x, nchannels = dense_block(x,nblocks,nchannels)
x, nchannels = transition(x,nchannels)
x, nchannels = dense_block(x,nblocks,nchannels)
x, nchannels = transition(x,nchannels)
x, nchannels = dense_block(x,nblocks,nchannels)
x, nchannels = transition(x,nchannels)
x = bn_relu(x)
x = GlobalAveragePooling2D()(x)
x = dense_layer(x)
return x
def densenet(img_input,classes_num):
def bn_relu(x):
x = BatchNormalization()(x)
x = Activation('relu')(x)
return x
def bottleneck(x):
channels = growth_rate * 4
x = bn_relu(x)
x = Conv2D(channels,kernel_size=(1,1),strides=(1,1),padding='same',kernel_initializer=he_normal(),kernel_regularizer=regularizers.l2(weight_decay),use_bias=False)(x)
x = bn_relu(x)
x = Conv2D(growth_rate,kernel_size=(3,3),strides=(1,1),padding='same',kernel_initializer=he_normal(),kernel_regularizer=regularizers.l2(weight_decay),use_bias=False)(x)
return x
def single(x):
x = bn_relu(x)
x = Conv2D(growth_rate,kernel_size=(3,3),strides=(1,1),padding='same',kernel_initializer=he_normal(),kernel_regularizer=regularizers.l2(weight_decay),use_bias=False)(x)
return x
def transition(x, inchannels):
outchannels = int(inchannels * compression)
x = bn_relu(x)
x = Conv2D(outchannels,kernel_size=(1,1),strides=(1,1),padding='same',kernel_initializer=he_normal(),kernel_regularizer=regularizers.l2(weight_decay),use_bias=False)(x)
x = AveragePooling2D((2,2), strides=(2, 2))(x)
return x, outchannels
def dense_block(x,blocks,nchannels):
concat = x
for i in range(blocks):
x = bottleneck(concat)
concat = concatenate([x,concat], axis=-1)
nchannels += growth_rate
return concat, nchannels
def dense_layer(x):
return Dense(classes_num,activation='softmax',kernel_initializer=he_normal(),kernel_regularizer=regularizers.l2(weight_decay))(x)
nblocks = (depth - 4) // 6
nchannels = growth_rate * 2
x = Conv2D(nchannels,kernel_size=(3,3),strides=(1,1),padding='same',kernel_initializer=he_normal(),kernel_regularizer=regularizers.l2(weight_decay),use_bias=False)(img_input)
x, nchannels = dense_block(x,nblocks,nchannels)
x, nchannels = transition(x,nchannels)
x, nchannels = dense_block(x,nblocks,nchannels)
x, nchannels = transition(x,nchannels)
x, nchannels = dense_block(x,nblocks,nchannels)
x, nchannels = transition(x,nchannels)
x = bn_relu(x)
x = GlobalAveragePooling2D()(x)
x = dense_layer(x)
return x
def build_model():
model = Sequential()
# ???????4????????????5*5?1??????????,????1??
model.add(Convolution2D(4, 5, 5, border_mode='valid', dim_ordering='th', input_shape=(1, 20, 20)))
model.add(ZeroPadding2D((1, 1)))
model.add(BatchNormalization())
model.add(Activation('tanh'))
# ???????8????????????3*3?4??????????????????????
model.add(GaussianNoise(0.001))
model.add(UpSampling2D(size=(2, 2), dim_ordering='th'))
model.add(AtrousConvolution2D(16, 3, 3, border_mode='valid', dim_ordering='th'))
model.add(ZeroPadding2D((1, 1)))
model.add(BatchNormalization())
model.add(Activation('tanh'))
# model.add(MaxPooling2D(pool_size=(2, 2), dim_ordering='th'))
model.add(AveragePooling2D(pool_size=(2, 2), dim_ordering='th'))
model.add(Activation('tanh'))
# ???????16????????????4*4
model.add(AtrousConvolution2D(8, 3, 3, border_mode='valid', dim_ordering='th'))
model.add(ZeroPadding2D((1, 1)))
model.add(BatchNormalization())
model.add(Activation('linear'))
# ???????16????????????4*4
model.add(GaussianNoise(0.002))
model.add(AtrousConvolution2D(4, 3, 3, border_mode='valid', dim_ordering='th'))
model.add(ZeroPadding2D((1, 1)))
model.add(BatchNormalization())
model.add(Activation('tanh'))
model.add(Dropout(0.2))
# model.add(MaxPooling2D(pool_size=(2, 2), dim_ordering='th'))
model.add(AveragePooling2D(pool_size=(2, 2), dim_ordering='th'))
model.add(Activation('tanh'))
# ??????????????????flatten????
model.add(Flatten())
model.add(Dense(8))
model.add(BatchNormalization())
model.add(Activation('tanh'))
model.add(Dense(1))
model.add(Activation('linear'))
start = time.time()
# ??SGD + momentum
# model.compile????loss??????(????)
# sgd = SGD(lr=0.05, decay=1e-6, momentum=0.9, nesterov=True)
# model.compile(loss="mse", optimizer=sgd)
model.compile(loss="mse", optimizer="rmsprop") # mse kld # Nadam rmsprop
print "Compilation Time : ", time.time() - start
return model
archs.py 文件源码
项目:kaggle-dstl-satellite-imagery-feature-detection
作者: alno
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def rnet1(input_shapes, n_classes):
def conv(size, x):
x = Convolution2D(size, 3, 3, border_mode='same', init='he_normal', bias=False)(x)
x = BatchNormalization(axis=1, mode=0)(x)
x = PReLU(shared_axes=[2, 3])(x)
return x
def unet_block(sizes, inp):
x = inp
skips = []
for sz in sizes[:-1]:
x = conv(sz, x)
skips.append(x)
x = MaxPooling2D((2, 2))(x)
x = conv(sizes[-1], x)
for sz in reversed(sizes[:-1]):
x = conv(sz, merge([UpSampling2D((2, 2))(x), skips.pop()], mode='concat', concat_axis=1))
return x
def fcn_block(sizes, inp):
x = inp
for sz in sizes:
x = conv(sz, x)
return Dropout(0.2)(x)
# Build piramid of inputs
inp0 = Input(input_shapes['in'], name='in')
inp1 = AveragePooling2D((2, 2))(inp0)
inp2 = AveragePooling2D((2, 2))(inp1)
# Build outputs in resnet fashion
out2 = unet_block([32, 48], inp2)
#out2 = merge([unet_block([32, 48, 32], merge([inp2, out2], mode='concat', concat_axis=1)), out2], mode='sum')
out1 = UpSampling2D((2, 2))(out2)
#out1 = merge([unet_block([32, 32, 48], merge([inp1, out1], mode='concat', concat_axis=1)), out1], mode='sum')
out1 = merge([unet_block([32, 48, 64], merge([inp1, out1], mode='concat', concat_axis=1)), out1], mode='sum')
out0 = UpSampling2D((2, 2))(out1)
out0 = merge([unet_block([32, 48, 64], merge([inp0, out0], mode='concat', concat_axis=1)), out0], mode='sum')
out0 = merge([unet_block([32, 48, 64], merge([inp0, out0], mode='concat', concat_axis=1)), out0], mode='sum')
# Final convolution
out = Convolution2D(n_classes, 1, 1, activation='sigmoid')(out0)
return Model(input=inp0, output=out)
archs.py 文件源码
项目:kaggle-dstl-satellite-imagery-feature-detection
作者: alno
项目源码
文件源码
阅读 21
收藏 0
点赞 0
评论 0
def rnet1_mi(input_shapes, n_classes):
def conv(size, x):
x = Convolution2D(size, 3, 3, border_mode='same', init='he_normal', bias=False)(x)
x = BatchNormalization(axis=1, mode=0)(x)
x = PReLU(shared_axes=[2, 3])(x)
return x
def unet_block(sizes, inp):
x = inp
skips = []
for sz in sizes[:-1]:
x = conv(sz, x)
skips.append(x)
x = MaxPooling2D((2, 2))(x)
x = conv(sizes[-1], x)
for sz in reversed(sizes[:-1]):
x = conv(sz, merge([UpSampling2D((2, 2))(x), skips.pop()], mode='concat', concat_axis=1))
return x
def radd(out, inp, block):
block_in = merge([inp, out], mode='concat', concat_axis=1)
block_out = block(block_in)
return merge([block_out, out], mode='sum')
in_I = Input(input_shapes['in_I'], name='in_I')
in_M = Input(input_shapes['in_M'], name='in_M')
# Build piramid of inputs
inp0 = in_I
inp1 = AveragePooling2D((2, 2))(inp0)
inp2 = merge([AveragePooling2D((2, 2))(inp1), in_M], mode='concat', concat_axis=1)
inp3 = AveragePooling2D((2, 2))(inp2)
# Build outputs in resnet fashion
out3 = unet_block([32, 48], inp3)
out2 = UpSampling2D((2, 2))(out3)
out2 = radd(out2, inp2, lambda x: unet_block([32, 48], x))
out1 = UpSampling2D((2, 2))(out2)
out1 = radd(out1, inp1, lambda x: unet_block([32, 48], x))
out1 = radd(out1, inp1, lambda x: unet_block([32, 48, 64], x))
out0 = UpSampling2D((2, 2))(out1)
out0 = radd(out0, inp0, lambda x: unet_block([32, 48], x))
out0 = radd(out0, inp0, lambda x: unet_block([32, 48, 64], x))
# Final convolution
out = Convolution2D(n_classes, 1, 1, activation='sigmoid')(out0)
return Model(input=[in_I, in_M], output=out)
def define_model(weight_path = None):
input = Input(shape=(224, 224, 3))
conv1_7x7_s2 = Conv2D(filters=64, kernel_size=(7, 7), strides=(2, 2), padding='same', activation='relu', kernel_regularizer=l2(0.01))(input)
maxpool1_3x3_s2 = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same')(conv1_7x7_s2)
conv2_3x3_reduce = Conv2D(filters=64, kernel_size=(1, 1), padding='same', activation='relu', kernel_regularizer=l2(0.01))(maxpool1_3x3_s2)
conv2_3x3 = Conv2D(filters=192, kernel_size=(3, 3), padding='same', activation='relu', kernel_regularizer=l2(0.01))(conv2_3x3_reduce)
maxpool2_3x3_s2 = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same')(conv2_3x3)
inception_3a = inception_model(input=maxpool2_3x3_s2, filters_1x1=64, filters_3x3_reduce=96, filters_3x3=128, filters_5x5_reduce=16, filters_5x5=32, filters_pool_proj=32)
inception_3b = inception_model(input=inception_3a, filters_1x1=128, filters_3x3_reduce=128, filters_3x3=192, filters_5x5_reduce=32, filters_5x5=96, filters_pool_proj=64)
maxpool3_3x3_s2 = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same')(inception_3b)
inception_4a = inception_model(input=maxpool3_3x3_s2, filters_1x1=192, filters_3x3_reduce=96, filters_3x3=208, filters_5x5_reduce=16, filters_5x5=48, filters_pool_proj=64)
inception_4b = inception_model(input=inception_4a, filters_1x1=160, filters_3x3_reduce=112, filters_3x3=224, filters_5x5_reduce=24, filters_5x5=64, filters_pool_proj=64)
inception_4c = inception_model(input=inception_4b, filters_1x1=128, filters_3x3_reduce=128, filters_3x3=256, filters_5x5_reduce=24, filters_5x5=64, filters_pool_proj=64)
inception_4d = inception_model(input=inception_4c, filters_1x1=112, filters_3x3_reduce=144, filters_3x3=288, filters_5x5_reduce=32, filters_5x5=64, filters_pool_proj=64)
inception_4e = inception_model(input=inception_4d, filters_1x1=256, filters_3x3_reduce=160, filters_3x3=320, filters_5x5_reduce=32, filters_5x5=128, filters_pool_proj=128)
maxpool4_3x3_s2 = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same')(inception_4e)
inception_5a = inception_model(input=maxpool4_3x3_s2, filters_1x1=256, filters_3x3_reduce=160, filters_3x3=320, filters_5x5_reduce=32, filters_5x5=128, filters_pool_proj=128)
inception_5b = inception_model(input=inception_5a, filters_1x1=384, filters_3x3_reduce=192, filters_3x3=384, filters_5x5_reduce=48, filters_5x5=128, filters_pool_proj=128)
averagepool1_7x7_s1 = AveragePooling2D(pool_size=(7, 7), strides=(7, 7), padding='same')(inception_5b)
drop1 = Dropout(rate=0.4)(averagepool1_7x7_s1)
linear = Dense(units=1000, activation='softmax', kernel_regularizer=l2(0.01))(keras.layers.core.Flatten(drop1))
last = linear
model = Model(inputs=input, outputs=last)
model.summary()
def ecog_1d_model(channels=None, weights=None):
input_tensor = Input(shape=(1, channels, 1000))
# Block 1
x = AveragePooling2D((1, 5), name='pre_pool')(input_tensor)
x = Convolution2D(4, 1, 3, border_mode='same', name='block1_conv1')(x)
# x = BatchNormalization(axis=1)(x)
x = Activation('relu')(x)
x = MaxPooling2D((1, 3), name='block1_pool')(x)
# Block 2
x = Convolution2D(8, 1, 3, border_mode='same', name='block2_conv1')(x)
# x = BatchNormalization(axis=1)(x)
x = Activation('relu')(x)
x = MaxPooling2D((1, 3), name='block2_pool')(x)
# Block 3
x = Convolution2D(16, 1, 3, border_mode='same', name='block3_conv1')(x)
# x = BatchNormalization(axis=1)(x)
x = Activation('relu')(x)
x = MaxPooling2D((1, 2), name='block3_pool')(x)
# Block 4
# x = Convolution2D(32, 1, 3, border_mode='same', name='block4_conv1')(x)
# x = BatchNormalization(axis=1)(x)
# x = Activation('relu')(x)
# x = MaxPooling2D((1, 2), name='block4_pool')(x)
x = Flatten(name='flatten')(x)
x = Dropout(0.5)(x)
x = Dense(64, W_regularizer=l2(0.01), name='fc1')(x)
#x = BatchNormalization()(x)
#x = Activation('relu')(x)
#x = Dropout(0.5)(x)
#x = Dense(1, name='predictions')(x)
# x = BatchNormalization()(x)
predictions = Activation('sigmoid')(x)
# for layer in base_model.layers[:10]:
# layer.trainable = False
model = Model(input=input_tensor, output=predictions)
if weights is not None:
model.load_weights(weights)
return model
def CarND(input_shape, crop_shape):
model = Sequential()
# Crop
# model.add(Cropping2D(((80,20),(1,1)), input_shape=input_shape, name="Crop"))
model.add(Cropping2D(crop_shape, input_shape=input_shape, name="Crop"))
# Resize
model.add(AveragePooling2D(pool_size=(1,4), name="Resize", trainable=False))
# Normalize input.
model.add(BatchNormalization(axis=1, name="Normalize"))
# Reduce dimensions through trainable convolution, activation, and
# pooling layers.
model.add(Convolution2D(24, 3, 3, subsample=(2,2), name="Convolution2D1", activation="relu"))
model.add(MaxPooling2D(name="MaxPool1"))
model.add(Convolution2D(36, 3, 3, subsample=(1,1), name="Convolution2D2", activation="relu"))
model.add(MaxPooling2D(name="MaxPool2"))
model.add(Convolution2D(48, 3, 3, subsample=(1,1), name="Convolution2D3", activation="relu"))
model.add(MaxPooling2D(name="MaxPool3"))
# Dropout for regularization
model.add(Dropout(0.1, name="Dropout"))
# Flatten input in a non-trainable layer before feeding into
# fully-connected layers.
model.add(Flatten(name="Flatten"))
# Model steering through trainable layers comprising dense units
# as ell as dropout units for regularization.
model.add(Dense(100, activation="relu", name="FC2"))
model.add(Dense(50, activation="relu", name="FC3"))
model.add(Dense(10, activation="relu", name="FC4"))
# Generate output (steering angles) with a single non-trainable
# node.
model.add(Dense(1, name="Readout", trainable=False))
return model
# #+RESULTS:
# Here is a summary of the actual model, as generated directly by
# =model.summary= in Keras.
def build_model(self):
from resnet50 import identity_block, conv_block
from keras.layers import Input
from keras import layers
from keras.layers import Dense
from keras.layers import Activation
from keras.layers import Flatten
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import GlobalMaxPooling2D
from keras.layers import ZeroPadding2D
from keras.layers import AveragePooling2D
from keras.layers import GlobalAveragePooling2D
from keras.layers import BatchNormalization
from keras.models import Model
from keras.preprocessing import image
import keras.backend as K
from keras.utils import layer_utils
x = ZeroPadding2D((3, 3))(input_shape=self.X[0].shape)
x = Conv2D(64, (7, 7), strides=(2, 2), name='conv1')(x)
x = BatchNormalization(axis=bn_axis, name='bn_conv1')(x)
x = Activation('relu')(x)
x = MaxPooling2D((3, 3), strides=(2, 2))(x)
x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1))
x = identity_block(x, 3, [64, 64, 256], stage=2, block='b')
x = identity_block(x, 3, [64, 64, 256], stage=2, block='c')
x = conv_block(x, 3, [128, 128, 512], stage=3, block='a')
x = identity_block(x, 3, [128, 128, 512], stage=3, block='b')
x = identity_block(x, 3, [128, 128, 512], stage=3, block='c')
x = identity_block(x, 3, [128, 128, 512], stage=3, block='d')
x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a')
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b')
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c')
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d')
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e')
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f')
x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a')
x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b')
x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c')
x = AveragePooling2D((7, 7), name='avg_pool')(x)
x = Flatten()(x)
x = Dense(2, activation='softmax', name = 'fc1000')(x)
self.model = Model(inputs, x, name='resnet50')
def pooling(layer, layer_in, layerId):
poolMap = {
('1D', 'MAX'): MaxPooling1D,
('2D', 'MAX'): MaxPooling2D,
('3D', 'MAX'): MaxPooling3D,
('1D', 'AVE'): AveragePooling1D,
('2D', 'AVE'): AveragePooling2D,
('3D', 'AVE'): AveragePooling3D,
}
out = {}
layer_type = layer['params']['layer_type']
pool_type = layer['params']['pool']
padding = get_padding(layer)
if (layer_type == '1D'):
strides = layer['params']['stride_w']
kernel = layer['params']['kernel_w']
if (padding == 'custom'):
p_w = layer['params']['pad_w']
out[layerId + 'Pad'] = ZeroPadding1D(padding=p_w)(*layer_in)
padding = 'valid'
layer_in = [out[layerId + 'Pad']]
elif (layer_type == '2D'):
strides = (layer['params']['stride_h'], layer['params']['stride_w'])
kernel = (layer['params']['kernel_h'], layer['params']['kernel_w'])
if (padding == 'custom'):
p_h, p_w = layer['params']['pad_h'], layer['params']['pad_w']
out[layerId + 'Pad'] = ZeroPadding2D(padding=(p_h, p_w))(*layer_in)
padding = 'valid'
layer_in = [out[layerId + 'Pad']]
else:
strides = (layer['params']['stride_h'], layer['params']['stride_w'],
layer['params']['stride_d'])
kernel = (layer['params']['kernel_h'], layer['params']['kernel_w'],
layer['params']['kernel_d'])
if (padding == 'custom'):
p_h, p_w, p_d = layer['params']['pad_h'], layer['params']['pad_w'],\
layer['params']['pad_d']
out[layerId + 'Pad'] = ZeroPadding3D(padding=(p_h, p_w, p_d))(*layer_in)
padding = 'valid'
layer_in = [out[layerId + 'Pad']]
out[layerId] = poolMap[(layer_type, pool_type)](pool_size=kernel, strides=strides, padding=padding)(
*layer_in)
return out
# ********** Locally-connected Layers **********