コード例 #1
0
def create_dlinknet():
    inputs = Input(shape=INPUT_SHAPE)
    inputs_ = Conv2D(64, kernel_size=(3, 3), padding='same')(inputs)
    inputs_ = BatchNormalization()(inputs_)
    inputs_ = Activation('relu')(inputs_)
    max_pool_inputs = MaxPooling2D((2, 2), strides=(2, 2))(inputs_)

    encoded_1, encoded_pool_1 = encoder_block(max_pool_inputs,
                                              num_filters=64,
                                              num_res_blocks=3)
    encoded_2, encoded_pool_2 = encoder_block(encoded_pool_1,
                                              num_filters=128,
                                              num_res_blocks=4)
    encoded_3, encoded_pool_3 = encoder_block(encoded_pool_2,
                                              num_filters=256,
                                              num_res_blocks=6)
    encoded_4, encoded_pool_4 = encoder_block(encoded_pool_3,
                                              num_filters=512,
                                              num_res_blocks=3)

    center = dilated_center_block(encoded_4, 512)

    decoded_1 = Add()([decoder_block(center, 256), encoded_3])
    decoded_2 = Add()([decoder_block(decoded_1, 128), encoded_2])
    decoded_3 = Add()([decoder_block(decoded_2, 64), encoded_1])
    decoded_4 = decoder_block(decoded_3, 64)

    final = Conv2DTranspose(32, kernel_size=(3, 3), padding='same')(decoded_4)
    outputs = Conv2D(1, (1, 1), activation='sigmoid')(final)
    model_i = Model(inputs=[inputs], outputs=[outputs])
    model_i.compile(optimizer='adam', loss=combined_loss, metrics=[dice_coeff])
    model_i.summary()
    # model_i.load_weights(save_model_path)
    return model_i
コード例 #2
0
def up_projection(lt_, nf, s, block):
    with tf.name_scope('up_' + str(block)):
        if s == 2:
            ht = Conv2DTranspose(nf, 2, strides=2)(lt_)
            ht = PReLU()(ht)
            lt = ZeroPadding2D(2)(ht)
            lt = Conv2D(nf, 6, 2)(lt)
            lt = PReLU()(lt)
            et = Subtract()([lt, lt_])
            ht1 = Conv2DTranspose(nf, 2, strides=2)(et)
            ht1 = PReLU()(ht1)
            ht1 = Add()([ht, ht1])
            return (ht1)
        if s == 4:
            ht = Conv2DTranspose(nf, 4, strides=4)(lt_)
            ht = PReLU()(ht)
            lt = ZeroPadding2D(2)(ht)
            lt = Conv2D(nf, 8, strides=4)(lt)
            lt = PReLU()(lt)
            et = Subtract()([lt, lt_])
            ht1 = Conv2DTranspose(nf, 4, strides=4)(et)
            ht1 = PReLU()(ht1)
            ht1 = Add()([ht, ht1])
            return (ht1)
        if s == 8:
            ht = Conv2DTranspose(nf, 8, strides=8)(lt_)
            ht = PReLU()(ht)
            lt = ZeroPadding2D(2)(ht)
            lt = Conv2D(nf, 12, strides=8)(lt)
            lt = PReLU()(lt)
            et = Subtract()([lt, lt_])
            ht1 = Conv2DTranspose(nf, 8, strides=8)(et)
            ht1 = PReLU()(ht1)
            ht1 = Add()([ht, ht1])
        return (ht1)
def phase_residual_block_2(X):
    X_main = Conv2D(16, (1, 1),
                    strides=(2, 2),
                    padding='same',
                    kernel_initializer='he_normal')(X)
    X_main = BatchNormalization()(X_main)
    X_main = Conv2D(16, (3, 3), padding='same',
                    kernel_initializer='he_normal')(X_main)
    X_main = BatchNormalization()(X_main)
    X_main = Lambda(crelu)(X_main)
    X_main = Conv2D(48, (1, 1), padding='same',
                    kernel_initializer='he_normal')(X_main)
    X_main = BatchNormalization()(X_main)
    X_res_branch = Conv2D(48, (1, 1),
                          strides=(2, 2),
                          padding='same',
                          kernel_initializer='he_normal')(X)
    X_res_branch = BatchNormalization()(X_res_branch)
    X_combined = Add()([X_main, X_res_branch])
    X_main = Conv2D(16, (1, 1), padding='same',
                    kernel_initializer='he_normal')(X_combined)
    X_main = BatchNormalization()(X_main)
    X_main = Conv2D(16, (3, 3), padding='same',
                    kernel_initializer='he_normal')(X_main)
    X_main = BatchNormalization()(X_main)
    X_main = Lambda(crelu)(X_main)
    X_main = Conv2D(48, (1, 1), padding='same',
                    kernel_initializer='he_normal')(X_main)
    X_main = BatchNormalization()(X_main)
    X_combined = Add()([X_main, X_combined])
    return X_combined
