我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用keras.layers.Activation()。
def cnn_word_model(self): embed_input = Input(shape=(self.opt['max_sequence_length'], self.opt['embedding_dim'],)) outputs = [] for i in range(len(self.kernel_sizes)): output_i = Conv1D(self.opt['filters_cnn'], kernel_size=self.kernel_sizes[i], activation=None, kernel_regularizer=l2(self.opt['regul_coef_conv']), padding='same')(embed_input) output_i = BatchNormalization()(output_i) output_i = Activation('relu')(output_i) output_i = GlobalMaxPooling1D()(output_i) outputs.append(output_i) output = concatenate(outputs, axis=1) output = Dropout(rate=self.opt['dropout_rate'])(output) output = Dense(self.opt['dense_dim'], activation=None, kernel_regularizer=l2(self.opt['regul_coef_dense']))(output) output = BatchNormalization()(output) output = Activation('relu')(output) output = Dropout(rate=self.opt['dropout_rate'])(output) output = Dense(1, activation=None, kernel_regularizer=l2(self.opt['regul_coef_dense']))(output) output = BatchNormalization()(output) act_output = Activation('sigmoid')(output) model = Model(inputs=embed_input, outputs=act_output) return model
def __init__(self, input_shape, lr=0.01, n_layers=2, n_hidden=8, rate_dropout=0.2, loss='risk_estimation'): print("initializing..., learing rate %s, n_layers %s, n_hidden %s, dropout rate %s." %(lr, n_layers, n_hidden, rate_dropout)) self.model = Sequential() self.model.add(Dropout(rate=rate_dropout, input_shape=(input_shape[0], input_shape[1]))) for i in range(0, n_layers - 1): self.model.add(LSTM(n_hidden * 4, return_sequences=True, activation='tanh', recurrent_activation='hard_sigmoid', kernel_initializer='glorot_uniform', recurrent_initializer='orthogonal', bias_initializer='zeros', dropout=rate_dropout, recurrent_dropout=rate_dropout)) self.model.add(LSTM(n_hidden, return_sequences=False, activation='tanh', recurrent_activation='hard_sigmoid', kernel_initializer='glorot_uniform', recurrent_initializer='orthogonal', bias_initializer='zeros', dropout=rate_dropout, recurrent_dropout=rate_dropout)) self.model.add(Dense(1, kernel_initializer=initializers.glorot_uniform())) # self.model.add(BatchNormalization(axis=-1, moving_mean_initializer=Constant(value=0.5), # moving_variance_initializer=Constant(value=0.25))) self.model.add(BatchRenormalization(axis=-1, beta_init=Constant(value=0.5))) self.model.add(Activation('relu_limited')) opt = RMSprop(lr=lr) self.model.compile(loss=loss, optimizer=opt, metrics=['accuracy'])
def first_block(tensor_input,filters,kernel_size=3,pooling_size=1,dropout=0.5): k1,k2 = filters out = Conv1D(k1,1,padding='same')(tensor_input) out = BatchNormalization()(out) out = Activation('relu')(out) out = Dropout(dropout)(out) out = Conv1D(k2,kernel_size,padding='same')(out) pooling = MaxPooling1D(pooling_size,padding='same')(tensor_input) # out = merge([out,pooling],mode='sum') out = add([out,pooling]) return out
def get_model(): inputs = Input(shape=(64, 64, 3)) conv_1 = Conv2D(1, (3, 3), strides=(1, 1), padding='same')(inputs) act_1 = Activation('relu')(conv_1) conv_2 = Conv2D(64, (3, 3), strides=(1, 1), padding='same')(act_1) act_2 = Activation('relu')(conv_2) deconv_1 = Conv2DTranspose(64, (3, 3), strides=(1, 1), padding='same')(act_2) act_3 = Activation('relu')(deconv_1) merge_1 = concatenate([act_3, act_1], axis=3) deconv_2 = Conv2DTranspose(1, (3, 3), strides=(1, 1), padding='same')(merge_1) act_4 = Activation('relu')(deconv_2) model = Model(inputs=[inputs], outputs=[act_4]) model.compile(optimizer='adadelta', loss=dice_coef_loss, metrics=[dice_coef]) return model
def get_unet0(num_start_filters=32): inputs = Input((img_rows, img_cols, num_channels)) conv1 = ConvBN2(inputs, num_start_filters) pool1 = MaxPooling2D(pool_size=(2, 2))(conv1) conv2 = ConvBN2(pool1, 2 * num_start_filters) pool2 = MaxPooling2D(pool_size=(2, 2))(conv2) conv3 = ConvBN2(pool2, 4 * num_start_filters) pool3 = MaxPooling2D(pool_size=(2, 2))(conv3) conv4 = ConvBN2(pool3, 8 * num_start_filters) pool4 = MaxPooling2D(pool_size=(2, 2))(conv4) conv5 = ConvBN2(pool4, 16 * num_start_filters) up6 = concatenate([UpSampling2D(size=(2, 2))(conv5), conv4]) conv6 = ConvBN2(up6, 8 * num_start_filters) up7 = concatenate([UpSampling2D(size=(2, 2))(conv6), conv3]) conv7 = ConvBN2(up7, 4 * num_start_filters) up8 = concatenate([UpSampling2D(size=(2, 2))(conv7), conv2]) conv8 = ConvBN2(up8, 2 * num_start_filters) up9 = concatenate([UpSampling2D(size=(2, 2))(conv8), conv1]) conv9 = Conv2D(num_start_filters, (3, 3), padding="same", kernel_initializer="he_uniform")(up9) conv9 = BatchNormalization()(conv9) conv9 = Activation('selu')(conv9) conv9 = Conv2D(num_start_filters, (3, 3), padding="same", kernel_initializer="he_uniform")(conv9) crop9 = Cropping2D(cropping=((16, 16), (16, 16)))(conv9) conv9 = BatchNormalization()(crop9) conv9 = Activation('selu')(conv9) conv10 = Conv2D(num_mask_channels, (1, 1))(conv9) model = Model(inputs=inputs, outputs=conv10) return model
def build_simpleCNN(input_shape = (32, 32, 3), num_output = 10): h, w, nch = input_shape assert h == w, 'expect input shape (h, w, nch), h == w' images = Input(shape = (h, h, nch)) x = Conv2D(64, (4, 4), strides = (1, 1), kernel_initializer = init, padding = 'same')(images) x = BatchNormalization()(x) x = Activation('relu')(x) x = MaxPooling2D(pool_size = (2, 2))(x) x = Conv2D(128, (4, 4), strides = (1, 1), kernel_initializer = init, padding = 'same')(x) x = BatchNormalization()(x) x = Activation('relu')(x) x = MaxPooling2D(pool_size = (2, 2))(x) x = Flatten()(x) outputs = Dense(num_output, kernel_initializer = init, activation = 'softmax')(x) model = Model(inputs = images, outputs = outputs) return model
def model_generator(): model = Sequential() nch = 256 reg = lambda: l1l2(l1=1e-7, l2=1e-7) h = 5 model.add(Dense(nch * 4 * 4, input_dim=100, W_regularizer=reg())) model.add(BatchNormalization(mode=0)) model.add(Reshape(dim_ordering_shape((nch, 4, 4)))) model.add(Convolution2D(nch / 2, h, h, border_mode='same', W_regularizer=reg())) model.add(BatchNormalization(mode=0, axis=1)) model.add(LeakyReLU(0.2)) model.add(UpSampling2D(size=(2, 2))) model.add(Convolution2D(nch / 2, h, h, border_mode='same', W_regularizer=reg())) model.add(BatchNormalization(mode=0, axis=1)) model.add(LeakyReLU(0.2)) model.add(UpSampling2D(size=(2, 2))) model.add(Convolution2D(nch / 4, h, h, border_mode='same', W_regularizer=reg())) model.add(BatchNormalization(mode=0, axis=1)) model.add(LeakyReLU(0.2)) model.add(UpSampling2D(size=(2, 2))) model.add(Convolution2D(3, h, h, border_mode='same', W_regularizer=reg())) model.add(Activation('sigmoid')) return model
def model_generator(): nch = 256 g_input = Input(shape=[100]) H = Dense(nch * 14 * 14)(g_input) H = BatchNormalization(mode=2)(H) H = Activation('relu')(H) H = dim_ordering_reshape(nch, 14)(H) H = UpSampling2D(size=(2, 2))(H) H = Convolution2D(int(nch / 2), 3, 3, border_mode='same')(H) H = BatchNormalization(mode=2, axis=1)(H) H = Activation('relu')(H) H = Convolution2D(int(nch / 4), 3, 3, border_mode='same')(H) H = BatchNormalization(mode=2, axis=1)(H) H = Activation('relu')(H) H = Convolution2D(1, 1, 1, border_mode='same')(H) g_V = Activation('sigmoid')(H) return Model(g_input, g_V)
def lstm_word_model(self): embed_input = Input(shape=(self.opt['max_sequence_length'], self.opt['embedding_dim'],)) output = Bidirectional(LSTM(self.opt['units_lstm'], activation='tanh', kernel_regularizer=l2(self.opt['regul_coef_lstm']), dropout=self.opt['dropout_rate']))(embed_input) output = Dropout(rate=self.opt['dropout_rate'])(output) output = Dense(self.opt['dense_dim'], activation=None, kernel_regularizer=l2(self.opt['regul_coef_dense']))(output) output = BatchNormalization()(output) output = Activation('relu')(output) output = Dropout(rate=self.opt['dropout_rate'])(output) output = Dense(1, activation=None, kernel_regularizer=l2(self.opt['regul_coef_dense']))(output) output = BatchNormalization()(output) act_output = Activation('sigmoid')(output) model = Model(inputs=embed_input, outputs=act_output) return model
def create_attention_layer(self, input_dim_a, input_dim_b): """Create an attention layer of a model.""" inp_a = Input(shape=(input_dim_a, self.hidden_dim,)) inp_b = Input(shape=(input_dim_b, self.hidden_dim,)) val = np.concatenate((np.zeros((self.max_sequence_length-1,1)), np.ones((1,1))), axis=0) kcon = K.constant(value=val, dtype='float32') inp_b_perm = Lambda(lambda x: K.permute_dimensions(x, (0,2,1)))(inp_b) last_state = Lambda(lambda x: K.permute_dimensions(K.dot(x, kcon), (0,2,1)))(inp_b_perm) ker_in = glorot_uniform(seed=self.seed) outp_a = Dense(self.attention_dim, input_shape=(input_dim_a, self.hidden_dim), kernel_initializer=ker_in, activation='relu')(inp_a) outp_last = Dense(self.attention_dim, input_shape=(1, self.hidden_dim), kernel_initializer=ker_in, activation='relu')(last_state) outp_last_perm = Lambda(lambda x: K.permute_dimensions(x, (0,2,1)))(outp_last) outp = Lambda(lambda x: K.batch_dot(x[0], x[1], axes=[1, 2]))([outp_last_perm, outp_a]) outp_norm = Activation('softmax')(outp) outp_norm_perm = Lambda(lambda x: K.permute_dimensions(x, (0,2,1)))(outp_norm) model = Model(inputs=[inp_a, inp_b], outputs=outp_norm_perm, name="attention_generator") return model
def create_attention_layer_f(self, input_dim_a, input_dim_b): """Create an attention layer of a model.""" inp_a = Input(shape=(input_dim_a, self.hidden_dim,)) inp_b = Input(shape=(input_dim_b, self.hidden_dim,)) val = np.concatenate((np.zeros((self.max_sequence_length-1,1)), np.ones((1,1))), axis=0) kcon = K.constant(value=val, dtype='float32') inp_b_perm = Lambda(lambda x: K.permute_dimensions(x, (0,2,1)))(inp_b) last_state = Lambda(lambda x: K.permute_dimensions(K.dot(x, kcon), (0,2,1)))(inp_b_perm) ker_in = glorot_uniform(seed=self.seed) outp_a = Dense(self.attention_dim, input_shape=(input_dim_a, self.hidden_dim), kernel_initializer=ker_in, activation='relu')(inp_a) outp_last = Dense(self.attention_dim, input_shape=(1, self.hidden_dim), kernel_initializer=ker_in, activation='relu')(last_state) outp_last_perm = Lambda(lambda x: K.permute_dimensions(x, (0,2,1)))(outp_last) outp = Lambda(lambda x: K.batch_dot(x[0], x[1], axes=[1, 2]))([outp_last_perm, outp_a]) outp_norm = Activation('softmax')(outp) outp_norm_perm = Lambda(lambda x: K.permute_dimensions(x, (0,2,1)))(outp_norm) model = Model(inputs=[inp_a, inp_b], outputs=outp_norm_perm, name="att_generator_forw") return model
def create_attention_layer_b(self, input_dim_a, input_dim_b): """Create an attention layer of a model.""" inp_a = Input(shape=(input_dim_a, self.hidden_dim,)) inp_b = Input(shape=(input_dim_b, self.hidden_dim,)) val = np.concatenate((np.ones((1,1)), np.zeros((self.max_sequence_length-1,1))), axis=0) kcon = K.constant(value=val, dtype='float32') inp_b_perm = Lambda(lambda x: K.permute_dimensions(x, (0,2,1)))(inp_b) last_state = Lambda(lambda x: K.permute_dimensions(K.dot(x, kcon), (0,2,1)))(inp_b_perm) ker_in = glorot_uniform(seed=self.seed) outp_a = Dense(self.attention_dim, input_shape=(input_dim_a, self.hidden_dim), kernel_initializer=ker_in, activation='relu')(inp_a) outp_last = Dense(self.attention_dim, input_shape=(1, self.hidden_dim), kernel_initializer=ker_in, activation='relu')(last_state) outp_last_perm = Lambda(lambda x: K.permute_dimensions(x, (0,2,1)))(outp_last) outp = Lambda(lambda x: K.batch_dot(x[0], x[1], axes=[1, 2]))([outp_last_perm, outp_a]) outp_norm = Activation('softmax')(outp) outp_norm_perm = Lambda(lambda x: K.permute_dimensions(x, (0,2,1)))(outp_norm) model = Model(inputs=[inp_a, inp_b], outputs=outp_norm_perm, name="att_generator_back") return model
def create_actor_network(self, state_size,action_dim): print("Now we build the model") # Batch norm version S = Input(shape=[state_size]) s1 = BatchNormalization()(S) s1 = Dense(HIDDEN1_UNITS)(s1) s1 = BatchNormalization()(s1) s1 = Activation('relu')(s1) s1 = Dense(HIDDEN2_UNITS)(s1) s1 = BatchNormalization()(s1) h1 = Activation('relu')(s1) Steering = Dense(1,activation='tanh')(h1) Acceleration = Dense(1,activation='sigmoid')(h1) Brake = Dense(1,activation='sigmoid')(h1) # V = merge([Steering,Acceleration,Brake],mode='concat') V = layers.concatenate([Steering,Acceleration,Brake]) model = Model(inputs=S,outputs=V) return model, model.trainable_weights, S
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 block17(input, scale=1.0, activation_fn='relu'): if K.image_dim_ordering() == "th": channel_axis = 1 else: channel_axis = -1 shortcut = input tower_conv = conv2d_bn(input, 192, 1, 1, activ_fn=activation_fn) tower_conv1_0 = conv2d_bn(input, 128, 1, 1, activ_fn=activation_fn) tower_conv1_1 = conv2d_bn(tower_conv1_0, 160, 1, 7, activ_fn=activation_fn) tower_conv1_2 = conv2d_bn(tower_conv1_1, 192, 7, 1, activ_fn=activation_fn) mixed = merge([tower_conv, tower_conv1_2], mode='concat', concat_axis=channel_axis) up = conv2d_bn(mixed, 1088, 1, 1, activ_fn=False, normalize=False) up = Lambda(do_scale, output_shape=K.int_shape(up)[1:], arguments={'scale':scale})(up) net = merge([shortcut, up], mode='sum') if activation_fn: net = Activation(activation_fn)(net) return net
def block8(input, scale=1.0, activation_fn='relu'): if K.image_dim_ordering() == "th": channel_axis = 1 else: channel_axis = -1 shortcut = input tower_conv = conv2d_bn(input, 192, 1, 1, activ_fn=activation_fn) tower_conv1_0 = conv2d_bn(input, 192, 1, 1, activ_fn=activation_fn) tower_conv1_1 = conv2d_bn(tower_conv1_0, 224, 1, 3, activ_fn=activation_fn) tower_conv1_2 = conv2d_bn(tower_conv1_1, 256, 3, 1, activ_fn=activation_fn) mixed = merge([tower_conv, tower_conv1_2], mode='concat', concat_axis=channel_axis) up = conv2d_bn(mixed, 2080, 1, 1, activ_fn=False, normalize=False) up = Lambda(do_scale, output_shape=K.int_shape(up)[1:], arguments={'scale':scale})(up) net = merge([shortcut, up], mode='sum') if activation_fn: net = Activation(activation_fn)(net) return net
def make_model(dense_layer_sizes, filters, kernel_size, pool_size): '''Creates model comprised of 2 convolutional layers followed by dense layers dense_layer_sizes: List of layer sizes. This list has one number for each layer filters: Number of convolutional filters in each convolutional layer kernel_size: Convolutional kernel size pool_size: Size of pooling area for max pooling ''' model = Sequential() model.add(Conv2D(filters, kernel_size, padding='valid', input_shape=input_shape)) model.add(Activation('relu')) model.add(Conv2D(filters, kernel_size)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=pool_size)) model.add(Dropout(0.25)) model.add(Flatten()) for layer_size in dense_layer_sizes: model.add(Dense(layer_size)) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Dense(num_classes)) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy']) return model
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 get_model(): input_shape = (image_size, image_size, 3) model = Sequential() model.add(Conv2D(32, kernel_size=(3, 3), padding='same', input_shape=input_shape)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Conv2D(64, kernel_size=(3, 3), padding='same')) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Conv2D(128, kernel_size=(3, 3), padding='same')) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Conv2D(n_classes, kernel_size=(3, 3), padding='same')) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(GlobalAveragePooling2D()) print (model.summary()) #sys.exit(0) # model.compile(loss=keras.losses.mean_squared_error, optimizer= keras.optimizers.Adadelta()) return model
def repeated_block(x,filters,kernel_size=3,pooling_size=1,dropout=0.5): k1,k2 = filters out = BatchNormalization()(x) out = Activation('relu')(out) out = Conv1D(k1,kernel_size,strides=2,padding='same')(out) out = BatchNormalization()(out) out = Activation('relu')(out) out = Dropout(dropout)(out) out = Conv1D(k2,kernel_size,strides=2,padding='same')(out) pooling = MaxPooling1D(pooling_size,strides=4,padding='same')(x) out = add([out, pooling]) #out = merge([out,pooling]) return out
def __call__(self, model): original = model tanh_out = CausalAtrousConvolution1D(self.filters, 2, atrous_rate=self.rate, border_mode='valid')(model) tanh_out = Activation('tanh')(tanh_out) sigm_out = CausalAtrousConvolution1D(self.filters, 2, atrous_rate=self.rate, border_mode='valid')(model) sigm_out = Activation('sigmoid')(sigm_out) model = Merge(mode='mul')([tanh_out, sigm_out]) skip_x = Convolution1D(self.filters, 1, border_mode='same')(model) res_x = Convolution1D(self.filters, 1, border_mode='same')(model) res_x = Merge(mode='sum')([original, res_x]) return res_x, skip_x
def test_keras_export(self): tests = open(os.path.join(settings.BASE_DIR, 'tests', 'unit', 'keras_app', 'keras_export_test.json'), 'r') response = json.load(tests) tests.close() net = yaml.safe_load(json.dumps(response['net'])) net = {'l0': net['Input'], 'l1': net['ReLU']} # Test 1 net['l0']['connection']['output'].append('l1') inp = data(net['l0'], '', 'l0')['l0'] temp = activation(net['l1'], [inp], 'l1') model = Model(inp, temp['l1']) self.assertEqual(model.layers[1].__class__.__name__, 'Activation') # Test 2 net['l1']['params']['negative_slope'] = 1 net['l0']['connection']['output'].append('l1') inp = data(net['l0'], '', 'l0')['l0'] temp = activation(net['l1'], [inp], 'l1') model = Model(inp, temp['l1']) self.assertEqual(model.layers[1].__class__.__name__, 'LeakyReLU')
def test_valid_workflow(self): # Create image URI dataframe label_cardinality = 10 image_uri_df = self._create_train_image_uris_and_labels( repeat_factor=3, cardinality=label_cardinality) # We need a small model so that machines with limited resources can run it model = Sequential() model.add(Flatten(input_shape=(299, 299, 3))) model.add(Dense(label_cardinality)) model.add(Activation("softmax")) estimator = self._get_estimator(model) self.assertTrue(estimator._validateParams()) transformers = estimator.fit(image_uri_df) self.assertEqual(1, len(transformers)) self.assertIsInstance(transformers[0]['transformer'], KerasImageFileTransformer)
def test_keras_transformer_single_dim(self): """ Test that KerasTransformer correctly handles single-dimensional input data. """ # Construct a model for simple binary classification (with a single hidden layer) model = Sequential() input_shape = [10] model.add(Dense(units=10, input_shape=input_shape, bias_initializer=self._getKerasModelWeightInitializer(), kernel_initializer=self._getKerasModelWeightInitializer())) model.add(Activation('relu')) model.add(Dense(units=1, bias_initializer=self._getKerasModelWeightInitializer(), kernel_initializer=self._getKerasModelWeightInitializer())) model.add(Activation('sigmoid')) # Compare KerasTransformer output to raw Keras model output self._test_keras_transformer_helper(model, model_filename="keras_transformer_single_dim")
def fire_module(x, fire_id, squeeze=16, expand=64): s_id = 'fire' + str(fire_id) + '/' if K.image_data_format() == 'channels_first': channel_axis = 1 else: channel_axis = 3 x = Convolution2D(squeeze, (1, 1), padding='valid', name=s_id + sq1x1)(x) x = Activation('relu', name=s_id + relu + sq1x1)(x) left = Convolution2D(expand, (1, 1), padding='valid', name=s_id + exp1x1)(x) left = Activation('relu', name=s_id + relu + exp1x1)(left) right = Convolution2D(expand, (3, 3), padding='same', name=s_id + exp3x3)(x) right = Activation('relu', name=s_id + relu + exp3x3)(right) x = concatenate([left, right], axis=channel_axis, name=s_id + 'concat') return x # Original SqueezeNet from paper.
def keepsize_256(nx, ny, noise, depth, activation='relu', n_filters=64, l2_reg=1e-7): """ Deep residual network that keeps the size of the input throughout the whole network """ def residual(inputs, n_filters): x = ReflectionPadding2D()(inputs) x = Conv2D(n_filters, (3, 3), padding='valid', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg))(x) x = BatchNormalization()(x) x = Activation(activation)(x) x = ReflectionPadding2D()(x) x = Conv2D(n_filters, (3, 3), padding='valid', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg))(x) x = BatchNormalization()(x) x = add([x, inputs]) return x inputs = Input(shape=(nx, ny, 1)) x = GaussianNoise(noise)(inputs) x = ReflectionPadding2D()(x) x = Conv2D(n_filters, (3, 3), padding='valid', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg))(x) x0 = Activation(activation)(x) x = residual(x0, n_filters) for i in range(depth-1): x = residual(x, n_filters) x = ReflectionPadding2D()(x) x = Conv2D(n_filters, (3, 3), padding='valid', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg))(x) x = BatchNormalization()(x) x = add([x, x0]) # Upsampling for superresolution x = UpSampling2D()(x) x = ReflectionPadding2D()(x) x = Conv2D(4*n_filters, (3, 3), padding='valid', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg))(x) x = Activation(activation)(x) final = Conv2D(1, (1, 1), padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg))(x) return Model(inputs=inputs, outputs=final)
def KDDClassifier(): model = Sequential() model.add(Dense(output_dim=100, input_dim=10)) model.add(Activation("relu")) model.add(Dense(output_dim=100, input_dim=100)) model.add(Activation("relu")) model.add(Dense(output_dim=100, input_dim=100)) model.add(Activation("relu")) model.add(Dense(output_dim=100, input_dim=100)) model.add(Activation("relu")) model.add(Dense(output_dim=100, input_dim=100)) model.add(Activation("relu")) model.add(Dense(output_dim=2, input_dim=100)) model.add(Activation("softmax")) return model
def KDDRegressor(): model = Sequential() model.add(Dense(output_dim=100, input_dim=10)) model.add(Activation("relu")) model.add(Dense(output_dim=100, input_dim=100)) model.add(Activation("relu")) model.add(Dense(output_dim=100, input_dim=100)) model.add(Activation("relu")) model.add(Dense(output_dim=100, input_dim=100)) model.add(Activation("relu")) model.add(Dense(output_dim=100, input_dim=100)) model.add(Activation("relu")) model.add(Dense(output_dim=1, input_dim=100)) return model
def VSRegressor(): model = Sequential() model.add(Dense(output_dim=100, input_dim=20*8)) model.add(Activation("relu")) model.add(Dense(output_dim=100, input_dim=100)) model.add(Activation("relu")) model.add(Dense(output_dim=100, input_dim=100)) model.add(Activation("relu")) model.add(Dense(output_dim=100, input_dim=100)) model.add(Activation("relu")) model.add(Dense(output_dim=100, input_dim=100)) model.add(Activation("relu")) model.add(Dense(output_dim=20, input_dim=100)) return model
def res_block(input_tensor, nb_filters=16, block=0, subsample_factor=1): subsample = (subsample_factor, subsample_factor, subsample_factor) x = BatchNormalization(axis=4)(input_tensor) x = Activation('relu')(x) x = Convolution3D(nb_filters, 3, 3, 3, subsample=subsample, border_mode='same')(x) x = BatchNormalization(axis=4)(x) x = Activation('relu')(x) x = Convolution3D(nb_filters, 3, 3, 3, subsample=(1, 1, 1), border_mode='same')(x) if subsample_factor > 1: shortcut = Convolution3D(nb_filters, 1, 1, 1, subsample=subsample, border_mode='same')(input_tensor) else: shortcut = input_tensor x = merge([x, shortcut], mode='sum') return x
def res_block(input_tensor, nb_filters=16, block=0, subsample_factor=1): subsample = (subsample_factor, subsample_factor) x = BatchNormalization(axis=3)(input_tensor) x = Activation('relu')(x) x = Convolution2D(nb_filters, 3, 3, subsample=subsample, border_mode='same')(x) x = BatchNormalization(axis=3)(x) x = Activation('relu')(x) x = Convolution2D(nb_filters, 3, 3, subsample=(1, 1), border_mode='same')(x) if subsample_factor > 1: shortcut = Convolution2D(nb_filters, 1, 1, subsample=subsample, border_mode='same')(input_tensor) else: shortcut = input_tensor x = merge([x, shortcut], mode='sum') return x
def prep_model(inputs, N, s0pad, s1pad, c, granlevels=1): # LSTM lstm = LSTM(N, return_sequences=True, implementation=2, kernel_regularizer=l2(c['l2reg']), recurrent_regularizer=l2(c['l2reg']), bias_regularizer=l2(c['l2reg'])) x1 = inputs[0] x2 = inputs[1] h1 = lstm(x1) h2 = lstm(x2) W_x = Dense(N, kernel_initializer='glorot_uniform', use_bias=True, kernel_regularizer=l2(c['l2reg'])) W_h = Dense(N, kernel_initializer='orthogonal', use_bias=True, kernel_regularizer=l2(c['l2reg'])) sigmoid = Activation('sigmoid') a1 = multiply([x1, sigmoid( add([W_x(x1), W_h(h1)]) )]) a2 = multiply([x2, sigmoid( add([W_x(x2), W_h(h2)]) )]) # Averaging avg = Lambda(function=lambda x: K.mean(x, axis=1), output_shape=lambda shape: (shape[0], ) + shape[2:]) gran1 = avg(a1) gran2 = avg(a2) return [gran1, gran2], N
def model_cnn(net_layers, input_shape): inp = Input(shape=input_shape) model = inp for cl in net_layers['conv_layers']: model = Conv2D(filters=cl[0], kernel_size=cl[1], activation='relu')(model) if cl[4]: model = MaxPooling2D()(model) if cl[2]: model = BatchNormalization()(model) if cl[3]: model = Dropout(0.2)(model) model = Flatten()(model) for dl in net_layers['dense_layers']: model = Dense(dl[0])(model) model = Activation('relu')(model) if dl[1]: model = BatchNormalization()(model) if dl[2]: model = Dropout(0.2)(model) model = Dense(1)(model) model = Activation('sigmoid')(model) model = Model(inp, model) return model # %% # LSTM architecture # conv_layers -> [(filters, kernel_size, BatchNormaliztion, Dropout, MaxPooling)] # dense_layers -> [(num_neurons, BatchNormaliztion, Dropout)]