Exemplo n.º 1
0
    def create_model(self, height=32, width=32, channels=3, load_weights=False, batch_size=128):
        """
            Creates a model to remove / reduce noise from upscaled images.
        """
        from keras.layers.convolutional import Deconvolution2D

        # Perform check that model input shape is divisible by 4
        init = super(DenoisingAutoEncoderSR, self).create_model(height, width, channels, load_weights, batch_size)

        if K.image_dim_ordering() == "th":
            output_shape = (None, channels, width, height)
        else:
            output_shape = (None, width, height, channels)

        level1_1 = Convolution2D(self.n1, 3, 3, activation='relu', border_mode='same')(init)
        level2_1 = Convolution2D(self.n1, 3, 3, activation='relu', border_mode='same')(level1_1)

        level2_2 = Deconvolution2D(self.n1, 3, 3, activation='relu', output_shape=output_shape, border_mode='same')(level2_1)
        level2 = merge([level2_1, level2_2], mode='sum')

        level1_2 = Deconvolution2D(self.n1, 3, 3, activation='relu', output_shape=output_shape, border_mode='same')(level2)
        level1 = merge([level1_1, level1_2], mode='sum')

        decoded = Convolution2D(channels, 5, 5, activation='linear', border_mode='same')(level1)

        model = Model(init, decoded)
        adam = optimizers.Adam(lr=1e-3)
        model.compile(optimizer=adam, loss='mse', metrics=[PSNRLoss])
        if load_weights: model.load_weights(self.weight_path)

        self.model = model
        return model
Exemplo n.º 2
0
    def create_model(self,
                     height=33,
                     width=33,
                     channels=3,
                     load_weights=False,
                     batch_size=128):
        """
            Creates a model to remove / reduce noise from upscaled images.
        """
        from keras.layers.convolutional import Deconvolution2D
        from keras.layers import merge

        if K.image_dim_ordering() == "th":
            shape = (channels, width, height)
        else:
            shape = (width, height, channels)

        init = Input(shape=shape)

        level1_1 = Convolution2D(self.n1,
                                 3,
                                 3,
                                 activation='relu',
                                 border_mode='same')(init)
        level2_1 = Convolution2D(self.n1,
                                 3,
                                 3,
                                 activation='relu',
                                 border_mode='same')(level1_1)

        level2_2 = Deconvolution2D(self.n1,
                                   3,
                                   3,
                                   activation='relu',
                                   output_shape=(None, channels, height,
                                                 width),
                                   border_mode='same')(level2_1)
        level2 = merge([level2_1, level2_2], mode='sum')

        level1_2 = Deconvolution2D(self.n1,
                                   3,
                                   3,
                                   activation='relu',
                                   output_shape=(None, channels, height,
                                                 width),
                                   border_mode='same')(level2)
        level1 = merge([level1_1, level1_2], mode='sum')

        decoded = Convolution2D(channels,
                                5,
                                5,
                                activation='linear',
                                border_mode='same')(level1)

        model = Model(init, decoded)
        model.compile(optimizer='adam', loss='mse', metrics=[PSNRLoss])
        if load_weights: model.load_weights("weights/Denoising AutoEncoder.h5")

        self.model = model
        return model
Exemplo n.º 3
0
def generator_model():

    a = keras.initializers.RandomNormal()
    model = Sequential()
    model.add(
        Dense(input_dim=100,
              output_dim=1024 * 4 * 4,
              activation='relu',
              kernel_initializer=a))
    #model.add(BatchNormalization())
    model.add(Reshape((4, 4, 1024)))
    model.add(
        Deconvolution2D(512,
                        kernel_size=(3),
                        strides=(2),
                        border_mode="same",
                        activation='relu',
                        kernel_initializer=a))
    #model.add(BatchNormalization())

    model.add(
        Deconvolution2D(256,
                        kernel_size=(3),
                        strides=(2),
                        border_mode="same",
                        activation='relu',
                        kernel_initializer=a))
    #model.add(BatchNormalization())

    model.add(
        Deconvolution2D(128,
                        kernel_size=(3),
                        strides=(2),
                        border_mode="same",
                        activation='relu',
                        kernel_initializer=a))
    model.add(BatchNormalization())

    model.add(
        Deconvolution2D(64,
                        kernel_size=(5),
                        strides=(2),
                        border_mode="same",
                        activation='relu',
                        kernel_initializer=a))
    #model.add(BatchNormalization())

    model.add(
        Deconvolution2D(3,
                        kernel_size=(5),
                        strides=(2),
                        border_mode="same",
                        activation='sigmoid',
                        kernel_initializer='random_uniform'))
    #model.add(BatchNormalization(axis = 1))

    return model
