Python tensorflow.keras.layers.Lambda() Examples

The following are 30 code examples of tensorflow.keras.layers.Lambda(). 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: gan.py    From deepchem with MIT License 6 votes vote down vote up
def create_generator_loss(self, discrim_output):
    """Create the loss function for the generator.

    The default implementation is appropriate for most cases.  Subclasses can
    override this if the need to customize it.

    Parameters
    ----------
    discrim_output: Tensor
      the output from the discriminator on a batch of generated data.  This is
      its estimate of the probability that each sample is training data.

    Returns
    -------
    A Tensor equal to the loss function to use for optimizing the generator.
    """
    return Lambda(lambda x: -tf.reduce_mean(tf.math.log(x + 1e-10)))(
        discrim_output) 
Example #2
Source File: custom_activation.py    From Echo with MIT License 6 votes vote down vote up
def call(self, inputs):
        def brelu(x):
            # get shape of X, we are interested in the last axis, which is constant
            shape = K.int_shape(x)
            # last axis
            dim = shape[-1]
            # half of the last axis (+1 if necessary)
            dim2 = dim // 2
            if dim % 2 != 0:
                dim2 += 1
            # multiplier will be a tensor of alternated +1 and -1
            multiplier = K.ones((dim2,))
            multiplier = K.stack([multiplier, -multiplier], axis=-1)
            if dim % 2 != 0:
                multiplier = multiplier[:-1]
            # adjust multiplier shape to the shape of x
            multiplier = K.reshape(multiplier, tuple(1 for _ in shape[:-1]) + (-1,))
            return multiplier * tf.nn.relu(multiplier * x)

        return Lambda(brelu)(inputs) 
Example #3
Source File: ResNextFPN.py    From TF.Keras-Commonly-used-models with Apache License 2.0 6 votes vote down vote up
def grouped_convolution_block(input, grouped_channels, cardinality, strides, weight_decay=5e-4):
    init = input
    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, strides),
                   kernel_initializer='he_normal', kernel_regularizer=l2(weight_decay))(init)
        x = BatchNormalization(axis=3)(x)
        x = Activation('relu')(x)
        return x

    for c in range(cardinality):
        x = Lambda(lambda z: z[:, :, :, c * grouped_channels:(c + 1) * grouped_channels])(input)
        x = Conv2D(grouped_channels, (3, 3), padding='same', use_bias=False, strides=(strides, strides),
                   kernel_initializer='he_normal', kernel_regularizer=l2(weight_decay))(x)
        group_list.append(x)

    group_merge = concatenate(group_list, axis=3)
    x = BatchNormalization(axis=3)(group_merge)
    x = Activation('relu')(x)
    return x 
Example #4
Source File: mobilenet_v3.py    From keras-YOLOv3-model-set with MIT License 6 votes vote down vote up
def _se_block(inputs, filters, se_ratio, prefix):
    x = GlobalAveragePooling2D(name=prefix + 'squeeze_excite/AvgPool')(inputs)
    if K.image_data_format() == 'channels_first':
        x = Reshape((filters, 1, 1))(x)
    else:
        x = Reshape((1, 1, filters))(x)
    x = Conv2D(_depth(filters * se_ratio),
                      kernel_size=1,
                      padding='same',
                      name=prefix + 'squeeze_excite/Conv')(x)
    x = ReLU(name=prefix + 'squeeze_excite/Relu')(x)
    x = Conv2D(filters,
                      kernel_size=1,
                      padding='same',
                      name=prefix + 'squeeze_excite/Conv_1')(x)
    x = Activation(hard_sigmoid)(x)
    #if K.backend() == 'theano':
        ## For the Theano backend, we have to explicitly make
        ## the excitation weights broadcastable.
        #x = Lambda(
            #lambda br: K.pattern_broadcast(br, [True, True, True, False]),
            #output_shape=lambda input_shape: input_shape,
            #name=prefix + 'squeeze_excite/broadcast')(x)
    x = Multiply(name=prefix + 'squeeze_excite/Mul')([inputs, x])
    return x 
