Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
- `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.
Returns: a Keras Model
'''
channel_axis = 1 if K.image_data_format() == 'channels_first' else -1
N = list(depth)
# block 1 (initial conv block)
x = Conv2D(initial_conv_filters, (7, 7), padding='same', use_bias=False, strides=(2, 2),
kernel_initializer='he_normal', kernel_regularizer=l2(weight_decay))(img_input)
x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)
# block 2 (projection block)
for i in range(N[0]):
if bottleneck:
x = _resnet_bottleneck_block(x, filters[0], width)
else:
x = _resnet_block(x, filters[0], width)
# block 3 - N
for k in range(1, len(N)):
if bottleneck:
x = _resnet_bottleneck_block(x, filters[k], width, strides=(2, 2))
else:
x = _resnet_block(x, filters[k], width, strides=(2, 2))
for i in range(N[k] - 1):
inputs = Input((img_rows, img_cols, 1))
conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(inputs)
conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(pool1)
conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
conv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(pool2)
conv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv3)
pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
conv4 = Conv2D(256, (3, 3), activation='relu', padding='same')(pool3)
conv4 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv4)
pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)
conv5 = Conv2D(512, (3, 3), activation='relu', padding='same')(pool4)
conv5 = Conv2D(512, (3, 3), activation='relu', padding='same')(conv5)
up6 = concatenate([Conv2DTranspose(256, (2, 2), strides=(2, 2), padding='same')(conv5), conv4], axis=3)
conv6 = Conv2D(256, (3, 3), activation='relu', padding='same')(up6)
conv6 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv6)
up7 = concatenate([Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same')(conv6), conv3], axis=3)
conv7 = Conv2D(128, (3, 3), activation='relu', padding='same')(up7)
conv7 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv7)
up8 = concatenate([Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(conv7), conv2], axis=3)
conv8 = Conv2D(64, (3, 3), activation='relu', padding='same')(up8)
conv8 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv8)
# Block 1
block1_conv1 = Conv2D(64,(3, 3),activation='relu',padding='same')(inputs)
block1_conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(block1_conv1)
block1_pool=MaxPooling2D((2, 2), strides=(2, 2))(block1_conv2)
# Block 2
block2_conv1 = Conv2D(128, (3, 3), activation='relu', padding='same')(block1_pool)
block2_conv2 = Conv2D(128, (3, 3), activation='relu', padding='same')(block2_conv1)
block2_pool = MaxPooling2D((2, 2), strides=(2, 2))(block2_conv2)
# Block 3
block3_conv1 = Conv2D(256, (3, 3), activation='relu', padding='same')(block2_pool)
block3_conv2 = Conv2D(256, (3, 3), activation='relu', padding='same')(block3_conv1)
block3_conv3 = Conv2D(256, (3, 3), activation='relu', padding='same')(block3_conv2)
block3_pool = MaxPooling2D((2, 2), strides=(2, 2))(block3_conv3)
# Block 4
block4_conv1 = Conv2D(512, (3, 3), activation='relu', padding='same')(block3_pool)
block4_conv2 = Conv2D(512, (3, 3), activation='relu', padding='same')(block4_conv1)
block4_conv3 = Conv2D(512, (3, 3), activation='relu', padding='same')(block4_conv2)
block4_pool = MaxPooling2D((2, 2), strides=(2, 2))(block4_conv3)
# Block 5
block5_conv1 = Conv2D(512, (3, 3), activation='relu', padding='same')(block4_pool)
block5_conv2 = Conv2D(512, (3, 3), activation='relu', padding='same')(block5_conv1)
block5_conv3 = Conv2D(512, (3, 3), activation='relu', padding='same')(block5_conv2)
block5_pool = MaxPooling2D((2, 2), strides=(2, 2))(block5_conv3)
# The following blocks were added by qiaozh--------------------------------------------
# Block6
block6_conv = Conv2D(4096,(7,7), activation='relu', padding='same')(block5_pool)
# Encoder
input_tensor = Input(shape=(320, 320, 3))
x = ZeroPadding2D((1, 1))(input_tensor)
x = Conv2D(64, (kernel, kernel), activation='relu', name='conv1_1')(x)
x = ZeroPadding2D((1, 1))(x)
x = Conv2D(64, (kernel, kernel), activation='relu', name='conv1_2')(x)
orig_1 = x
x = MaxPooling2D((2, 2), strides=(2, 2))(x)
x = ZeroPadding2D((1, 1))(x)
x = Conv2D(128, (kernel, kernel), activation='relu', name='conv2_1')(x)
x = ZeroPadding2D((1, 1))(x)
x = Conv2D(128, (kernel, kernel), activation='relu', name='conv2_2')(x)
orig_2 = x
x = MaxPooling2D((2, 2), strides=(2, 2))(x)
x = ZeroPadding2D((1, 1))(x)
x = Conv2D(256, (kernel, kernel), activation='relu', name='conv3_1')(x)
x = ZeroPadding2D((1, 1))(x)
x = Conv2D(256, (kernel, kernel), activation='relu', name='conv3_2')(x)
x = ZeroPadding2D((1, 1))(x)
x = Conv2D(256, (kernel, kernel), activation='relu', name='conv3_3')(x)
orig_3 = x
x = MaxPooling2D((2, 2), strides=(2, 2))(x)
x = ZeroPadding2D((1, 1))(x)
x = Conv2D(512, (kernel, kernel), activation='relu', name='conv4_1')(x)
x = ZeroPadding2D((1, 1))(x)
x = Conv2D(512, (kernel, kernel), activation='relu', name='conv4_2')(x)
x = ZeroPadding2D((1, 1))(x)
x = Conv2D(512, (kernel, kernel), activation='relu', name='conv4_3')(x)
def MCNN_RS(num_PC,img_rows,img_cols):
CNNInput = Input(shape=[num_PC,img_rows,img_cols],name='CNNInput')
CONV1 = Convolution2D(32, 3, 3, activation='relu', border_mode='same', name='CONV1')(CNNInput)
POOL1 = MaxPooling2D((1, 2, 2), strides=(2,2), name='POOL1')(CONV1)
CONV2 = Convolution2D(32, 3, 3, activation='relu', border_mode='same', name='CONV2')(POOL1)
POOL2 = MaxPooling2D((1,2, 2), strides=(2,2), name='POOL2')(CONV2)
CONV3 = Convolution2D(32, 3, 3, activation='relu', border_mode='same', name='CONV3')(POOL2)
POOL3 = MaxPooling2D((1,2,2), strides=(2,2), name='POOL3')(CONV3)
FLATTEN1 = Flatten(name='FLATTEN1')(POOL1)
FLATTEN2 = Flatten(name='FLATTEN2')(POOL2)
FLATTEN3 = Flatten(name='FLATTEN3')(POOL3)
DENSE1 = Dense(128,activation='relu', name='DENSE1')(FLATTEN1)
DENSE2 = Dense(128,activation='relu', name='DENSE2')(FLATTEN2)
DENSE3 = Dense(128,activation='relu', name='DENSE3')(FLATTEN3)
CNNDense = merge([DENSE1, DENSE2, DENSE3], mode='sum', name='CNNDense')
CNNSOFTMAX = Dense(nb_classes,activation='softmax', name='CNNSOFTMAX')(CNNDense)
model = Model(input=[CNNInput], output=[CNNSOFTMAX])
img_input = Input(shape=input_shape)
else:
if not K.is_keras_tensor(input_tensor):
img_input = Input(tensor=input_tensor)
else:
img_input = input_tensor
if K.image_dim_ordering() == 'tf':
bn_axis = 3
else:
bn_axis = 1
x = ZeroPadding2D((3, 3))(img_input)
x = Convolution2D(64, 7, 7, subsample=(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')
inputs = Input((isz, isz, 1))
conv1 = Conv2D(32, (3, 3), activation="relu", padding="same")(inputs)
conv1 = Conv2D(32, (3, 3), activation="relu", padding="same")(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
conv2 = Conv2D(64, (3, 3), activation="relu", padding="same")(pool1)
conv2 = Conv2D(64, (3, 3), activation="relu", padding="same")(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
conv3 = Conv2D(128, (3, 3), activation="relu", padding="same")(pool2)
conv3 = Conv2D(128, (3, 3), activation="relu", padding="same")(conv3)
pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
conv4 = Conv2D(256, (3, 3), activation="relu", padding="same")(pool3)
conv4 = Conv2D(256, (3, 3), activation="relu", padding="same")(conv4)
pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)
conv5 = Conv2D(512, (3, 3), activation="relu", padding="same")(pool4)
conv5 = Conv2D(512, (3, 3), activation="relu", padding="same")(conv5)
up6 = concatenate([UpSampling2D(size=(2, 2))(conv5), conv4], 3)
up6 = Dropout(0.5)(up6)
conv6 = Conv2D(256, (3, 3), activation="relu", padding="same")(up6)
conv6 = Conv2D(256, (3, 3), activation="relu", padding="same")(conv6)
up7 = concatenate([UpSampling2D(size=(2, 2))(conv6), conv3], 3)
up7 = Dropout(0.5)(up7)
conv7 = Conv2D(128, (3, 3), activation="relu", padding="same")(up7)
conv7 = Conv2D(128, (3, 3), activation="relu", padding="same")(conv7)
up8 = concatenate([UpSampling2D(size=(2, 2))(conv7), conv2], 3)
up8 = Dropout(0.5)(up8)
if __name__ == '__main__':
width, height = 10, 20 # standard tetris friends rules
batch_size = 20
epoch_size = 1000
nb_epochs = 100
engine = TetrisEngine(width, height)
reinforcer = TetrisReinforcer(engine)
input = Input(shape=(width, height), dtype='float32')
reshape = Reshape((1, width, height))(input)
conv = Convolution2D(32, 2, 2)(reshape)
dropout = Dropout(0.5)(conv)
maxpool = MaxPooling2D((2, 2))(dropout)
flat = Flatten()(maxpool)
dense = Dense(engine.nb_actions, activation='softmax')(flat)
model = ReinforcementModel(input=[input], output=[dense])
model.compile('sgd')
# try training a regular model
# model_normal = Model(input=[input], output=[output])
# model_normal.compile('sgd', 'mse')
# print(model_normal.predict([np.asarray([engine.board])]))
fit_reinforcement(model, reinforcer, batch_size, epoch_size, nb_epochs)
"""
if train_data is not None:
nfreq=train_data.shape[1]
ntime=train_data.shape[2]
model = Sequential()
# this applies 32 convolution filters of size 5x5 each.
model.add(Conv2D(nfilt1, (5, 5), activation='relu', input_shape=(nfreq, ntime, 1)))
#model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
# Randomly drop some fraction of nodes (set weights to 0)
model.add(Dropout(0.4))
model.add(Conv2D(nfilt2, (5, 5), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.4))
model.add(Flatten())
if features_only is True:
model.add(BatchNormalization()) # hack
return model, []
model.add(Dense(256, activation='relu')) # should be 1024 hack
# model.add(Dense(1024, activation='relu')) # remove for now hack
model.add(Dropout(0.5))
model.add(Dense(2, activation='softmax'))
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='binary_crossentropy', optimizer=sgd, metrics=['accuracy'])
x = ZeroPadding2D((1, 1))(x)
x = Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', name='conv2_1')(x)
x = ZeroPadding2D((1, 1))(x)
x = Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', name='conv2_2')(x)
orig_2 = x
x = MaxPooling2D((2, 2), strides=(2, 2))(x)
x = ZeroPadding2D((1, 1))(x)
x = Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', name='conv3_1')(x)
x = ZeroPadding2D((1, 1))(x)
x = Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', name='conv3_2')(x)
x = ZeroPadding2D((1, 1))(x)
x = Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', name='conv3_3')(x)
orig_3 = x
x = MaxPooling2D((2, 2), strides=(2, 2))(x)
x = ZeroPadding2D((1, 1))(x)
x = Conv2D(512, (3, 3), activation='relu', kernel_initializer='he_normal', name='conv4_1')(x)
x = ZeroPadding2D((1, 1))(x)
x = Conv2D(512, (3, 3), activation='relu', kernel_initializer='he_normal', name='conv4_2')(x)
x = ZeroPadding2D((1, 1))(x)
x = Conv2D(512, (3, 3), activation='relu', kernel_initializer='he_normal', name='conv4_3')(x)
orig_4 = x
x = MaxPooling2D((2, 2), strides=(2, 2))(x)
x = ZeroPadding2D((1, 1))(x)
x = Conv2D(512, (3, 3), activation='relu', kernel_initializer='he_normal', name='conv5_1')(x)
x = ZeroPadding2D((1, 1))(x)
x = Conv2D(512, (3, 3), activation='relu', kernel_initializer='he_normal', name='conv5_2')(x)
x = ZeroPadding2D((1, 1))(x)
x = Conv2D(512, (3, 3), activation='relu', kernel_initializer='he_normal', name='conv5_3')(x)