Exemplo n.º 4
0
def make_dcgan_generator(Xk_g, n_lat, n_chan=1):
    n_g_hid1 = 1024  # size of hidden layer in generator layer 1
    n_g_hid2 = 128  # size of hidden layer in generator layer 2

    # x = Dense(n_g_hid1, init=conv2D_init)(Xk_g)
    Xk_g = Reshape((-1, 16, 64))(Xk_g)
    x = Dense(n_g_hid2, init=conv2D_init)(Xk_g)
    # x = BatchNormalization(mode=2, )(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)

    # x = Dense(n_g_hid2*7*7, init=conv2D_init)(x)
    x = Dense(16, init=conv2D_init)(x)
    # x = Reshape((n_g_hid2, 7, 7))(x)
    # x = BatchNormalization(mode=2, axis=1)(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)

    # x = Deconvolution2D(64, 5, 5, output_shape=(128, 64, 14, 14),
    #       border_mode='same', activation=None, subsample=(2,2),
    #       init=conv2D_init, dim_ordering='th')(x)
    x = Deconvolution2D(64,
                        5,
                        5,
                        output_shape=(64, 64, 32, 32),
                        border_mode='same',
                        activation=None,
                        subsample=(2, 2),
                        init=conv2D_init,
                        dim_ordering='th')(x)
    # x = BatchNormalization(mode=2, axis=1)(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)

    # g = Deconvolution2D(n_chan, 5, 5, output_shape=(128, n_chan, 28, 28),
    #       border_mode='same', activation='sigmoid', subsample=(2,2),
    #       init=conv2D_init, dim_ordering='th')(x)

    g = Deconvolution2D(n_chan,
                        5,
                        5,
                        output_shape=(64, n_chan, 64, 64),
                        border_mode='same',
                        activation='sigmoid',
                        subsample=(2, 2),
                        init=conv2D_init,
                        dim_ordering='th')(x)

    # generator = Model(inputs=Xk_g, outputs=g, name='generator')
    # generator.summary()

    return g