Example #5
Source File: layers.py    From keras-YOLOv3-model-set with MIT License 6 votes vote down vote up
def yolo2lite_predictions(feature_maps, feature_channel_nums, num_anchors, num_classes):
    f1, f2 = feature_maps
    f1_channel_num, f2_channel_num = feature_channel_nums

    x1 = compose(
        Depthwise_Separable_Conv2D_BN_Leaky(filters=f1_channel_num, kernel_size=(3, 3), block_id_str='pred_1'),
        Depthwise_Separable_Conv2D_BN_Leaky(filters=f1_channel_num, kernel_size=(3, 3), block_id_str='pred_2'))(f1)

    # Here change the f2 channel number to f2_channel_num//8 first,
    # then expand back to f2_channel_num//2 with "space_to_depth_x2"
    x2 = DarknetConv2D_BN_Leaky(f2_channel_num//8, (1, 1))(f2)
    # TODO: Allow Keras Lambda to use func arguments for output_shape?
    x2_reshaped = Lambda(
        space_to_depth_x2,
        output_shape=space_to_depth_x2_output_shape,
        name='space_to_depth')(x2)

    x = Concatenate()([x2_reshaped, x1])
    x = Depthwise_Separable_Conv2D_BN_Leaky(filters=f1_channel_num, kernel_size=(3, 3), block_id_str='pred_3')(x)
    y = DarknetConv2D(num_anchors * (num_classes + 5), (1, 1), name='predict_conv')(x)

    return y 
Example #6
Source File: layers.py    From keras-YOLOv3-model-set with MIT License 6 votes vote down vote up
def yolo2_predictions(feature_maps, feature_channel_nums, num_anchors, num_classes):
    f1, f2 = feature_maps
    f1_channel_num, f2_channel_num = feature_channel_nums

    x1 = compose(
        DarknetConv2D_BN_Leaky(f1_channel_num, (3, 3)),
        DarknetConv2D_BN_Leaky(f1_channel_num, (3, 3)))(f1)

    # Here change the f2 channel number to f2_channel_num//8 first,
    # then expand back to f2_channel_num//2 with "space_to_depth_x2"
    x2 = DarknetConv2D_BN_Leaky(f2_channel_num//8, (1, 1))(f2)
    # TODO: Allow Keras Lambda to use func arguments for output_shape?
    x2_reshaped = Lambda(
        space_to_depth_x2,
        output_shape=space_to_depth_x2_output_shape,
        name='space_to_depth')(x2)

    x = Concatenate()([x2_reshaped, x1])
    x = DarknetConv2D_BN_Leaky(f1_channel_num, (3, 3))(x)
    y = DarknetConv2D(num_anchors * (num_classes + 5), (1, 1), name='predict_conv')(x)

    return y 
Example #7
Source File: model.py    From keras-YOLOv3-model-set with MIT License 6 votes vote down vote up
def get_yolo2_inference_model(model_type, anchors, num_classes, weights_path=None, input_shape=None, confidence=0.1):
    '''create the inference model, for YOLOv2'''
    #K.clear_session() # get a new session
    num_anchors = len(anchors)

    image_shape = Input(shape=(2,), dtype='int64', name='image_shape')

    model_body, _ = get_yolo2_model(model_type, num_anchors, num_classes, input_shape=input_shape)
    print('Create YOLOv2 {} model with {} anchors and {} classes.'.format(model_type, num_anchors, num_classes))

    if weights_path:
        model_body.load_weights(weights_path, by_name=False)#, skip_mismatch=True)
        print('Load weights {}.'.format(weights_path))

    boxes, scores, classes = Lambda(batched_yolo2_postprocess, name='yolo2_postprocess',
            arguments={'anchors': anchors, 'num_classes': num_classes, 'confidence': confidence})(
        [model_body.output, image_shape])

    model = Model([model_body.input, image_shape], [boxes, scores, classes])

    return model 
Example #8
Source File: cmvae.py    From AirSim-Drone-Racing-VAE-Imitation with MIT License 6 votes vote down vote up
def __init__(self, n_z, gate_dim=4, res=96, trainable_model=True):
        super(CmvaeDirect, self).__init__()
        # create the base models:
        self.q_img = dronet.Dronet(num_outputs=n_z*2, include_top=True)
        self.p_img = decoders.ImgDecoder()
        self.p_R = transformer.NonLinearTransformer()
        self.p_Theta = transformer.NonLinearTransformer()
        self.p_Psi = transformer.NonLinearTransformer()
        self.p_Phi = transformer.NonLinearTransformer()
        # Create sampler
        self.mean_params = Lambda(lambda x: x[:, : n_z])
        self.stddev_params = Lambda(lambda x: x[:, n_z:])
        self.R_params = Lambda(lambda x: x[:, 0])
        self.Theta_params = Lambda(lambda x: x[:, 1])
        self.Psi_params = Lambda(lambda x: x[:, 2])
        self.Phi_params = Lambda(lambda x: x[:, 3]) 
Example #9
Source File: attention.py    From keras-attention-mechanism with Apache License 2.0 6 votes vote down vote up
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 #10
Source File: bprmf.py    From DeepRec with GNU General Public License v3.0 6 votes vote down vote up
def build_network(self, num_factor=30):
        self.P = tf.Variable(tf.random.normal([self.num_user, num_factor], stddev=0.01))
        self.Q = tf.Variable(tf.random.normal([self.num_item, num_factor], stddev=0.01))
        # user_id = tf.squeeze(self.user_id)
        # item_id = tf.squeeze(self.item_id)
        # neg_item_id = tf.squeeze(self.neg_item_id)
        user_id = Lambda(lambda x: tf.squeeze(x))(self.user_id)
        item_id = Lambda(lambda x: tf.squeeze(x))(self.item_id)
        neg_item_id = Lambda(lambda x: tf.squeeze(x))(self.neg_item_id)

        user_latent_factor = EmbeddingLookup(self.P)(user_id)
        item_latent_factor = EmbeddingLookup(self.Q)(item_id)
        neg_item_latent_factor = EmbeddingLookup(self.Q)(neg_item_id)
        # user_latent_factor = tf.nn.embedding_lookup(self.P, user_id)
        # item_latent_factor = tf.nn.embedding_lookup(self.Q, item_id)
        # neg_item_latent_factor = tf.nn.embedding_lookup(self.Q, neg_item_id)

        pred_y = tf.reduce_sum(tf.multiply(user_latent_factor, item_latent_factor), 1)
        pred_y_neg = tf.reduce_sum(tf.multiply(user_latent_factor, neg_item_latent_factor), 1)
        self.model = Model(inputs=[self.user_id, self.item_id, self.neg_item_id],
                           outputs=[pred_y, pred_y_neg])
        # self.optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate).minimize(self.loss) 
Example #11
Source File: network.py    From ivis with GNU General Public License v2.0 6 votes vote down vote up
def triplet_network(base_network, embedding_dims=2, embedding_l2=0.0):
    def output_shape(shapes):
        shape1, shape2, shape3 = shapes
        return (3, shape1[0],)

    input_a = Input(shape=base_network.input_shape[1:])
    input_p = Input(shape=base_network.input_shape[1:])
    input_n = Input(shape=base_network.input_shape[1:])

    embeddings = Dense(embedding_dims,
                       kernel_regularizer=l2(embedding_l2))(base_network.output)
    network = Model(base_network.input, embeddings)

    processed_a = network(input_a)
    processed_p = network(input_p)
    processed_n = network(input_n)

    triplet = Lambda(K.stack,
                     output_shape=output_shape,
                     name='stacked_triplets')([processed_a,
                                               processed_p,
                                               processed_n],)
    model = Model([input_a, input_p, input_n], triplet)

    return model, processed_a, processed_p, processed_n 
Example #12
Source File: BeplerContactPredictor.py    From tape-neurips2019 with MIT License 6 votes vote down vote up
def __init__(self,
                 input_name: str = 'encoder_output',
                 output_name: str = 'contact_prob'):
        super().__init__()
        self._input_name = input_name
        self._output_name = output_name

        def concat_pairs(tensor):
            input_mul = tensor[:, :, None] * tensor[:, None, :]
            input_sub = tf.abs(tensor[:, :, None] - tensor[:, None, :])
            output = tf.concat((input_mul, input_sub), -1)
            return output

        self.get_pairwise_feature_vector = Lambda(concat_pairs)

        self.predict_contact_map = Stack()
        self.predict_contact_map.add(Conv2D(32, 1, use_bias=True, padding='same', activation='relu'))
        self.predict_contact_map.add(Conv2D(1, 7, use_bias=True, padding='same', activation='linear')) 
Example #13
Source File: convert.py    From tf2-yolo3 with Apache License 2.0 6 votes vote down vote up
def make_eval_model_from_trained_model(model, anchors, masks, num_classes=10, tiny=True):
    if tiny:
        output_0, output_1 = model.outputs
        boxes_0 = Lambda(lambda x: yolo_boxes(x, anchors[masks[0]], num_classes, False), name='yolo_boxes_0')(output_0)
        boxes_1 = Lambda(lambda x: yolo_boxes(x, anchors[masks[1]], num_classes, False), name='yolo_boxes_1')(output_1)
        outputs = Lambda(lambda x: yolo_nms(x, anchors, masks, num_classes),
                         name='yolo_nms')((boxes_0[:3], boxes_1[:3]))
        model = tf.keras.Model(model.inputs, outputs, name='yolov3_tiny')
    else:
        output_0, output_1, output_2 = model.outputs
        boxes_0 = Lambda(lambda x: yolo_boxes(x, anchors[masks[0]], num_classes, False), name='yolo_boxes_0')(output_0)
        boxes_1 = Lambda(lambda x: yolo_boxes(x, anchors[masks[1]], num_classes, False), name='yolo_boxes_1')(output_1)
        boxes_2 = Lambda(lambda x: yolo_boxes(x, anchors[masks[2]], num_classes, False), name='yolo_boxes_1')(output_2)
        outputs = Lambda(lambda x: yolo_nms(x, anchors, masks, num_classes),
                         name='yolo_nms')((boxes_0[:3], boxes_1[:3], boxes_2[:3]))
        model = tf.keras.Model(model.inputs, outputs, name='yolov3')
    return model 
Example #14
Source File: gan.py    From deepchem with MIT License 6 votes vote down vote up
def create_discriminator_loss(self, discrim_output_train, discrim_output_gen):
    """Create the loss function for the discriminator.

    The default implementation is appropriate for most cases.  Subclasses can
    override this if the need to customize it.

    Parameters
    ----------
    discrim_output_train: Tensor
      the output from the discriminator on a batch of training data.  This is
      its estimate of the probability that each sample is training data.
    discrim_output_gen: Tensor
      the output from the discriminator on a batch of generated data.  This is
      its estimate of the probability that each sample is training data.

    Returns
    -------
    A Tensor equal to the loss function to use for optimizing the discriminator.
    """
    return Lambda(lambda x: -tf.reduce_mean(tf.math.log(x[0]+1e-10) + tf.math.log(1-x[1]+1e-10)))([discrim_output_train, discrim_output_gen]) 
Example #15
Source File: keras_layers.py    From DeepPavlov with Apache License 2.0 5 votes vote down vote up
def multiplicative_self_attention(units, n_hidden=None, n_output_features=None, activation=None):
    """
    Compute multiplicative self attention for time series of vectors (with batch dimension)
    the formula: score(h_i, h_j) = <W_1 h_i,  W_2 h_j>,  W_1 and W_2 are learnable matrices
    with dimensionality [n_hidden, n_input_features]

    Args:
        units: tf tensor with dimensionality [batch_size, time_steps, n_input_features]
        n_hidden: number of units in hidden representation of similarity measure
        n_output_features: number of features in output dense layer
        activation: activation at the output

    Returns:
        output: self attended tensor with dimensionality [batch_size, time_steps, n_output_features]
    """
    n_input_features = K.int_shape(units)[2]
    if n_hidden is None:
        n_hidden = n_input_features
    if n_output_features is None:
        n_output_features = n_input_features
    exp1 = Lambda(lambda x: expand_tile(x, axis=1))(units)
    exp2 = Lambda(lambda x: expand_tile(x, axis=2))(units)
    queries = Dense(n_hidden)(exp1)
    keys = Dense(n_hidden)(exp2)
    scores = Lambda(lambda x: K.sum(queries * x, axis=3, keepdims=True))(keys)
    attention = Lambda(lambda x: softmax(x, axis=2))(scores)
    mult = Multiply()([attention, exp1])
    attended_units = Lambda(lambda x: K.sum(x, axis=2))(mult)
    output = Dense(n_output_features, activation=activation)(attended_units)
    return output 
Example #16
Source File: ResidueResidueContactPredictor.py    From tape-neurips2019 with MIT License 5 votes vote down vote up
def __init__(self,
                 input_name: str = 'encoder_output',
                 output_name: str = 'sequence_logits'):
        super().__init__()
        self._input_name = input_name
        self._output_name = output_name

        self.get_pairwise_feature_vector = Stack()
        self.get_pairwise_feature_vector.add(Dense(64, activation='linear'))

        def concat_pairs(tensor):
            seqlen = tf.shape(tensor)[1]
            input_left = tf.tile(tensor[:, :, None], (1, 1, seqlen, 1))
            input_right = tf.tile(tensor[:, None, :], (1, seqlen, 1, 1))
            output = tf.concat((input_left, input_right), -1)
            return output

        self.get_pairwise_feature_vector.add(Lambda(concat_pairs))

        self.predict_contact_map = Stack()
        self.predict_contact_map.add(
            PaddedConv(2, 64, 1, dropout=0.1))
        for layer in range(30):
            self.predict_contact_map.add(
                ResidualBlock(2, 64, 3, dropout=0.1, add_checkpoint=layer % 5 == 0))
        self.predict_contact_map.add(Dense(1, activation='linear')) 
Example #17
Source File: cmvae.py    From AirSim-Drone-Racing-VAE-Imitation with MIT License 5 votes vote down vote up
def __init__(self, n_z, gate_dim=4, res=96, trainable_model=True):
        super(Cmvae, self).__init__()
        # create the 3 base models:
        self.q_img = dronet.Dronet(num_outputs=n_z*2, include_top=True)
        self.p_img = decoders.ImgDecoder()
        self.p_gate = decoders.GateDecoder(gate_dim=gate_dim)
        # Create sampler
        self.mean_params = Lambda(lambda x: x[:, : n_z])
        self.stddev_params = Lambda(lambda x: x[:, n_z:]) 
Example #18
Source File: module.py    From Centernet-Tensorflow2.0 with Apache License 2.0 5 votes vote down vote up
def top_pool(inputs):
    _, h, w, c = inputs.get_shape().as_list()
    x = layers.Lambda(lambda x: tf.reverse(x,[1]))(inputs)
    x = layers.Lambda(lambda x: tf.pad(x, tf.constant([[0, 0], [h-1, 0], [0, 0], [0, 0]]), constant_values=0))(x)
    x = layers.Lambda(lambda x: tf.nn.max_pool(x, (h, 1), (1,1), padding="VALID"))(x)
    out = layers.Lambda(lambda x: tf.reverse(x, [1]))(x)
    return out 
Example #19
Source File: module.py    From Centernet-Tensorflow2.0 with Apache License 2.0 5 votes vote down vote up
def left_pool(inputs):
    _, h, w, c = inputs.get_shape().as_list()
    x = layers.Lambda(lambda x: tf.reverse(x, [2]))(inputs)
    x = layers.Lambda(lambda x: tf.pad(x, tf.constant([[0, 0], [0, 0], [w-1, 0], [0, 0]]), constant_values=0))(x)
    x = layers.Lambda(lambda x:tf.nn.max_pool(x, (1, w), (1, 1), padding="VALID"))(x)
    out = layers.Lambda(lambda x: tf.reverse(x, [2]))(x)
    return out 
Example #20
Source File: module.py    From Centernet-Tensorflow2.0 with Apache License 2.0 5 votes vote down vote up
def bottom_pool(inputs):
    _, h, w, c = inputs.get_shape().as_list()
    x = layers.Lambda(lambda x: tf.pad(inputs, tf.constant([[0, 0], [h - 1, 0], [0, 0], [0, 0]]), constant_values=0))(inputs)
    out = layers.Lambda(lambda x: tf.nn.max_pool(x, (h, 1), (1, 1), padding="VALID"))(x)
    return out 
Example #21
Source File: module.py    From Centernet-Tensorflow2.0 with Apache License 2.0 5 votes vote down vote up
def right_pool(inputs):
    _, h, w, c = inputs.get_shape().as_list()
    x = layers.Lambda(lambda x: tf.pad(inputs, tf.constant([[0, 0], [0, 0], [w - 1, 0], [0, 0]]), constant_values=0))(inputs)
    out = layers.Lambda(lambda x: tf.nn.max_pool(x, (1, w), (1, 1), padding="VALID"))(x)
    return out 
Example #22
Source File: decode.py    From Centernet-Tensorflow2.0 with Apache License 2.0 5 votes vote down vote up
def CtDetDecode(model, hm_index=3, reg_index=4, wh_index=5, k=100, output_stride=4):
  def _decode(args):
    hm, reg, wh = args
    return _ctdet_decode(hm, reg, wh, k=k, output_stride=output_stride)
  output = Lambda(_decode)([model.outputs[i] for i in [hm_index, reg_index, wh_index]])
  model = Model(model.input, output)
  return model 
Example #23
Source File: model.py    From keras-YOLOv3-model-set with MIT License 5 votes vote down vote up
def get_yolo2_train_model(model_type, anchors, num_classes, weights_path=None, freeze_level=1, optimizer=Adam(lr=1e-3, decay=1e-6), label_smoothing=0, model_pruning=False, pruning_end_step=10000):
    '''create the training model, for YOLOv2'''
    #K.clear_session() # get a new session
    num_anchors = len(anchors)

    # y_true in form of relative x, y, w, h, objectness, class
    y_true_input = Input(shape=(None, None, num_anchors, 6))

    model_body, backbone_len = get_yolo2_model(model_type, num_anchors, num_classes, model_pruning=model_pruning, pruning_end_step=pruning_end_step)
    print('Create YOLOv2 {} model with {} anchors and {} classes.'.format(model_type, num_anchors, num_classes))
    print('model layer number:', len(model_body.layers))

    if weights_path:
        model_body.load_weights(weights_path, by_name=True)#, skip_mismatch=True)
        print('Load weights {}.'.format(weights_path))

    if freeze_level in [1, 2]:
        # Freeze the backbone part or freeze all but final feature map & input layers.
        num = (backbone_len, len(model_body.layers)-2)[freeze_level-1]
        for i in range(num): model_body.layers[i].trainable = False
        print('Freeze the first {} layers of total {} layers.'.format(num, len(model_body.layers)))
    elif freeze_level == 0:
        # Unfreeze all layers.
        for i in range(len(model_body.layers)):
            model_body.layers[i].trainable= True
        print('Unfreeze all of the layers.')

    model_loss, location_loss, confidence_loss, class_loss = Lambda(yolo2_loss, name='yolo_loss',
            arguments={'anchors': anchors, 'num_classes': num_classes, 'label_smoothing': label_smoothing})(
            [model_body.output, y_true_input])

    model = Model([model_body.input, y_true_input], model_loss)

    loss_dict = {'location_loss':location_loss, 'confidence_loss':confidence_loss, 'class_loss':class_loss}
    add_metrics(model, loss_dict)

    model.compile(optimizer=optimizer, loss={
        # use custom yolo_loss Lambda layer.
        'yolo_loss': lambda y_true, y_pred: y_pred})

    return model 
Example #24
Source File: layers.py    From keras-YOLOv3-model-set with MIT License 5 votes vote down vote up
def space_to_depth_x2(x):
    """Thin wrapper for Tensorflow space_to_depth with block_size=2."""
    # Import currently required to make Lambda work.
    # See: https://github.com/fchollet/keras/issues/5088#issuecomment-273851273
    import tensorflow as tf
    return tf.nn.space_to_depth(x, block_size=2) 
Example #25
Source File: gan.py    From deepchem with MIT License 5 votes vote down vote up
def create_discriminator_loss(self, discrim_output_train, discrim_output_gen):
    return Lambda(lambda x: tf.reduce_mean(x[0] - x[1]))(
        [discrim_output_train[0], discrim_output_gen]) + discrim_output_train[1] 
Example #26
Source File: gan.py    From deepchem with MIT License 5 votes vote down vote up
def create_generator_loss(self, discrim_output):
    return Lambda(lambda x: tf.reduce_mean(x))(discrim_output) 
Example #27
Source File: shufflenet_v2.py    From keras-YOLOv3-model-set with MIT License 5 votes vote down vote up
def channel_split(x, name=''):
    # equipartition
    in_channles = x.shape.as_list()[-1]
    ip = in_channles // 2
    c_hat = Lambda(lambda z: z[:, :, :, 0:ip], name='%s/sp%d_slice' % (name, 0))(x)
    c = Lambda(lambda z: z[:, :, :, ip:], name='%s/sp%d_slice' % (name, 1))(x)
    return c_hat, c 
Example #28
Source File: SEResNeXt.py    From TF.Keras-Commonly-used-models with Apache License 2.0 5 votes vote down vote up
def residual_layer(self, x, out_dim):
        '''
        Residual block.
        '''
        for i in range(self.num_block):
            input_dim = int(np.shape(x)[-1])
            
            if input_dim*2 == out_dim:
                flag = True
                stride = 2
                channel = input_dim // 2
            else:
                flag = False
                stride = 1
            
            subway_x = self.split_layer(x, stride)
            subway_x = self.conv_bn(subway_x, out_dim, 1, 1)
            subway_x = self.squeeze_excitation_layer(subway_x, out_dim)
            
            if flag:
                pad_x = AveragePooling2D(pool_size=(2,2), strides=(2,2), padding='same')(x)
                pad_x = Lambda(self.channel_zeropad, output_shape=self.channel_zeropad_output)(pad_x)
            else:
                pad_x = x
            
            x = self.activation(add([pad_x, subway_x]))
                
        return x 
Example #29
Source File: mobilenet_base.py    From TF.Keras-Commonly-used-models with Apache License 2.0 5 votes vote down vote up
def _bottleneck(self, inputs, filters, kernel, e, s, squeeze, nl):
        """Bottleneck
        This function defines a basic bottleneck structure.
        # Arguments
            inputs: Tensor, input tensor of conv layer.
            filters: Integer, the dimensionality of the output space.
            kernel: An integer or tuple/list of 2 integers, specifying the
                width and height of the 2D convolution window.
            e: Integer, expansion factor.
                t is always applied to the input size.
            s: An integer or tuple/list of 2 integers,specifying the strides
                of the convolution along the width and height.Can be a single
                integer to specify the same value for all spatial dimensions.
            squeeze: Boolean, Whether to use the squeeze.
            nl: String, nonlinearity activation type.
        # Returns
            Output tensor.
        """

        channel_axis = 1 if K.image_data_format() == 'channels_first' else -1
        input_shape = K.int_shape(inputs)
        tchannel = input_shape[channel_axis] * e
        r = s == 1 and input_shape[3] == filters

        x = self._conv_block(inputs, tchannel, (1, 1), (1, 1), nl)

        x = DepthwiseConv2D(kernel, strides=(s, s), depth_multiplier=1, padding='same')(x)
        x = BatchNormalization(axis=channel_axis)(x)

        if squeeze:
            x = Lambda(lambda x: x * self._squeeze(x))(x)

        x = self._return_activation(x, nl)

        x = Conv2D(filters, (1, 1), strides=(1, 1), padding='same')(x)
        x = BatchNormalization(axis=channel_axis)(x)

        if r:
            x = Add()([x, inputs])

        return x 
Example #30
Source File: dual_path_network.py    From TF.Keras-Commonly-used-models with Apache License 2.0 5 votes vote down vote up
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