コード例 #4
0
ファイル: cplx_model.py プロジェクト: lamductan/modis_utils
    def _create_decoder(input_shape, encode_block):
        conv3 = encode_block.get_layer('conv3').output
        x = Input(shape=input_shape, name='decoder_input')
        net = Add()([x, conv3])

        conv2 = encode_block.get_layer('conv2').output
        net = Conv2DTranspose(filters=64,
                              kernel_size=3,
                              strides=2,
                              name='dconv1')(x)
        net = Add()([net, conv2])
        net = BatchNormalization()(net)

        net = Conv2DTranspose(filters=32,
                              kernel_size=3,
                              strides=2,
                              name='dconv2')(net)
        net = BatchNormalization()(net)

        encoder_input = encode_block.get_layer('encoder_input').output
        net = Conv2DTranspose(filters=1,
                              kernel_size=3,
                              strides=2,
                              name='dconv3')(net)
        net = Add()([net, encoder_input])
        net = BatchNormalization()(net)

        net = Model(inputs=[x, encode_block.input],
                    outputs=net,
                    name='decoder')
        return net
コード例 #5
0
ファイル: yolo_net.py プロジェクト: yanni1995/yolov3_tf2.0
def resdual_net(x, num_filters, num_blocks, name=None):
    x = Lambda(padding)(x)
    x = Conv2D(filters=num_filters,
               kernel_size=3,
               strides=2,
               padding='VALID',
               use_bias=False)(x)
    x = BatchNormalization()(x)
    x = LeakyReLU(alpha=0.1)(x)
    for i in range(num_blocks):
        y = Conv2D(filters=num_filters // 2,
                   kernel_size=1,
                   strides=1,
                   padding='VALID',
                   use_bias=False)(x)
        y = BatchNormalization()(y)
        y = LeakyReLU(alpha=0.1)(y)

        y = Lambda(padding)(y)
        y = Conv2D(filters=num_filters,
                   kernel_size=3,
                   strides=1,
                   padding='VALID',
                   use_bias=False)(y)
        y = BatchNormalization()(y)
        y = LeakyReLU(alpha=0.1)(y)
        if i == num_blocks - 1:
            x = Add(name=name)([x, y])
        else:
            x = Add()([x, y])
    return x
コード例 #6
0
def edsr(scale, num_filters=64, num_res_blocks=8, res_block_scaling=None):
    x_in = Input(shape=(None, None, 3))

    # print(tf.keras.backend.shape(x_in))
    x = Lambda(normalize)(x_in)
    # print(tf.keras.backend.shape(x))
    x = b = Conv2D(num_filters, 3, padding='same')(x)
    # print(tf.keras.backend.shape(x))
    for i in range(num_res_blocks):

        b = res_block(b, num_filters, res_block_scaling)
        print(tf.keras.backend.shape(b))
        if i == 8:
            b = Add()([x, b])
            b = cpy = upsample(b, 2, num_filters, 'upscale_1')
            b = srcnn(b)
        # if i == 15:
        #   b = upsample(b, 2, num_filters, 'upscale_2')

    b = Conv2D(num_filters, 3, padding='same')(b)
    # print(tf.keras.backend.shape(b))
    # x = Add()([x, b])
    # print(tf.keras.backend.shape(b))
    b = Add()([b, cpy])
    x = upsample(b, 2, num_filters, 'upscale_2')
    x = srcnn(x)
    # x = upsample(x, 4, num_filters, 'upscale_3')
    # print(tf.keras.backend.shape(b))
    x = Conv2D(3, 3, padding='same')(x)
    # print(tf.keras.backend.shape(b))

    x = Lambda(denormalize)(x)
    # print(tf.keras.backend.shape(b))
    # print("Done")
    return Model(x_in, x, name="edsr")
コード例 #7
0
ファイル: STResNet.py プロジェクト: uZeroJ/DeepSTN
def stresnet(c_conf=(3, 2, 32, 32), p_conf=(3, 2, 32, 32), t_conf=(3, 2, 32, 32),
             external_dim=8, nb_residual_unit=3, CF=64):
    '''
    C - Temporal Closeness
    P - Period
    T - Trend
    conf = (len_seq, nb_flow, map_height, map_width)
    external_dim
    '''

    # main input
    main_inputs = []
    outputs = []
    for conf in [c_conf, p_conf, t_conf]:
        if conf is not None:
            len_seq, nb_flow, map_height, map_width = conf
            input = Input(shape=(nb_flow * len_seq, map_height, map_width))
            main_inputs.append(input)
            # Conv1
            conv1 = Convolution2D(
                filters=CF, kernel_size=(3, 3), padding="same")(input)
            # [nb_residual_unit] Residual Units
            residual_output = ResUnits(_residual_unit, nb_filter=CF,
                                       repetations=nb_residual_unit)(conv1)
            # Conv2
            activation = Activation('relu')(residual_output)
            conv2 = Convolution2D(
                filters=nb_flow, kernel_size=(3, 3), padding="same")(activation)
            outputs.append(conv2)

    # parameter-matrix-based fusion
    if len(outputs) == 1:
        main_output = outputs[0]
    else:
        from BikeNYC.DST_network.ilayer import iLayer
        new_outputs = []
        for output in outputs:
            new_outputs.append(iLayer()(output))
        main_output = Add()(new_outputs)

    # fusing with external component
    if external_dim != None and external_dim > 0:
        # external input
        external_input = Input(shape=(external_dim,))
        main_inputs.append(external_input)
        embedding = Dense(units=10)(external_input)
        embedding = Activation('relu')(embedding)
        h1 = Dense(units=nb_flow * map_height * map_width)(embedding)
        activation = Activation('relu')(h1)
        external_output = Reshape((nb_flow, map_height, map_width))(activation)
        main_output = Add()([main_output, external_output])
    else:
        print('external_dim:', external_dim)

    main_output = Activation('tanh')(main_output)
    model = Model(input=main_inputs, output=main_output)

    return model