def timeDist_DeepEM2D_Net(input, scale=True):
    # Input Shape is 299 x 299 x 3 (tf) or 3 x 299 x 299 (th)
    w = 256
    h = 256
    x1, z1 = time_inception_resnet_stem_2Dconv(input)
    x1 = time_inception_resnet_v2_A(x1, scale_residual=scale)
    x2 = time_reduction_A(x1, k=256, l=256, m=384, n=384)
    x2 = time_inception_resnet_v2_B(x2, scale_residual=scale)
    x3 = time_reduction_resnet_v2_B(x2)
    x3 = time_inception_resnet_v2_C(x3, scale_residual=scale)
    u1 = TimeDistributed(
        Deconvolution2D(2,
                        5,
                        5,
                        output_shape=(None, 2, h, w),
                        subsample=(2, 2),
                        border_mode='same'))(z1)
    u1 = BatchNormalization(axis=2)(u1)

    u2 = TimeDistributed(
        Deconvolution2D(2,
                        9,
                        9,
                        output_shape=(None, 2, h, w),
                        subsample=(4, 4),
                        border_mode='same'))(x1)
    u2 = BatchNormalization(axis=2)(u2)

    u3 = TimeDistributed(
        Deconvolution2D(2,
                        17,
                        17,
                        output_shape=(None, 2, h, w),
                        subsample=(8, 8),
                        border_mode='same'))(x2)
    u3 = BatchNormalization(axis=2)(u3)

    u4 = TimeDistributed(
        Deconvolution2D(2,
                        33,
                        33,
                        output_shape=(None, 2, h, w),
                        subsample=(16, 16),
                        border_mode='same'))(x3)
    u4 = BatchNormalization(axis=2)(u4)
    merged = merge([u1, u2, u3, u4], mode='sum')

    out = Lambda(time_dist_softmax,
                 output_shape=time_dist_softmax_out_shape)(merged)
    return out
    def decoder_model(self):
        z_in = Input(shape=(self.n_latent,))
        s_in = Input(shape=(self.n_nuisance,))
        z = Dense(int(self.samples//2 * self.n_kernels), use_bias=False)(concatenate([z_in, s_in]))
        z = Reshape((1, int(self.samples//2), self.n_kernels))(z)
        z = UpSampling2D(size=(1, 2))(z)
        z = Deconvolution2D(self.n_kernels, (self.chans, 1), use_bias=False)(z)
        z = BatchNormalization(axis=3, epsilon=1e-05, momentum=0.1)(z)
        z = Activation('elu')(z)
        z = Deconvolution2D(1, (1, 40), padding='same', use_bias=False)(z)
        z = BatchNormalization(axis=3, epsilon=1e-05, momentum=0.1)(z)
        x_hat = Activation('elu')(z)

        return Model([z_in, s_in], x_hat, name='dec')
Exemplo n.º 7
0
    def create_sr_model(self, ip):

        x = Convolution2D(64,
                          3,
                          3,
                          activation='relu',
                          border_mode='same',
                          name='sr_res_conv1')(ip)

        nb_residual = 5 if self.small_model else 15

        for i in range(nb_residual):
            x = self._residual_block(x, i + 1)

        x = Deconvolution2D(64,
                            3,
                            3,
                            activation='relu',
                            border_mode='same',
                            subsample=(2, 2),
                            name='sr_res_deconv1',
                            output_shape=(self.batch_size, 64,
                                          self.img_width * 2,
                                          self.img_height * 2))(x)

        x = Deconvolution2D(64,
                            3,
                            3,
                            activation='relu',
                            border_mode='same',
                            subsample=(2, 2),
                            name='sr_res_deconv2',
                            output_shape=(self.batch_size, 64,
                                          self.img_width * 4,
                                          self.img_height * 4))(x)

        tv_regularizer = TVRegularizer(img_width=self.img_width * 4,
                                       img_height=self.img_height * 4,
                                       weight=self.tv_weight)
        x = Convolution2D(3,
                          3,
                          3,
                          activation="linear",
                          border_mode='same',
                          activity_regularizer=tv_regularizer,
                          name='sr_res_conv_final')(x)
        #x = Denormalize(name='sr_res_output')(x)

        return x
    def transition_up_block(self,
                            input_tensor,
                            nb_filters,
                            type='deconv',
                            output_shape=None,
                            weight_decay=1E-4):
        ''' deconv Upscaling (factor = 2)

        Args:
            input_tensor: keras tensor
            nb_filters: number of layers
            type:'deconv'. Determines type of upsampling performed
            output_shape: required if type = 'deconv'. Output shape of tensor
            weight_decay: weight decay factor

        Returns: keras tensor, after applying batch_norm, relu-conv, dropout, maxpool

        '''

        x = Deconvolution2D(nb_filters,
                            3,
                            3,
                            output_shape,
                            activation='relu',
                            border_mode='same',
                            subsample=(2, 2))(input_tensor)
Exemplo n.º 9
0
def deconv_block_unet(x,
                      x2,
                      f,
                      h,
                      w,
                      batch_size,
                      name,
                      bn_mode,
                      bn_axis,
                      bn=True,
                      dropout=False):

    o_shape = (batch_size, h * 2, w * 2, f)
    x = Activation("relu")(x)
    x = Deconvolution2D(f,
                        3,
                        3,
                        output_shape=o_shape,
                        subsample=(2, 2),
                        border_mode="same")(x)
    if bn:
        x = BatchNormalization(mode=bn_mode, axis=bn_axis)(x)
    if dropout:
        x = Dropout(0.5)(x)
    x = merge([x, x2], mode='concat', concat_axis=bn_axis)

    return x
Exemplo n.º 10
0
def bottleneck(encoder, output, upsample=False, reverse_module=False):
    internal = output / 4
    input_stride = 2 if upsample else 1
    
    x = Convolution2D(internal, input_stride, input_stride, border_mode='same', bias=False)(encoder)
    x = BatchNormalization(momentum=0.1)(x)
    x = Activation('relu')(x)
    if not upsample:
        x = Convolution2D(internal, 3, 3, border_mode='same', bias=True)(x)
    else:
        b, w, h, nb_filters = encoder.get_shape().as_list()
        in_shape = x.get_shape().as_list()
        x = Deconvolution2D(internal, 3, 3, output_shape=(None, w * 2, h * 2, internal), border_mode='same', subsample=(2, 2), input_shape=in_shape)(x)
    x = BatchNormalization(momentum=0.1)(x)
    x = Activation('relu')(x)

    x = Convolution2D(output, 1, 1, border_mode='same', bias=False)(x)

    other = encoder
    if encoder.get_shape()[-1] != output or upsample:
        other = Convolution2D(output, 1, 1, border_mode='same', bias=False)(other)
        other = BatchNormalization(momentum=0.1)(other)
        if upsample and reverse_module:
            other = UpSampling2D(size=(2, 2))(other)
        
    if not upsample or reverse_module:
        x = BatchNormalization(momentum=0.1)(x)
    else:
        return x
    
    decoder = merge([x, other], mode='sum')
    decoder = Activation('relu')(decoder)
    return decoder
Exemplo n.º 11
0
def __transition_up_block(ip, nb_filters, type='upsampling', output_shape=None, weight_decay=1E-4):
    ''' SubpixelConvolutional Upscaling (factor = 2)
    Args:
        ip: keras tensor
        nb_filters: number of layers
        type: can be 'upsampling', 'subpixel', 'deconv', or 'atrous'. Determines type of upsampling performed
        output_shape: required if type = 'deconv'. Output shape of tensor
        weight_decay: weight decay factor
    Returns: keras tensor, after applying upsampling operation.
    '''

    if type == 'upsampling':
        x = UpSampling2D()(ip)
    elif type == 'subpixel':
        x = Convolution2D(nb_filters, 3, 3, activation="relu", border_mode='same', W_regularizer=l2(weight_decay),
                          bias=False, init='he_uniform')(ip)
        x = SubPixelUpscaling(scale_factor=2)(x)
        x = Convolution2D(nb_filters, 3, 3, activation="relu", border_mode='same', W_regularizer=l2(weight_decay),
                          bias=False, init='he_uniform')(x)
    elif type == 'atrous':
        # waiting on https://github.com/fchollet/keras/issues/4018
        x = AtrousConvolution2D(nb_filters, 3, 3, activation="relu", W_regularizer=l2(weight_decay),
                                bias=False, atrous_rate=(2, 2), init='he_uniform')(ip)
    else:
        x = Deconvolution2D(nb_filters, 3, 3, output_shape, activation='relu', border_mode='same',
                            subsample=(2, 2), init='he_uniform')(ip)

    return x
Exemplo n.º 12
0
 def f(x):
     norm = BatchNormalization(mode=2, axis=3)(x)
     activation = Activation("relu")(norm)
     return Deconvolution2D(
         nb_filter, nb_row, nb_col, W_regularizer=l2(1e-4),
         subsample=subsample, output_shape=output_shape,
         init="he_normal", border_mode="same")(activation)
Exemplo n.º 13
0
def get_up_convolution(n_filters, pool_size, kernel_size=(2, 2), strides=(2, 2),
                       deconvolution=False):
    if deconvolution:
        return Deconvolution2D(filters=n_filters, kernel_size=kernel_size,
                               strides=strides)
    else:
        return UpSampling2D(size=pool_size)
Exemplo n.º 14
0
def deconv_block(input, nb_filter, output_shape):
    deconv = Deconvolution2D(nb_filter, nb_row=4, nb_col=4, output_shape=output_shape, padding='same', subsample=(2,2))(input)
    relu1 = Activation("relu")(deconv)
    conv1 = Convolution2D(nb_filter, nb_row=3, nb_col=3, padding='same')(relu1)
    relu2 = Activation("relu")(conv1)
    conv2 = Convolution2D(nb_filter, nb_row=3, nb_col=3, padding='same')(relu2)
    relu3 = Activation("relu")(conv2)
    return relu3
Exemplo n.º 15
0
def Deconvolution(f, output_shape, k=2, s=2, **kwargs):
    """Convenience method for Transposed Convolutions."""
    return Deconvolution2D(f,
                           k,
                           k,
                           output_shape=output_shape,
                           subsample=(s, s),
                           **kwargs)
 def TransitionUp(self, filters, input_shape, output_shape):
     model = self.model
     model.add(
         Deconvolution2D(filters,
                         3,
                         3,
                         output_shape=output_shape,
                         subsample=(2, 2),
                         border_mode='same',
                         input_shape=input_shape))
Exemplo n.º 17
0
    def build_model(self):
        input = Input(batch_shape=self.input_shape)
        x = ZeroPadding2D((3, 3))(input)
        x = Convolution2D(16, 5, 5, border_mode='same', name='conv1')(x)
        x = BatchNormalization(axis=3)(x)
        x = Activation('relu')(x)
        x = MaxPooling2D((2, 2), strides=(2, 2), name='maxpool1')(x)

        x = Convolution2D(32, 5, 5, name='conv2', border_mode='same')(x)
        x = BatchNormalization(axis=3)(x)
        x = Activation('relu')(x)
        x = MaxPooling2D((2, 2), strides=(2, 2), name='maxpool2')(x)
        x = Dropout(0.6)(x)

        x = Convolution2D(64, 5, 5, border_mode='same', name='conv3')(x)
        x = BatchNormalization(axis=3)(x)
        x = Activation('relu')(x)
        x = MaxPooling2D((2, 2), strides=(2, 2), name='maxpool3')(x)
        x = Dropout(0.6)(x)

        # x = Convolution2D(64, 5, 5, border_mode='same', name='conv4')(x)
        # x = BatchNormalization(axis=3)(x)
        # x = Activation('relu')(x)
        # x = MaxPooling2D((2, 2), strides=(2, 2), name='maxpool4')(x)
        # x = Dropout(0.6)(x)

        x = Convolution2D(64,
                          1,
                          1,
                          border_mode='same',
                          activation='relu',
                          name='conv5')(x)
        x = Dropout(0.5)(x)
        class_out = Deconvolution2D(1,
                                    8,
                                    8,
                                    output_shape=self.output_shape,
                                    subsample=(8, 8),
                                    activation='sigmoid',
                                    name='class_out')(x)

        model = Model(input, output=class_out)

        optimizer = Adam(lr=self.learning_rate,
                         beta_1=0.9,
                         beta_2=0.999,
                         epsilon=1e-08,
                         decay=0.0)
        model.compile(optimizer=optimizer,
                      loss={'class_out': 'binary_crossentropy'},
                      metrics=['binary_accuracy'])
        if self.weight_file:
            model.load_weights(self.weight_file)
        model.summary()
        return model
Exemplo n.º 18
0
def get_model():
    aliases = {}
    Input_1 = Input(shape=(3, 32, 32), name='Input_1')
    Convolution2D_1 = Convolution2D(name='Convolution2D_1',
                                    activation='relu',
                                    nb_col=3,
                                    nb_row=3,
                                    nb_filter=16)(Input_1)
    MaxPooling2D_1 = MaxPooling2D(name='MaxPooling2D_1',
                                  pool_size=(3, 3))(Convolution2D_1)
    Convolution2D_2 = Convolution2D(name='Convolution2D_2',
                                    activation='relu',
                                    nb_col=3,
                                    nb_row=3,
                                    nb_filter=2)(MaxPooling2D_1)
    MaxPooling2D_2 = MaxPooling2D(name='MaxPooling2D_2')(Convolution2D_2)
    UpSampling2D_10 = UpSampling2D(name='UpSampling2D_10')(MaxPooling2D_2)
    Deconvolution2D_2 = Deconvolution2D(name='Deconvolution2D_2',
                                        nb_col=3,
                                        nb_row=3,
                                        subsample=(1, 1),
                                        activation='relu',
                                        nb_filter=16,
                                        output_shape=(None, 16, 10,
                                                      10))(UpSampling2D_10)
    UpSampling2D_1 = UpSampling2D(name='UpSampling2D_1',
                                  size=(3, 3))(Deconvolution2D_2)
    Deconvolution2D_3 = Deconvolution2D(name='Deconvolution2D_3',
                                        nb_col=3,
                                        nb_row=3,
                                        subsample=(1, 1),
                                        activation='sigmoid',
                                        nb_filter=3,
                                        output_shape=(None, 3, 32,
                                                      32))(UpSampling2D_1)

    model = Model([Input_1], [Deconvolution2D_3])
    return aliases, model
Exemplo n.º 19
0
def build(encoder, nc, in_shape, dropout_rate=0.1):
    enet = bottleneck(encoder, 64, upsample=True,
                      reverse_module=True)  # bottleneck 4.0
    enet = bottleneck(enet, 64)  # bottleneck 4.1
    enet = bottleneck(enet, 64)  # bottleneck 4.2
    enet = bottleneck(enet, 16, upsample=True,
                      reverse_module=True)  # bottleneck 5.0
    enet = bottleneck(enet, 16)  # bottleneck 5.1

    enet = Deconvolution2D(nc, (2, 2),
                           padding='same',
                           strides=(2, 2),
                           input_shape=in_shape)(enet)
    return enet
Exemplo n.º 20
0
def generator_model(img_rows, img_cols):
    # imgs: input: 256x256xch
    # U-Net structure, must change to relu
    inputs = Input((IN_CH, img_cols, img_rows))

    e1 = BatchNormalization()(inputs)
    e1 = Convolution2D(64, 4, 4, subsample=(2, 2), activation='relu', init='uniform', border_mode='same')(e1)
    e1 = BatchNormalization()(e1)
    e2 = Convolution2D(128, 4, 4, subsample=(2, 2), activation='relu', init='uniform', border_mode='same')(e1)
    e2 = BatchNormalization()(e2)

    e3 = Convolution2D(256, 4, 4, subsample=(2, 2), activation='relu', init='uniform', border_mode='same')(e2)
    e3 = BatchNormalization()(e3)
    e4 = Convolution2D(512, 4, 4, subsample=(2, 2), activation='relu', init='uniform', border_mode='same')(e3)
    e4 = BatchNormalization()(e4)

    e5 = Convolution2D(512, 4, 4, subsample=(2, 2), activation='relu', init='uniform', border_mode='same')(e4)
    e5 = BatchNormalization()(e5)
    e6 = Convolution2D(512, 4, 4, subsample=(2, 2), activation='relu', init='uniform', border_mode='same')(e5)
    e6 = BatchNormalization()(e6)

    d1 = Deconvolution2D(512, 5, 5, subsample=(2, 2), activation='relu', init='uniform', output_shape=(None, 512, 2, 2),
                         border_mode='same')(e6)
    d1 = merge([d1, e5], mode='concat', concat_axis=1)
    d1 = BatchNormalization()(d1)

    d2 = Deconvolution2D(512, 5, 5, subsample=(2, 2), activation='relu', init='uniform', output_shape=(None, 512, 4, 4),
                         border_mode='same')(d1)
    d2 = merge([d2, e4], mode='concat', concat_axis=1)
    d2 = BatchNormalization()(d2)

    d3 = Dropout(0.2)(d2)
    d3 = Deconvolution2D(512, 5, 5, subsample=(2, 2), activation='relu', init='uniform', output_shape=(None, 512, 8, 8),
                         border_mode='same')(d3)
    d3 = merge([d3, e3], mode='concat', concat_axis=1)
    d3 = BatchNormalization()(d3)

    d4 = Dropout(0.2)(d3)
    d4 = Deconvolution2D(512, 5, 5, subsample=(2, 2), activation='relu', init='uniform',
                         output_shape=(None, 512, 16, 16), border_mode='same')(d4)
    d4 = merge([d4, e2], mode='concat', concat_axis=1)
    d4 = BatchNormalization()(d4)

    d5 = Dropout(0.2)(d4)
    d5 = Deconvolution2D(256, 5, 5, subsample=(2, 2), activation='relu', init='uniform',
                         output_shape=(None, 256, 32, 32), border_mode='same')(d5)
    d5 = merge([d5, e1], mode='concat', concat_axis=1)
    d5 = BatchNormalization()(d5)

    d6 = Deconvolution2D(3, 5, 5, subsample=(2, 2), activation='relu', init='uniform', output_shape=(None, 3, 64, 64),
                         border_mode='same')(d5)

    d6 = BatchNormalization()(d6)
    d7 = Activation('tanh')(d6)

    model = Model(input=inputs, output=d7)
    return model
def deconvolution(inputs,filters,step,dropout):
    _,height,width,_ = (inputs.get_shape()).as_list()
    decoder = Deconvolution2D(filters,4,4,
                              output_shape=(None,2*height,2*width,filters),
                              subsample=(2,2),
                              border_mode='same',
                              name='Deconv_%d' % (8-step))(inputs)
    decoder = BatchNormalization(name='DBat_%d' % (8-step))(decoder)
    if step == 8:
        decoder = Activation(activation='tanh')(decoder)
    else:
        decoder = LeakyReLU(alpha=0.2,name='DLRelu_%d' % (8-step))(decoder)   
    if dropout[step-1] > 0:
        decoder = Dropout(dropout[step-1])(decoder)
    return decoder
Exemplo n.º 22
0
def transform_model(weight_loss_pix=5e-4):
    inputs = Input(shape=(128, 128, 3))
    x1 = Convolution2D(64, 5, 5, border_mode='same')(inputs)
    x2 = LeakyReLU(alpha=0.3, name='wkcw')(x1)
    x3 = BatchNormalization()(x2)
    x4 = Convolution2D(128, 4, 4, border_mode='same', subsample=(2, 2))(x3)
    x5 = LeakyReLU(alpha=0.3)(x4)
    x6 = BatchNormalization()(x5)
    x7 = Convolution2D(256, 4, 4, border_mode='same', subsample=(2, 2))(x6)
    x8 = LeakyReLU(alpha=0.3)(x7)
    x9 = BatchNormalization()(x8)
    x10 = Deconvolution2D(128,
                          3,
                          3,
                          output_shape=(None, 64, 64, 128),
                          border_mode='same',
                          subsample=(2, 2))(x9)
    x11 = BatchNormalization()(x10)
    x12 = Deconvolution2D(64,
                          3,
                          3,
                          output_shape=(None, 128, 128, 64),
                          border_mode='same',
                          subsample=(2, 2))(x11)
    x13 = BatchNormalization()(x12)
    x14 = Deconvolution2D(
        3,
        4,
        4,
        output_shape=(None, 128, 128, 3),
        border_mode='same',
        activity_regularizer=activity_l1(weight_loss_pix))(x13)
    output = merge([inputs, x14], mode='sum')
    model = Model(input=inputs, output=output)

    return model
Exemplo n.º 23
0
def Deconvolution(f, output_shape, k=2, s=2, **kwargs):
    """Convenience method for Transposed Convolutions."""
    if KERAS_2:
        return Conv2DTranspose(f,
                               kernel_size=(k, k),
                               strides=(s, s),
                               data_format=K.image_data_format(),
                               **kwargs)
    else:
        return Deconvolution2D(f,
                               k,
                               k,
                               output_shape=output_shape,
                               subsample=(s, s),
                               **kwargs)
Exemplo n.º 24
0
def make_dcgan_generator(Xk_g, n_lat):
    n_g_hid1 = 1024  # size of hidden layer in generator layer 1
    n_g_hid2 = 128  # size of hidden layer in generator layer 2

    x = Dense(n_g_hid1, init=conv2D_init)(Xk_g)
    x = BatchNormalization(mode=2, )(x)
    x = Activation('relu')(x)

    x = Dense(n_g_hid2 * 7 * 7, init=conv2D_init)(x)
    x = Reshape((n_g_hid2, 7, 7))(x)
    x = BatchNormalization(mode=2, axis=1)(x)
    x = Activation('relu')(x)

    x = Deconvolution2D(64,
                        5,
                        5,
                        output_shape=(128, 64, 14, 14),
                        border_mode='same',
                        activation=None,
                        subsample=(2, 2),
                        init=conv2D_init,
                        dim_ordering='th')(x)
    x = BatchNormalization(mode=2, axis=1)(x)
    x = Activation('relu')(x)

    g = Deconvolution2D(n_chan,
                        5,
                        5,
                        output_shape=(128, n_chan, 28, 28),
                        border_mode='same',
                        activation='sigmoid',
                        subsample=(2, 2),
                        init=conv2D_init,
                        dim_ordering='th')(x)

    return g
Exemplo n.º 25
0
def build(encoder, nc, in_shape, dropout_rate=0.1):
    # print(encoder.get_shape().as_list())
    enet = bottleneck(encoder, 64, upsample=True, reverse_module=True)  # bottleneck 4.0
    # print(enet.get_shape().as_list())
    # import sys
    # sys.exit()
    enet = bottleneck(enet, 64)  # bottleneck 4.1
    enet = bottleneck(enet, 64)  # bottleneck 4.2
    enet = bottleneck(enet, 16, upsample=True, reverse_module=True)  # bottleneck 5.0
    enet = bottleneck(enet, 16)  # bottleneck 5.1

    out_shape = enet.get_shape().as_list()
    out_shape = [out_shape[0], 2 * out_shape[1], 2 * out_shape[2], nc]
    enet = Deconvolution2D(nc, 2, 2, output_shape=out_shape, border_mode='same', subsample=(2, 2), input_shape=in_shape)(enet)
    return enet
Exemplo n.º 26
0
def _deconv_shortcut(x, residual, output_shape):
    # Expand channels of shortcut to match residual.
    # Stride appropriately to match residual (width, height).
    # Should be int if network architecture is correctly configured.
    stride_width = residual._keras_shape[1] / x._keras_shape[1]
    stride_height = residual._keras_shape[2] / x._keras_shape[2]
    equal_channels = residual._keras_shape[3] == x._keras_shape[3]

    shortcut = x
    if stride_width > 1 or stride_height > 1 or not equal_channels:
        shortcut = Deconvolution2D(
            residual._keras_shape[3], 1, 1,
            subsample=(stride_width, stride_height),
            output_shape=output_shape,
            init="he_normal", border_mode="valid")(x)
    return merge([shortcut, residual], mode="sum")
Exemplo n.º 27
0
def create_model():
    model = Sequential()
    # first convolutional block
    model.add(
        Convolution2D(32,
                      3,
                      3,
                      input_shape=(DIM, DIM, CHANNELS),
                      border_mode='same'))
    model.add(Activation('relu'))
    model.add(Convolution2D(32, 3, 3, border_mode='same'))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))

    # second convolutional block
    model.add(Convolution2D(64, 3, 3, border_mode='same'))
    model.add(Activation('relu'))
    model.add(Convolution2D(64, 3, 3, border_mode='same'))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))

    # third convolutional block
    model.add(Convolution2D(128, 3, 3, border_mode='same'))
    model.add(Activation('relu'))
    model.add(Convolution2D(128, 3, 3, border_mode='same'))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))

    # back to the original dim
    model.add(Convolution2D(1024, 5, 5, border_mode='same'))
    model.add(Activation('relu'))
    model.add(Convolution2D(128, 1, 1, border_mode='same'))
    model.add(
        Deconvolution2D(CLASSES,
                        16,
                        16,
                        output_shape=(BATCHSIZE, DIM, DIM, CLASSES),
                        subsample=(8, 8),
                        border_mode='same'))
    model.add(Reshape((DIM * DIM, CLASSES)))
    model.add(Activation('softmax'))
    optmzr = adam(lr=LEARNING_RATE)
    model.compile(loss='categorical_crossentropy',
                  optimizer=optmzr,
                  metrics=['accuracy'])
    print(model.summary())
    return model
