我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用keras.layers.convolutional.Convolution2D()。
def make_generator(): """Creates a generator model that takes a 100-dimensional noise vector as a "seed", and outputs images of size 28x28x1.""" model = Sequential() model.add(Dense(1024, input_dim=100)) model.add(LeakyReLU()) model.add(Dense(128 * 7 * 7)) model.add(BatchNormalization()) model.add(LeakyReLU()) if K.image_data_format() == 'channels_first': model.add(Reshape((128, 7, 7), input_shape=(128 * 7 * 7,))) bn_axis = 1 else: model.add(Reshape((7, 7, 128), input_shape=(128 * 7 * 7,))) bn_axis = -1 model.add(Conv2DTranspose(128, (5, 5), strides=2, padding='same')) model.add(BatchNormalization(axis=bn_axis)) model.add(LeakyReLU()) model.add(Convolution2D(64, (5, 5), padding='same')) model.add(BatchNormalization(axis=bn_axis)) model.add(LeakyReLU()) model.add(Conv2DTranspose(64, (5, 5), strides=2, padding='same')) model.add(BatchNormalization(axis=bn_axis)) model.add(LeakyReLU()) # Because we normalized training inputs to lie in the range [-1, 1], # the tanh function should be used for the output of the generator to ensure its output # also lies in this range. model.add(Convolution2D(1, (5, 5), padding='same', activation='tanh')) return model
def main(): game_width = 12 game_height = 9 nb_frames = 4 actions = ((-1, 0), (1, 0), (0, -1), (0, 1), (0, 0)) # Recipe of deep reinforcement learning model model = Sequential() model.add(Convolution2D( 16, nb_row=3, nb_col=3, activation='relu', input_shape=(nb_frames, game_height, game_width))) model.add(Convolution2D(32, nb_row=3, nb_col=3, activation='relu')) model.add(Flatten()) model.add(Dense(256, activation='relu')) model.add(Dense(len(actions))) model.compile(RMSprop(), 'MSE') agent = Agent( model, nb_frames, snake_game, actions, size=(game_width, game_height)) agent.train(nb_epochs=10000, batch_size=64, gamma=0.8, save_model=True) agent.play(nb_rounds=10)
def discriminator_model(): model = Sequential() model.add(Convolution2D(64,5,5, border_mode='same', input_shape=(1,28,28), dim_ordering="th")) model.add(Activation('tanh')) model.add(MaxPooling2D(pool_size=(2,2), dim_ordering="th")) model.add(Convolution2D(128,5,5, border_mode='same', dim_ordering="th")) model.add(Activation('tanh')) model.add(MaxPooling2D(pool_size=(2,2), dim_ordering="th")) model.add(Flatten()) model.add(Dense(1024)) model.add(Activation('tanh')) model.add(Dense(1)) model.add(Activation('sigmoid')) return model
def discriminator_model(): """ return a (b, 1) logits""" model = Sequential() model.add(Convolution2D(64, 4, 4,border_mode='same',input_shape=(IN_CH*2, img_cols, img_rows))) model.add(BatchNormalization(mode=2)) model.add(Activation('tanh')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Convolution2D(128, 4, 4,border_mode='same')) model.add(BatchNormalization(mode=2)) model.add(Activation('tanh')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Convolution2D(512, 4, 4,border_mode='same')) model.add(BatchNormalization(mode=2)) model.add(Activation('tanh')) model.add(Convolution2D(1, 4, 4,border_mode='same')) model.add(BatchNormalization(mode=2)) model.add(Activation('tanh')) model.add(Activation('sigmoid')) return model
def discriminator_model(): model = Sequential() model.add(Convolution2D( 64, 5, 5, border_mode='same', input_shape=(1, 28, 28))) model.add(Activation('tanh')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Convolution2D(128, 5, 5)) model.add(Activation('tanh')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Flatten()) model.add(Dense(1024)) model.add(Activation('tanh')) model.add(Dense(1)) model.add(Activation('sigmoid')) return model
def init_model(): """ """ start_time = time.time() print 'Compiling model...' model = Sequential() model.add(Convolution2D(64, 3,3, border_mode='valid', input_shape=INPUT_SHAPE)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2,2))) model.add(Dropout(.25)) model.add(Flatten()) model.add(Dense(10)) model.add(Activation('softmax')) rms = RMSprop() model.compile(loss='categorical_crossentropy', optimizer=rms, metrics=['accuracy']) print 'Model compiled in {0} seconds'.format(time.time() - start_time) model.summary() return model
def make_discriminator(): """Creates a discriminator model that takes an image as input and outputs a single value, representing whether the input is real or generated. Unlike normal GANs, the output is not sigmoid and does not represent a probability! Instead, the output should be as large and negative as possible for generated inputs and as large and positive as possible for real inputs. Note that the improved WGAN paper suggests that BatchNormalization should not be used in the discriminator.""" model = Sequential() if K.image_data_format() == 'channels_first': model.add(Convolution2D(64, (5, 5), padding='same', input_shape=(1, 28, 28))) else: model.add(Convolution2D(64, (5, 5), padding='same', input_shape=(28, 28, 1))) model.add(LeakyReLU()) model.add(Convolution2D(128, (5, 5), kernel_initializer='he_normal', strides=[2, 2])) model.add(LeakyReLU()) model.add(Convolution2D(128, (5, 5), kernel_initializer='he_normal', padding='same', strides=[2, 2])) model.add(LeakyReLU()) model.add(Flatten()) model.add(Dense(1024, kernel_initializer='he_normal')) model.add(LeakyReLU()) model.add(Dense(1, kernel_initializer='he_normal')) return model
def conv2d_bn(x, nb_filter, nb_row, nb_col, border_mode='same', subsample=(1, 1), bias=False, activ_fn='relu', normalize=True): """ Utility function to apply conv + BN. (Slightly modified from https://github.com/fchollet/keras/blob/master/keras/applications/inception_v3.py) """ if K.image_dim_ordering() == "th": channel_axis = 1 else: channel_axis = -1 if not normalize: bias = True x = Convolution2D(nb_filter, nb_row, nb_col, subsample=subsample, border_mode=border_mode, bias=bias)(x) if normalize: x = BatchNormalization(axis=channel_axis)(x) if activ_fn: x = Activation(activ_fn)(x) return x
def fire_module(x, squeeze=16, expand=64): x = Convolution2D(squeeze, 1, 1, border_mode='valid')(x) x = Activation('relu')(x) left = Convolution2D(expand, 1, 1, border_mode='valid')(x) left = Activation('relu')(left) right= ZeroPadding2D(padding=(1, 1))(x) right = Convolution2D(expand, 3, 3, border_mode='valid')(right) right = Activation('relu')(right) y = merge([left, right], mode='concat', concat_axis=1) return y # Original SqueezeNet from paper. Global Average Pool implemented manually with Average Pooling Layer
def fire_module(x, squeeze=16, expand=64): x = Convolution2D(squeeze, 1, 1, border_mode='valid')(x) x = Activation('relu')(x) left = Convolution2D(expand, 1, 1, border_mode='valid')(x) left = Activation('relu')(left) right= ZeroPadding2D(padding=(1, 1))(x) right = Convolution2D(expand, 3, 3, border_mode='valid')(right) right = Activation('relu')(right) x = merge([left, right], mode='concat', concat_axis=1) return x # Original SqueezeNet from paper. Global Average Pool implemented manually with Average Pooling Layer
def conv2d_bn(x, nb_filter, nb_row, nb_col, border_mode='same', subsample=(1, 1), bias=False): """ Utility function to apply conv + BN. (Slightly modified from https://github.com/fchollet/keras/blob/master/keras/applications/inception_v3.py) """ if K.image_dim_ordering() == "th": channel_axis = 1 else: channel_axis = -1 x = Convolution2D(nb_filter, nb_row, nb_col, subsample=subsample, border_mode=border_mode, bias=bias)(x) x = BatchNormalization(axis=channel_axis)(x) x = Activation('relu')(x) return x
def create_model(self, height=32, width=32, channels=3, load_weights=False, batch_size=128): """ Creates a model to be used to scale images of specific height and width. """ init = super(ImageSuperResolutionModel, self).create_model(height, width, channels, load_weights, batch_size) x = Convolution2D(self.n1, self.f1, self.f1, activation='relu', border_mode='same', name='level1')(init) x = Convolution2D(self.n2, self.f2, self.f2, activation='relu', border_mode='same', name='level2')(x) out = Convolution2D(channels, self.f3, self.f3, border_mode='same', name='output')(x) model = Model(init, out) adam = optimizers.Adam(lr=1e-3) model.compile(optimizer=adam, loss='mse', metrics=[PSNRLoss]) if load_weights: model.load_weights(self.weight_path) self.model = model return model
def create_model(self, height=32, width=32, channels=3, load_weights=False, batch_size=128): """ Creates a model to be used to scale images of specific height and width. """ init = super(ExpantionSuperResolution, self).create_model(height, width, channels, load_weights, batch_size) x = Convolution2D(self.n1, self.f1, self.f1, activation='relu', border_mode='same', name='level1')(init) x1 = Convolution2D(self.n2, self.f2_1, self.f2_1, activation='relu', border_mode='same', name='lavel1_1')(x) x2 = Convolution2D(self.n2, self.f2_2, self.f2_2, activation='relu', border_mode='same', name='lavel1_2')(x) x3 = Convolution2D(self.n2, self.f2_3, self.f2_3, activation='relu', border_mode='same', name='lavel1_3')(x) x = merge([x1, x2, x3], mode='ave') out = Convolution2D(channels, self.f3, self.f3, activation='relu', border_mode='same', name='output')(x) model = Model(init, out) adam = optimizers.Adam(lr=1e-3) model.compile(optimizer=adam, loss='mse', metrics=[PSNRLoss]) if load_weights: model.load_weights(self.weight_path) self.model = model return model
def create_model(self, height=16, width=16, channels=3, load_weights=False, batch_size=128): # Note height, width = 16 instead of 32 like usual init = super(EfficientSubPixelConvolutionalSR, self).create_model(height, width, channels, load_weights, batch_size) x = Convolution2D(self.n1, self.f1, self.f1, activation='relu', border_mode='same', name='level1')(init) x = Convolution2D(self.n2, self.f2, self.f2, activation='relu', border_mode='same', name='level2')(x) x = self._upscale_block(x, 1) out = Convolution2D(3, 5, 5, activation='linear', border_mode='same')(x) model = Model(init, out) adam = optimizers.Adam(lr=1e-3) model.compile(optimizer=adam, loss='mse', metrics=[PSNRLoss]) if load_weights: model.load_weights(self.weight_path) self.model = model return model
def get_simple_model(): model = Sequential() model.add(ZeroPadding2D(padding=(3, 3), input_shape=(nb_input_layers, NB_ROWS, NB_COLS))) model.add(Convolution2D(96, 5, 5)) model.add(Activation('relu')) model.add(ZeroPadding2D(padding=(1, 1))) model.add(Convolution2D(192, 3, 3)) model.add(Activation('relu')) model.add(Flatten()) model.add(Dense(nb_classes)) model.add(Activation('softmax')) print("Compiling model") model.compile(loss='categorical_crossentropy', optimizer='adam') print("Compiled model") return model ###############################################################################
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 train(img_shape): classes = ['ALB', 'BET', 'DOL', 'LAG', 'NoF', 'OTHER', 'SHARK', 'YFT'] # Model model = Sequential() model.add(Convolution2D( 32, 3, 3, input_shape=img_shape, activation='relu', W_constraint=maxnorm(3))) model.add(Dropout(0.2)) model.add(Convolution2D(32, 3, 3, activation='relu', W_constraint=maxnorm(3))) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Flatten()) model.add(Dense(512, activation='relu', W_constraint=maxnorm(3))) model.add(Dropout(0.5)) model.add(Dense(len(classes), activation='softmax')) features, labels = get_featurs_labels(img_shape) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) model.fit(features, labels, nb_epoch=10, batch_size=32, validation_split=0.2, verbose=1) return model
def fire_module(x, fire_id, squeeze=16, expand=64, dim_ordering='th'): s_id = 'fire' + str(fire_id) + '/' if dim_ordering is 'tf': c_axis = 3 else: c_axis = 1 x = Convolution2D(squeeze, 1, 1, border_mode='valid', name=s_id + sq1x1)(x) x = Activation('relu', name=s_id + relu + sq1x1)(x) left = Convolution2D(expand, 1, 1, border_mode='valid', name=s_id + exp1x1)(x) left = Activation('relu', name=s_id + relu + exp1x1)(left) right = Convolution2D(expand, 3, 3, border_mode='same', name=s_id + exp3x3)(x) right = Activation('relu', name=s_id + relu + exp3x3)(right) x = merge([left, right], mode='concat', concat_axis=c_axis, name=s_id + 'concat') return x # Original SqueezeNet from paper.
def make_dcgan_discriminator(Xk_d): x = Convolution2D(nb_filter=64, nb_row=5, nb_col=5, subsample=(2,2), activation=None, border_mode='same', init='glorot_uniform', dim_ordering='th')(Xk_d) x = BatchNormalization(mode=2, axis=1)(x) x = LeakyReLU(0.2)(x) x = Convolution2D(nb_filter=128, nb_row=5, nb_col=5, subsample=(2,2), activation=None, border_mode='same', init='glorot_uniform', dim_ordering='th')(x) x = BatchNormalization(mode=2, axis=1)(x) x = LeakyReLU(0.2)(x) x = Flatten()(x) x = Dense(1024)(x) x = BatchNormalization(mode=2)(x) x = LeakyReLU(0.2)(x) d = Dense(1, activation=None)(x) return d
def make_dcgan_discriminator(Xk_d): x = Convolution2D(nb_filter=64, nb_row=4, nb_col=4, subsample=(2,2), activation=None, border_mode='same', init=conv2D_init, dim_ordering='th')(Xk_d) # x = BatchNormalization(mode=2, axis=1)(x) # <- makes things much worse! x = LeakyReLU(0.2)(x) x = Convolution2D(nb_filter=128, nb_row=4, nb_col=4, subsample=(2,2), activation=None, border_mode='same', init=conv2D_init, dim_ordering='th')(x) x = BatchNormalization(mode=2, axis=1)(x) x = LeakyReLU(0.2)(x) x = Flatten()(x) x = Dense(1024, init=conv2D_init)(x) x = BatchNormalization(mode=2)(x) x = LeakyReLU(0.2)(x) d = Dense(1, activation=None)(x) return d
def model_default(input_shape): model = Sequential() model.add(Convolution2D(32,8,8,subsample=(4,4), border_mode='same',init='he_uniform',input_shape=input_shape)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Convolution2D(64,4,4, subsample=(2,2),border_mode='same' , init='he_uniform')) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Convolution2D(64,3,3, subsample=(1,1),border_mode='same' , init='he_uniform')) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Flatten()) model.add(Dense(512, init='he_uniform')) model.add(Activation('relu')) model.add(Dense(2, init='he_uniform')) return model # Model WITH BATCHNORM NO MAXPOOL NO Dropout
def buildConvolution(self, name): filters = self.params.get('filters') nb_filter = self.params.get('nb_filter') assert filters assert nb_filter convs = [] for fsz in filters: layer_name = '%s-conv-%d' % (name, fsz) conv = Convolution2D( nb_filter=nb_filter, nb_row=fsz, nb_col=self.wdim, border_mode='valid', init='glorot_uniform', W_constraint=maxnorm(self.params.get('w_maxnorm')), b_constraint=maxnorm(self.params.get('b_maxnorm')), name=layer_name ) convs.append(conv) self.layers['%s-convolution' % name] = convs
def test_img_clf(self): print('image classification data:') (X_train, y_train), (X_test, y_test) = get_test_data(nb_train=1000, nb_test=200, input_shape=(3, 32, 32), classification=True, nb_class=2) print('X_train:', X_train.shape) print('X_test:', X_test.shape) print('y_train:', y_train.shape) print('y_test:', y_test.shape) y_train = to_categorical(y_train) y_test = to_categorical(y_test) model = Sequential() model.add(Convolution2D(32, 3, 32, 32)) model.add(Activation('relu')) model.add(Flatten()) model.add(Dense(32, y_test.shape[-1])) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='rmsprop') history = model.fit(X_train, y_train, nb_epoch=12, batch_size=16, validation_data=(X_test, y_test), show_accuracy=True, verbose=2) self.assertTrue(history.validation_accuracy[-1] > 0.9)
def mnist_cnn(args, input_image): shape = (args.channels, args.height, args.width) x = Convolution2D(32, 5, 5, activation='relu', border_mode='valid', input_shape=shape)(input_image) x = MaxPooling2D((2,2))(x) x = Convolution2D(64, 3, 3, activation='relu', border_mode='same')(x) x = Dropout(0.2)(x) x = MaxPooling2D((2,2))(x) x = Flatten()(x) x = Dense(128, activation='relu')(x) x = Dense(64, activation='relu')(x) predictions = Dense(args.num_labels, activation='softmax')(x) # this creates a model that includes # the Input layer and three Dense layers model = Model(input=input_image, output=predictions) model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) model.summary() return model
def Net_model(lr=0.005,decay=1e-6,momentum=0.9): model = Sequential() model.add(Convolution2D(nb_filters1, nb_conv, nb_conv, border_mode='valid', input_shape=(1, img_rows, img_cols))) model.add(Activation('tanh')) model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool))) model.add(Convolution2D(nb_filters2, nb_conv, nb_conv)) model.add(Activation('tanh')) model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool))) #model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(1000)) #Full connection model.add(Activation('tanh')) #model.add(Dropout(0.5)) model.add(Dense(nb_classes)) model.add(Activation('softmax')) sgd = SGD(lr=lr, decay=decay, momentum=momentum, nesterov=True) model.compile(loss='categorical_crossentropy', optimizer=sgd) return model
def transform_model(weight_loss_pix=5e-4): inputs = Input(shape=( 128, 128, 3)) x1 = Convolution2D(64, 5, 5, border_mode='same')(inputs) x2 = LeakyReLU(alpha=0.3, name='wkcw')(x1) x3 = BatchNormalization()(x2) x4 = Convolution2D(128, 4, 4, border_mode='same', subsample=(2,2))(x3) x5 = LeakyReLU(alpha=0.3)(x4) x6 = BatchNormalization()(x5) x7 = Convolution2D(256, 4, 4, border_mode='same', subsample=(2,2))(x6) x8 = LeakyReLU(alpha=0.3)(x7) x9 = BatchNormalization()(x8) x10 = Deconvolution2D(128, 3, 3, output_shape=(None, 64, 64, 128), border_mode='same', subsample=(2,2))(x9) x11 = BatchNormalization()(x10) x12 = Deconvolution2D(64, 3, 3, output_shape=(None, 128, 128, 64), border_mode='same', subsample=(2,2))(x11) x13 = BatchNormalization()(x12) x14 = Deconvolution2D(3, 4, 4, output_shape=(None, 128, 128, 3), border_mode='same', activity_regularizer=activity_l1(weight_loss_pix))(x13) output = merge([inputs, x14], mode='sum') model = Model(input=inputs, output=output) return model
def seqCNN_CPT(c_conf=(4, 3, 32, 32), p_conf=(4, 3, 32, 32), t_conf=(4, 3, 32, 32)): ''' C - Temporal Closeness P - Period T - Trend conf = (nb_flow, seq_len, map_height, map_width) ''' model = Sequential() components = [] for conf in [c_conf, p_conf, t_conf]: if conf is not None: components.append(seqCNNBaseLayer1(conf)) nb_flow = conf[0] model.add(Merge(components, mode='concat', concat_axis=1)) # concat model.add(Convolution2D(64, 3, 3, border_mode='same')) model.add(Activation('relu')) model.add(Convolution2D(64, 3, 3, border_mode='same')) model.add(Activation('relu')) model.add(Convolution2D(nb_flow, 3, 3, border_mode='same')) model.add(Activation('tanh')) return model
def seqCNN_BN(n_flow=4, seq_len=3, map_height=32, map_width=32): model=Sequential() model.add(Convolution2D(64, 3, 3, input_shape=(n_flow*seq_len, map_height, map_width), border_mode='same')) model.add(LeakyReLU(0.2)) model.add(BatchNormalization()) model.add(Convolution2D(128, 3, 3, border_mode='same')) model.add(LeakyReLU(0.2)) model.add(BatchNormalization()) model.add(Convolution2D(64, 3, 3, border_mode='same')) model.add(LeakyReLU(0.2)) model.add(BatchNormalization()) model.add(Convolution2D(n_flow, 3, 3, border_mode='same')) model.add(Activation('tanh')) return model
def seqCNN_LReLU(n_flow=4, seq_len=3, map_height=32, map_width=32): model=Sequential() model.add(Convolution2D(64, 3, 3, input_shape=(n_flow*seq_len, map_height, map_width), border_mode='same')) model.add(LeakyReLU(0.2)) # model.add(BatchNormalization()) model.add(Convolution2D(128, 3, 3, border_mode='same')) model.add(LeakyReLU(0.2)) # model.add(BatchNormalization()) model.add(Convolution2D(64, 3, 3, border_mode='same')) model.add(LeakyReLU(0.2)) # model.add(BatchNormalization()) model.add(Convolution2D(n_flow, 3, 3, border_mode='same')) model.add(Activation('tanh')) return model
def getCNN(): model = Sequential() model.add(Convolution2D(32, 3, 3, activation='relu', input_shape=kNetImageShape)) model.add(Convolution2D(32, 3, 3, activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Convolution2D(64, 3, 3, activation='relu')) model.add(Convolution2D(64, 3, 3, activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(256, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(kNetNumClasses, activation='softmax')) model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) return model
def get_config(self): config = {'name': self.__class__.__name__, 'nb_filter': self.nb_filter, 'nb_row': self.nb_row, 'nb_col': self.nb_col, 'init': self.init.__name__, 'activation': self.activation.__name__, 'border_mode': self.border_mode, 'subsample': self.subsample, 'dim_ordering': self.dim_ordering, 'W_regularizer': self.W_regularizer.get_config() if self.W_regularizer else None, 'b_regularizer': self.b_regularizer.get_config() if self.b_regularizer else None, 'activity_regularizer': self.activity_regularizer.get_config() if self.activity_regularizer else None, 'W_constraint': self.W_constraint.get_config() if self.W_constraint else None, 'b_constraint': self.b_constraint.get_config() if self.b_constraint else None} base_config = super(Convolution2D, self).get_config() return dict(list(base_config.items()) + list(config.items()))
def build_model(nb_filters=32, nb_pool=2, nb_conv=3): C_1 = 64 C_2 = 32 C_3 = 16 c = Convolution2D(C_1, nb_conv, nb_conv, border_mode='same', input_shape=(3, 32, 32)) mp = MaxPooling2D(pool_size=(nb_pool, nb_pool)) c2 = Convolution2D(C_2, nb_conv, nb_conv, border_mode='same', input_shape=(3, 32, 32)) mp2 = MaxPooling2D(pool_size=(nb_pool, nb_pool)) d = Dense(100) encoder = get_encoder(c, c2, d, mp, mp2) decoder = get_decoder(C_1, C_2, C_3, c, c2, d, mp, mp2, nb_pool) graph = Graph() graph.add_input(name='input', input_shape=(3, 32, 32)) graph.add_node(encoder, name='encoder', input='input') graph.add_node(decoder, name='decoder', input='encoder') graph.add_output(name='autoencoder_feedback', input='decoder') graph.compile('rmsprop', {'autoencoder_feedback': 'mean_squared_error'}) return graph
def build_model(nb_filters=32, nb_pool=2, nb_conv=3): model = models.Sequential() d = Dense(30) c = Convolution2D(nb_filters, nb_conv, nb_conv, border_mode='same', input_shape=(1, 28, 28)) mp =MaxPooling2D(pool_size=(nb_pool, nb_pool)) # ========= ENCODER ======================== model.add(c) model.add(Activation('tanh')) model.add(mp) model.add(Dropout(0.25)) # ========= BOTTLENECK ====================== model.add(Flatten()) model.add(d) model.add(Activation('tanh')) # ========= BOTTLENECK^-1 ===================== model.add(DependentDense(nb_filters * 14 * 14, d)) model.add(Activation('tanh')) model.add(Reshape((nb_filters, 14, 14))) # ========= DECODER ========================= model.add(DePool2D(mp, size=(nb_pool, nb_pool))) model.add(Deconvolution2D(c, border_mode='same')) model.add(Activation('tanh')) return model
def conv_block(ip, nb_filter, dropout_rate=None, weight_decay=1E-4): ''' Apply BatchNorm, Relu 3x3, Conv2D, optional dropout Args: ip: Input keras tensor nb_filter: number of filters dropout_rate: dropout rate weight_decay: weight decay factor Returns: keras tensor with batch_norm, relu and convolution2d added ''' x = Activation('relu')(ip) x = Convolution2D(nb_filter, 3, 3, init="he_uniform", border_mode="same", bias=False, W_regularizer=l2(weight_decay))(x) if dropout_rate: x = Dropout(dropout_rate)(x) return x
def transition_block(ip, nb_filter, dropout_rate=None, weight_decay=1E-4): ''' Apply BatchNorm, Relu 1x1, Conv2D, optional dropout and Maxpooling2D Args: ip: keras tensor nb_filter: number of filters dropout_rate: dropout rate weight_decay: weight decay factor Returns: keras tensor, after applying batch_norm, relu-conv, dropout, maxpool ''' concat_axis = 1 if K.image_dim_ordering() == "th" else -1 x = Convolution2D(nb_filter, 1, 1, init="he_uniform", border_mode="same", bias=False, W_regularizer=l2(weight_decay))(ip) if dropout_rate: x = Dropout(dropout_rate)(x) x = AveragePooling2D((2, 2), strides=(2, 2))(x) x = BatchNormalization(mode=0, axis=concat_axis, gamma_regularizer=l2(weight_decay), beta_regularizer=l2(weight_decay))(x) return x
def conv2d_bn(x, nb_filter, num_row, num_col, padding='same', strides=(1, 1), use_bias=False): """ Utility function to apply conv + BN. (Slightly modified from https://github.com/fchollet/keras/blob/master/keras/applications/inception_v3.py) """ if K.image_data_format() == 'channels_first': channel_axis = 1 else: channel_axis = -1 x = Convolution2D(nb_filter, (num_row, num_col), strides=strides, padding=padding, use_bias=use_bias, kernel_regularizer=regularizers.l2(0.00004), kernel_initializer=initializers.VarianceScaling(scale=2.0, mode='fan_in', distribution='normal', seed=None))(x) x = BatchNormalization(axis=channel_axis, momentum=0.9997, scale=False)(x) x = Activation('relu')(x) return x
def create_model(learning_rate=0.1, momentum=0.9): model = Sequential() model.add(Convolution2D(20, 9, 9, border_mode='same', input_shape=(3, SIZE, SIZE))) model.add(Activation("relu")) model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2))) model.add(Convolution2D(50, 5, 5, activation = "relu")) model.add(Activation("relu")) model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2))) model.add(Flatten()) model.add(Dense(768, input_dim=3072, init='uniform', activation = 'relu')) model.add(Dropout(0.1)) model.add(Dense(384, init = 'uniform', activation = 'relu', W_constraint=maxnorm(3))) model.add(Dense(4)) model.add(Activation("softmax")) sgd = SGD(lr=learning_rate, momentum=momentum, nesterov=True, decay=1e-6) model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=["accuracy"]) return model
def two_conv_layer(x, nb_channels, kernel_size=3, stride=1, l2_reg=1e-4, first=False): if first: # Skip BN-Relu out = x else: out = BatchNormalization()(x) out = Activation('relu')(out) out = Convolution2D(nb_channels, kernel_size, kernel_size, subsample=(stride, stride), border_mode='same', init='he_normal', W_regularizer=l2(l2_reg), bias=False)(out) out = BatchNormalization()(out) out = Activation('relu')(out) out = Convolution2D(nb_channels, kernel_size, kernel_size, border_mode='same', init='he_normal', W_regularizer=l2(l2_reg), bias=False)(out) return out
def downsample_block(x, nb_channels, kernel_size=3, bottleneck=True, l2_reg=1e-4): if bottleneck: out = bottleneck_layer(x, nb_channels, kernel_size=kernel_size, stride=2, l2_reg=l2_reg) # The output channels is 4x bigger on this case nb_channels = nb_channels * 4 else: out = two_conv_layer(x, nb_channels, kernel_size=kernel_size, stride=2, l2_reg=l2_reg) # Projection on the shortcut proj = Convolution2D(nb_channels, 1, 1, subsample=(2, 2), border_mode='valid', init='he_normal', W_regularizer=l2(l2_reg), bias=False)(x) # proj = AveragePooling2D((1, 1), (2, 2))(x) out = merge([proj, out], mode='sum') return out
def resnet_model(nb_blocks, bottleneck=True, l2_reg=1e-4): nb_channels = [16, 32, 64] inputs = Input((32, 32, 3)) x = Convolution2D(16, 3, 3, border_mode='same', init='he_normal', W_regularizer=l2(l2_reg), bias=False)(inputs) x = BatchNormalization()(x) x = Activation('relu')(x) for n, f in zip(nb_channels, [True, False, False]): x = block_stack(x, n, nb_blocks, bottleneck=bottleneck, l2_reg=l2_reg, first=f) # Last BN-Relu x = BatchNormalization()(x) x = Activation('relu')(x) x = GlobalAveragePooling2D()(x) x = Dense(10)(x) x = Activation('softmax')(x) model = Model(input=inputs, output=x) return model
def _upscale_block(self, ip, id): ''' As per suggestion from http://distill.pub/2016/deconv-checkerboard/, I am swapping out SubPixelConvolution to simple Nearest Neighbour Upsampling ''' init = ip x = Convolution2D(128, 3, 3, activation="linear", border_mode='same', name='sr_res_upconv1_%d' % id, init=self.init)(init) x = LeakyReLU(alpha=0.25, name='sr_res_up_lr_%d_1_1' % id)(x) x = UpSampling2D(name='sr_res_upscale_%d' % id)(x) #x = SubPixelUpscaling(r=2, channels=32)(x) x = Convolution2D(128, 3, 3, activation="linear", border_mode='same', name='sr_res_filter1_%d' % id, init=self.init)(x) x = LeakyReLU(alpha=0.3, name='sr_res_up_lr_%d_1_2' % id)(x) return x
def create_model(img_rows, img_cols): model = Sequential() #initialize model model.add(Convolution2D(4, 3, 3, border_mode='same', activation='relu', init='he_normal', input_shape=(1, img_rows, img_cols))) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Convolution2D(8, 3, 3, border_mode='same', activation='relu', init='he_normal')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(2)) model.add(Activation('softmax')) adm = Adamax() #sgd = SGD(lr=1e-3, decay=1e-6, momentum=0.9, nesterov=True) model.compile(optimizer=adm, loss='categorical_crossentropy') return model
def test_export_model(self): input = Input(shape=(3, 4, 4), name='input', dtype='float32') out = Convolution2D(3, 3, 3, init='he_normal', subsample=(1, 1), border_mode='same', activation='linear')(input) out = Activation('relu')(out) model = Model(input=[input], output=[out]) data_model_folder = dir + "/../fixture/model_export" if K._BACKEND == 'tensorflow': import tensorflow as tf saver = tf.train.Saver() else: saver = None export_model(model, data_model_folder, saver=saver) os.remove(data_model_folder + '/archi.json') os.remove(data_model_folder + '/last_weights.hdf5') if K._BACKEND == 'tensorflow': os.remove(data_model_folder + '/checkpoint') os.remove(data_model_folder + '/tf-last_weights') os.remove(data_model_folder + '/tf-last_weights.meta') os.remove(data_model_folder + '/tf-model_graph') os.remove(data_model_folder + '/tf-frozen_model.pb') os.rmdir(data_model_folder)
def fire_module(x, fire_id, squeeze=16, expand=64, dim_ordering='tf'): s_id = 'fire' + str(fire_id) + '/' if dim_ordering is 'tf': c_axis = 3 else: c_axis = 1 x = Convolution2D(squeeze, 1, 1, border_mode='valid', name=s_id + sq1x1)(x) x = Activation('relu', name=s_id + relu + sq1x1)(x) left = Convolution2D(expand, 1, 1, border_mode='valid', name=s_id + exp1x1)(x) left = Activation('relu', name=s_id + relu + exp1x1)(left) right = Convolution2D(expand, 3, 3, border_mode='same', name=s_id + exp3x3)(x) right = Activation('relu', name=s_id + relu + exp3x3)(right) x = merge([left, right], mode='concat', concat_axis=c_axis, name=s_id + 'concat') return x # Original SqueezeNet from paper.
def VGG_16(weights_path=None): model = Sequential() model.add(ZeroPadding2D((1,1),input_shape=(3,224,224))) print "convolution" model.add(Convolution2D(64, 3, 3, activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(64, 3, 3, activation='relu')) model.add(MaxPooling2D((2,2), strides=(2,2))) model.add(Flatten()) print"FLATTEN" model.add(Dense(400, activation='relu')) model.add(Dropout(0.5)) print"YO" model.add(Dense(10, activation='softmax')) return model
def __transition_block(ip, nb_filter, compression=1.0, dropout_rate=None, weight_decay=1E-4): ''' Apply BatchNorm, Relu 1x1, Conv2D, optional compression, dropout and Maxpooling2D Args: ip: keras tensor nb_filter: number of filters compression: calculated as 1 - reduction. Reduces the number of feature maps in the transition block. dropout_rate: dropout rate weight_decay: weight decay factor Returns: keras tensor, after applying batch_norm, relu-conv, dropout, maxpool ''' concat_axis = 1 if K.image_dim_ordering() == "th" else -1 x = BatchNormalization(mode=0, axis=concat_axis, gamma_regularizer=l2(weight_decay), beta_regularizer=l2(weight_decay))(ip) x = Activation('relu')(x) x = Convolution2D(int(nb_filter * compression), 1, 1, init="he_uniform", border_mode="same", bias=False, W_regularizer=l2(weight_decay))(x) if dropout_rate: x = Dropout(dropout_rate)(x) x = AveragePooling2D((2, 2), strides=(2, 2))(x) return x
def inception_resnet_v2_B(input, scale_residual=True): if K.image_dim_ordering() == "th": channel_axis = 1 else: channel_axis = -1 # Input is relu activation init = input ir1 = Convolution2D(192, 1, 1, activation='relu', border_mode='same')(input) ir2 = Convolution2D(128, 1, 1, activation='relu', border_mode='same')(input) ir2 = Convolution2D(160, 1, 7, activation='relu', border_mode='same')(ir2) ir2 = Convolution2D(192, 7, 1, activation='relu', border_mode='same')(ir2) ir_merge = merge([ir1, ir2], mode='concat', concat_axis=channel_axis) ir_conv = Convolution2D(1152, 1, 1, activation='linear', border_mode='same')(ir_merge) if scale_residual: ir_conv = Lambda(lambda x: x * 0.1)(ir_conv) out = merge([init, ir_conv], mode='sum') out = BatchNormalization(axis=channel_axis)(out) out = Activation("relu")(out) return out
def inception_resnet_v2_C(input, scale_residual=True): if K.image_dim_ordering() == "th": channel_axis = 1 else: channel_axis = -1 # Input is relu activation init = input ir1 = Convolution2D(192, 1, 1, activation='relu', border_mode='same')(input) ir2 = Convolution2D(192, 1, 1, activation='relu', border_mode='same')(input) ir2 = Convolution2D(224, 1, 3, activation='relu', border_mode='same')(ir2) ir2 = Convolution2D(256, 3, 1, activation='relu', border_mode='same')(ir2) ir_merge = merge([ir1, ir2], mode='concat', concat_axis=channel_axis) ir_conv = Convolution2D(2144, 1, 1, activation='linear', border_mode='same')(ir_merge) if scale_residual: ir_conv = Lambda(lambda x: x * 0.1)(ir_conv) out = merge([init, ir_conv], mode='sum') out = BatchNormalization(axis=channel_axis)(out) out = Activation("relu")(out) return out