コード例 #8
0
    def add_model(self, input_data, target_data=None):
        """Implements core of model that transforms input_data into predictions.

        The core transformation for this model which transforms a batch of input
        data into a batch of predictions.

        Args:
          input_data: A tensor of shape (batch_size, num_steps, time_stamps).
          target_data: A tensor of shape (batch_size, num_steps, time_stamps).
        Returns:
          predict: A tensor of shape (batch_size, num_steps, time_stamps)
        """
        # Consider signal matrix as an image with channels.
        height = self.config.num_steps
        width = self.config.time_stamps
        channels = self.config.channels
        batch_size = self.config.batch_size

        # input_data: (-1, height, width, channels)
        input_data = tf.reshape(input_data, [-1, channels, height, width])
        input_data = tf.transpose(input_data, perm=[0, 2, 3, 1])

        x0 = Convolution2D(64, (3, 3),
                           activation='relu',
                           padding='same',
                           name='sr_res_conv1')(input_data)

        x1 = Convolution2D(64, (3, 3),
                           activation='relu',
                           padding='same',
                           strides=(2, 2),
                           name='sr_res_conv2')(x0)
        x2 = Convolution2D(64, (3, 3),
                           activation='relu',
                           padding='same',
                           strides=(2, 2),
                           name='sr_res_conv3')(x1)

        x = self._residual_block(x2, 1)
        for i in range(self.config.nb_residual):
            x = self._residual_block(x, i + 2)
        x = Add()([x, x2])

        x = self._upscale_block(x, 1)
        x = Add()([x, x1])

        x = self._upscale_block(x, 2)
        x = Add()([x, x0])

        output = Convolution2D(self.config.channels, (3, 3),
                               activation="linear",
                               padding='same',
                               name='sr_res_conv_final')(x)

        prediction = tf.transpose(output, perm=[0, 3, 1, 2])
        prediction = tf.reshape(prediction, [batch_size, height, width])
        return prediction
コード例 #9
0
ファイル: resnet50.py プロジェクト: amitregmi/ocr-experiments
def backbone(input_shape=(512, 512, 3)):
    image = Input(shape=input_shape, name="image")
    base_model = ResNet50(input_tensor=image, include_top=False, weights=None)
    x = base_model.get_layer(name="activation_48").output
    x = _deconv_block(x, 1024)
    x = Add()([x, base_model.get_layer(name="activation_39").output])
    x = _deconv_block(x, 512)
    x = Add()([x, base_model.get_layer(name="activation_21").output])
    model = Model(image, x)
    return model, 8
コード例 #10
0
def relation_attention_module(glob_fts, fts):
    attention_weights = []
    for i in range(len(fts)):
        fts[i] = Concatenate()([fts[i], glob_fts])
        weight = attention_extractor(fts[i])
        fts[i] = Multiply()([fts[i], weight])
        attention_weights.append(weight)
    total_weights = Add()(attention_weights)
    numerator = Add()(
        fts)  # numerator of fraction in definition of P_ran (see paper)
    final_representation = Lambda(lambda_divide)([numerator, total_weights])
    return final_representation
コード例 #11
0
def self_attention_module(crops, CNN):
    attention_weights = []
    for i in range(len(crops)):
        crops[i] = CNN(crops[i])
        weight = attention_extractor(crops[i])
        crops[i] = Multiply()([crops[i], weight])
        attention_weights.append(weight)
    total_weights = Add()(attention_weights)
    numerator = Add()(
        crops)  # numerator of fraction in definition of F_m (see paper)
    global_feature_representation = Lambda(lambda_divide)(
        [numerator, total_weights])
    return global_feature_representation
