Python keras.backend.name_scope() Examples
The following are 30
code examples of keras.backend.name_scope().
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.backend
, or try the search function
.
Example #1
Source File: convolutional_encoder.py From deep_qa with Apache License 2.0 | 6 votes |
def build(self, input_shape): # We define convolution, maxpooling and dense layers first. self.convolution_layers = [Convolution1D(filters=self.num_filters, kernel_size=ngram_size, activation=self.conv_layer_activation, kernel_regularizer=self.regularizer(), bias_regularizer=self.regularizer()) for ngram_size in self.ngram_filter_sizes] self.projection_layer = Dense(self.output_dim) # Building all layers because these sub-layers are not explitly part of the computatonal graph. for convolution_layer in self.convolution_layers: with K.name_scope(convolution_layer.name): convolution_layer.build(input_shape) maxpool_output_dim = self.num_filters * len(self.ngram_filter_sizes) projection_input_shape = (input_shape[0], maxpool_output_dim) with K.name_scope(self.projection_layer.name): self.projection_layer.build(projection_input_shape) # Defining the weights of this "layer" as the set of weights from all convolution # and maxpooling layers. self.trainable_weights = [] for layer in self.convolution_layers + [self.projection_layer]: self.trainable_weights.extend(layer.trainable_weights) super(CNNEncoder, self).build(input_shape)
Example #2
Source File: adamlr.py From StyleGAN-Keras with MIT License | 6 votes |
def __init__(self, lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0., amsgrad=False, multipliers=None, debug_verbose=False,**kwargs): super(Adam_lr_mult, self).__init__(**kwargs) with K.name_scope(self.__class__.__name__): self.iterations = K.variable(0, dtype='int64', name='iterations') self.lr = K.variable(lr, name='lr') self.beta_1 = K.variable(beta_1, name='beta_1') self.beta_2 = K.variable(beta_2, name='beta_2') self.decay = K.variable(decay, name='decay') if epsilon is None: epsilon = K.epsilon() self.epsilon = epsilon self.initial_decay = decay self.amsgrad = amsgrad self.multipliers = multipliers self.debug_verbose = debug_verbose
Example #3
Source File: hypertree_model.py From costar_plan with Apache License 2.0 | 6 votes |
def tile_vector_as_image_channels(vector_op, image_shape): """ Takes a vector of length n and an image shape BHWC, and repeat the vector as channels at each pixel. # Params vector_op: A tensor vector to tile. image_shape: A list of integers [width, height] with the desired dimensions. """ with K.name_scope('tile_vector_as_image_channels'): ivs = K.shape(vector_op) # reshape the vector into a single pixel vector_pixel_shape = [ivs[0], 1, 1, ivs[1]] vector_op = K.reshape(vector_op, vector_pixel_shape) # tile the pixel into a full image tile_dimensions = [1, image_shape[1], image_shape[2], 1] vector_op = K.tile(vector_op, tile_dimensions) if K.backend() is 'tensorflow': output_shape = [ivs[0], image_shape[1], image_shape[2], ivs[1]] vector_op.set_shape(output_shape) return vector_op
Example #4
Source File: grasp_loss.py From costar_plan with Apache License 2.0 | 6 votes |
def mean_true(y_true, y_pred): """ mean ground truth value metric useful for determining summary statistics when using the multi-dataset loader # Arguments y_true: [ground_truth_label, y_height_coordinate, x_width_coordinate] Shape of y_true is [batch_size, 3], or [ground_truth_label] with shape [batch_size]. y_pred: Predicted values with shape [batch_size, img_height, img_width, 1]. """ with K.name_scope(name='mean_true') as scope: if len(K.int_shape(y_true)) == 2 and K.int_shape(y_true)[1] == 3: y_true = K.cast(y_true[:, :1], 'float32') return K.mean(y_true)
Example #5
Source File: AdamAccumulate.py From Coloring-greyscale-images with MIT License | 6 votes |
def __init__(self, lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0., amsgrad=False, accum_iters=1, **kwargs): if accum_iters < 1: raise ValueError('accum_iters must be >= 1') super(AdamAccumulate, self).__init__(**kwargs) with K.name_scope(self.__class__.__name__): self.iterations = K.variable(0, dtype='int64', name='iterations') self.lr = K.variable(lr, name='lr') self.beta_1 = K.variable(beta_1, name='beta_1') self.beta_2 = K.variable(beta_2, name='beta_2') self.decay = K.variable(decay, name='decay') if epsilon is None: epsilon = K.epsilon() self.epsilon = epsilon self.initial_decay = decay self.amsgrad = amsgrad self.accum_iters = K.variable(accum_iters, K.dtype(self.iterations)) self.accum_iters_float = K.cast(self.accum_iters, K.floatx())
Example #6
Source File: rectified_adam.py From keras_rectified_adam with MIT License | 6 votes |
def __init__(self, lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0., weight_decay=0.0, **kwargs): super(RectifiedAdam, self).__init__(**kwargs) with K.name_scope(self.__class__.__name__): self.iterations = K.variable(0, dtype='int64', name='iterations') self.lr = K.variable(lr, name='lr') self.beta_1 = K.variable(beta_1, name='beta_1') self.beta_2 = K.variable(beta_2, name='beta_2') self.decay = K.variable(decay, name='decay') if epsilon is None: epsilon = K.epsilon() self.epsilon = epsilon self.initial_decay = decay self.weight_decay = float(weight_decay)
Example #7
Source File: grasp_loss.py From costar_plan with Apache License 2.0 | 6 votes |
def segmentation_gaussian_binary_crossentropy( y_true, y_pred, gaussian_sigma=3): with K.name_scope(name='segmentation_gaussian_binary_crossentropy') as scope: if keras.backend.ndim(y_true) == 4: # sometimes the dimensions are expanded from 2 to 4 # to meet Keras' expectations. # In that case reduce them back to 2 y_true = K.squeeze(y_true, axis=-1) y_true = K.squeeze(y_true, axis=-1) results = segmentation_gaussian_measurement_batch( y_true, y_pred, measurement=segmentation_losses.binary_crossentropy, gaussian_sigma=gaussian_sigma) return results
Example #8
Source File: grasp_loss.py From costar_plan with Apache License 2.0 | 6 votes |
def gripper_coordinate_y_true(y_true, y_pred=None): """ Get the label found in y_true which also contains coordinates. # Arguments y_true: [ground_truth_label, y_height_coordinate, x_width_coordinate] Shape of y_true is [batch_size, 3]. y_pred: Predicted values with shape [batch_size, img_height, img_width, 1]. """ with K.name_scope(name="gripper_coordinate_y_true") as scope: if keras.backend.ndim(y_true) == 4: # sometimes the dimensions are expanded from 2 to 4 # to meet Keras' expectations. # In that case reduce them back to 2 y_true = K.squeeze(y_true, axis=-1) y_true = K.squeeze(y_true, axis=-1) label = K.cast(y_true[:, :1], 'float32') return label
Example #9
Source File: grasp_loss.py From costar_plan with Apache License 2.0 | 6 votes |
def gripper_coordinate_y_pred(y_true, y_pred): """ Get the predicted value at the coordinate found in y_true. # Arguments y_true: [ground_truth_label, y_height_coordinate, x_width_coordinate] Shape of y_true is [batch_size, 3]. y_pred: Predicted values with shape [batch_size, img_height, img_width, 1]. """ with K.name_scope(name="gripper_coordinate_y_pred") as scope: if keras.backend.ndim(y_true) == 4: # sometimes the dimensions are expanded from 2 to 4 # to meet Keras' expectations. # In that case reduce them back to 2 y_true = K.squeeze(y_true, axis=-1) y_true = K.squeeze(y_true, axis=-1) yx_coordinate = K.cast(y_true[:, 1:], 'int32') yx_shape = K.shape(yx_coordinate) sample_index = K.expand_dims(K.arange(yx_shape[0]), axis=-1) byx_coordinate = K.concatenate([sample_index, yx_coordinate], axis=-1) # maybe need to transpose yx_coordinate? gripper_coordinate_y_predicted = tf.gather_nd(y_pred, byx_coordinate) return gripper_coordinate_y_predicted
Example #10
Source File: padam.py From keras-contrib with MIT License | 6 votes |
def __init__(self, lr=1e-1, beta_1=0.9, beta_2=0.999, epsilon=1e-8, decay=0., amsgrad=False, partial=1. / 8., **kwargs): if partial < 0 or partial > 0.5: raise ValueError( "Padam: 'partial' must be a positive float with a maximum " "value of `0.5`, since higher values will cause divergence " "during training." ) super(Padam, self).__init__(**kwargs) with K.name_scope(self.__class__.__name__): self.iterations = K.variable(0, dtype='int64', name='iterations') self.lr = K.variable(lr, name='lr') self.beta_1 = K.variable(beta_1, name='beta_1') self.beta_2 = K.variable(beta_2, name='beta_2') self.decay = K.variable(decay, name='decay') if epsilon is None: epsilon = K.epsilon() self.epsilon = epsilon self.partial = partial self.initial_decay = decay self.amsgrad = amsgrad
Example #11
Source File: adabound.py From keras-adabound with MIT License | 6 votes |
def __init__(self, lr=0.001, final_lr=0.1, beta_1=0.9, beta_2=0.999, gamma=1e-3, epsilon=None, decay=0., amsbound=False, weight_decay=0.0, **kwargs): super(AdaBound, self).__init__(**kwargs) if not 0. <= gamma <= 1.: raise ValueError("Invalid `gamma` parameter. Must lie in [0, 1] range.") with K.name_scope(self.__class__.__name__): self.iterations = K.variable(0, dtype='int64', name='iterations') self.lr = K.variable(lr, name='lr') self.beta_1 = K.variable(beta_1, name='beta_1') self.beta_2 = K.variable(beta_2, name='beta_2') self.decay = K.variable(decay, name='decay') self.final_lr = final_lr self.gamma = gamma if epsilon is None: epsilon = K.epsilon() self.epsilon = epsilon self.initial_decay = decay self.amsbound = amsbound self.weight_decay = float(weight_decay) self.base_lr = float(lr)
Example #12
Source File: sparsenet.py From keras-SparseNet with MIT License | 6 votes |
def _transition_block(ip, nb_filter, compression=1.0, 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_data_format() == 'channels_first' else -1 with K.name_scope('transition_block'): x = BatchNormalization(axis=concat_axis, epsilon=1e-5, momentum=0.1)(ip) x = Activation('relu')(x) x = Conv2D(int(nb_filter * compression), (1, 1), kernel_initializer='he_normal', padding='same', use_bias=False, kernel_regularizer=l2(weight_decay))(x) x = AveragePooling2D((2, 2), strides=(2, 2))(x) return x
Example #13
Source File: lars.py From keras-contrib with MIT License | 6 votes |
def __init__(self, lr, momentum=0.9, weight_decay=0.0001, eeta=0.001, epsilon=0.0, nesterov=False, **kwargs): if momentum < 0.0: raise ValueError("momentum should be positive: %s" % momentum) if weight_decay < 0.0: raise ValueError("weight_decay is not positive: %s" % weight_decay) super(LARS, self).__init__(**kwargs) with K.name_scope(self.__class__.__name__): self.iterations = K.variable(0, dtype='int64', name='iterations') self.lr = K.variable(lr, name='lr') self.momentum = K.variable(momentum, name='momentum') self.weight_decay = K.variable(weight_decay, name='weight_decay') self.eeta = K.variable(eeta, name='eeta') self.epsilon = epsilon self.nesterov = nesterov
Example #14
Source File: yogi.py From keras-contrib with MIT License | 6 votes |
def __init__(self, lr=0.01, beta_1=0.9, beta_2=0.999, epsilon=1e-3, decay=0., **kwargs): super(Yogi, self).__init__(**kwargs) if beta_1 <= 0 or beta_1 >= 1: raise ValueError("beta_1 has to be in ]0, 1[") if beta_2 <= 0 or beta_2 >= 1: raise ValueError("beta_2 has to be in ]0, 1[") with K.name_scope(self.__class__.__name__): self.iterations = K.variable(0, dtype='int64', name='iterations') self.lr = K.variable(lr, name='lr') self.beta_1 = K.variable(beta_1, name='beta_1') self.beta_2 = K.variable(beta_2, name='beta_2') self.decay = K.variable(decay, name='decay') if epsilon is None: epsilon = K.epsilon() if epsilon <= 0: raise ValueError("epsilon has to be larger than 0") self.epsilon = epsilon self.initial_decay = decay
Example #15
Source File: models.py From DigiX_HuaWei_Population_Age_Attribution_Predict with MIT License | 5 votes |
def __init__(self, lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-8, decay=0., **kwargs): super(AMSgrad, self).__init__(**kwargs) with K.name_scope(self.__class__.__name__): self.iterations = K.variable(0, dtype='int64', name='iterations') self.lr = K.variable(lr, name='lr') self.beta_1 = K.variable(beta_1, name='beta_1') self.beta_2 = K.variable(beta_2, name='beta_2') self.decay = K.variable(decay, name='decay') self.epsilon = epsilon self.initial_decay = decay
Example #16
Source File: optimization.py From BERT_with_keras with MIT License | 5 votes |
def __init__(self, lr, num_train_steps, num_warmup_steps, weight_decay_rate=0.0, beta_1=0.9, beta_2=0.999, epsilon=1e-6, bias_corrected=False, exclude_from_weight_decay=None, **kwargs): super(AdamWeightDecayOpt, self).__init__(**kwargs) with K.name_scope(self.__class__.__name__): self.iterations = K.variable(0, dtype='int64', name='iterations') self.lr = K.variable(lr, name='lr') self.beta_1 = K.variable(beta_1, name='beta_1') self.beta_2 = K.variable(beta_2, name='beta_2') self.epsilon = epsilon self.weight_decay_rate = weight_decay_rate self.exclude_from_weight_decay = exclude_from_weight_decay self.num_train_steps = num_train_steps self.num_warmup_steps = num_warmup_steps self.bias_corrected = bias_corrected
Example #17
Source File: optimizers_225.py From keras-adamw with MIT License | 5 votes |
def __init__(self, lr=0.001, beta_1=0.9, beta_2=0.999, amsgrad=False, epsilon=None, decay=0.0, model=None, zero_penalties=True, batch_size=32, total_iterations=0, total_iterations_wd=None, use_cosine_annealing=False, lr_multipliers=None, weight_decays=None, init_verbose=True, eta_min=0, eta_max=1, t_cur=0, **kwargs): if total_iterations > 1: weight_decays = _init_weight_decays(model, zero_penalties, weight_decays) eta_t = kwargs.pop('eta_t', 1.) super(AdamW, self).__init__(**kwargs) with K.name_scope(self.__class__.__name__): self.iterations = K.variable(0, dtype='int64', name='iterations') self.lr = K.variable(lr, name='lr') self.beta_1 = K.variable(beta_1, name='beta_1') self.beta_2 = K.variable(beta_2, name='beta_2') self.decay = K.variable(decay, name='decay') self.eta_min = K.constant(eta_min, name='eta_min') self.eta_max = K.constant(eta_max, name='eta_max') self.eta_t = K.variable(eta_t, dtype='float32', name='eta_t') self.t_cur = K.variable(t_cur, dtype='int64', name='t_cur') self.initial_decay = decay self.epsilon = epsilon or K.epsilon() self.batch_size = batch_size self.total_iterations = total_iterations self.total_iterations_wd = total_iterations_wd or total_iterations self.amsgrad = amsgrad self.lr_multipliers = lr_multipliers self.weight_decays = weight_decays or {} self.init_verbose = init_verbose self.use_cosine_annealing = use_cosine_annealing _check_args(self, total_iterations, use_cosine_annealing, weight_decays) self._init_lr = lr # to print lr_mult setup self._init_notified = False
Example #18
Source File: optimizers.py From keras-adamw with MIT License | 5 votes |
def __init__(self, learning_rate=0.001, beta_1=0.9, beta_2=0.999, amsgrad=False, model=None, zero_penalties=True, batch_size=32, total_iterations=0, total_iterations_wd=None, use_cosine_annealing=False, lr_multipliers=None, weight_decays=None, init_verbose=True, eta_min=0, eta_max=1, t_cur=0, **kwargs): if total_iterations > 1: weight_decays = _init_weight_decays(model, zero_penalties, weight_decays) self.initial_decay = kwargs.pop('decay', 0.0) self.epsilon = kwargs.pop('epsilon', K.epsilon()) learning_rate = kwargs.pop('lr', learning_rate) eta_t = kwargs.pop('eta_t', 1.) super(AdamW, self).__init__(**kwargs) with K.name_scope(self.__class__.__name__): self.iterations = K.variable(0, dtype='int64', name='iterations') self.learning_rate = K.variable(learning_rate, name='learning_rate') self.beta_1 = K.variable(beta_1, name='beta_1') self.beta_2 = K.variable(beta_2, name='beta_2') self.decay = K.variable(self.initial_decay, name='decay') self.eta_min = K.constant(eta_min, name='eta_min') self.eta_max = K.constant(eta_max, name='eta_max') self.eta_t = K.variable(eta_t, dtype='float32', name='eta_t') self.t_cur = K.variable(t_cur, dtype='int64', name='t_cur') self.batch_size = batch_size self.total_iterations = total_iterations self.total_iterations_wd = total_iterations_wd or total_iterations self.amsgrad = amsgrad self.lr_multipliers = lr_multipliers self.weight_decays = weight_decays or {} self.init_verbose = init_verbose self.use_cosine_annealing = use_cosine_annealing _check_args(self, total_iterations, use_cosine_annealing, weight_decays) self._init_lr = learning_rate # to print lr_mult setup self._init_notified = False
Example #19
Source File: optimizers.py From keras-adamw with MIT License | 5 votes |
def __init__(self, learning_rate=0.002, beta_1=0.9, beta_2=0.999, model=None, zero_penalties=True, batch_size=32, total_iterations=0, total_iterations_wd=None, use_cosine_annealing=False, lr_multipliers=None, weight_decays=None, init_verbose=True, eta_min=0, eta_max=1, t_cur=0, **kwargs): if total_iterations > 1: weight_decays = _init_weight_decays(model, zero_penalties, weight_decays) self.schedule_decay = kwargs.pop('schedule_decay', 0.004) self.epsilon = kwargs.pop('epsilon', K.epsilon()) learning_rate = kwargs.pop('lr', learning_rate) eta_t = kwargs.pop('eta_t', 1.) super(NadamW, self).__init__(**kwargs) with K.name_scope(self.__class__.__name__): self.iterations = K.variable(0, dtype='int64', name='iterations') self.m_schedule = K.variable(1., name='m_schedule') self.learning_rate = K.variable(learning_rate, name='learning_rate') self.beta_1 = K.variable(beta_1, name='beta_1') self.beta_2 = K.variable(beta_2, name='beta_2') self.eta_min = K.constant(eta_min, name='eta_min') self.eta_max = K.constant(eta_max, name='eta_max') self.eta_t = K.variable(eta_t, dtype='float32', name='eta_t') self.t_cur = K.variable(t_cur, dtype='int64', name='t_cur') self.batch_size = batch_size self.total_iterations = total_iterations self.total_iterations_wd = total_iterations_wd or total_iterations self.lr_multipliers = lr_multipliers self.weight_decays = weight_decays or {} self.use_cosine_annealing = use_cosine_annealing self.init_verbose = init_verbose _check_args(self, total_iterations, use_cosine_annealing, weight_decays) self._init_lr = learning_rate # to print lr_mult setup self._init_notified = False
Example #20
Source File: adamw.py From EAST with GNU General Public License v3.0 | 5 votes |
def __init__(self, lr=0.001, beta_1=0.9, beta_2=0.999, weight_decay=1e-4, # decoupled weight decay (1/4) epsilon=1e-8, decay=0., **kwargs): super(AdamW, self).__init__(**kwargs) with K.name_scope(self.__class__.__name__): self.iterations = K.variable(0, dtype='int64', name='iterations') self.lr = K.variable(lr, name='lr') self.beta_1 = K.variable(beta_1, name='beta_1') self.beta_2 = K.variable(beta_2, name='beta_2') self.decay = K.variable(decay, name='decay') self.wd = K.variable(weight_decay, name='weight_decay') # decoupled weight decay (2/4) self.epsilon = epsilon self.initial_decay = decay
Example #21
Source File: optimizers.py From keras-adamw with MIT License | 5 votes |
def __init__(self, learning_rate=0.01, momentum=0., nesterov=False, model=None, zero_penalties=True, batch_size=32, total_iterations=0, total_iterations_wd=None, use_cosine_annealing=False, lr_multipliers=None, weight_decays=None, init_verbose=True, eta_min=0, eta_max=1, t_cur=0, **kwargs): if total_iterations > 1: weight_decays = _init_weight_decays(model, zero_penalties, weight_decays) self.initial_decay = kwargs.pop('decay', 0.0) learning_rate = kwargs.pop('lr', learning_rate) eta_t = kwargs.pop('eta_t', 1.) super(SGDW, self).__init__(**kwargs) with K.name_scope(self.__class__.__name__): self.iterations = K.variable(0, dtype='int64', name='iterations') self.learning_rate = K.variable(learning_rate, name='learning_rate') self.momentum = K.variable(momentum, name='momentum') self.decay = K.variable(self.initial_decay, name='decay') self.eta_min = K.constant(eta_min, name='eta_min') self.eta_max = K.constant(eta_max, name='eta_max') self.eta_t = K.variable(eta_t, dtype='float32', name='eta_t') self.t_cur = K.variable(t_cur, dtype='int64', name='t_cur') self.batch_size = batch_size self.total_iterations = total_iterations self.total_iterations_wd = total_iterations_wd or total_iterations self.nesterov = nesterov self.lr_multipliers = lr_multipliers self.weight_decays = weight_decays or {} self.init_verbose = init_verbose self.use_cosine_annealing = use_cosine_annealing _check_args(self, total_iterations, use_cosine_annealing, weight_decays) self._init_lr = learning_rate # to print lr_mult setup self._init_notified = False
Example #22
Source File: nasnet.py From neural-image-assessment with MIT License | 5 votes |
def _separable_conv_block(ip, filters, kernel_size=(3, 3), strides=(1, 1), weight_decay=5e-5, id=None): '''Adds 2 blocks of [relu-separable conv-batchnorm] # Arguments: ip: input tensor filters: number of output filters per layer kernel_size: kernel size of separable convolutions strides: strided convolution for downsampling weight_decay: l2 regularization weight id: string id # Returns: a Keras tensor ''' channel_dim = 1 if K.image_data_format() == 'channels_first' else -1 with K.name_scope('separable_conv_block_%s' % id): x = Activation('relu')(ip) x = SeparableConv2D(filters, kernel_size, strides=strides, name='separable_conv_1_%s' % id, padding='same', use_bias=False, kernel_initializer='he_normal', kernel_regularizer=l2(weight_decay))(x) x = BatchNormalization(axis=channel_dim, momentum=_BN_DECAY, epsilon=_BN_EPSILON, name="separable_conv_1_bn_%s" % (id))(x) x = Activation('relu')(x) x = SeparableConv2D(filters, kernel_size, name='separable_conv_2_%s' % id, padding='same', use_bias=False, kernel_initializer='he_normal', kernel_regularizer=l2(weight_decay))(x) x = BatchNormalization(axis=channel_dim, momentum=_BN_DECAY, epsilon=_BN_EPSILON, name="separable_conv_2_bn_%s" % (id))(x) return x
Example #23
Source File: nasnet.py From keras-contrib with MIT License | 5 votes |
def _separable_conv_block(ip, filters, kernel_size=(3, 3), strides=(1, 1), weight_decay=5e-5, id=None): '''Adds 2 blocks of [relu-separable conv-batchnorm] # Arguments: ip: input tensor filters: number of output filters per layer kernel_size: kernel size of separable convolutions strides: strided convolution for downsampling weight_decay: l2 regularization weight id: string id # Returns: a Keras tensor ''' channel_dim = 1 if K.image_data_format() == 'channels_first' else -1 with K.name_scope('separable_conv_block_%s' % id): x = Activation('relu')(ip) x = SeparableConv2D(filters, kernel_size, strides=strides, name='separable_conv_1_%s' % id, padding='same', use_bias=False, kernel_initializer='he_normal', kernel_regularizer=l2(weight_decay))(x) x = BatchNormalization(axis=channel_dim, momentum=_BN_DECAY, epsilon=_BN_EPSILON, name="separable_conv_1_bn_%s" % id)(x) x = Activation('relu')(x) x = SeparableConv2D(filters, kernel_size, name='separable_conv_2_%s' % id, padding='same', use_bias=False, kernel_initializer='he_normal', kernel_regularizer=l2(weight_decay))(x) x = BatchNormalization(axis=channel_dim, momentum=_BN_DECAY, epsilon=_BN_EPSILON, name="separable_conv_2_bn_%s" % id)(x) return x
Example #24
Source File: networks.py From Speech_emotion_recognition_BLSTM with MIT License | 5 votes |
def create_softmax_la_network(input_shape, nb_lstm_cells=128, nb_classes=7): ''' input_shape: (time_steps, features,) ''' with K.name_scope('BLSTMLayer'): # Bi-directional Long Short-Term Memory for learning the temporal aggregation input_feature = Input(shape=input_shape) x = Masking(mask_value=globalvars.masking_value)(input_feature) x = Dense(globalvars.nb_hidden_units, activation='relu')(x) x = Dropout(0.5)(x) x = Dense(globalvars.nb_hidden_units, activation='relu')(x) x = Dropout(0.5)(x) y = Bidirectional(LSTM(nb_lstm_cells, return_sequences=True, dropout=0.5))(x) with K.name_scope('AttentionLayer'): # Logistic regression for learning the attention parameters with a standalone feature as input input_attention = Input(shape=(nb_lstm_cells * 2,)) u = Dense(nb_lstm_cells * 2, activation='softmax')(input_attention) # To compute the final weights for the frames which sum to unity alpha = dot([u, y], axes=-1) # inner prod. alpha = Activation('softmax')(alpha) with K.name_scope('WeightedPooling'): # Weighted pooling to get the utterance-level representation z = dot([alpha, y], axes=1) # Get posterior probability for each emotional class output = Dense(nb_classes, activation='softmax')(z) return Model(inputs=[input_attention, input_feature], outputs=output)
Example #25
Source File: sparsenet.py From keras-SparseNet with MIT License | 5 votes |
def _conv_block(ip, nb_filter, bottleneck=False, dropout_rate=None, weight_decay=1e-4): ''' Apply BatchNorm, Relu, 3x3 Conv2D, optional bottleneck block and dropout Args: ip: Input keras tensor nb_filter: number of filters bottleneck: add bottleneck block dropout_rate: dropout rate weight_decay: weight decay factor Returns: keras tensor with batch_norm, relu and convolution2d added (optional bottleneck) ''' concat_axis = 1 if K.image_data_format() == 'channels_first' else -1 with K.name_scope('conv_block'): x = BatchNormalization(axis=concat_axis, momentum=0.1, epsilon=1e-5)(ip) x = Activation('relu')(x) if bottleneck: inter_channel = nb_filter * 4 # Obtained from https://github.com/liuzhuang13/DenseNet/blob/master/densenet.lua x = Conv2D(inter_channel, (1, 1), kernel_initializer='he_normal', padding='same', use_bias=False, kernel_regularizer=l2(weight_decay))(x) x = BatchNormalization(axis=concat_axis, epsilon=1e-5, momentum=0.1)(x) x = Activation('relu')(x) x = Conv2D(nb_filter, (3, 3), kernel_initializer='he_normal', padding='same', use_bias=False)(x) if dropout_rate: x = Dropout(dropout_rate)(x) return x
Example #26
Source File: nasnet.py From neural-image-assessment with MIT License | 5 votes |
def _add_auxiliary_head(x, classes, weight_decay): '''Adds an auxiliary head for training the model From section A.7 "Training of ImageNet models" of the paper, all NASNet models are trained using an auxiliary classifier around 2/3 of the depth of the network, with a loss weight of 0.4 # Arguments x: input tensor classes: number of output classes weight_decay: l2 regularization weight # Returns a keras Tensor ''' img_height = 1 if K.image_data_format() == 'channels_last' else 2 img_width = 2 if K.image_data_format() == 'channels_last' else 3 channel_axis = 1 if K.image_data_format() == 'channels_first' else -1 with K.name_scope('auxiliary_branch'): auxiliary_x = Activation('relu')(x) auxiliary_x = AveragePooling2D((5, 5), strides=(3, 3), padding='valid', name='aux_pool')(auxiliary_x) auxiliary_x = Conv2D(128, (1, 1), padding='same', use_bias=False, name='aux_conv_projection', kernel_initializer='he_normal', kernel_regularizer=l2(weight_decay))(auxiliary_x) auxiliary_x = BatchNormalization(axis=channel_axis, momentum=_BN_DECAY, epsilon=_BN_EPSILON, name='aux_bn_projection')(auxiliary_x) auxiliary_x = Activation('relu')(auxiliary_x) auxiliary_x = Conv2D(768, (auxiliary_x._keras_shape[img_height], auxiliary_x._keras_shape[img_width]), padding='valid', use_bias=False, kernel_initializer='he_normal', kernel_regularizer=l2(weight_decay), name='aux_conv_reduction')(auxiliary_x) auxiliary_x = BatchNormalization(axis=channel_axis, momentum=_BN_DECAY, epsilon=_BN_EPSILON, name='aux_bn_reduction')(auxiliary_x) auxiliary_x = Activation('relu')(auxiliary_x) auxiliary_x = GlobalAveragePooling2D()(auxiliary_x) auxiliary_x = Dense(classes, activation='softmax', kernel_regularizer=l2(weight_decay), name='aux_predictions')(auxiliary_x) return auxiliary_x
Example #27
Source File: grasp_loss.py From costar_plan with Apache License 2.0 | 5 votes |
def segmentation_single_pixel_measurement(y_true, y_pred, measurement=keras.losses.binary_crossentropy, name=None): """ Applies metric or loss function at a specific pixel coordinate. # Arguments y_true: [ground_truth_label, y_height_coordinate, x_width_coordinate] Shape of y_true is [batch_size, 3]. y_pred: Predicted values with shape [batch_size, img_height, img_width, 1]. """ if name is None: name = 'grasp_segmentation_single_pixel_measurement' with K.name_scope(name=name) as scope: label = gripper_coordinate_y_true(y_true) single_pixel_y_pred = gripper_coordinate_y_pred(y_true, y_pred) return measurement(label, single_pixel_y_pred)
Example #28
Source File: grasp_loss.py From costar_plan with Apache License 2.0 | 5 votes |
def mean_pred_single_pixel(y_true, y_pred): """ mean predicted value metric at individual pixel coordinates. useful for detecting perverse conditions such as 100% grasp_success == True """ with K.name_scope(name='mean_pred_single_pixel') as scope: single_pixel_y_pred = gripper_coordinate_y_pred(y_true, y_pred) return K.mean(single_pixel_y_pred)
Example #29
Source File: hypertree_model.py From costar_plan with Apache License 2.0 | 5 votes |
def concat_images_with_tiled_vector_layer(images, vector, image_shape=None, vector_shape=None): """Tile a vector as if it were channels onto every pixel of an image. This version is designed to be used as layers within a Keras model. # Params images: a list of images to combine, must have equal dimensions vector: the 1D vector to tile onto every pixel. image_shape: Tuple with 3 entries defining the shape (batch, height, width) images should be expected to have, do not specify the number of batches. vector_shape: Tuple with 3 entries defining the shape (batch, height, width) images should be expected to have, do not specify the number of batches. """ with K.name_scope('concat_images_with_tiled_vector_layer'): if not isinstance(images, list): images = [images] if vector_shape is None: # check if K.shape, K.int_shape, or vector.get_shape().as_list()[1:] is better # https://github.com/fchollet/keras/issues/5211 # TODO(ahundt) ensure shape works in both google brain/cornell dataset input tensor and keras Input() aka numpy array cases vector_shape = K.int_shape(vector)[1:] if image_shape is None: # check if K.shape, K.int_shape, or image.get_shape().as_list()[1:] is better # https://github.com/fchollet/keras/issues/5211 # TODO(ahundt) ensure shape works in both google brain/cornell dataset input tensor and keras Input() aka numpy array cases image_shape = K.int_shape(images[0])[1:] vector = Reshape([1, 1, vector_shape[-1]])(vector) tile_shape = (int(1), int(image_shape[0]), int(image_shape[1]), int(1)) tiled_vector = Lambda(lambda x: K.tile(x, tile_shape))(vector) x = Concatenate(axis=-1)([] + images + [tiled_vector]) return x
Example #30
Source File: hypertree_model.py From costar_plan with Apache License 2.0 | 5 votes |
def add_images_with_tiled_vector_layer(images, vector, image_shape=None, vector_shape=None): """Tile a vector as if it were channels onto every pixel of an image. This version is designed to be used as layers within a Keras model. # Params images: a list of images to combine, must have equal dimensions vector: the 1D vector to tile onto every pixel image_shape: Tuple with 3 entries defining the shape (batch, height, width) images should be expected to have, do not specify the number of batches. vector_shape: Tuple with 3 entries defining the shape (batch, height, width) images should be expected to have, do not specify the number of batches. """ with K.name_scope('add_images_with_tiled_vector_layer'): if not isinstance(images, list): images = [images] if vector_shape is None: # check if K.shape, K.int_shape, or vector.get_shape().as_list()[1:] is better # https://github.com/fchollet/keras/issues/5211 vector_shape = K.int_shape(vector)[1:] if image_shape is None: # check if K.shape, K.int_shape, or image.get_shape().as_list()[1:] is better # https://github.com/fchollet/keras/issues/5211 image_shape = K.int_shape(images[0])[1:] vector = Reshape([1, 1, vector_shape[-1]])(vector) tile_shape = (int(1), int(image_shape[0]), int(image_shape[1]), int(1)) tiled_vector = Lambda(lambda x: K.tile(x, tile_shape))(vector) x = Add()([] + images + [tiled_vector]) return x