Python tensorflow.keras.layers.concatenate() Examples
The following are 30
code examples of tensorflow.keras.layers.concatenate().
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
tensorflow.keras.layers
, or try the search function
.
Example #1
Source File: dense.py From MIScnn with GNU General Public License v3.0 | 6 votes |
def expanding_layer_2D(input, neurons, concatenate_link, ba_norm, ba_norm_momentum): up = concatenate([Conv2DTranspose(neurons, (2, 2), strides=(2, 2), padding='same')(input), concatenate_link], axis=-1) conv1 = Conv2D(neurons, (3, 3,), activation='relu', padding='same')(up) if ba_norm : conv1 = BatchNormalization(momentum=ba_norm_momentum)(conv1) conc1 = concatenate([up, conv1], axis=-1) conv2 = Conv2D(neurons, (3, 3), activation='relu', padding='same')(conc1) if ba_norm : conv2 = BatchNormalization(momentum=ba_norm_momentum)(conv2) conc2 = concatenate([up, conv2], axis=-1) return conc2 #-----------------------------------------------------# # Subroutines 3D # #-----------------------------------------------------# # Create a contracting layer
Example #2
Source File: dropout_vnet.py From bcnn with MIT License | 6 votes |
def up_stage(inputs, skip, filters, kernel_size=3, activation="relu", padding="SAME"): up = UpSampling3D()(inputs) up = Conv3D(filters, 2, activation=activation, padding=padding)(up) up = GroupNormalization()(up) merge = concatenate([skip, up]) merge = GroupNormalization()(merge) conv = Conv3D(filters, kernel_size, activation=activation, padding=padding)(merge) conv = GroupNormalization()(conv) conv = Conv3D(filters, kernel_size, activation=activation, padding=padding)(conv) conv = GroupNormalization()(conv) conv = SpatialDropout3D(0.5)(conv, training=True) return conv
Example #3
Source File: dropout_unet.py From bcnn with MIT License | 6 votes |
def up_stage(inputs, skip, filters, kernel_size=3, activation="relu", padding="SAME"): up = UpSampling2D()(inputs) up = Conv2D(filters, 2, activation=activation, padding=padding)(up) up = GroupNormalization()(up) merge = concatenate([skip, up]) merge = GroupNormalization()(merge) conv = Conv2D(filters, kernel_size, activation=activation, padding=padding)(merge) conv = GroupNormalization()(conv) conv = Conv2D(filters, kernel_size, activation=activation, padding=padding)(conv) conv = GroupNormalization()(conv) conv = SpatialDropout2D(0.5)(conv, training=True) return conv
Example #4
Source File: bayesian_unet.py From bcnn with MIT License | 6 votes |
def up_stage(inputs, skip, filters, prior_fn, kernel_size=3, activation="relu", padding="SAME"): up = UpSampling2D()(inputs) up = tfp.layers.Convolution2DFlipout(filters, 2, activation=activation, padding=padding, kernel_prior_fn=prior_fn)(up) up = GroupNormalization()(up) merge = concatenate([skip, up]) merge = GroupNormalization()(merge) conv = tfp.layers.Convolution2DFlipout(filters, kernel_size, activation=activation, padding=padding, kernel_prior_fn=prior_fn)(merge) conv = GroupNormalization()(conv) conv = tfp.layers.Convolution2DFlipout(filters, kernel_size, activation=activation, padding=padding, kernel_prior_fn=prior_fn)(conv) conv = GroupNormalization()(conv) return conv
Example #5
Source File: bayesian_vnet.py From bcnn with MIT License | 6 votes |
def up_stage(inputs, skip, filters, prior_fn, kernel_size=3, activation="relu", padding="SAME"): up = UpSampling3D()(inputs) up = tfp.layers.Convolution3DFlipout(filters, 2, activation=activation, padding=padding, kernel_prior_fn=prior_fn)(up) up = GroupNormalization()(up) merge = concatenate([skip, up]) merge = GroupNormalization()(merge) conv = tfp.layers.Convolution3DFlipout(filters, kernel_size, activation=activation, padding=padding, kernel_prior_fn=prior_fn)(merge) conv = GroupNormalization()(conv) conv = tfp.layers.Convolution3DFlipout(filters, kernel_size, activation=activation, padding=padding, kernel_prior_fn=prior_fn)(conv) conv = GroupNormalization()(conv) return conv
Example #6
Source File: residual.py From MIScnn with GNU General Public License v3.0 | 6 votes |
def expanding_layer_2D(input, neurons, concatenate_link, ba_norm, ba_norm_momentum): up = concatenate([Conv2DTranspose(neurons, (2, 2), strides=(2, 2), padding='same')(input), concatenate_link], axis=-1) conv1 = Conv2D(neurons, (3, 3,), activation='relu', padding='same')(up) if ba_norm : conv1 = BatchNormalization(momentum=ba_norm_momentum)(conv1) conv2 = Conv2D(neurons, (3, 3), activation='relu', padding='same')(conv1) if ba_norm : conv2 = BatchNormalization(momentum=ba_norm_momentum)(conv2) shortcut = Conv2D(neurons, (1, 1), activation='relu', padding="same")(up) add_layer = add([shortcut, conv2]) return add_layer #-----------------------------------------------------# # Subroutines 3D # #-----------------------------------------------------# # Create a contracting layer
Example #7
Source File: squeezenet.py From keras-YOLOv3-model-set with MIT License | 6 votes |
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 = Conv2D(squeeze, (1, 1), padding='valid', name=s_id + sq1x1)(x) x = Activation('relu', name=s_id + relu + sq1x1)(x) left = Conv2D(expand, (1, 1), padding='valid', name=s_id + exp1x1)(x) left = Activation('relu', name=s_id + relu + exp1x1)(left) right = Conv2D(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.
Example #8
Source File: attention.py From keras-attention-mechanism with Apache License 2.0 | 6 votes |
def attention_3d_block(hidden_states): """ Many-to-one attention mechanism for Keras. @param hidden_states: 3D tensor with shape (batch_size, time_steps, input_dim). @return: 2D tensor with shape (batch_size, 128) @author: felixhao28. """ hidden_size = int(hidden_states.shape[2]) # Inside dense layer # hidden_states dot W => score_first_part # (batch_size, time_steps, hidden_size) dot (hidden_size, hidden_size) => (batch_size, time_steps, hidden_size) # W is the trainable weight matrix of attention Luong's multiplicative style score score_first_part = Dense(hidden_size, use_bias=False, name='attention_score_vec')(hidden_states) # score_first_part dot last_hidden_state => attention_weights # (batch_size, time_steps, hidden_size) dot (batch_size, hidden_size) => (batch_size, time_steps) h_t = Lambda(lambda x: x[:, -1, :], output_shape=(hidden_size,), name='last_hidden_state')(hidden_states) score = dot([score_first_part, h_t], [2, 1], name='attention_score') attention_weights = Activation('softmax', name='attention_weight')(score) # (batch_size, time_steps, hidden_size) dot (batch_size, time_steps) => (batch_size, hidden_size) context_vector = dot([hidden_states, attention_weights], [1, 1], name='context_vector') pre_activation = concatenate([context_vector, h_t], name='attention_output') attention_vector = Dense(128, use_bias=False, activation='tanh', name='attention_vector')(pre_activation) return attention_vector
Example #9
Source File: get_activations_test.py From keract with MIT License | 6 votes |
def test_shape_1(self): # model definition i1 = Input(shape=(10,), name='i1') i2 = Input(shape=(10,), name='i2') a = Dense(1, name='fc1')(i1) b = Dense(1, name='fc2')(i2) c = concatenate([a, b], name='concat') d = Dense(1, name='out')(c) model = Model(inputs=[i1, i2], outputs=[d]) # inputs to the model x = [np.random.uniform(size=(32, 10)), np.random.uniform(size=(32, 10))] # call to fetch the activations of the model. activations = get_activations(model, x, auto_compile=True) # OrderedDict so its ok to .values() self.assertListEqual([a.shape for a in activations.values()], [(32, 10), (32, 10), (32, 1), (32, 1), (32, 2), (32, 1)])
Example #10
Source File: get_activations_test.py From keract with MIT License | 6 votes |
def test_inputs_order(self): i10 = Input(shape=(10,), name='i1') i40 = Input(shape=(40,), name='i4') i30 = Input(shape=(30,), name='i3') i20 = Input(shape=(20,), name='i2') a = Dense(1, name='fc1')(concatenate([i10, i40, i30, i20], name='concat')) model = Model(inputs=[i40, i30, i20, i10], outputs=[a]) x = [ np.random.uniform(size=(1, 40)), np.random.uniform(size=(1, 30)), np.random.uniform(size=(1, 20)), np.random.uniform(size=(1, 10)) ] acts = get_activations(model, x) self.assertListEqual(list(acts['i1'].shape), [1, 10]) self.assertListEqual(list(acts['i2'].shape), [1, 20]) self.assertListEqual(list(acts['i3'].shape), [1, 30]) self.assertListEqual(list(acts['i4'].shape), [1, 40])
Example #11
Source File: plain.py From MIScnn with GNU General Public License v3.0 | 5 votes |
def create_model_2D(self, input_shape, n_labels=2): # Input layer inputs = Input(input_shape) # Start the CNN Model chain with adding the inputs as first tensor cnn_chain = inputs # Cache contracting normalized conv layers # for later copy & concatenate links contracting_convs = [] # Contracting layers for i in range(0, len(self.feature_map)): neurons = self.feature_map[i] cnn_chain = conv_layer_2D(cnn_chain, neurons, self.ba_norm, strides=1) cnn_chain = conv_layer_2D(cnn_chain, neurons, self.ba_norm, strides=1) contracting_convs.append(cnn_chain) cnn_chain = MaxPooling2D(pool_size=(2, 2))(cnn_chain) # Middle Layer neurons = self.feature_map[-1] cnn_chain = conv_layer_2D(cnn_chain, neurons, self.ba_norm, strides=1) cnn_chain = conv_layer_2D(cnn_chain, neurons, self.ba_norm, strides=1) # Expanding Layers for i in reversed(range(0, len(self.feature_map))): neurons = self.feature_map[i] cnn_chain = Conv2DTranspose(neurons, (2, 2), strides=(2, 2), padding='same')(cnn_chain) cnn_chain = concatenate([cnn_chain, contracting_convs[i]], axis=-1) cnn_chain = conv_layer_2D(cnn_chain, neurons, self.ba_norm, strides=1) cnn_chain = conv_layer_2D(cnn_chain, neurons, self.ba_norm, strides=1) # Output Layer conv_out = Conv2D(n_labels, (1, 1), activation=self.activation)(cnn_chain) # Create Model with associated input and output layers model = Model(inputs=[inputs], outputs=[conv_out]) # Return model return model #---------------------------------------------# # Create 3D Model # #---------------------------------------------#
Example #12
Source File: standard.py From MIScnn with GNU General Public License v3.0 | 5 votes |
def expanding_layer_2D(input, neurons, concatenate_link, ba_norm, ba_norm_momentum): up = concatenate([Conv2DTranspose(neurons, (2, 2), strides=(2, 2), padding='same')(input), concatenate_link], axis=-1) conv1 = Conv2D(neurons, (3, 3,), activation='relu', padding='same')(up) if ba_norm : conv1 = BatchNormalization(momentum=ba_norm_momentum)(conv1) conv2 = Conv2D(neurons, (3, 3), activation='relu', padding='same')(conv1) if ba_norm : conv2 = BatchNormalization(momentum=ba_norm_momentum)(conv2) return conv2 #-----------------------------------------------------# # Subroutines 3D # #-----------------------------------------------------# # Create a contracting layer
Example #13
Source File: standard.py From MIScnn with GNU General Public License v3.0 | 5 votes |
def create_model_3D(self, input_shape, n_labels=2): # Input layer inputs = Input(input_shape) # Start the CNN Model chain with adding the inputs as first tensor cnn_chain = inputs # Cache contracting normalized conv layers # for later copy & concatenate links contracting_convs = [] # Contracting Layers for i in range(0, self.depth): neurons = self.n_filters * 2**i cnn_chain, last_conv = contracting_layer_3D(cnn_chain, neurons, self.ba_norm, self.ba_norm_momentum) contracting_convs.append(last_conv) # Middle Layer neurons = self.n_filters * 2**self.depth cnn_chain = middle_layer_3D(cnn_chain, neurons, self.ba_norm, self.ba_norm_momentum) # Expanding Layers for i in reversed(range(0, self.depth)): neurons = self.n_filters * 2**i cnn_chain = expanding_layer_3D(cnn_chain, neurons, contracting_convs[i], self.ba_norm, self.ba_norm_momentum) # Output Layer conv_out = Conv3D(n_labels, (1, 1, 1), activation=self.activation)(cnn_chain) # Create Model with associated input and output layers model = Model(inputs=[inputs], outputs=[conv_out]) # Return model return model #-----------------------------------------------------# # Subroutines 2D # #-----------------------------------------------------# # Create a contracting layer
Example #14
Source File: standard.py From MIScnn with GNU General Public License v3.0 | 5 votes |
def create_model_2D(self, input_shape, n_labels=2): # Input layer inputs = Input(input_shape) # Start the CNN Model chain with adding the inputs as first tensor cnn_chain = inputs # Cache contracting normalized conv layers # for later copy & concatenate links contracting_convs = [] # Contracting Layers for i in range(0, self.depth): neurons = self.n_filters * 2**i cnn_chain, last_conv = contracting_layer_2D(cnn_chain, neurons, self.ba_norm, self.ba_norm_momentum) contracting_convs.append(last_conv) # Middle Layer neurons = self.n_filters * 2**self.depth cnn_chain = middle_layer_2D(cnn_chain, neurons, self.ba_norm, self.ba_norm_momentum) # Expanding Layers for i in reversed(range(0, self.depth)): neurons = self.n_filters * 2**i cnn_chain = expanding_layer_2D(cnn_chain, neurons, contracting_convs[i], self.ba_norm, self.ba_norm_momentum) # Output Layer conv_out = Conv2D(n_labels, (1, 1), activation=self.activation)(cnn_chain) # Create Model with associated input and output layers model = Model(inputs=[inputs], outputs=[conv_out]) # Return model return model #---------------------------------------------# # Create 3D Model # #---------------------------------------------#
Example #15
Source File: residual.py From MIScnn with GNU General Public License v3.0 | 5 votes |
def create_model_2D(self, input_shape, n_labels=2): # Input layer inputs = Input(input_shape) # Start the CNN Model chain with adding the inputs as first tensor cnn_chain = inputs # Cache contracting normalized conv layers # for later copy & concatenate links contracting_convs = [] # Contracting Layers for i in range(0, self.depth): neurons = self.n_filters * 2**i cnn_chain, last_conv = contracting_layer_2D(cnn_chain, neurons, self.ba_norm, self.ba_norm_momentum) contracting_convs.append(last_conv) # Middle Layer neurons = self.n_filters * 2**self.depth cnn_chain = middle_layer_2D(cnn_chain, neurons, self.ba_norm, self.ba_norm_momentum) # Expanding Layers for i in reversed(range(0, self.depth)): neurons = self.n_filters * 2**i cnn_chain = expanding_layer_2D(cnn_chain, neurons, contracting_convs[i], self.ba_norm, self.ba_norm_momentum) # Output Layer conv_out = Conv2D(n_labels, (1, 1), activation=self.activation)(cnn_chain) # Create Model with associated input and output layers model = Model(inputs=[inputs], outputs=[conv_out]) # Return model return model #---------------------------------------------# # Create 3D Model # #---------------------------------------------#
Example #16
Source File: multiRes.py From MIScnn with GNU General Public License v3.0 | 5 votes |
def MultiResBlock_3D(U, inp, alpha = 1.67): ''' MultiRes Block Arguments: U {int} -- Number of filters in a corrsponding UNet stage inp {keras layer} -- input layer Returns: [keras layer] -- [output layer] ''' W = alpha * U shortcut = inp shortcut = conv3d_bn(shortcut, int(W*0.167) + int(W*0.333) + int(W*0.5), 1, 1, 1, activation=None, padding='same') conv3x3 = conv3d_bn(inp, int(W*0.167), 3, 3, 3, activation='relu', padding='same') conv5x5 = conv3d_bn(conv3x3, int(W*0.333), 3, 3, 3, activation='relu', padding='same') conv7x7 = conv3d_bn(conv5x5, int(W*0.5), 3, 3, 3, activation='relu', padding='same') out = concatenate([conv3x3, conv5x5, conv7x7], axis=4) out = BatchNormalization(axis=4)(out) out = add([shortcut, out]) out = Activation('relu')(out) out = BatchNormalization(axis=4)(out) return out
Example #17
Source File: residual.py From MIScnn with GNU General Public License v3.0 | 5 votes |
def expanding_layer_3D(input, neurons, concatenate_link, ba_norm, ba_norm_momentum): up = concatenate([Conv3DTranspose(neurons, (2, 2, 2), strides=(2, 2, 2), padding='same')(input), concatenate_link], axis=4) conv1 = Conv3D(neurons, (3, 3, 3), activation='relu', padding='same')(up) if ba_norm : conv1 = BatchNormalization(momentum=ba_norm_momentum)(conv1) conv2 = Conv3D(neurons, (3, 3, 3), activation='relu', padding='same')(conv1) if ba_norm : conv2 = BatchNormalization(momentum=ba_norm_momentum)(conv2) shortcut = Conv3D(neurons, (1, 1, 1), activation='relu', padding="same")(up) add_layer = add([shortcut, conv2]) return add_layer
Example #18
Source File: compact.py From MIScnn with GNU General Public License v3.0 | 5 votes |
def contracting_layer_3D(input, neurons, ba_norm, ba_norm_momentum): conv1 = Conv3D(neurons, (3,3,3), activation='relu', padding='same')(input) if ba_norm : conv1 = BatchNormalization(momentum=ba_norm_momentum)(conv1) conv2 = Conv3D(neurons, (3,3,3), activation='relu', padding='same')(conv1) if ba_norm : conv2 = BatchNormalization(momentum=ba_norm_momentum)(conv2) conc = concatenate([input, conv2], axis=-1) pool = MaxPooling3D(pool_size=(2, 2, 2))(conc) return pool, conc # Create the middle layer between the contracting and expanding layers
Example #19
Source File: inception_v2.py From keras_imagenet with MIT License | 5 votes |
def inception(x, filters): """Utility function to implement the inception module. # Arguments x: input tensor. filters: a list of filter sizes. # Returns Output tensor after applying the inception. """ if len(filters) != 4: raise ValueError('filters should have 4 components') if len(filters[1]) != 2 or len(filters[2]) != 2: raise ValueError('incorrect spec of filters') branch1x1 = conv2d_bn(x, filters[0], (1, 1)) branch3x3 = conv2d_bn(x, filters[1][0], (1, 1)) branch3x3 = conv2d_bn(branch3x3, filters[1][1], (3, 3)) # branch5x5 is implemented with two 3x3 conv2d's branch5x5 = conv2d_bn(x, filters[2][0], (1, 1)) branch5x5 = conv2d_bn(branch5x5, filters[2][1], (3, 3)) branch5x5 = conv2d_bn(branch5x5, filters[2][1], (3, 3)) # use AveragePooling2D here branchpool = layers.AveragePooling2D( pool_size=(3, 3), strides=(1, 1), padding='same')(x) branchpool = conv2d_bn(branchpool, filters[3], (1, 1)) concat_axis = 1 if backend.image_data_format() == 'channels_first' else 3 x = layers.concatenate( [branch1x1, branch3x3, branch5x5, branchpool], axis=concat_axis) return x
Example #20
Source File: inception_v2.py From keras_imagenet with MIT License | 5 votes |
def inception_s2(x, filters): """Utility function to implement the 'stride-2' inception module. # Arguments x: input tensor. filters: a list of filter sizes. # Returns Output tensor after applying the 'stride-2' inception. """ if len(filters) != 2: raise ValueError('filters should have 2 components') if len(filters[0]) != 2 or len(filters[1]) != 2: raise ValueError('incorrect spec of filters') branch3x3 = conv2d_bn(x, filters[0][0], (1, 1)) branch3x3 = conv2d_bn(branch3x3, filters[0][1], (3, 3), strides=(2, 2)) branch5x5 = conv2d_bn(x, filters[1][0], (1, 1)) branch5x5 = conv2d_bn(branch5x5, filters[1][1], (3, 3)) branch5x5 = conv2d_bn(branch5x5, filters[1][1], (3, 3), strides=(2, 2)) # use MaxPooling2D here branchpool = layers.MaxPooling2D( pool_size=(3, 3), strides=(2, 2), padding='same')(x) concat_axis = 1 if backend.image_data_format() == 'channels_first' else 3 x = layers.concatenate( [branch3x3, branch5x5, branchpool], axis=concat_axis) return x
Example #21
Source File: googlenet.py From keras_imagenet with MIT License | 5 votes |
def inception(x, filters): """Utility function to implement the inception module. # Arguments x: input tensor. filters: a list of filter sizes. # Returns Output tensor after applying the inception. """ if len(filters) != 4: raise ValueError('filters should have 4 components') if len(filters[1]) != 2 or len(filters[2]) != 2: raise ValueError('incorrect spec of filters') branch1x1 = conv2d_bn(x, filters[0], (1, 1)) branch3x3 = conv2d_bn(x, filters[1][0], (1, 1)) branch3x3 = conv2d_bn(branch3x3, filters[1][1], (3, 3)) branch5x5 = conv2d_bn(x, filters[2][0], (1, 1)) branch5x5 = conv2d_bn(branch5x5, filters[2][1], (5, 5)) branchpool = layers.AveragePooling2D( pool_size=(3, 3), strides=(1, 1), padding='same')(x) branchpool = conv2d_bn(branchpool, filters[3], (1, 1)) if backend.image_data_format() == 'channels_first': concat_axis = 1 else: concat_axis = 3 x = layers.concatenate( [branch1x1, branch3x3, branch5x5, branchpool], axis=concat_axis) return x
Example #22
Source File: inception_mobilenet.py From keras_imagenet with MIT License | 5 votes |
def _mixed_s2(x, filters, name=None): """Utility function to implement the 'stride-2' mixed block. # Arguments x: input tensor. filters: a list of filter sizes. name: name of the ops # Returns Output tensor after applying the 'stride-2' mixed block. """ if len(filters) != 2: raise ValueError('filters should have 2 components') name1 = name + '_3x3' if name else None branch3x3 = _depthwise_conv2d_bn(x, filters[0], kernel_size=(3, 3), strides=(2, 2), name=name1) name1 = name + '_5x5' if name else None branch5x5 = _depthwise_conv2d_bn(x, filters[1], kernel_size=(5, 5), strides=(2, 2), name=name1) name1 = name + '_pool' if name else None branchpool = layers.MaxPooling2D(pool_size=(3, 3), padding='same', strides=(2, 2), name=name1)(x) concat_axis = 1 if backend.image_data_format() == 'channels_first' else 3 x = layers.concatenate([branch3x3, branch5x5, branchpool], axis=concat_axis, name=name) return x
Example #23
Source File: SEResNeXt.py From TF.Keras-Commonly-used-models with Apache License 2.0 | 5 votes |
def channel_zeropad(self, x): ''' Zero-padding for channle dimensions. Note that padded channles are added like (Batch, H, W, 2/x + x + 2/x). ''' shape = list(x.shape) y = K.zeros_like(x) if self.channel_axis == 3: y = y[:, :, :, :shape[self.channel_axis]//2] else: y = y[:, :shape[self.channel_axis]//2, :, :] return concatenate([y, x, y], self.channel_axis)
Example #24
Source File: SEResNeXt.py From TF.Keras-Commonly-used-models with Apache License 2.0 | 5 votes |
def split_layer(self, x, stride): ''' Parallel operation of transform layers for ResNeXt structure. ''' splitted_branches = list() for i in range(self.num_split): branch = self.transform_layer(x, stride) splitted_branches.append(branch) return concatenate(splitted_branches, axis=self.channel_axis)
Example #25
Source File: GoogleNet.py From TF.Keras-Commonly-used-models with Apache License 2.0 | 5 votes |
def Inception(x,nb_filter): branch1x1 = Conv2d_BN(x,nb_filter,(1,1), padding='same',strides=(1,1),name=None) branch3x3 = Conv2d_BN(x,nb_filter,(1,1), padding='same',strides=(1,1),name=None) branch3x3 = Conv2d_BN(branch3x3,nb_filter,(3,3), padding='same',strides=(1,1),name=None) branch5x5 = Conv2d_BN(x,nb_filter,(1,1), padding='same',strides=(1,1),name=None) branch5x5 = Conv2d_BN(branch5x5,nb_filter,(1,1), padding='same',strides=(1,1),name=None) branchpool = MaxPooling2D(pool_size=(3,3),strides=(1,1),padding='same')(x) branchpool = Conv2d_BN(branchpool,nb_filter,(1,1),padding='same',strides=(1,1),name=None) x = concatenate([branch1x1,branch3x3,branch5x5,branchpool],axis=3) return x
Example #26
Source File: dual_path_network.py From TF.Keras-Commonly-used-models with Apache License 2.0 | 5 votes |
def _grouped_convolution_block(input, grouped_channels, cardinality, strides, weight_decay=5e-4): ''' Adds a grouped convolution block. It is an equivalent block from the paper Args: input: input tensor grouped_channels: grouped number of filters cardinality: cardinality factor describing the number of groups strides: performs strided convolution for downscaling if > 1 weight_decay: weight decay term Returns: a keras tensor ''' init = input channel_axis = 1 if K.image_data_format() == 'channels_first' else -1 group_list = [] if cardinality == 1: # with cardinality 1, it is a standard convolution x = Conv2D(grouped_channels, (3, 3), padding='same', use_bias=False, strides=strides, kernel_initializer='he_normal', kernel_regularizer=l2(weight_decay))(init) x = BatchNormalization(axis=channel_axis)(x) x = Activation('relu')(x) return x for c in range(cardinality): x = Lambda(lambda z: z[:, :, :, c * grouped_channels:(c + 1) * grouped_channels] if K.image_data_format() == 'channels_last' else lambda z: z[:, c * grouped_channels:(c + 1) * grouped_channels, :, :])(input) x = Conv2D(grouped_channels, (3, 3), padding='same', use_bias=False, strides=strides, kernel_initializer='he_normal', kernel_regularizer=l2(weight_decay))(x) group_list.append(x) group_merge = concatenate(group_list, axis=channel_axis) group_merge = BatchNormalization(axis=channel_axis)(group_merge) group_merge = Activation('relu')(group_merge) return group_merge
Example #27
Source File: Res2Net.py From TF.Keras-Commonly-used-models with Apache License 2.0 | 5 votes |
def res2net_block(num_filters, slice_num): def layer(input_tensor): short_cut = input_tensor x = Conv_Mish_BN(num_filters=num_filters, kernel_size=(1, 1))(input_tensor) slice_list = slice_layer(x, slice_num, x.shape[-1]) side = Conv_Mish_BN(num_filters=num_filters//slice_num, kernel_size=(3, 3))(slice_list[1]) z = concatenate([slice_list[0], side]) # for one and second stage for i in range(2, len(slice_list)): y = Conv_Mish_BN(num_filters=num_filters//slice_num, kernel_size=(3, 3))(add([side, slice_list[i]])) side = y z = concatenate([z, y]) z = Conv_Mish_BN(num_filters=num_filters, kernel_size=(1, 1))(z) out = concatenate([z, short_cut]) return out return layer
Example #28
Source File: dense.py From MIScnn with GNU General Public License v3.0 | 5 votes |
def middle_layer_3D(input, neurons, ba_norm, ba_norm_momentum): conv_m1 = Conv3D(neurons, (3, 3, 3), activation='relu', padding='same')(input) if ba_norm : conv_m1 = BatchNormalization(momentum=ba_norm_momentum)(conv_m1) conc1 = concatenate([input, conv_m1], axis=-1) conv_m2 = Conv3D(neurons, (3, 3, 3), activation='relu', padding='same')(conc1) if ba_norm : conv_m2 = BatchNormalization(momentum=ba_norm_momentum)(conv_m2) conc2 = concatenate([input, conv_m2], axis=-1) return conc2 # Create an expanding layer
Example #29
Source File: attn_augconv.py From keras-attention-augmented-convs with MIT License | 5 votes |
def augmented_conv2d(ip, filters, kernel_size=(3, 3), strides=(1, 1), depth_k=0.2, depth_v=0.2, num_heads=8, relative_encodings=True): """ Builds an Attention Augmented Convolution block. Args: ip: keras tensor. filters: number of output filters. kernel_size: convolution kernel size. strides: strides of the convolution. depth_k: float or int. Number of filters for k. Computes the number of filters for `v`. If passed as float, computed as `filters * depth_k`. depth_v: float or int. Number of filters for v. Computes the number of filters for `k`. If passed as float, computed as `filters * depth_v`. num_heads: int. Number of attention heads. Must be set such that `depth_k // num_heads` is > 0. relative_encodings: bool. Whether to use relative encodings or not. Returns: a keras tensor. """ # input_shape = K.int_shape(ip) channel_axis = 1 if K.image_data_format() == 'channels_first' else -1 depth_k, depth_v = _normalize_depth_vars(depth_k, depth_v, filters) conv_out = _conv_layer(filters - depth_v, kernel_size, strides)(ip) # Augmented Attention Block qkv_conv = _conv_layer(2 * depth_k + depth_v, (1, 1), strides)(ip) attn_out = AttentionAugmentation2D(depth_k, depth_v, num_heads, relative_encodings)(qkv_conv) attn_out = _conv_layer(depth_v, kernel_size=(1, 1))(attn_out) output = concatenate([conv_out, attn_out], axis=channel_axis) output = BatchNormalization()(output) return output
Example #30
Source File: stackedgan-mnist-6.2.1.py From Advanced-Deep-Learning-with-Keras with MIT License | 5 votes |
def build_generator(latent_codes, image_size, feature1_dim=256): """Build Generator Model sub networks Two sub networks: 1) Class and noise to feature1 (intermediate feature) 2) feature1 to image # Arguments latent_codes (Layers): dicrete code (labels), noise and feature1 features image_size (int): Target size of one side (assuming square image) feature1_dim (int): feature1 dimensionality # Returns gen0, gen1 (Models): Description below """ # Latent codes and network parameters labels, z0, z1, feature1 = latent_codes # image_resize = image_size // 4 # kernel_size = 5 # layer_filters = [128, 64, 32, 1] # gen1 inputs inputs = [labels, z1] # 10 + 50 = 62-dim x = concatenate(inputs, axis=1) x = Dense(512, activation='relu')(x) x = BatchNormalization()(x) x = Dense(512, activation='relu')(x) x = BatchNormalization()(x) fake_feature1 = Dense(feature1_dim, activation='relu')(x) # gen1: classes and noise (feature2 + z1) to feature1 gen1 = Model(inputs, fake_feature1, name='gen1') # gen0: feature1 + z0 to feature0 (image) gen0 = gan.generator(feature1, image_size, codes=z0) return gen0, gen1