コード例 #12
0
def UNet3D_Isensee(input_shape=(4, 128, 128, 128),
                   nchannels=(16, 32, 64, 128, 256),
                   n_labels=3,
                   activation='sigmoid',
                   n_segmentation_level=3):
    inputs = Input(input_shape)
    current_layer = inputs
    downsample_level = []
    segmentation_level = []
    for idx, filters in enumerate(nchannels):
        if idx == 0:
            in_conv = convolution_module(current_layer,
                                         filters,
                                         activation=LeakyReLU,
                                         instance_norm=True)
        else:
            in_conv = convolution_module(current_layer,
                                         filters,
                                         activation=LeakyReLU,
                                         strides=(2, 2, 2),
                                         instance_norm=True)
        context_layer = context_module(in_conv, filters, dropout=0.3)
        add_layer = Add()([in_conv, context_layer])
        downsample_level.append(add_layer)
        current_layer = add_layer

    for idx, filters in reversed(list(enumerate(nchannels[:-1:]))):
        upsample_conv = upsampling_module(current_layer, filters)
        current_layer = concatenate([upsample_conv, downsample_level[idx]],
                                    axis=1)
        localization = localization_module(current_layer, filters)
        current_layer = localization
        if idx < n_segmentation_level:
            layer = Conv3D(n_labels,
                           kernel_size=(1, 1, 1),
                           data_format='channels_first')(localization)
            segmentation_level.append(layer)

    output_layer = None
    for idx in range(len(segmentation_level)):
        if output_layer is None:
            output_layer = segmentation_level[idx]
        else:
            output_layer = Add()([output_layer, segmentation_level[idx]])
        if idx != len(segmentation_level) - 1:
            output_layer = UpSampling3D(
                size=(2, 2, 2), data_format='channels_first')(output_layer)

    act = Activation(activation)(output_layer)
    model = Model(inputs=inputs, outputs=act)
    return model
コード例 #13
0
def backbone(input_shape=(512, 512, 3)):
    image = Input(shape=input_shape, name="image")
    base_model = MobileNet(input_tensor=image,
                           include_top=False,
                           weights="imagenet")
    x = base_model.output
    x = _deconv_block(x, 512, kernel_size=3)
    y = base_model.layers[81].output
    x = Add()([x, y])
    x = _deconv_block(x, 256, kernel_size=3)
    y = base_model.layers[39].output
    x = Add()([x, y])
    model = Model(image, x)
    return model, 8
コード例 #14
0
def build_model():
    input = Input(shape=(257, 1091, 1))
    [x, weight] = attentionModule(input)

    x = Conv2D(16, kernel_size=(3, 3), strides=(1,1), padding="same", kernel_initializer="glorot_normal")(x)

    #block 1
    residual = x
    x = _bn_relu(x)
    x = Conv2D(16, kernel_size=(3,   3), strides=(1,1), padding="same", kernel_initializer="glorot_normal")(x)
    x = _bn_relu(x)
    x = Conv2D(16, kernel_size=(3, 3), strides=(1,1), padding="same", kernel_initializer="glorot_normal")(x)
    x = Add()([x, residual]) #x += residual
    x = MaxPooling2D(pool_size=(1, 2))(x)
    x = Conv2D(32, kernel_size=(3, 3), dilation_rate=(2,2), kernel_initializer="glorot_normal")(x)
    x = ZeroPadding2D()(x)

    x = residualBlock(pool_size=(1,2), dilation_rate=(4,4))(x)
    x = residualBlock(pool_size=(2,2), dilation_rate=(4,4))(x)
    x = residualBlock(pool_size=(2,2), dilation_rate=(8,8))(x)
    x = residualBlock(pool_size=(2,2), dilation_rate=(8,8))(x)

    x = Flatten()(x)

    x = Dense(32, kernel_initializer="glorot_normal")(x)

    residual = x

    x = _bn_relu_dense(x)
    x = Dense(32, kernel_initializer="glorot_normal")(x)
    x = _bn_relu_dense(x)
    x = Dense(32, kernel_initializer="glorot_normal")(x)
    x = Add()([x, residual]) #x += residual
    ###

    residual = x
    x = _bn_relu_dense(x)
    x = Dense(32, kernel_initializer="glorot_normal")(x)
    x = _bn_relu_dense(x)
    x = Dense(32, kernel_initializer="glorot_normal")(x)
    x = Add()([x, residual]) #x += residual

    ###
    x = _bn_relu_dense(x)
    dense = Dense(1, kernel_initializer="glorot_normal", activation="sigmoid")(x)

    model = Model(inputs=input, outputs=dense)
    weight_model = Model(inputs=input, outputs=weight)
    return model, weight_model