Exemplo n.º 28
0
def deconvolutional_block(x,
                          block_idx,
                          nb_filter,
                          output_shape,
                          k_size=3,
                          subsample=(2, 2)):
    name = "block%s_deconv2D" % block_idx
    x = Deconvolution2D(nb_filter,
                        k_size,
                        k_size,
                        output_shape=output_shape,
                        name=name,
                        border_mode='same',
                        subsample=subsample)(x)
    x = BatchNormalization(mode=2, axis=1)(x)
    x = Activation("relu")(x)
    return x
def transition_up_block(ip,
                        nb_filters,
                        type='subpixel',
                        output_shape=None,
                        weight_decay=1E-4):
    ''' SubpixelConvolutional Upscaling (factor = 2)

    Args:
        ip: keras tensor
        nb_filters: number of layers
        type: can be 'subpixel' or 'deconv'. Determines type of upsampling performed
        output_shape: required if type = 'deconv'. Output shape of tensor
        weight_decay: weight decay factor

    Returns: keras tensor, after applying batch_norm, relu-conv, dropout, maxpool

    '''

    if type == 'subpixel':
        x = Convolution2D(nb_filters,
                          3,
                          3,
                          activation="relu",
                          border_mode='same',
                          W_regularizer=l2(weight_decay),
                          bias=False)(ip)
        x = SubPixelUpscaling(r=2, channels=int(nb_filters // 4))(x)
        x = Convolution2D(nb_filters,
                          3,
                          3,
                          activation="relu",
                          border_mode='same',
                          W_regularizer=l2(weight_decay),
                          bias=False)(x)

    else:
        x = Deconvolution2D(nb_filters,
                            3,
                            3,
                            output_shape,
                            activation='relu',
                            border_mode='same',
                            subsample=(2, 2))(ip)

    return x
Exemplo n.º 30
0
def bottleneck(encoder, output, upsample=False, reverse_module=False):
    internal = output / 4
    input_stride = 2 if upsample else 1

    x = Convolution2D(internal, (input_stride, input_stride),
                      padding='same',
                      use_bias=False)(encoder)
    x = BatchNormalization(momentum=0.1)(x)
    x = Activation('relu')(x)
    if not upsample:
        x = Convolution2D(internal, (3, 3), padding='same', use_bias=True)(x)
    else:
        in_shape = K.int_shape(x)
        x = Deconvolution2D(internal, (3, 3),
                            padding='same',
                            strides=(2, 2),
                            input_shape=in_shape)(x)
    x = BatchNormalization(momentum=0.1)(x)
    x = Activation('relu')(x)

    x = Convolution2D(output, (1, 1), padding='same', use_bias=False)(x)

    other = encoder
    if encoder.get_shape()[-1] != output or upsample:
        other = Convolution2D(output, (1, 1), padding='same',
                              use_bias=False)(other)
        other = BatchNormalization(momentum=0.1)(other)
        if upsample and reverse_module:
            other = UpSampling2D(size=(2, 2))(other)

    if not upsample or reverse_module:
        x = BatchNormalization(momentum=0.1)(x)
    else:
        return x

    decoder = add([x, other])
    decoder = Activation('relu')(decoder)
    return decoder