Python keras.layers.merge() Examples
The following are 30
code examples of keras.layers.merge().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
keras.layers
, or try the search function
.
Example #1
Source File: dcn_resnet.py From sam with MIT License | 6 votes |
def conv_block_atrous(input_tensor, kernel_size, filters, stage, block, atrous_rate=(2, 2)): nb_filter1, nb_filter2, nb_filter3 = filters bn_axis = 1 conv_name_base = 'res' + str(stage) + block + '_branch' bn_name_base = 'bn' + str(stage) + block + '_branch' x = Convolution2D(nb_filter1, 1, 1, name=conv_name_base + '2a')(input_tensor) x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2a')(x) x = Activation('relu')(x) x = AtrousConvolution2D(nb_filter2, kernel_size, kernel_size, border_mode='same', atrous_rate=atrous_rate, name=conv_name_base + '2b')(x) x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2b')(x) x = Activation('relu')(x) x = Convolution2D(nb_filter3, 1, 1, name=conv_name_base + '2c')(x) x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2c')(x) shortcut = Convolution2D(nb_filter3, 1, 1, name=conv_name_base + '1')(input_tensor) shortcut = BatchNormalization(axis=bn_axis, name=bn_name_base + '1')(shortcut) x = merge([x, shortcut], mode='sum') x = Activation('relu')(x) return x
Example #2
Source File: inception_v4.py From cnn_evaluation_smoke with GNU General Public License v3.0 | 6 votes |
def block_inception_a(input): if K.image_dim_ordering() == "th": channel_axis = 1 else: channel_axis = -1 branch_0 = conv2d_bn(input, 96, 1, 1) branch_1 = conv2d_bn(input, 64, 1, 1) branch_1 = conv2d_bn(branch_1, 96, 3, 3) branch_2 = conv2d_bn(input, 64, 1, 1) branch_2 = conv2d_bn(branch_2, 96, 3, 3) branch_2 = conv2d_bn(branch_2, 96, 3, 3) branch_3 = AveragePooling2D((3,3), strides=(1,1), border_mode='same')(input) branch_3 = conv2d_bn(branch_3, 96, 1, 1) x = merge([branch_0, branch_1, branch_2, branch_3], mode='concat', concat_axis=channel_axis) return x
Example #3
Source File: keras_yolo.py From PiCamNN with MIT License | 6 votes |
def yolo_body(inputs, num_anchors, num_classes): """Create YOLO_V2 model CNN body in Keras.""" darknet = Model(inputs, darknet_body()(inputs)) conv13 = darknet.get_layer('batchnormalization_13').output conv20 = compose( DarknetConv2D_BN_Leaky(1024, 3, 3), DarknetConv2D_BN_Leaky(1024, 3, 3))(darknet.output) # TODO: Allow Keras Lambda to use func arguments for output_shape? conv13_reshaped = Lambda( space_to_depth_x2, output_shape=space_to_depth_x2_output_shape, name='space_to_depth')(conv13) # Concat conv13 with conv20. x = merge([conv13_reshaped, conv20], mode='concat') x = DarknetConv2D_BN_Leaky(1024, 3, 3)(x) x = DarknetConv2D(num_anchors * (num_classes + 5), 1, 1)(x) return Model(inputs, x)
Example #4
Source File: aae.py From Keras-GAN with MIT License | 6 votes |
def build_encoder(self): # Encoder img = Input(shape=self.img_shape) h = Flatten()(img) h = Dense(512)(h) h = LeakyReLU(alpha=0.2)(h) h = Dense(512)(h) h = LeakyReLU(alpha=0.2)(h) mu = Dense(self.latent_dim)(h) log_var = Dense(self.latent_dim)(h) latent_repr = merge([mu, log_var], mode=lambda p: p[0] + K.random_normal(K.shape(p[0])) * K.exp(p[1] / 2), output_shape=lambda p: p[0]) return Model(img, latent_repr)
Example #5
Source File: AgentActClassifyingModel.py From end2end_dialog with MIT License | 6 votes |
def _build(self): print('Building Graph ...') inputs = Input(shape=(self.window_size, self.userTagIntent_vocab_size), name='tagIntent_input') lstm_forward = LSTM(output_dim=self.hidden_size, return_sequences=False, name='LSTM_forward')(inputs) lstm_forward = Dropout(self.dropout)(lstm_forward) lstm_backward = LSTM(output_dim=self.hidden_size, return_sequences=False, go_backwards=True, name='LSTM_backward')(inputs) lstm_backward = Dropout(self.dropout)(lstm_backward) lstm_concat = merge([lstm_forward, lstm_backward], mode='concat', concat_axis=-1, name='merge_bidirections') act_softmax = Dense(output_dim=self.agentAct_vocab_size, activation='sigmoid')(lstm_concat) self.model = Model(input=inputs, output=act_softmax) self.model.compile(optimizer=self.optimizer, loss='binary_crossentropy')
Example #6
Source File: resnet.py From convnet-study with MIT License | 6 votes |
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
Example #7
Source File: train_res.py From u-net with MIT License | 6 votes |
def _up_block(block,mrge, nb_filters): up = merge([Convolution2D(2*nb_filters, 2, 2, border_mode='same')(UpSampling2D(size=(2, 2))(block)), mrge], mode='concat', concat_axis=1) # conv = Convolution2D(4*nb_filters, 1, 1, activation='relu', border_mode='same')(up) conv = Convolution2D(nb_filters, 3, 3, activation='relu', border_mode='same')(up) conv = Convolution2D(nb_filters, 3, 3, activation='relu', border_mode='same')(conv) # conv = Convolution2D(4*nb_filters, 1, 1, activation='relu', border_mode='same')(conv) # conv = Convolution2D(nb_filters, 3, 3, activation='relu', border_mode='same')(conv) # conv = Convolution2D(nb_filters, 1, 1, activation='relu', border_mode='same')(conv) # conv = Convolution2D(4*nb_filters, 1, 1, activation='relu', border_mode='same')(conv) # conv = Convolution2D(nb_filters, 3, 3, activation='relu', border_mode='same')(conv) # conv = Convolution2D(nb_filters, 1, 1, activation='relu', border_mode='same')(conv) return conv # http://arxiv.org/pdf/1512.03385v1.pdf # 50 Layer resnet
Example #8
Source File: train_res.py From u-net with MIT License | 6 votes |
def _shortcut(input, residual): # Expand channels of shortcut to match residual. # Stride appropriately to match residual (width, height) # Should be int if network architecture is correctly configured. stride_width = input._keras_shape[2] / residual._keras_shape[2] stride_height = input._keras_shape[3] / residual._keras_shape[3] equal_channels = residual._keras_shape[1] == input._keras_shape[1] shortcut = input # 1 X 1 conv if shape is different. Else identity. if stride_width > 1 or stride_height > 1 or not equal_channels: shortcut = Convolution2D(nb_filter=residual._keras_shape[1], nb_row=1, nb_col=1, subsample=(stride_width, stride_height), init="he_normal", border_mode="valid")(input) return merge([shortcut, residual], mode="sum") # Builds a residual block with repeating bottleneck blocks.
Example #9
Source File: resnet.py From deep_learning with MIT License | 6 votes |
def conv_block(x, nb_filter, kernel_size=3): k1, k2, k3 = nb_filter shortcut = x out = Conv2D(k1, kernel_size=(1,1), strides=(2,2), padding="valid",activation="relu")(x) out = BatchNormalization(axis=3)(out) out = out = Conv2D(k2, kernel_size=(kernel_size,kernel_size), strides=(1,1), padding="same",activation="relu")(out) out = BatchNormalization()(out) out = Conv2D(k3, kernel_size=(1,1), strides=(1,1), padding="valid")(out) out = BatchNormalization(axis=3)(out) shortcut = Conv2D(k3, kernel_size=(1,1), strides=(2,2), padding="valid")(shortcut) shortcut = BatchNormalization(axis=3)(shortcut) # out = merge([out, shortcut], mode='sum') out = layers.add([out, shortcut]) out = Activation('relu')(out) return out
Example #10
Source File: resnet.py From deep_learning with MIT License | 6 votes |
def identity_block(x, nb_filter, kernel_size=3): k1, k2, k3 = nb_filter shortcut = x out = Conv2D(k1, kernel_size=(1,1), strides=(1,1),padding="valid",activation="relu")(x) out = BatchNormalization(axis=3)(out) out = Conv2D(k2, kernel_size=(3,3), strides=(1,1), padding='same',activation="relu")(out) out = BatchNormalization(axis=3)(out) out = Conv2D(k3, kernel_size=(1,1), strides=(1,1),padding="valid")(out) out = BatchNormalization(axis=3)(out) # out = merge([out, shortcut], mode='sum') out= layers.add([out,shortcut]) out = Activation('relu')(out) return out
Example #11
Source File: inception_v4.py From cnn_evaluation_smoke with GNU General Public License v3.0 | 6 votes |
def block_reduction_a(input): if K.image_dim_ordering() == "th": channel_axis = 1 else: channel_axis = -1 branch_0 = conv2d_bn(input, 384, 3, 3, subsample=(2,2), border_mode='valid') branch_1 = conv2d_bn(input, 192, 1, 1) branch_1 = conv2d_bn(branch_1, 224, 3, 3) branch_1 = conv2d_bn(branch_1, 256, 3, 3, subsample=(2,2), border_mode='valid') branch_2 = MaxPooling2D((3,3), strides=(2,2), border_mode='valid')(input) x = merge([branch_0, branch_1, branch_2], mode='concat', concat_axis=channel_axis) return x
Example #12
Source File: inception_v4.py From cnn_evaluation_smoke with GNU General Public License v3.0 | 6 votes |
def block_inception_b(input): if K.image_dim_ordering() == "th": channel_axis = 1 else: channel_axis = -1 branch_0 = conv2d_bn(input, 384, 1, 1) branch_1 = conv2d_bn(input, 192, 1, 1) branch_1 = conv2d_bn(branch_1, 224, 1, 7) branch_1 = conv2d_bn(branch_1, 256, 7, 1) branch_2 = conv2d_bn(input, 192, 1, 1) branch_2 = conv2d_bn(branch_2, 192, 7, 1) branch_2 = conv2d_bn(branch_2, 224, 1, 7) branch_2 = conv2d_bn(branch_2, 224, 7, 1) branch_2 = conv2d_bn(branch_2, 256, 1, 7) branch_3 = AveragePooling2D((3,3), strides=(1,1), border_mode='same')(input) branch_3 = conv2d_bn(branch_3, 128, 1, 1) x = merge([branch_0, branch_1, branch_2, branch_3], mode='concat', concat_axis=channel_axis) return x
Example #13
Source File: GMF.py From neural_collaborative_filtering with Apache License 2.0 | 6 votes |
def get_model(num_users, num_items, latent_dim, regs=[0,0]): # Input variables user_input = Input(shape=(1,), dtype='int32', name = 'user_input') item_input = Input(shape=(1,), dtype='int32', name = 'item_input') MF_Embedding_User = Embedding(input_dim = num_users, output_dim = latent_dim, name = 'user_embedding', init = init_normal, W_regularizer = l2(regs[0]), input_length=1) MF_Embedding_Item = Embedding(input_dim = num_items, output_dim = latent_dim, name = 'item_embedding', init = init_normal, W_regularizer = l2(regs[1]), input_length=1) # Crucial to flatten an embedding vector! user_latent = Flatten()(MF_Embedding_User(user_input)) item_latent = Flatten()(MF_Embedding_Item(item_input)) # Element-wise product of user and item embeddings predict_vector = merge([user_latent, item_latent], mode = 'mul') # Final prediction layer #prediction = Lambda(lambda x: K.sigmoid(K.sum(x)), output_shape=(1,))(predict_vector) prediction = Dense(1, activation='sigmoid', init='lecun_uniform', name = 'prediction')(predict_vector) model = Model(input=[user_input, item_input], output=prediction) return model
Example #14
Source File: inception_v4.py From cnn_evaluation_smoke with GNU General Public License v3.0 | 6 votes |
def block_reduction_b(input): if K.image_dim_ordering() == "th": channel_axis = 1 else: channel_axis = -1 branch_0 = conv2d_bn(input, 192, 1, 1) branch_0 = conv2d_bn(branch_0, 192, 3, 3, subsample=(2, 2), border_mode='valid') branch_1 = conv2d_bn(input, 256, 1, 1) branch_1 = conv2d_bn(branch_1, 256, 1, 7) branch_1 = conv2d_bn(branch_1, 320, 7, 1) branch_1 = conv2d_bn(branch_1, 320, 3, 3, subsample=(2,2), border_mode='valid') branch_2 = MaxPooling2D((3, 3), strides=(2, 2), border_mode='valid')(input) x = merge([branch_0, branch_1, branch_2], mode='concat', concat_axis=channel_axis) return x
Example #15
Source File: dcn_resnet.py From sam with MIT License | 6 votes |
def identity_block(input_tensor, kernel_size, filters, stage, block): nb_filter1, nb_filter2, nb_filter3 = filters bn_axis = 1 conv_name_base = 'res' + str(stage) + block + '_branch' bn_name_base = 'bn' + str(stage) + block + '_branch' x = Convolution2D(nb_filter1, 1, 1, name=conv_name_base + '2a')(input_tensor) x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2a')(x) x = Activation('relu')(x) x = Convolution2D(nb_filter2, kernel_size, kernel_size, border_mode='same', name=conv_name_base + '2b')(x) x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2b')(x) x = Activation('relu')(x) x = Convolution2D(nb_filter3, 1, 1, name=conv_name_base + '2c')(x) x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2c')(x) x = merge([x, input_tensor], mode='sum') x = Activation('relu')(x) return x
Example #16
Source File: resnet.py From keras-frcnn with Apache License 2.0 | 5 votes |
def identity_block(input_tensor, kernel_size, filters, stage, block, trainable=True): '''The identity_block is the block that has no conv layer at shortcut # Arguments input_tensor: input tensor kernel_size: defualt 3, the kernel size of middle conv layer at main path filters: list of integers, the nb_filters of 3 conv layer at main path stage: integer, current stage label, used for generating layer names block: 'a','b'..., current block label, used for generating layer names ''' nb_filter1, nb_filter2, nb_filter3 = filters if K.image_dim_ordering() == 'tf': bn_axis = 3 else: bn_axis = 1 conv_name_base = 'res' + str(stage) + block + '_branch' bn_name_base = 'bn' + str(stage) + block + '_branch' x = Convolution2D(nb_filter1, 1, 1, name=conv_name_base + '2a', trainable=trainable)(input_tensor) x = FixedBatchNormalization(trainable=False,axis=bn_axis, name=bn_name_base + '2a')(x) x = Activation('relu')(x) x = Convolution2D(nb_filter2, kernel_size, kernel_size, border_mode='same', name=conv_name_base + '2b', trainable=trainable)(x) x = FixedBatchNormalization(trainable=False,axis=bn_axis, name=bn_name_base + '2b')(x) x = Activation('relu')(x) x = Convolution2D(nb_filter3, 1, 1, name=conv_name_base + '2c', trainable=trainable)(x) x = FixedBatchNormalization(trainable=False,axis=bn_axis, name=bn_name_base + '2c')(x) x = merge([x, input_tensor], mode='sum') x = Activation('relu')(x) return x
Example #17
Source File: resnet.py From keras-frcnn with Apache License 2.0 | 5 votes |
def conv_block(input_tensor, kernel_size, filters, stage, block, strides=(2, 2), trainable=True): '''conv_block is the block that has a conv layer at shortcut # Arguments input_tensor: input tensor kernel_size: defualt 3, the kernel size of middle conv layer at main path filters: list of integers, the nb_filters of 3 conv layer at main path stage: integer, current stage label, used for generating layer names block: 'a','b'..., current block label, used for generating layer names Note that from stage 3, the first conv layer at main path is with subsample=(2,2) And the shortcut should have subsample=(2,2) as well ''' nb_filter1, nb_filter2, nb_filter3 = filters if K.image_dim_ordering() == 'tf': bn_axis = 3 else: bn_axis = 1 conv_name_base = 'res' + str(stage) + block + '_branch' bn_name_base = 'bn' + str(stage) + block + '_branch' x = Convolution2D(nb_filter1, 1, 1, subsample=strides, name=conv_name_base + '2a', trainable=trainable)(input_tensor) x = FixedBatchNormalization(trainable=False,axis=bn_axis, name=bn_name_base + '2a')(x) x = Activation('relu')(x) x = Convolution2D(nb_filter2, kernel_size, kernel_size, border_mode='same', name=conv_name_base + '2b', trainable=trainable)(x) x = FixedBatchNormalization(trainable=False,axis=bn_axis, name=bn_name_base + '2b')(x) x = Activation('relu')(x) x = Convolution2D(nb_filter3, 1, 1, name=conv_name_base + '2c', trainable=trainable)(x) x = FixedBatchNormalization(trainable=False,axis=bn_axis, name=bn_name_base + '2c')(x) shortcut = Convolution2D(nb_filter3, 1, 1, subsample=strides, name=conv_name_base + '1', trainable=trainable)(input_tensor) shortcut = FixedBatchNormalization(trainable=False,axis=bn_axis, name=bn_name_base + '1')(shortcut) x = merge([x, shortcut], mode='sum') x = Activation('relu')(x) return x
Example #18
Source File: keras_ddpg.py From costar_plan with Apache License 2.0 | 5 votes |
def __init__(self, env, *args, **kwargs): super(KerasDDPGAgent, self).__init__(*args, **kwargs) self.env = env #assert len(env.action_space.shape) == 1 #TODO: is there a way to output a tuple (6,1) nb_actions = sum(sum(1 for i in row if i) for row in self.env.action_space.sample()) #TODO: terminology? feature or observation? observation = env.reset() print ">>>>>>>>>>>>>>>>>>>", observation.shape # TODO: find a way to customize network actor = Sequential() actor.add(Flatten(input_shape=(1,) + observation.shape)) actor.add(Dense(16)) actor.add(Activation('relu')) actor.add(Dense(16)) actor.add(Activation('relu')) actor.add(Dense(16)) actor.add(Activation('relu')) actor.add(Dense(nb_actions)) actor.add(Activation('tanh')) actor.add(Lambda(lambda x: x * 3.14159)) print(actor.summary()) action_input = Input(shape=(nb_actions,), name='action_input') observation_input = Input(shape=(1,) + observation.shape, name='observation_input') flattened_observation = Flatten()(observation_input) x = merge([action_input, flattened_observation], mode='concat') x = Dense(32)(x) x = Activation('relu')(x) x = Dense(32)(x) x = Activation('relu')(x) x = Dense(32)(x) x = Activation('relu')(x) x = Dense(1)(x) x = Activation('linear')(x) critic = Model(input=[action_input, observation_input], output=x) print(critic.summary()) memory = SequentialMemory(limit=500000, window_length=1) random_process = OrnsteinUhlenbeckProcess(size=nb_actions, theta=.15, mu=0., sigma=.3) self.agent = DDPGAgent(nb_actions=nb_actions, actor=actor, critic=critic, critic_action_input=action_input, memory=memory, nb_steps_warmup_critic=1000, nb_steps_warmup_actor=1000, random_process=random_process, gamma=.99, target_model_update=1e-3) self.agent.compile(Adam(lr=.001, clipnorm=1.), metrics=['mae'])
Example #19
Source File: models.py From sam with MIT License | 5 votes |
def sam_vgg(x): # Dilated Convolutional Network dcn = dcn_vgg(input_tensor=x[0]) # Attentive Convolutional LSTM att_convlstm = Lambda(repeat, repeat_shape)(dcn.output) att_convlstm = AttentiveConvLSTM(nb_filters_in=512, nb_filters_out=512, nb_filters_att=512, nb_cols=3, nb_rows=3)(att_convlstm) # Learned Prior (1) priors1 = LearningPrior(nb_gaussian=nb_gaussian, init=gaussian_priors_init)(x[1]) concateneted = merge([att_convlstm, priors1], mode='concat', concat_axis=1) learned_priors1 = AtrousConvolution2D(512, 5, 5, border_mode='same', activation='relu', atrous_rate=(4, 4))(concateneted) # Learned Prior (2) priors2 = LearningPrior(nb_gaussian=nb_gaussian, init=gaussian_priors_init)(x[1]) concateneted = merge([learned_priors1, priors2], mode='concat', concat_axis=1) learned_priors2 = AtrousConvolution2D(512, 5, 5, border_mode='same', activation='relu', atrous_rate=(4, 4))(concateneted) # Final Convolutional Layer outs = Convolution2D(1, 1, 1, border_mode='same', activation='relu')(learned_priors2) outs_up = Lambda(upsampling, upsampling_shape)(outs) return [outs_up, outs_up, outs_up]
Example #20
Source File: resnet.py From keras-frcnn with Apache License 2.0 | 5 votes |
def identity_block_td(input_tensor, kernel_size, filters, stage, block, trainable=True): '''The identity_block is the block that has no conv layer at shortcut # Arguments input_tensor: input tensor kernel_size: defualt 3, the kernel size of middle conv layer at main path filters: list of integers, the nb_filters of 3 conv layer at main path stage: integer, current stage label, used for generating layer names block: 'a','b'..., current block label, used for generating layer names ''' nb_filter1, nb_filter2, nb_filter3 = filters if K.image_dim_ordering() == 'tf': bn_axis = 3 else: bn_axis = 1 conv_name_base = 'res' + str(stage) + block + '_branch' bn_name_base = 'bn' + str(stage) + block + '_branch' x = TimeDistributed(Convolution2D(nb_filter1, 1, 1, trainable=trainable, init='normal'), name=conv_name_base + '2a')(input_tensor) x = TimeDistributed(FixedBatchNormalization(trainable=False,axis=bn_axis), name=bn_name_base + '2a')(x) x = Activation('relu')(x) x = TimeDistributed(Convolution2D(nb_filter2, kernel_size, kernel_size, trainable=trainable, init='normal',border_mode='same'), name=conv_name_base + '2b')(x) x = TimeDistributed(FixedBatchNormalization(trainable=False,axis=bn_axis), name=bn_name_base + '2b')(x) x = Activation('relu')(x) x = TimeDistributed(Convolution2D(nb_filter3, 1, 1, trainable=trainable, init='normal'), name=conv_name_base + '2c')(x) x = TimeDistributed(FixedBatchNormalization(trainable=False,axis=bn_axis), name=bn_name_base + '2c')(x) x = merge([x, input_tensor], mode='sum') x = Activation('relu')(x) return x
Example #21
Source File: Densenet.py From CNNArt with Apache License 2.0 | 5 votes |
def denseblock_altern(x, nb_layers, nb_filter, growth_rate, dropout_rate=None, weight_decay=1E-4): """Build a denseblock where the output of each conv_factory is fed to subsequent ones. (Alternative of a above) :param x: keras model :param nb_layers: int -- the number of layers of conv_ factory to append to the model. :param nb_filter: int -- number of filters :param dropout_rate: int -- dropout rate :param weight_decay: int -- weight decay factor :returns: keras model with nb_layers of conv_factory appended :rtype: keras model * The main difference between this implementation and the implementation above is that the one above """ if K.image_dim_ordering() == "th": concat_axis = 1 elif K.image_dim_ordering() == "tf": concat_axis = -1 for i in range(nb_layers): merge_tensor = conv_factory(x, growth_rate, dropout_rate, weight_decay) x = merge([merge_tensor, x], mode='concat', concat_axis=concat_axis) nb_filter += growth_rate return x, nb_filter
Example #22
Source File: resnet.py From keras-frcnn with Apache License 2.0 | 5 votes |
def conv_block_td(input_tensor, kernel_size, filters, stage, block, strides=(2, 2), trainable=True): '''conv_block is the block that has a conv layer at shortcut # Arguments input_tensor: input tensor kernel_size: defualt 3, the kernel size of middle conv layer at main path filters: list of integers, the nb_filters of 3 conv layer at main path stage: integer, current stage label, used for generating layer names block: 'a','b'..., current block label, used for generating layer names Note that from stage 3, the first conv layer at main path is with subsample=(2,2) And the shortcut should have subsample=(2,2) as well ''' nb_filter1, nb_filter2, nb_filter3 = filters if K.image_dim_ordering() == 'tf': bn_axis = 3 else: bn_axis = 1 conv_name_base = 'res' + str(stage) + block + '_branch' bn_name_base = 'bn' + str(stage) + block + '_branch' x = TimeDistributed(Convolution2D(nb_filter1, 1, 1, subsample=strides, trainable=trainable, init='normal'), name=conv_name_base + '2a')(input_tensor) x = TimeDistributed(FixedBatchNormalization(trainable=False,axis=bn_axis), name=bn_name_base + '2a')(x) x = Activation('relu')(x) x = TimeDistributed(Convolution2D(nb_filter2, kernel_size, kernel_size, border_mode='same', trainable=trainable, init='normal'), name=conv_name_base + '2b')(x) x = TimeDistributed(FixedBatchNormalization(trainable=False,axis=bn_axis), name=bn_name_base + '2b')(x) x = Activation('relu')(x) x = TimeDistributed(Convolution2D(nb_filter3, 1, 1, init='normal'), name=conv_name_base + '2c', trainable=trainable)(x) x = TimeDistributed(FixedBatchNormalization(trainable=False,axis=bn_axis), name=bn_name_base + '2c')(x) shortcut = TimeDistributed(Convolution2D(nb_filter3, 1, 1, subsample=strides, trainable=trainable, init='normal'), name=conv_name_base + '1')(input_tensor) shortcut = TimeDistributed(FixedBatchNormalization(trainable=False,axis=bn_axis), name=bn_name_base + '1')(shortcut) x = merge([x, shortcut], mode='sum') x = Activation('relu')(x) return x
Example #23
Source File: models.py From sam with MIT License | 5 votes |
def sam_resnet(x): # Dilated Convolutional Network dcn = dcn_resnet(input_tensor=x[0]) conv_feat = Convolution2D(512, 3, 3, border_mode='same', activation='relu')(dcn.output) # Attentive Convolutional LSTM att_convlstm = Lambda(repeat, repeat_shape)(conv_feat) att_convlstm = AttentiveConvLSTM(nb_filters_in=512, nb_filters_out=512, nb_filters_att=512, nb_cols=3, nb_rows=3)(att_convlstm) # Learned Prior (1) priors1 = LearningPrior(nb_gaussian=nb_gaussian, init=gaussian_priors_init)(x[1]) concateneted = merge([att_convlstm, priors1], mode='concat', concat_axis=1) learned_priors1 = AtrousConvolution2D(512, 5, 5, border_mode='same', activation='relu', atrous_rate=(4, 4))(concateneted) # Learned Prior (2) priors2 = LearningPrior(nb_gaussian=nb_gaussian, init=gaussian_priors_init)(x[1]) concateneted = merge([learned_priors1, priors2], mode='concat', concat_axis=1) learned_priors2 = AtrousConvolution2D(512, 5, 5, border_mode='same', activation='relu', atrous_rate=(4, 4))(concateneted) # Final Convolutional Layer outs = Convolution2D(1, 1, 1, border_mode='same', activation='relu')(learned_priors2) outs_up = Lambda(upsampling, upsampling_shape)(outs) return [outs_up, outs_up, outs_up]
Example #24
Source File: layer_utils.py From keras-extras with Apache License 2.0 | 5 votes |
def make_densedense(output_dim, inputs): out_arr = [] for layer in inputs: out_dense = Dense(output_dim)(layer) out_arr.append(out_dense) if len(out_arr) == 1: return out_arr[0] else: return merge(out_arr, mode='sum')
Example #25
Source File: wrn_batchnorm.py From BatchRenormalization with MIT License | 5 votes |
def ___conv4_block(input, k=1, dropout=0.0): init = input channel_axis = 1 if K.image_dim_ordering() == "th" else -1 # Check if input number of filters is same as 64 * k, else create convolution2d for this input if K.image_dim_ordering() == "th": if init._keras_shape[1] != 64 * k: init = Convolution2D(64 * k, 1, 1, activation='linear', border_mode='same')(init) else: if init._keras_shape[-1] != 64 * k: init = Convolution2D(64 * k, 1, 1, activation='linear', border_mode='same')(init) x = Convolution2D(64 * k, 3, 3, border_mode='same')(input) x = BatchNormalization(axis=channel_axis)(x) x = Activation('relu')(x) if dropout > 0.0: x = Dropout(dropout)(x) x = Convolution2D(64 * k, 3, 3, border_mode='same')(x) x = BatchNormalization(axis=channel_axis)(x) x = Activation('relu')(x) m = merge([init, x], mode='sum') return m
Example #26
Source File: wrn_batchnorm.py From BatchRenormalization with MIT License | 5 votes |
def __conv3_block(input, k=1, dropout=0.0): init = input channel_axis = 1 if K.image_dim_ordering() == "th" else -1 # Check if input number of filters is same as 32 * k, else create convolution2d for this input if K.image_dim_ordering() == "th": if init._keras_shape[1] != 32 * k: init = Convolution2D(32 * k, 1, 1, activation='linear', border_mode='same')(init) else: if init._keras_shape[-1] != 32 * k: init = Convolution2D(32 * k, 1, 1, activation='linear', border_mode='same')(init) x = Convolution2D(32 * k, 3, 3, border_mode='same')(input) x = BatchNormalization(axis=channel_axis)(x) x = Activation('relu')(x) if dropout > 0.0: x = Dropout(dropout)(x) x = Convolution2D(32 * k, 3, 3, border_mode='same')(x) x = BatchNormalization(axis=channel_axis)(x) x = Activation('relu')(x) m = merge([init, x], mode='sum') return m
Example #27
Source File: wrn_batchnorm.py From BatchRenormalization with MIT License | 5 votes |
def __conv2_block(input, k=1, dropout=0.0): init = input channel_axis = 1 if K.image_dim_ordering() == "th" else -1 # Check if input number of filters is same as 16 * k, else create convolution2d for this input if K.image_dim_ordering() == "th": if init._keras_shape[1] != 16 * k: init = Convolution2D(16 * k, 1, 1, activation='linear', border_mode='same')(init) else: if init._keras_shape[-1] != 16 * k: init = Convolution2D(16 * k, 1, 1, activation='linear', border_mode='same')(init) x = Convolution2D(16 * k, 3, 3, border_mode='same')(input) x = BatchNormalization(axis=channel_axis)(x) x = Activation('relu')(x) if dropout > 0.0: x = Dropout(dropout)(x) x = Convolution2D(16 * k, 3, 3, border_mode='same')(x) x = BatchNormalization(axis=channel_axis)(x) x = Activation('relu')(x) m = merge([init, x], mode='sum') return m
Example #28
Source File: SlotTaggingModel_multitask.py From end2end_dialog with MIT License | 5 votes |
def _build(self): print('Building Graph ...') words_input = Input(shape=(self.maxlen_userUtter,), dtype='int32', name='words_input') # reserve 0 for masking, therefore vocab_size + 1 embeddings = Embedding(input_dim=self.word_vocab_size + 1, output_dim=self.embedding_size, input_length=self.maxlen_userUtter, mask_zero=True)(words_input) embeddings = Dropout(self.dropout)(embeddings) lstm_forward = LSTM(output_dim=self.hidden_size, return_sequences=True, name='LSTM_forward')(embeddings) lstm_forward = Dropout(self.dropout)(lstm_forward) lstm_backward = LSTM(output_dim=self.hidden_size, return_sequences=True, go_backwards=True, name='LSTM_backward')(embeddings) lstm_backward = Dropout(self.dropout)(lstm_backward) lstm_concat = merge([lstm_forward, lstm_backward], mode='concat', concat_axis=-1, name='merge_bidirections') slot_softmax_seq = TimeDistributed(Dense( output_dim=self.userTag_vocab_size, activation='softmax'), name='slot_output')(lstm_concat) intent_summary = LSTM(output_dim=self.hidden_size, return_sequences=False, name='summarize_to_dense')(lstm_concat) intent_summary = Dropout(self.dropout)(intent_summary) # intent_softmax = Dense(output_dim=self.userIntent_vocab_size, # activation='softmax', name='intent_output')(intent_summary) intent_softmax = Dense(output_dim=self.userIntent_vocab_size, activation='sigmoid', name='intent_output')(intent_summary) self.model = Model(input=words_input, output=[ slot_softmax_seq, intent_softmax]) self.model.compile(optimizer=self.optimizer, # metrics=['accuracy'], sample_weight_mode={ 'slot_output': 'temporal', 'intent_output': None}, loss={'slot_output': self.loss, 'intent_output': 'binary_crossentropy'})
Example #29
Source File: sim-gan.py From SimGAN with MIT License | 5 votes |
def refiner_network(input_image_tensor): """ The refiner network, Rθ, is a residual network (ResNet). It modifies the synthetic image on a pixel level, rather than holistically modifying the image content, preserving the global structure and annotations. :param input_image_tensor: Input tensor that corresponds to a synthetic image. :return: Output tensor that corresponds to a refined synthetic image. """ def resnet_block(input_features, nb_features=64, nb_kernel_rows=3, nb_kernel_cols=3): """ A ResNet block with two `nb_kernel_rows` x `nb_kernel_cols` convolutional layers, each with `nb_features` feature maps. See Figure 6 in https://arxiv.org/pdf/1612.07828v1.pdf. :param input_features: Input tensor to ResNet block. :return: Output tensor from ResNet block. """ y = layers.Convolution2D(nb_features, nb_kernel_rows, nb_kernel_cols, border_mode='same')(input_features) y = layers.Activation('relu')(y) y = layers.Convolution2D(nb_features, nb_kernel_rows, nb_kernel_cols, border_mode='same')(y) y = layers.merge([input_features, y], mode='sum') return layers.Activation('relu')(y) # an input image of size w × h is convolved with 3 × 3 filters that output 64 feature maps x = layers.Convolution2D(64, 3, 3, border_mode='same', activation='relu')(input_image_tensor) # the output is passed through 4 ResNet blocks for _ in range(4): x = resnet_block(x) # the output of the last ResNet block is passed to a 1 × 1 convolutional layer producing 1 feature map # corresponding to the refined synthetic image return layers.Convolution2D(img_channels, 1, 1, border_mode='same', activation='tanh')(x)
Example #30
Source File: artificial_example.py From mann with GNU General Public License v3.0 | 5 votes |
def neural_network(domain_adaptation=False): """ moment alignment neural network (MANN) - Zellinger, Werner, et al. "Robust unsupervised domain adaptation for neural networks via moment alignment.", arXiv preprint arXiv:1711.06114, 2017 """ # layer definition input_s = Input(shape=(2,), name='souce_input') input_t = Input(shape=(2,), name='target_input') encoding = Dense(N_HIDDEN_NODES, activation='sigmoid', name='hidden') prediction = Dense(N_CLASSES, activation='softmax', name='pred') # network architecture encoded_s = encoding(input_s) encoded_t = encoding(input_t) pred_s = prediction(encoded_s) pred_t = prediction(encoded_t) dense_s_t = merge([encoded_s,encoded_t], mode='concat', concat_axis=1) # input/output definition nn = Model(input=[input_s,input_t], output=[pred_s,pred_t,dense_s_t]) # seperate model for activation visualization visualize_model = Model(input=[input_s,input_t], output=[encoded_s,encoded_t]) # compile model if domain_adaptation==False: cmd_weight = 0. else: # Please note that the loss weight of the cmd is one per default # (see paper). cmd_weight = 1. nn.compile(loss=['categorical_crossentropy', 'categorical_crossentropy',cmd], loss_weights=[1.,0.,cmd_weight], optimizer=Adadelta(), metrics=['accuracy']) return nn, visualize_model