コード例 #15
0
ファイル: submodule.py プロジェクト: YanWQ/LLF-Net
def channel_attention_m(x, residual=False, stream=False):
    if not stream:
        # dims: BxHxWxCxM (M streams)
        if isinstance(x, list):
            x = Lambda(lambda var: K.stack(var, axis=4))(x)
        y = GlobalMaxPooling3D()(x)
        y = Lambda(lambda var: K.expand_dims(
            K.expand_dims(K.expand_dims(var, axis=1), axis=2), axis=3))(y)
        y = Conv3D(filters=int(K.int_shape(x)[-1] / 2),
                   kernel_size=1,
                   strides=1)(y)
        y = Activation("relu")(y)
        y = Conv3D(filters=K.int_shape(x)[-1], kernel_size=1, strides=1)(y)
        y = Activation("softmax")(y)
        y = Lambda(lambda var: tf.multiply(*var))([x, y])
        if residual:
            y = Add()([y, x])
    else:
        # dims: BxHxWxCxM (M streams)
        y = GlobalMaxPooling3D()(x)
        y = Lambda(lambda var: K.expand_dims(
            K.expand_dims(K.expand_dims(var, axis=1), axis=2), axis=3))(y)
        y = Conv3D(filters=int(K.int_shape(x)[-1] / 2),
                   kernel_size=1,
                   strides=1)(y)
        y = Activation("relu")(y)
        y = Conv3D(filters=2, kernel_size=1, strides=1)(y)
        y = Activation("sigmoid")(y)

        y_l = []
        c = int(x.get_shape().as_list()[-1] / 2)
        for i in range(2):
            ind_st = i * c
            ind_end = (i + 1) * c
            x_sub = Lambda(slicing,
                           arguments={
                               'index': ind_st,
                               'index_end': ind_end
                           })(x)
            y_sub = Lambda(slicing, arguments={
                'index': i,
                'index_end': i + 1
            })(y)
            y = Lambda(lambda var: tf.multiply(*var))([x_sub, y_sub])
            if residual:
                y = Add()([y, x_sub])
            y_l.append(y)
        y = concatenate(y_l)
    return y
コード例 #16
0
ファイル: model.py プロジェクト: fidsusj/bomberman_rl
def compile_dueling_deep_q_network():
    he = variance_scaling_initializer()

    model_input = Input(shape=(STATE_SHAPE, ))
    x = Dense(DENSE_LAYER_DIMS,
              input_shape=(STATE_SHAPE, ),
              kernel_initializer=he,
              activation=ACTIVATION_FUNCTION)(model_input)
    # x = Dense(DENSE_LAYER_DIMS, kernel_initializer=he, activation=ACTIVATION_FUNCTION)(x)

    # val_stream, adv_stream = Lambda(lambda w: tf.split(w, 2, 3))(x)
    val_stream, adv_stream = Lambda(lambda w: tf.split(w, 2, 1))(x)

    val_stream = Flatten()(val_stream)
    val = Dense(1, kernel_initializer=he)(val_stream)

    adv_stream = Flatten()(adv_stream)
    adv = Dense(NUMBER_OF_ACTIONS, kernel_initializer=he)(adv_stream)

    # Combine streams into Q-Values
    reduce_mean = Lambda(lambda w: tf.reduce_mean(w, axis=1, keepdims=True)
                         )  # custom layer for reduce mean

    q_vals = Add()([val, Subtract()([adv, reduce_mean(adv)])])

    # Build model
    model = Model(model_input, q_vals)
    model.compile(Adam(LEARNING_RATE), loss=LOSS_FUNCTION)

    return model
コード例 #17
0
def create_pyramid_level(backbone_input,
                         upsamplelike_input=None,
                         addition_input=None,
                         level=5,
                         ndim=2,
                         feature_size=256):
    """Create a pyramid layer from a particular backbone input layer.

    Args:
        backbone_input (layer): Backbone layer to use to create they pyramid layer
        upsamplelike_input ([type], optional): Defaults to None. Input to use
            as a template for shape to upsample to
        addition_input (layer, optional): Defaults to None. Layer to add to
            pyramid layer after conv and upsample
        level (int, optional): Defaults to 5. Level to use in layer names
        feature_size (int, optional): Defaults to 256. Number of filters for
            convolutional layer
        ndim: The spatial dimensions of the input data. Default is 2,
            but it also works with 3
    Returns:
        (pyramid final, pyramid upsample): Pyramid layer after processing,
            upsampled pyramid layer
    """

    acceptable_ndims = {2, 3}
    if ndim not in acceptable_ndims:
        raise ValueError('Only 2 and 3 dimensional networks are supported')

    reduced_name = 'C%s_reduced' % level
    upsample_name = 'P%s_upsampled' % level
    addition_name = 'P%s_merged' % level
    final_name = 'P%s' % level

    # Apply 1x1 conv to backbone layer
    if ndim == 2:
        pyramid = Conv2D(feature_size, (1, 1), strides=(1, 1),
                         padding='same', name=reduced_name)(backbone_input)
    else:
        pyramid = Conv3D(feature_size, (1, 1, 1), strides=(1, 1, 1),
                         padding='same', name=reduced_name)(backbone_input)

    # Upsample pyramid input
    if upsamplelike_input is not None:
        pyramid_upsample = UpsampleLike(name=upsample_name)(
            [pyramid, upsamplelike_input])
    else:
        pyramid_upsample = None

    # Add and then 3x3 conv
    if addition_input is not None:
        pyramid = Add(name=addition_name)([pyramid, addition_input])

    if ndim == 2:
        pyramid_final = Conv2D(feature_size, (3, 3), strides=(1, 1),
                               padding='same', name=final_name)(pyramid)
    else:
        pyramid_final = Conv3D(feature_size, (3, 3, 3), strides=(1, 1, 1),
                               padding='same', name=final_name)(pyramid)

    return pyramid_final, pyramid_upsample
コード例 #18
0
    def init_model(self):

        x = Input(shape=(11, 11, N_FEATURES))
        layer = Conv2D(128, 3, padding="same", activation="relu")(x)

        for _ in range(2):
            res = layer
            layer = Conv2D(128, 3, padding="same", activation="relu")(layer)
            layer = Conv2D(128, 3, padding="same", activation="relu")(layer)
            layer = Conv2D(128, 3, padding="same")(layer)
            layer = Add()([layer, res])
            layer = Activation("relu")(layer)

        y = Conv2D(64, 1, padding="same", activation="relu")(layer)
        y = Flatten()(y)
        y = Dense(64, activation="relu")(y)
        y = Dense(16, activation="relu")(y)
        y = Dense(N_ACTIONS, activation='softmax', name="y")(y)

        message = Conv2D(64, 1, padding="same", activation="relu")(layer)
        message = Flatten()(message)
        message = Dense(64, activation="relu")(message)
        message = Dense(16, activation="relu")(message)
        message = Dense(N_MESSAGE_BITS, name="message")(message)

        model = tf.keras.models.Model(inputs=x, outputs=[y, message])
        model.compile(loss='sparse_categorical_crossentropy',
                      optimizer=tf.keras.optimizers.Adam(lr=LR),
                      metrics=['accuracy'],
                      loss_weights=[1., 0.])
        self.model = model
def residual_block(input,
                   input_channels=None,
                   output_channels=None,
                   kernel_size=(3, 3),
                   stride=1):
    """
    full pre-activation residual block
    https://arxiv.org/pdf/1603.05027.pdf
    """
    if output_channels is None:
        output_channels = input.get_shape()[-1].value
    if input_channels is None:
        input_channels = output_channels // 4

    strides = (stride, stride)

    x = BatchNormalization()(input)
    x = Activation('relu')(x)
    x = Conv2D(input_channels, (1, 1))(x)

    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Conv2D(input_channels, kernel_size, padding='same', strides=stride)(x)

    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Conv2D(output_channels, (1, 1), padding='same')(x)

    if input_channels != output_channels or stride != 1:
        input = Conv2D(output_channels, (1, 1),
                       padding='same',
                       strides=strides)(input)

    x = Add()([x, input])
    return x
コード例 #20
0
def sr_resnet(num_filters=64, num_res_blocks=16):
    lr_input = Input(shape=(24, 24, 3))

    x_start = Conv2D(64, kernel_size=3, strides=1, padding='same')(lr_input)
    x_start = LeakyReLU(0.2)(x_start)

    x = RRDB(x_start)

    x = Conv2D(64, kernel_size=3, strides=1, padding='same')(x)
    x = Lambda(lambda x: x * 0.2)(x)
    x = Add()([x, x_start])

    x = upsample(x, 1)
    if upscaling_factor > 2:
        x = upsample(x, 2)
    if upscaling_factor > 4:
        x = upsample(x, 3)

    x = Conv2D(64, kernel_size=3, strides=1, padding='same')(x)
    x = LeakyReLU(0.2)(x)
    hr_output = Conv2D(channels,
                       kernel_size=3,
                       strides=1,
                       padding='same',
                       activation='tanh')(x)

    model = Model(inputs=lr_input, outputs=hr_output)

    return model
コード例 #21
0
ファイル: wdsr.py プロジェクト: romanoss/super-resolution
def wdsr(scale, num_filters, num_res_blocks, res_block_expansion,
         res_block_scaling, res_block):
    x_in = Input(shape=(None, None, 3))
    x = Lambda(normalize)(x_in)

    # main branch
    m = conv2d_weightnorm(num_filters, 3, padding='same')(x)
    for i in range(num_res_blocks):
        m = res_block(m,
                      num_filters,
                      res_block_expansion,
                      kernel_size=3,
                      scaling=res_block_scaling)
    m = conv2d_weightnorm(3 * scale**2,
                          3,
                          padding='same',
                          name=f'conv2d_main_scale_{scale}')(m)
    m = Lambda(subpixel_conv2d(scale))(m)

    # skip branch
    s = conv2d_weightnorm(3 * scale**2,
                          5,
                          padding='same',
                          name=f'conv2d_skip_scale_{scale}')(x)
    s = Lambda(subpixel_conv2d(scale))(s)

    x = Add()([m, s])
    x = Lambda(denormalize)(x)

    return Model(x_in, x, name="wdsr")
コード例 #22
0
    def build_model(self, vocab_size: int, vector_dim: int):
        """
        Builds the Keras model.
        :param vocab_size: The number of distinct words.
        :param vector_dim: The vector dimension of each word.
        :return: the Keras GloVe model.
        """
        input_target = Input((1, ), name="central_word_id")
        input_context = Input((1, ), name="context_word_id")

        central_embedding = Embedding(vocab_size,
                                      vector_dim,
                                      input_length=1,
                                      name=CNTRL_EMB)(input_target)
        central_bias = Embedding(vocab_size, 1, input_length=1,
                                 name=CNTRL_BS)(input_target)

        context_embedding = Embedding(vocab_size,
                                      vector_dim,
                                      input_length=1,
                                      name=CTX_EMB)(input_context)
        context_bias = Embedding(vocab_size, 1, input_length=1,
                                 name=CTX_BS)(input_context)

        dot_product = Dot(axes=-1)([central_embedding, context_embedding])
        dot_product = Reshape((1, ))(dot_product)
        bias_target = Reshape((1, ))(central_bias)
        bias_context = Reshape((1, ))(context_bias)

        prediction = Add()([dot_product, bias_target, bias_context])

        model = Model(inputs=[input_target, input_context], outputs=prediction)
        model.compile(loss=self.custom_loss, optimizer=Adagrad(lr=self.lr))
        print(model.summary())
        return model
コード例 #23
0
def RRDB(input):
    x = dense_block(input)
    x = dense_block(x)
    x = dense_block(x)
    x = Lambda(lambda x: x * 0.2)(x)
    out = Add()([x, input])
    return out
コード例 #24
0
    def _residual_block(self, ip, id):
        mode = True if self.config.mode == 'train' else False
        channel_axis = 1 if K.image_data_format() == 'channels_first' else -1
        init = ip

        x = Convolution2D(self.config.n, (3, 3),
                          activation='linear',
                          padding='same',
                          name='sr_res_conv_' + str(id) + '_1')(ip)

        x = BatchNormalization(axis=channel_axis,
                               name="sr_res_batchnorm_" + str(id) + "_1")(
                                   x, training=mode)
        x = Activation('relu', name="sr_res_activation_" + str(id) + "_1")(x)

        x = Convolution2D(64, (3, 3),
                          activation='linear',
                          padding='same',
                          name='sr_res_conv_' + str(id) + '_2')(x)
        x = BatchNormalization(axis=channel_axis,
                               name="sr_res_batchnorm_" + str(id) + "_2")(
                                   x, training=mode)

        m = Add(name="sr_res_merge_" + str(id))([x, init])

        return m
コード例 #25
0
 def build_model(self):
     input_state = keras.layers.Input(shape=self.state_shape, name='state_input')
     input_action = keras.layers.Input(shape=(self.action_size, ), name='action_input')
     input_size = self.get_input_size(self.state_shape)
     out = Reshape((input_size, ))(input_state)
     with tf.variable_scope(self.scope):
         for i in range(self.act_insert_block):
             out = dense_block(out, self.hiddens[i], self.activations[i],
                               self.layer_norm, self.noisy_layer)
         val = out
         adv = Concatenate(axis=1)([out, input_action])
         for i in range(self.act_insert_block, len(self.hiddens)):
             val = dense_block(val, self.hiddens[i], self.activations[i],
                               self.layer_norm, self.noisy_layer)
             adv = dense_block(adv, self.hiddens[i], self.activations[i],
                               self.layer_norm, self.noisy_layer)
         val = Dense(1, self.out_activation,
                     kernel_initializer=RandomUniform(-3e-3, 3e-3),
                     bias_initializer=RandomUniform(-3e-3, 3e-3))(val)
         adv = Dense(1, self.out_activation,
                     kernel_initializer=RandomUniform(-3e-3, 3e-3),
                     bias_initializer=RandomUniform(-3e-3, 3e-3))(adv)
         out = Add()([val, adv])
         model = keras.models.Model(inputs=[input_state, input_action], outputs=out)
     return model
コード例 #26
0
def wdsr(scale, num_filters, num_res_blocks, res_block_expansion,
         res_block_scaling, res_block):
    """ WDSR model edited to be single channel uint16 
    """
    x_in = Input(shape=(None, None, 1))
    x = Lambda(normalize)(x_in)

    # main branch
    #    m = conv2d_weightnorm(num_filters, 3, padding='same')(x)
    m = conv2d_weightnorm(num_filters, 1, padding='same')(x)
    for i in range(num_res_blocks):
        m = res_block(m,
                      num_filters,
                      res_block_expansion,
                      kernel_size=3,
                      scaling=res_block_scaling)
    m = conv2d_weightnorm(1 * scale**2,
                          3,
                          padding='same',
                          name=f'conv2d_main_scale_{scale}')(m)
    m = Lambda(pixel_shuffle(scale))(m)

    # skip branch
    s = conv2d_weightnorm(1 * scale**2,
                          5,
                          padding='same',
                          name=f'conv2d_skip_scale_{scale}')(x)
    s = Lambda(pixel_shuffle(scale))(s)

    x = Add()([m, s])
    x = Lambda(denormalize)(x)

    return Model(x_in, x, name="wdsr")
コード例 #27
0
def build_model(seq_len: int, word_embedding_dim: int, vocab_size: int,
                hidden_state_dim: int, learning_rate: float):
    sequence_input = Input(shape=(seq_len, ), dtype='int32')
    embeddings = Embedding(vocab_size,
                           word_embedding_dim,
                           input_length=seq_len)(sequence_input)
    lstm = Bidirectional(
        LSTM(hidden_state_dim,
             return_sequences=True,
             return_state=True,
             dropout=.5,
             recurrent_dropout=.4))(embeddings)
    lstm, forward_h, forward_c, backward_h, backward_c = Bidirectional(
        LSTM(hidden_state_dim,
             return_sequences=True,
             return_state=True,
             dropout=0.5,
             recurrent_dropout=.4))(lstm)
    state_h = Add()([forward_h, backward_h])
    attention = Attention(hidden_state_dim)
    context_vector, attention_weights = attention(lstm, state_h)
    dense = Dense(100, activation='relu')(context_vector)
    dropout = Dropout(rate=.3)(dense)
    output = Dense(1, activation='sigmoid')(dropout)
    model = Model(inputs=sequence_input, outputs=output, name="TweetsModel")

    print(model.summary())

    model.compile(optimizer=Nadam(lr=learning_rate),
                  loss='binary_crossentropy',
                  metrics=['accuracy'])

    return model
コード例 #28
0
def res_block(x_in, filters, scaling):
    x = Conv2D(filters, 3, padding='same', activation='relu')(x_in)
    x = Conv2D(filters, 3, padding='same')(x)
    if scaling:
        x = Lambda(lambda t: t * scaling)(x)
    x = Add()([x_in, x])
    return x
コード例 #29
0
def _inverted_res_block(inputs,
                        expansion,
                        stride,
                        alpha,
                        filters,
                        block_id,
                        skip_connection,
                        rate=1):
    in_channels = inputs.shape[-1].value  # inputs._keras_shape[-1]
    pointwise_conv_filters = int(filters * alpha)
    pointwise_filters = _make_divisible(pointwise_conv_filters, 8)
    x = inputs
    prefix = 'expanded_conv_{}_'.format(block_id)
    if block_id:
        # Expand

        x = Conv2D(expansion * in_channels,
                   kernel_size=1,
                   padding='same',
                   use_bias=False,
                   activation=None,
                   name=prefix + 'expand')(x)
        x = BatchNormalization(epsilon=1e-3,
                               momentum=0.999,
                               name=prefix + 'expand_BN')(x)
        x = Activation(tf.nn.relu6, name=prefix + 'expand_relu')(x)
    else:
        prefix = 'expanded_conv_'
    # Depthwise
    x = DepthwiseConv2D(kernel_size=3,
                        strides=stride,
                        activation=None,
                        use_bias=False,
                        padding='same',
                        dilation_rate=(rate, rate),
                        name=prefix + 'depthwise')(x)
    x = BatchNormalization(epsilon=1e-3,
                           momentum=0.999,
                           name=prefix + 'depthwise_BN')(x)

    x = Activation(tf.nn.relu6, name=prefix + 'depthwise_relu')(x)

    # Project
    x = Conv2D(pointwise_filters,
               kernel_size=1,
               padding='same',
               use_bias=False,
               activation=None,
               name=prefix + 'project')(x)
    x = BatchNormalization(epsilon=1e-3,
                           momentum=0.999,
                           name=prefix + 'project_BN')(x)

    if skip_connection:
        return Add(name=prefix + 'add')([inputs, x])

    # if in_channels == pointwise_filters and stride == 1:
    #    return Add(name='res_connect_' + str(block_id))([inputs, x])

    return x
コード例 #30
0
def dilated_center_block(input_tensor, num_filters):

    dilation_1 = Conv2D(num_filters,
                        kernel_size=(3, 3),
                        dilation_rate=(1, 1),
                        padding='same')(input_tensor)
    dilation_1 = Activation('relu')(dilation_1)

    dilation_2 = Conv2D(num_filters,
                        kernel_size=(3, 3),
                        dilation_rate=(2, 2),
                        padding='same')(dilation_1)
    dilation_2 = Activation('relu')(dilation_2)

    dilation_4 = Conv2D(num_filters,
                        kernel_size=(3, 3),
                        dilation_rate=(4, 4),
                        padding='same')(dilation_2)
    dilation_4 = Activation('relu')(dilation_4)

    dilation_8 = Conv2D(num_filters,
                        kernel_size=(3, 3),
                        dilation_rate=(8, 8),
                        padding='same')(dilation_4)
    dilation_8 = Activation('relu')(dilation_8)

    final_diliation = Add()(
        [input_tensor, dilation_1, dilation_2, dilation_4, dilation_8])

    return final_diliation