Exemplo n.º 1
0
def conv_vae(batch_input_shape,
             latent_dim=2,
             intermediate_dim=128,
             nb_filters=32,
             nb_conv=3,
             epsilon_std=0.01):
    batch_size, img_chns, img_rows, img_cols = batch_input_shape
    # Input
    x = Input(batch_shape=batch_input_shape)

    # Encoder
    c = Convolution2D(nb_filters,
                      nb_conv,
                      nb_conv,
                      border_mode='same',
                      activation='relu')(x)
    f = Flatten()(c)

    # Sampling
    h = Dense(intermediate_dim, activation='relu')(f)

    z_mean = Dense(latent_dim)(h)
    z_log_var = Dense(latent_dim)(h)

    def sampling(args):
        z_mean, z_log_var = args
        epsilon = K.random_normal(shape=(batch_size, latent_dim),
                                  mean=0.,
                                  std=epsilon_std)
        return z_mean + K.exp(z_log_var) * epsilon

    z = Lambda(sampling, output_shape=(latent_dim, ))([z_mean, z_log_var])

    # Decoder
    decoder_h = Dense(intermediate_dim, activation='relu')
    decoder_f = Dense(nb_filters * img_rows * img_cols, activation='relu')
    decoder_c = Reshape((nb_filters, img_rows, img_cols))
    decoder_mean = Deconvolution2D(img_chns,
                                   nb_conv,
                                   nb_conv,
                                   (batch_size, img_chns, img_rows, img_cols),
                                   border_mode='same')

    h_decoded = decoder_h(z)
    f_decoded = decoder_f(h_decoded)
    c_decoded = decoder_c(f_decoded)
    x_decoded_mean = decoder_mean(c_decoded)

    # Define loss
    def vae_loss(x, x_decoded_mean):
        # NOTE: binary_crossentropy expects a batch_size by dim for x and x_decoded_mean, so we MUST flatten these!
        x = K.flatten(x)
        x_decoded_mean = K.flatten(x_decoded_mean)
        xent_loss = objectives.mean_squared_error(x, x_decoded_mean)
        kl_loss = -0.5 * K.mean(
            1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
        return xent_loss + kl_loss

    # Compile Model
    vae = Model(x, x_decoded_mean)
    vae.compile(optimizer='rmsprop', loss=vae_loss)
    vae.summary()

    # build a model to project inputs on the latent space
    encoder = Model(x, z_mean)

    return vae, encoder
Exemplo n.º 2
0
    epsilon = K.random_normal(shape=(batch_size, latent_dim),
                              mean=0.,
                              stddev=epsilon_std)
    return z_mean + K.exp(z_log_var) * epsilon


z = Lambda(sampling, output_shape=(latent_dim, ),
           name='sample_layer')([z_mean, z_log_var])

dec = Dense(intermediate_dim, name='dec_hid')(z)
dec = Dense(8 * 8 * 256, activation='relu', name='fc_latent_in')(dec)
dec = BatchNormalization(name='debatch_1')(dec)
dec = Activation('relu')(dec)
dec = Reshape((8, 8, 256), name='reshape')(dec)
dec = Deconvolution2D(256, (5, 5),
                      strides=(2, 2),
                      padding='same',
                      name='deconv_1')(dec)
dec = BatchNormalization(name='debatch_2')(dec)
dec = Activation('relu')(dec)
dec = Deconvolution2D(128, (5, 5),
                      strides=(2, 2),
                      padding='same',
                      activation='relu',
                      name='deconv_2')(dec)
dec = BatchNormalization(name='debatch_3')(dec)
dec = Activation('relu')(dec)
dec = Deconvolution2D(32, (5, 5),
                      strides=(2, 2),
                      padding='same',
                      activation='relu',
                      name='deconv_3')(dec)
Exemplo n.º 3
0
model.add(
    Convolution2D(nb_filters,
                  kernal_size[0],
                  kernal_size[1],
                  border_mode='valid',
                  input_shape=input_shape))

# 3x3 convolution on top, with 16 output filters
model.add(Convolution2D(8, kernal_size[0], kernal_size[1],
                        border_mode='valid'))

# 3x3 transposed convolution with 32 output filters and stride 1x1 on a 16 24x24 images
model.add(
    Deconvolution2D(32,
                    kernal_size[0],
                    kernal_size[1],
                    output_shape=(None, 32, 22, 22),
                    border_mode='valid',
                    input_shape=(8, 16, 16)))

# 3x3 transposed convolution with 1 output filter and stride 1x1 on a 32 26x26 images
model.add(
    Deconvolution2D(1,
                    kernal_size[0],
                    kernal_size[1],
                    output_shape=(None, 1, 28, 28),
                    border_mode='valid',
                    input_shape=(32, 22, 22)))

model.summary()
'''
Compile and fit
Exemplo n.º 4
0
def DeepCapsNet(input_shape, n_class, routings):
    # assemble encoder
    x = Input(shape=input_shape)
    l = x

    l = Conv2D(128, (3, 3), strides=(1, 1), activation='relu',
               padding="same")(l)  # common conv layer
    l = BatchNormalization()(l)
    l = ConvertToCaps()(l)

    l = Conv2DCaps(32,
                   4,
                   kernel_size=(3, 3),
                   strides=(2, 2),
                   r_num=1,
                   b_alphas=[1, 1, 1])(l)
    l_skip = Conv2DCaps(32,
                        4,
                        kernel_size=(3, 3),
                        strides=(1, 1),
                        r_num=1,
                        b_alphas=[1, 1, 1])(l)
    l = Conv2DCaps(32,
                   4,
                   kernel_size=(3, 3),
                   strides=(1, 1),
                   r_num=1,
                   b_alphas=[1, 1, 1])(l)
    l = Conv2DCaps(32,
                   4,
                   kernel_size=(3, 3),
                   strides=(1, 1),
                   r_num=1,
                   b_alphas=[1, 1, 1])(l)
    l = layers.Add()([l, l_skip])

    l = Conv2DCaps(32,
                   8,
                   kernel_size=(3, 3),
                   strides=(2, 2),
                   r_num=1,
                   b_alphas=[1, 1, 1])(l)
    l_skip = Conv2DCaps(32,
                        8,
                        kernel_size=(3, 3),
                        strides=(1, 1),
                        r_num=1,
                        b_alphas=[1, 1, 1])(l)
    l = Conv2DCaps(32,
                   8,
                   kernel_size=(3, 3),
                   strides=(1, 1),
                   r_num=1,
                   b_alphas=[1, 1, 1])(l)
    l = Conv2DCaps(32,
                   8,
                   kernel_size=(3, 3),
                   strides=(1, 1),
                   r_num=1,
                   b_alphas=[1, 1, 1])(l)
    l = layers.Add()([l, l_skip])

    l = Conv2DCaps(32,
                   8,
                   kernel_size=(3, 3),
                   strides=(2, 2),
                   r_num=1,
                   b_alphas=[1, 1, 1])(l)
    l_skip = Conv2DCaps(32,
                        8,
                        kernel_size=(3, 3),
                        strides=(1, 1),
                        r_num=1,
                        b_alphas=[1, 1, 1])(l)
    l = Conv2DCaps(32,
                   8,
                   kernel_size=(3, 3),
                   strides=(1, 1),
                   r_num=1,
                   b_alphas=[1, 1, 1])(l)
    l = Conv2DCaps(32,
                   8,
                   kernel_size=(3, 3),
                   strides=(1, 1),
                   r_num=1,
                   b_alphas=[1, 1, 1])(l)
    l = layers.Add()([l, l_skip])
    l1 = l

    l = Conv2DCaps(32,
                   8,
                   kernel_size=(3, 3),
                   strides=(2, 2),
                   r_num=1,
                   b_alphas=[1, 1, 1])(l)
    l_skip = ConvCapsuleLayer3D(kernel_size=3,
                                num_capsule=32,
                                num_atoms=8,
                                strides=1,
                                padding='same',
                                routings=3)(l)
    l = Conv2DCaps(32,
                   8,
                   kernel_size=(3, 3),
                   strides=(1, 1),
                   r_num=1,
                   b_alphas=[1, 1, 1])(l)
    l = Conv2DCaps(32,
                   8,
                   kernel_size=(3, 3),
                   strides=(1, 1),
                   r_num=1,
                   b_alphas=[1, 1, 1])(l)
    l = layers.Add()([l, l_skip])
    l2 = l

    la = FlattenCaps()(l2)
    lb = FlattenCaps()(l1)
    l = layers.Concatenate(axis=-2)([la, lb])

    #     l = Dropout(0.4)(l)
    digits_caps = CapsuleLayer(num_capsule=n_class,
                               dim_capsule=32,
                               routings=routings,
                               channels=0,
                               name='digit_caps')(l)

    l = CapsToScalars(name='capsnet')(digits_caps)

    m_capsnet = models.Model(inputs=x, outputs=l, name='capsnet_model')

    y = Input(shape=(n_class, ))

    masked_by_y = Mask_CID()([digits_caps, y])
    masked = Mask_CID()(digits_caps)

    # Decoder Network
    decoder = models.Sequential(name='decoder')
    decoder.add(Dense(input_dim=32, activation="relu", output_dim=8 * 8 * 16))
    decoder.add(Reshape((8, 8, 16)))
    decoder.add(BatchNormalization(momentum=0.8))
    decoder.add(Deconvolution2D(64, 3, 3, subsample=(1, 1),
                                border_mode='same'))
    decoder.add(Deconvolution2D(32, 3, 3, subsample=(2, 2),
                                border_mode='same'))
    decoder.add(Deconvolution2D(16, 3, 3, subsample=(2, 2),
                                border_mode='same'))
    decoder.add(Deconvolution2D(8, 3, 3, subsample=(2, 2), border_mode='same'))
    decoder.add(Deconvolution2D(3, 3, 3, subsample=(1, 1), border_mode='same'))
    decoder.add(Activation("relu"))
    decoder.add(Reshape(target_shape=(64, 64, 3), name='out_recon'))

    train_model = models.Model(
        [x, y], [m_capsnet.output, decoder(masked_by_y)])
    eval_model = models.Model(x, [m_capsnet.output, decoder(masked)])
    train_model.summary()

    return train_model, eval_model
z = Lambda(sampling, output_shape=(latent_dim, ))([z_mean, z_log_var])

# we instantiate these layers separately so as to reuse them later
decoder_hid = Dense(intermediate_dim, activation='relu')
decoder_upsample = Dense(nb_filters * 14 * 14, activation='relu')

if K.image_dim_ordering() == 'th':
    output_shape = (batch_size, nb_filters, 14, 14)
else:
    output_shape = (batch_size, 14, 14, nb_filters)

decoder_reshape = Reshape(output_shape[1:])
decoder_deconv_1 = Deconvolution2D(nb_filters,
                                   nb_conv,
                                   nb_conv,
                                   output_shape,
                                   border_mode='same',
                                   subsample=(1, 1),
                                   activation='relu')
decoder_deconv_2 = Deconvolution2D(nb_filters,
                                   nb_conv,
                                   nb_conv,
                                   output_shape,
                                   border_mode='same',
                                   subsample=(1, 1),
                                   activation='relu')
if K.image_dim_ordering() == 'th':
    output_shape = (batch_size, nb_filters, 29, 29)
else:
    output_shape = (batch_size, 29, 29, nb_filters)
decoder_deconv_3_upsamp = Deconvolution2D(nb_filters,
                              mean=0.,
                              std=epsilon_std)
    return z_mean + K.exp(z_log_var) * epsilon


# note that "output_shape" isn't necessary with the TensorFlow backend
# so you could write `Lambda(sampling)([z_mean, z_log_var])`
z = Lambda(sampling, output_shape=(latent_dim, ))([z_mean, z_log_var])

# we instantiate these layers separately so as to reuse them later
decoder_h = Dense(intermediate_dim, activation='relu')
decoder_f = Dense(nb_filters * img_rows * img_cols, activation='relu')
decoder_c = Reshape((nb_filters, img_rows, img_cols))
decoder_mean = Deconvolution2D(img_chns,
                               nb_conv,
                               nb_conv,
                               (batch_size, img_chns, img_rows, img_cols),
                               border_mode='same')

h_decoded = decoder_h(z)
f_decoded = decoder_f(h_decoded)
c_decoded = decoder_c(f_decoded)
x_decoded_mean = decoder_mean(c_decoded)


def vae_loss(x, x_decoded_mean):
    # NOTE: binary_crossentropy expects a batch_size by dim for x and x_decoded_mean, so we MUST flatten these!
    x = K.flatten(x)
    x_decoded_mean = K.flatten(x_decoded_mean)
    xent_loss = objectives.binary_crossentropy(x, x_decoded_mean)
    kl_loss = -0.5 * K.mean(
Exemplo n.º 7
0
def fcn_32s_to_8s(fcn32model=None):

    if (fcn32model is None):
        fcn32model = fcn32_blank()

    fcn32shape = fcn32model.layers[-1].output_shape
    assert (len(fcn32shape) == 4)
    assert (fcn32shape[0] is None)  # batch axis
    assert (fcn32shape[3] == 21)  # number of filters
    assert (fcn32shape[1] == fcn32shape[2])  # must be square

    fcn32size = fcn32shape[1]  # INFO: =32 when images are 512x512

    if (fcn32size != 32):
        print(
            'WARNING : handling of image size different from 512x512 has not been tested'
        )

    sp4 = Convolution2D(21,
                        kernel_size=(1, 1),
                        padding='same',
                        activation=None,
                        name='score_pool4')

    # INFO : to replicate MatConvNet.DAGN.Sum layer see documentation at :
    # https://keras.io/getting-started/sequential-model-guide/
    summed = add(inputs=[
        sp4(fcn32model.layers[14].output), fcn32model.layers[-1].output
    ])

    deconv4_output_size = (fcn32size -
                           1) * 2 + 4  # INFO: =66 when images are 512x512
    s4deconv = Deconvolution2D(
        21,
        kernel_size=(4, 4),
        #output_shape=(None, 21, deconv4_output_size, deconv4_output_size),
        padding='valid',  # WARNING : valid, same or full ?
        strides=(2, 2),
        activation=None,
        name='score4')

    extra_margin4 = deconv4_output_size - fcn32size * 2  # INFO: =2 when images are 512x512
    assert (extra_margin4 > 0)
    assert (extra_margin4 % 2 == 0)
    crop_margin4 = Cropping2D(
        cropping=((0, extra_margin4),
                  (0,
                   extra_margin4)))  # INFO : cropping as deconv gained pixels

    score4 = crop_margin4(s4deconv(summed))

    # WARNING : check dimensions
    sp3 = Convolution2D(
        21,
        kernel_size=(1, 1),
        padding='same',  # WARNING : zero or same ? does not matter for 1x1
        activation=None,  # WARNING : to check
        name='score_pool3')

    score_final = add(inputs=[sp3(fcn32model.layers[10].output),
                              score4])  # WARNING : is that correct ?

    assert (fcn32size * 2 == fcn32model.layers[10].output_shape[1])
    deconvUP_output_size = (fcn32size * 2 -
                            1) * 8 + 16  # INFO: =520 when images are 512x512
    upsample = Deconvolution2D(
        21,
        kernel_size=(16, 16),
        #output_shape=(None, 21, deconvUP_output_size, deconvUP_output_size),
        padding='valid',  # WARNING : valid, same or full ?
        strides=(8, 8),
        activation=None,
        name='upsample')

    bigscore = upsample(score_final)

    extra_marginUP = deconvUP_output_size - (
        fcn32size * 2) * 8  # INFO: =8 when images are 512x512
    assert (extra_marginUP > 0)
    assert (extra_marginUP % 2 == 0)
    crop_marginUP = Cropping2D(
        cropping=((0, extra_marginUP),
                  (0,
                   extra_marginUP)))  # INFO : cropping as deconv gained pixels

    coarse = crop_marginUP(bigscore)

    return Model(fcn32model.input, coarse)
Exemplo n.º 8
0
decoder_model.add(Convolution2D(64, 3, 3, subsample = (1,1),  border_mode='same',
                        input_shape=intermediate_layer_output.shape[1:]))
decoder_model.add(Activation('relu'))
decoder_model.add(Convolution2D(64, 3, 3, subsample = (1,1),  border_mode='same'))
decoder_model.add(Activation('relu'))
decoder_model.add(Convolution2D(64, 3, 3, subsample = (1,1),  border_mode='same'))
decoder_model.add(Activation('relu'))


# decoder_model.add(ZeroPadding2D(padding=(1,1)))
#decoder_model.add(Deconvolution2D(64, 5, 5, output_shape=(None, 64, 8, 8), subsample = (2,2), border_mode = 'same'))
#decoder_model.add(Activation('relu'))
#decoder_model.add(Deconvolution2D(32, 5, 5,output_shape=(None, 32, 16, 16), subsample = (2,2), border_mode = 'same'))
#decoder_model.add(Activation('relu'))
decoder_model.add(Deconvolution2D(3, 5, 5,output_shape=(None, 3, 32, 32), subsample = (2,2), border_mode = 'same'))
decoder_model.add(Activation('relu'))
#decoder_model.add(Deconvolution2D(8, 5, 5,output_shape=(None, 8, 96, 96), subsample = (2,2), border_mode = 'same'))
#decoder_model.add(Activation('relu'))
#decoder_model.add(Deconvolution2D(3, 5, 5, output_shape=(None, 3, 32,32), subsample = (2,2), border_mode = 'same'))
#decoder_model.add(Activation('relu'))
#decoder_model.add(Deconvolution2D(256, 5, 5,output_shape=(None, 3, 25, 25),  subsample = (2,2), border_mode = 'same'))


# let's train the model using SGD + momentum (how original).
#sgd = SGD(lr = 0.1, decay = 1e-6, momentum = 0.9, nesterov = True)
rms= RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
decoder_model.compile(loss = 'mean_squared_error',
              optimizer = rms,metrics = ['mean_absolute_percentage_error'])

#print decoder_model.layers[-1].output_shape
Exemplo n.º 9
0
def activationModel(model):
    # grab the conv layers
    current_stack = []
    act_stack = []
    for layer in model.layers:
        if layer.name.startswith("conv"):
            current_stack.insert(0, layer)
            #print('*********8')
            #print(layer.outbound_nodes[0].outbound_layer.name)
            nextlayer = layer.outbound_nodes[0].outbound_layer
            #print(nextlayer.name)
            if (nextlayer.name.startswith("elu")):
                act_stack.insert(0, nextlayer)
            elif (nextlayer.name.startswith("activ")):
                act_stack.insert(0, nextlayer)
            else:
                act_stack.insert(0, layer)

    print("current stack:")
    for layer in current_stack:
        print(layer.name)
        print(layer.outbound_nodes[0].outbound_layer.name)
    print("act stack:")
    for layer in act_stack:
        print(layer.name)
    print("---------")

    lastone = None
    #  hold onto last one..
    for i, layer in enumerate(current_stack):
        #print(layer.name,i)
        our_shape = (layer.output_shape[1], layer.output_shape[2], 1)
        hidden_layer = act_stack[i]
        #print(hidden_layer.name)
        #print(layer.name)
        #print(our_shape)
        # average this layer
        name = 'lambda_new_' + str(i)
        c1 = Lambda(lambda xin: K.mean(xin, axis=3),
                    name=name)(hidden_layer.output)
        name = 'reshape_new_' + str(i)
        r1 = Reshape(our_shape, name=name)(c1)
        #lastone=r1
        if (i != 0):
            # if we aren't the bottom, multiply by output of layer below
            print("multiply")
            name = 'multiply_' + str(i)
            r1 = merge([r1, lastone], mode='mul', name=name)
            lastone = r1

        if (i < len(current_stack) - 1):
            print('do deconv')
            # deconv to the next bigger size
            bigger_shape = current_stack[i + 1].output_shape
        else:
            bigger_shape = model.input_shape

        bigger_shape = (bigger_shape[0], bigger_shape[1], bigger_shape[2], 1)

        if (LooseVersion(keras.__version__) > LooseVersion("2.0.0")):
            print("Keras 2")
            subsample = current_stack[i].strides
            nb_row, nb_col = current_stack[i].kernel_size
        else:
            subsample = current_stack[i].subsample
            nb_row = current_stack[i].nb_row
            nb_col = current_stack[i].nb_col
        #print("deconv params:")
        #print("subsample:",subsample)
        #nb_row,nb_col=current_stack[i].kernel_size
        #print("nb_col,nb_row:",nb_col,nb_row)
        #print("bigger_shape:",bigger_shape)
        name = 'deconv_new_' + str(i)
        print(name)
        print('Deconvolution2D(1,', nb_row, nb_col, 'output_shape=',
              bigger_shape, 'subsample= ', subsample, 'border_mode=valid',
              'activation=relu', 'init=one', 'name=', name)
        d1 = Deconvolution2D(1,
                             nb_row,
                             nb_col,
                             output_shape=bigger_shape,
                             subsample=subsample,
                             border_mode='valid',
                             activation='relu',
                             init='one',
                             name=name)(r1)

        if (d1._keras_shape[1] != bigger_shape[1]
                or d1._keras_shape[2] != bigger_shape[2]):
            print("d1:", d1._keras_shape)
            pad = 0
            if (d1._keras_shape[1] < bigger_shape[1]):
                if (d1._keras_shape[2] != bigger_shape[2]):
                    pad = 1
                else:
                    pad = 0
                z1 = ZeroPadding2D(padding=(1, pad), data_format=None)(d1)
                c1 = Cropping2D(cropping=((1, 0), (pad, 0)),
                                data_format=None)(z1)
                print("z1:", z1._keras_shape)
            else:
                print("making smaller")
                padx = d1._keras_shape[1] - bigger_shape[1]
                pady = d1._keras_shape[2] - bigger_shape[2]
                print("padx,pady", padx, pady)
                c1 = Cropping2D(cropping=((padx, 0), (pady, 0)),
                                data_format=None)(d1)
            print("c1:", c1._keras_shape)
            lastone = c1
        else:
            lastone = d1

    model2 = Model(input=model.input, output=[lastone])
    model2.summary()

    return model2
    z_mean, z_log_var = args
    epsilon = K.random_normal(shape=(batch_size, latent_dim),
                              mean=0., std=epsilon_std)
    return z_mean + K.exp(z_log_var) * epsilon

# note that "output_shape" isn't necessary with the TensorFlow backend
# so you could write `Lambda(sampling)([z_mean, z_log_var])`
z = Lambda(sampling, output_shape=(latent_dim,))([z_mean, z_log_var])

# we instantiate these layers separately so as to reuse them later
decoder_hid = Dense(intermediate_dim, activation='relu')
decoder_upsample = Dense(nb_filters * 14 * 14, activation='relu')
decoder_reshape = Reshape((nb_filters, 14, 14))
decoder_deconv_1 = Deconvolution2D(nb_filters, nb_conv, nb_conv,
                                   (batch_size, nb_filters, 14, 14),
                                   border_mode='same',
                                   subsample=(1, 1),
                                   activation='relu')
decoder_deconv_2 = Deconvolution2D(nb_filters, nb_conv, nb_conv,
                                   (batch_size, nb_filters, 14, 14),
                                   border_mode='same',
                                   subsample=(1, 1),
                                   activation='relu')
decoder_deconv_3_upsamp = Deconvolution2D(nb_filters, 2, 2,
                                          (batch_size, nb_filters, 29, 29),
                                          border_mode='valid',
                                          subsample=(2, 2),
                                          activation='relu')
decoder_mean_squash = Convolution2D(img_chns, 2, 2, border_mode='valid', activation='sigmoid')

hid_decoded = decoder_hid(z)
Exemplo n.º 11
0
def add_context(model, no_of_classes):

    model.add(
        Conv2D(no_of_classes * 2, (3, 3),
               padding="same",
               activation='relu',
               name="ct_conv1_1"))
    model.add(
        Conv2D(no_of_classes * 2, (3, 3),
               padding="same",
               activation='relu',
               name="ct_conv1_2"))

    model.add(
        Conv2D(no_of_classes * 4, (3, 3),
               padding="same",
               dilation_rate=(2, 2),
               activation='relu',
               name="ct_conv2_1"))
    model.add(
        Conv2D(no_of_classes * 8, (3, 3),
               padding="same",
               dilation_rate=(4, 4),
               activation='relu',
               name="ct_conv3_1"))
    model.add(
        Conv2D(no_of_classes * 16, (3, 3),
               padding='same',
               dilation_rate=(8, 8),
               activation='relu',
               name="ct_conv4_1"))
    model.add(
        Conv2D(no_of_classes * 32, (3, 3),
               padding='same',
               dilation_rate=(16, 16),
               activation='relu',
               name="ct_conv5_1"))

    model.add(
        Conv2D(no_of_classes * 32, (3, 3),
               padding='same',
               activation='relu',
               name="ct_fc1"))

    model.add(
        Deconvolution2D(no_of_classes,
                        kernel_size=(3, 3),
                        strides=(2, 2),
                        activation="relu",
                        name="ct_deconv_1",
                        padding="same"))
    model.add(
        Deconvolution2D(no_of_classes,
                        kernel_size=(3, 3),
                        strides=(2, 2),
                        activation="relu",
                        name="ct_deconv_2",
                        padding="same"))
    model.add(
        Deconvolution2D(no_of_classes,
                        kernel_size=(3, 3),
                        strides=(2, 2),
                        activation="relu",
                        name="ct_deconv_3",
                        padding="same"))
    model.add(Conv2D(no_of_classes, (1, 1), activation='relu',
                     name="ct_final"))
    return model
Exemplo n.º 12
0
def SSD(input_shape, num_classes=21):
    """SSD300 architecture.

    # Arguments
        input_shape: Shape of the input image,
            expected to be either (300, 300, 3) or (3, 300, 300)(not tested).
        num_classes: Number of classes including background.

    # References
        https://arxiv.org/abs/1512.02325
    """
    # net = {}
    # Block 1
    input_tensor = Input(shape=input_shape)
    img_size = (input_shape[1], input_shape[0])
    input_img = input_tensor
    conv1_1 = Conv2D(64, (3, 3),
                     activation='relu',
                     padding='same',
                     name='conv1_1')(input_img)
    conv1_2 = Conv2D(64, (3, 3),
                     activation='relu',
                     padding='same',
                     name='conv1_2')(conv1_1)
    pool1 = MaxPooling2D((2, 2), strides=(2, 2), padding='same',
                         name='pool1')(conv1_2)
    # Block 2
    conv2_1 = Conv2D(128, (3, 3),
                     activation='relu',
                     padding='same',
                     name='conv2_1')(pool1)
    conv2_2 = Conv2D(128, (3, 3),
                     activation='relu',
                     padding='same',
                     name='conv2_2')(conv2_1)
    pool2 = MaxPooling2D((2, 2), strides=(2, 2), padding='same',
                         name='pool2')(conv2_2)
    # Block 3
    conv3_1 = Conv2D(256, (3, 3),
                     activation='relu',
                     padding='same',
                     name='conv3_1')(pool2)
    conv3_2 = Conv2D(256, (3, 3),
                     activation='relu',
                     padding='same',
                     name='conv3_2')(conv3_1)
    conv3_3 = Conv2D(256, (3, 3),
                     activation='relu',
                     padding='same',
                     name='conv3_3')(conv3_2)
    pool3 = MaxPooling2D((2, 2), strides=(2, 2), padding='same',
                         name='pool3')(conv3_3)
    # Block 4
    conv4_1 = Conv2D(512, (3, 3),
                     activation='relu',
                     padding='same',
                     name='conv4_1')(pool3)
    conv4_2 = Conv2D(512, (3, 3),
                     activation='relu',
                     padding='same',
                     name='conv4_2')(conv4_1)
    conv4_3 = Conv2D(512, (3, 3),
                     activation='relu',
                     padding='same',
                     name='conv4_3')(conv4_2)
    pool4 = MaxPooling2D((2, 2), strides=(2, 2), padding='same',
                         name='pool4')(conv4_3)
    # Block 5
    conv5_1 = Conv2D(512, (3, 3),
                     activation='relu',
                     padding='same',
                     name='conv5_1')(pool4)
    conv5_2 = Conv2D(512, (3, 3),
                     activation='relu',
                     padding='same',
                     name='conv5_2')(conv5_1)
    conv5_3 = Conv2D(512, (3, 3),
                     activation='relu',
                     padding='same',
                     name='conv5_3')(conv5_2)
    pool5 = MaxPooling2D((3, 3), strides=(1, 1), padding='same',
                         name='pool5')(conv5_3)
    # FC6
    fc6 = Conv2D(1024, (3, 3),
                 dilation_rate=(6, 6),
                 activation='relu',
                 padding='same',
                 name='fc6')(pool5)
    #fc6 = Dropout(0.5, name='drop6')(fc6)
    # FC7
    fc7 = Conv2D(1024, (1, 1), padding='same', activation='relu',
                 name='fc7')(fc6)
    #fc7 = Dropout(0.5, name='drop7')(fc7)
    # Block 6
    conv6_1 = Conv2D(256, (1, 1),
                     activation='relu',
                     padding='same',
                     name='conv6_1')(fc7)
    conv6_2 = Conv2D(512, (3, 3),
                     strides=(2, 2),
                     activation='relu',
                     padding='same',
                     name='conv6_2')(conv6_1)
    # Block 7
    conv7_1 = Conv2D(128, (1, 1),
                     activation='relu',
                     padding='same',
                     name='conv7_1')(conv6_2)
    conv7_2 = ZeroPadding2D()(conv7_1)
    conv7_2 = Conv2D(256, (3, 3),
                     strides=(2, 2),
                     activation='relu',
                     padding='valid',
                     name='conv7_2')(conv7_2)
    # Block 8
    conv8_1 = Conv2D(128, (1, 1),
                     activation='relu',
                     padding='same',
                     name='conv8_1')(conv7_2)
    conv8_2 = Conv2D(256, (3, 3),
                     strides=(2, 2),
                     activation='relu',
                     padding='same',
                     name='conv8_2')(conv8_1)
    # Last Pool
    pool6 = GlobalAveragePooling2D(name='pool6')(conv8_2)
    # Prediction from conv4_3
    conv4 = Conv2D(512, (3, 3), padding='same', name='conv4')(conv4_3)
    conv4_3_norm = Normalize(num_classes - 1, name='conv4_3_norm')(conv4)
    deconv5_3 = Deconvolution2D(512, (3, 3),
                                strides=(2, 2),
                                padding='same',
                                name='deconv5_3')(conv5_3)
    conv_deconv5_3 = Conv2D(512, (3, 3), padding='same',
                            name='conv_deconv5_3')(deconv5_3)
    conv5_3_norm = Normalize((num_classes - 1) / 2,
                             name='conv_5_3_norm')(conv_deconv5_3)
    EltSUM = add([conv4_3_norm, conv5_3_norm])
    fusion_conv4_5 = Activation(name='fusion_conv4_5',
                                activation='relu')(EltSUM)
    num_priors = 3
    fusion_conv4_5_mbox_loc = Conv2D(
        num_priors * 4, (3, 3), padding='same',
        name='fusion_conv4_5_mbox_loc')(fusion_conv4_5)
    fusion_conv4_5_mbox_loc_flat = Flatten(
        name='fusion_conv4_5_mbox_loc_flat')(fusion_conv4_5_mbox_loc)
    name = 'fusion_conv4_5_mbox_conf'
    if num_classes != 21:
        name += '_{}'.format(num_classes)
    fusion_conv4_5_mbox_conf = Conv2D(num_priors * num_classes, (3, 3),
                                      padding='same',
                                      name=name)(fusion_conv4_5)
    fusion_conv4_5_mbox_conf_flat = Flatten(
        name='fusion_conv4_5_mbox_conf_flat')(fusion_conv4_5_mbox_conf)
    fusion_conv4_5_mbox_priorbox = PriorBox(
        img_size,
        30.0,
        aspect_ratios=[2],
        variances=[0.1, 0.1, 0.2, 0.2],
        name='fusion_conv4_5_mbox_priorbox')(fusion_conv4_5)
    # Prediction from fc7
    num_priors = 6
    fc7_mbox_loc = Conv2D(num_priors * 4, (3, 3),
                          padding='same',
                          name='fc7_mbox_loc')(fc7)
    fc7_mbox_loc_flat = Flatten(name='fc7_mbox_loc_flat')(fc7_mbox_loc)
    name = 'fc7_mbox_conf'
    if num_classes != 21:
        name += '_{}'.format(num_classes)
    fc7_mbox_conf = Conv2D(num_priors * num_classes, (3, 3),
                           padding='same',
                           name=name)(fc7)
    fc7_mbox_conf_flat = Flatten(name='fc7_mbox_conf_flat')(fc7_mbox_conf)
    fc7_mbox_priorbox = PriorBox(img_size,
                                 60.0,
                                 max_size=114.0,
                                 aspect_ratios=[2, 3],
                                 variances=[0.1, 0.1, 0.2, 0.2],
                                 name='fc7_mbox_priorbox')(fc7)
    # Prediction from conv6_2
    num_priors = 6
    conv6_2_mbox_loc = Conv2D(num_priors * 4, (3, 3),
                              padding='same',
                              name='conv6_2_mbox_loc')(conv6_2)
    conv6_2_mbox_loc_flat = Flatten(
        name='conv6_2_mbox_loc_flat')(conv6_2_mbox_loc)
    name = 'conv6_2_mbox_conf'
    if num_classes != 21:
        name += '_{}'.format(num_classes)
    conv6_2_mbox_conf = Conv2D(num_priors * num_classes, (3, 3),
                               padding='same',
                               name=name)(conv6_2)
    conv6_2_mbox_conf_flat = Flatten(
        name='conv6_2_mbox_conf_flat')(conv6_2_mbox_conf)
    conv6_2_mbox_priorbox = PriorBox(img_size,
                                     114.0,
                                     max_size=168.0,
                                     aspect_ratios=[2, 3],
                                     variances=[0.1, 0.1, 0.2, 0.2],
                                     name='conv6_2_mbox_priorbox')(conv6_2)
    # Prediction from conv7_2
    num_priors = 6
    conv7_2_mbox_loc = Conv2D(num_priors * 4, (3, 3),
                              padding='same',
                              name='conv7_2_mbox_loc')(conv7_2)
    conv7_2_mbox_loc_flat = Flatten(
        name='conv7_2_mbox_loc_flat')(conv7_2_mbox_loc)
    name = 'conv7_2_mbox_conf'
    if num_classes != 21:
        name += '_{}'.format(num_classes)
    conv7_2_mbox_conf = Conv2D(num_priors * num_classes, (3, 3),
                               padding='same',
                               name=name)(conv7_2)
    conv7_2_mbox_conf_flat = Flatten(
        name='conv7_2_mbox_conf_flat')(conv7_2_mbox_conf)
    conv7_2_mbox_priorbox = PriorBox(img_size,
                                     168.0,
                                     max_size=222.0,
                                     aspect_ratios=[2, 3],
                                     variances=[0.1, 0.1, 0.2, 0.2],
                                     name='conv7_2_mbox_priorbox')(conv7_2)
    # Prediction from conv8_2
    num_priors = 6
    conv8_2_mbox_loc = Conv2D(num_priors * 4, (3, 3),
                              padding='same',
                              name='conv8_2_mbox_loc')(conv8_2)
    conv8_2_mbox_loc_flat = Flatten(
        name='conv8_2_mbox_loc_flat')(conv8_2_mbox_loc)
    name = 'conv8_2_mbox_conf'
    if num_classes != 21:
        name += '_{}'.format(num_classes)
    conv8_2_mbox_conf = Conv2D(num_priors * num_classes, (3, 3),
                               padding='same',
                               name=name)(conv8_2)
    conv8_2_mbox_conf_flat = Flatten(
        name='conv8_2_mbox_conf_flat')(conv8_2_mbox_conf)
    conv8_2_mbox_priorbox = PriorBox(img_size,
                                     222.0,
                                     max_size=276.0,
                                     aspect_ratios=[2, 3],
                                     variances=[0.1, 0.1, 0.2, 0.2],
                                     name='conv8_2_mbox_priorbox')(conv8_2)
    # Prediction from pool6
    num_priors = 6
    pool6_mbox_loc_flat = Dense(num_priors * 4,
                                name='pool6_mbox_loc_flat')(pool6)
    name = 'pool6_mbox_conf_flat'
    if num_classes != 21:
        name += '_{}'.format(num_classes)
    pool6_mbox_conf_flat = Dense(num_priors * num_classes, name=name)(pool6)
    if K.image_dim_ordering() == 'tf':
        target_shape = (1, 1, 256)
    else:
        target_shape = (256, 1, 1)
    pool6_reshaped = Reshape(target_shape, name='pool6_reshaped')(pool6)
    pool6_mbox_priorbox = PriorBox(img_size,
                                   276.0,
                                   max_size=330.0,
                                   aspect_ratios=[2, 3],
                                   variances=[0.1, 0.1, 0.2, 0.2],
                                   name='pool6_mbox_priorbox')(pool6_reshaped)
    # Gather all predictions
    mbox_loc = concatenate([
        fusion_conv4_5_mbox_loc_flat, fc7_mbox_loc_flat, conv6_2_mbox_loc_flat,
        conv7_2_mbox_loc_flat, conv8_2_mbox_loc_flat, pool6_mbox_loc_flat
    ],
                           axis=1,
                           name='mbox_loc')
    mbox_conf = concatenate([
        fusion_conv4_5_mbox_conf_flat, fc7_mbox_conf_flat,
        conv6_2_mbox_conf_flat, conv7_2_mbox_conf_flat, conv8_2_mbox_conf_flat,
        pool6_mbox_conf_flat
    ],
                            axis=1,
                            name='mbox_conf')
    mbox_priorbox = concatenate([
        fusion_conv4_5_mbox_priorbox, fc7_mbox_priorbox, conv6_2_mbox_priorbox,
        conv7_2_mbox_priorbox, conv8_2_mbox_priorbox, pool6_mbox_priorbox
    ],
                                axis=1,
                                name='mbox_priorbox')
    if hasattr(mbox_loc, '_keras_shape'):
        num_boxes = mbox_loc._keras_shape[-1] // 4
    elif hasattr(mbox_loc, 'int_shape'):
        num_boxes = K.int_shape(mbox_loc)[-1] // 4
    mbox_loc = Reshape((num_boxes, 4), name='mbox_loc_final')(mbox_loc)
    mbox_conf = Reshape((num_boxes, num_classes),
                        name='mbox_conf_logits')(mbox_conf)
    mbox_conf = Activation('softmax', name='mbox_conf_final')(mbox_conf)
    predictions = concatenate([mbox_loc, mbox_conf, mbox_priorbox],
                              axis=2,
                              name='predictions')
    model = Model(input_img, predictions)
    return model
def get_2D_Deeply_supervised_network():
    inputs = Input((cm.img_rows, cm.img_cols, 1))

    conv1 = Convolution2D(8,
                          kernel_size=(9, 9),
                          activation='relu',
                          border_mode='same')(inputs)
    conv1 = Convolution2D(8,
                          kernel_size=(9, 9),
                          activation='relu',
                          border_mode='same')(conv1)
    pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
    deconv1 = Deconvolution2D(8,
                              kernel_size=(3, 3),
                              strides=(2, 2),
                              padding="same")(pool1)
    prediction1 = Convolution2D(1, 1, 1, activation='sigmoid')(deconv1)

    conv2 = Convolution2D(16,
                          kernel_size=(7, 7),
                          activation='relu',
                          border_mode='same')(pool1)
    conv2 = Convolution2D(32,
                          kernel_size=(7, 7),
                          activation='relu',
                          border_mode='same')(conv2)
    pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
    deconv2 = Deconvolution2D(8,
                              kernel_size=(3, 3),
                              strides=(2, 2),
                              padding="same")(pool2)
    deconv2 = Deconvolution2D(8,
                              kernel_size=(3, 3),
                              strides=(2, 2),
                              padding="same")(deconv2)
    prediction2 = Convolution2D(1, 1, 1, activation='sigmoid')(deconv2)

    conv3 = Convolution2D(32,
                          kernel_size=(5, 5),
                          activation='relu',
                          border_mode='same')(pool2)
    conv3 = Convolution2D(32,
                          kernel_size=(1, 1),
                          activation='relu',
                          border_mode='same')(conv3)
    deconv3 = Deconvolution2D(8,
                              kernel_size=(3, 3),
                              strides=(2, 2),
                              padding="same")(conv3)
    deconv3 = Deconvolution2D(8,
                              kernel_size=(3, 3),
                              strides=(2, 2),
                              padding="same")(deconv3)
    prediction3 = Convolution2D(1, 1, 1, activation='sigmoid')(deconv3)

    model1 = Model(input=inputs, output=prediction1)
    model2 = Model(input=inputs, output=prediction2)
    model3 = Model(input=inputs, output=prediction3)

    # model.compile(optimizer=Adam(lr=1.0e-5), loss=dice_coef_loss, metrics=[dice_coef])
    model1.compile(optimizer=Adam(lr=1.0e-6),
                   loss=lf.binary_crossentropy_loss,
                   metrics=[lf.binary_crossentropy])

    return model1
 def deconv_func(input_layer, pad_mode='same'):
     x = Deconvolution2D(32, 4, strides=2, padding=pad_mode)(input_layer)
     x = BatchNormalization()(x)
     x = Activation('relu')(x)
     return x
Exemplo n.º 15
0
# Conv Layer 6
model.add(Convolution2D(50, 3, 3, border_mode='valid', subsample=(1,1), activation = 'relu', name = 'Conv6'))
model.add(Dropout(0.3))

# Conv Layer 7
model.add(Convolution2D(60, 3, 3, border_mode='valid', subsample=(1,1), activation = 'relu', name = 'Conv7'))
model.add(Dropout(0.3))

# Pooling 3
model.add(MaxPooling2D(pool_size=pool_size))

# Upsample 1
model.add(UpSampling2D(size=pool_size))

# Deconv 1
model.add(Deconvolution2D(50, 3, 3, border_mode='valid', subsample=(1,1), activation = 'relu', 
                          output_shape = model.layers[8].output_shape, name = 'Deconv1'))
model.add(Dropout(0.3))

# Deconv 2
model.add(Deconvolution2D(40, 3, 3, border_mode='valid', subsample=(1,1), activation = 'relu', 
                          output_shape = model.layers[7].output_shape, name = 'Deconv2'))
model.add(Dropout(0.3))

# Upsample 2
model.add(UpSampling2D(size=pool_size))

# Deconv 3
model.add(Deconvolution2D(40, 3, 3, border_mode='valid', subsample=(1,1), activation = 'relu', 
                          output_shape = model.layers[5].output_shape, name = 'Deconv3'))
model.add(Dropout(0.2))
Exemplo n.º 16
0
def autoencoder_CNN(input_img):
    dropRate = 0.5

    #drop_input=Dropout(dropRate)(input_img)
    conv1 = Conv2D(128, (3, 3), activation='relu')(input_img)  #28 x 28 x 32
    #norm1=BatchNormalization()(conv1)
    pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)  #14 x 14 x 32
    #drop1=Dropout(dropRate)(pool1)

    conv2 = Conv2D(256, (3, 3), activation='relu')(pool1)  #14 x 14 x 64
    #norm2=BatchNormalization()(conv2)
    pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)  #7 x 7 x 64
    #drop2=Dropout(dropRate)(pool2)

    conv3 = Conv2D(512, (3, 3),
                   activation='relu')(pool2)  #7 x 7 x 128 (small and thick)
    pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)  #7 x 7 x 64
    # #norm3=BatchNormalization()(conv3)

    conv4 = Conv2D(16, (3, 3),
                   activation='relu')(pool3)  #7 x 7 x 128 (small and thick)
    # pool4 = MaxPooling2D(pool_size=(2, 2))(conv4) #7 x 7 x 64

    # conv5 = Conv2D(2048, (2, 2), activation='relu')(pool4) #7 x 7 x 128 (small and thick)
    # pool4 = MaxPooling2D(pool_size=(2, 2))(conv4) #7 x 7 x 64

    # #decoder

    de_conv1 = Deconvolution2D(512, (3, 3),
                               activation='relu',
                               output_shape=(None, 1, 14,
                                             14))(conv4)  #7 x 7 x 128

    up1 = UpSampling2D((2, 2))(de_conv1)
    de_conv2 = Deconvolution2D(256, (3, 3),
                               activation='relu',
                               output_shape=(None, 1, 30,
                                             30))(up1)  #7 x 7 x 128

    up2 = UpSampling2D((2, 2))(de_conv2)
    de_conv3 = Deconvolution2D(128, (3, 3),
                               activation='relu',
                               output_shape=(None, 1, 62,
                                             62))(up2)  #7 x 7 x 128

    up3 = UpSampling2D((2, 2))(de_conv3)
    de_conv4 = Deconvolution2D(1, (3, 3),
                               activation='relu',
                               output_shape=(None, 1, 126,
                                             126))(up3)  #7 x 7 x 128

    # #norm4=BatchNormalization()(conv4)
    # up1 = UpSampling2D((2,2))(conv4) # 14 x 14 x 128
    # #drop4=Dropout(dropRate)(up1)

    # conv5 = Deconvolution2D(256, (3, 3), activation='relu',output_shape=(None,1,64,64))(up1) # 14 x 14 x 64
    # #norm5=BatchNormalization()(conv5)
    # up2 = UpSampling2D((4,4))(conv5) # 28 x 28 x 64
    # #drop5=Dropout(dropRate)(up2)

    #decoded = Deconvolution2D(1, (3, 3), activation='sigmoid',output_shape=(None,1,130,130))(up2) # 28 x 28 x 1
    decoded = de_conv4

    # CNN classifier
    # CNN_pool_1 = MaxPooling2D(pool_size=(2, 2))() #16 x 16 x 128
    # # drop6=Dropout(dropRate)(CNN_pool_1)

    # CNN_conv_1 = Conv2D(512, (2, 2), activation='relu')(pool2) #1 x 1 x 64
    # # drop7=Dropout(dropRate)(CNN_conv_1)
    # CNN_pool_1 = MaxPooling2D(pool_size=(2, 2))(CNN_conv_1)

    #CNN_pool_2 = MaxPooling2D(pool_size=(2, 2))(CNN_conv_1) #7 x 7 x 64
    # flat1 = Flatten()(conv4)
    # dense1=Dense(2048, activation='relu')(flat1)
    # classifer=Dense(1175, activation='softmax')(dense1)
    return decoded
Exemplo n.º 17
0
def buildFCN(model,imgSize=224,categories=21):

  #os
  model.add(Permute((1,2,3),input_shape = (imgSize,imgSize,3)))
                                  # Downsampling path #
  #1st block
  #Adding convolution layers
  model.add(Convolution2D(64,kernel_size = (3,3),padding = "same",activation = "relu",name = "block1_conv1"))
  model.add(Convolution2D(64,kernel_size = (3,3),padding = "same",activation = "relu",name = "block1_conv2"))

  #Addding max pooling layer
  model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2),name = 'block1_pool'))

  #2nd block
  #Adding convolution layers
  model.add(Convolution2D(128,kernel_size = (3,3),padding = "same",activation = "relu",name = "block2_conv1"))
  model.add(Convolution2D(128,kernel_size = (3,3),padding = "same",activation = "relu",name = "block2_conv2"))

  #Addding max pooling layer
  model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2),name = 'block2_pool'))


  #3rd block
  #Adding convolution layers
  model.add(Convolution2D(256,kernel_size = (3,3),padding = "same",activation = "relu",name = "block3_conv1"))
  model.add(Convolution2D(256,kernel_size = (3,3),padding = "same",activation = "relu",name = "block3_conv2"))
  model.add(Convolution2D(256,kernel_size = (3,3),padding = "same",activation = "relu",name = "block3_conv3"))

  #Addding max pooling layer
  model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2),name = 'block3_pool'))

  #4th block
  #Adding convolution layers
  model.add(Convolution2D(512,kernel_size = (3,3),padding = "same",activation = "relu",name = "block4_conv1"))
  model.add(Convolution2D(512,kernel_size = (3,3),padding = "same",activation = "relu",name = "block4_conv2"))
  model.add(Convolution2D(512,kernel_size = (3,3),padding = "same",activation = "relu",name = "block4_conv3"))

  #Addding max pooling layer
  model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2),name = 'block4_pool'))

  #5th block
  #Adding convolution layers
  model.add(Convolution2D(512,kernel_size = (3,3),padding = "same",activation = "relu",name = "block5_conv1"))
  model.add(Convolution2D(512,kernel_size = (3,3),padding = "same",activation = "relu",name = "block5_conv2"))
  model.add(Convolution2D(512,kernel_size = (3,3),padding = "same",activation = "relu",name = "block5_conv3"))

  #Adding max pooling layer
  model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2),name = 'block5_pool'))

  model.add(Convolution2D(4096,kernel_size=(7,7),padding = "same",activation = "relu",name = "fc6"))

  #Replacing fully connnected layers of VGG Net using convolutions
  model.add(Convolution2D(4096,kernel_size=(1,1),padding = "same",activation = "relu",name = "fc7"))

  # Gives the classifications scores for each of the N classes including background
  model.add(Convolution2D(categories,kernel_size=(1,1),padding="same",activation="relu",name = "score_fr"))

  #Save convolution size
  desiredSize=model.layers[-1].output_shape[2]
                              # Upsampling  #

  #First deconv layer
  model.add(Deconvolution2D(categories,kernel_size=(4,4),strides = (2,2),padding = "valid"))
  actualSize=model.layers[-1].output_shape[2]
  extra=actualSize-2*desiredSize

  #Cropping to get correct size
  model.add(Cropping2D(cropping=((0,extra),(0,extra))))

  Conv_size = model.layers[-1].output_shape[2]

  #Conv to be applied on Pool4
  skip_con1 = Convolution2D(categories,kernel_size=(1,1),padding = "same",activation=None, name = "score_pool4")

  #Addig skip connection which takes adds the output of Max pooling layer 4 to current layer
  Summed = add(inputs = [skip_con1(model.layers[14].output),model.layers[-1].output])

  #Upsampling output of first skip connection
  x = Deconvolution2D(categories,kernel_size=(4,4),strides = (2,2),padding = "valid",activation=None,name = "score4")(Summed)
  x = Cropping2D(cropping=((0,2),(0,2)))(x)


  #Conv to be applied to pool3
  skip_con2 = Convolution2D(categories,kernel_size=(1,1),padding = "same",activation=None, name = "score_pool3")

  #Adding skip connection which takes output og Max pooling layer 3 to current layer
  Summed = add(inputs = [skip_con2(model.layers[10].output),x])

  #Final Up convolution which restores the original image size
  Up = Deconvolution2D(categories,kernel_size=(16,16),strides = (8,8),
                       padding = "valid",activation = None,name = "upsample")(Summed)

  #Cropping the extra part obtained due to transpose convolution
  final = Cropping2D(cropping = ((0,8),(0,8)))(Up)

  return Model(model.input, final)
# instantiate encoder model
encoder = Model(inputs, [z_mean, z_log_var, z], name='encoder')
#encoder.summary()
plot_model(encoder, to_file='vae_mlp_encoder.png', show_shapes=True)

# build decoder model
latent_inputs = Input(shape=(latent_dim,), name='z_sampling')
latent_inputs.shape
x = Dense(intermediate_dim, activation='relu', input_shape=(latent_dim,))(latent_inputs)
x.shape
yy = Dense(intermediate_dim, activation='relu', input_shape=(intermediate_dim,))(x)
yy.shape
yy = Reshape((16,16,2))(yy)
#y.reshape(8,8,8)
yy.shape
x = Deconvolution2D(16, (3, 3), activation='relu', padding='same', input_shape=(16,16,2))(yy)
x.shape
x = UpSampling2D((2, 2), input_shape=(16,16,16))(x)
x.shape
x = Deconvolution2D(8, (3, 3), activation='relu', padding='same', input_shape=(32,32,16))(x)
x.shape
x = UpSampling2D((2, 2), input_shape=(32,32,8))(x)
x.shape
x = Deconvolution2D(3, (3, 3), activation='relu', padding='same', input_shape=(64,64,8))(x)
x.shape
x = UpSampling2D((2, 2), input_shape=(64,64,3))(x)
x.shape
outputs = Deconvolution2D(1, (3, 3), activation='sigmoid', padding='same', input_shape=(128,128,3))(x)
outputs.shape
outputs = UpSampling2D((1, 1), input_shape=(128,128,1))(outputs)
outputs.shape
Exemplo n.º 19
0
def FCN(FCN_CLASSES = 21):


    #(samples, channels, rows, cols)
    input_img = Input(shape=(3, 224, 224))
    #(3*224*224)
    x = Convolution2D(64, 3, 3, activation='relu',border_mode='same')(input_img)
    x = Convolution2D(64, 3, 3, activation='relu',border_mode='same')(x)
    x = MaxPooling2D((2, 2), strides=(2, 2))(x)
    #(64*112*112)
    x = Convolution2D(128, 3, 3, activation='relu',border_mode='same')(x)
    x = Convolution2D(128, 3, 3, activation='relu',border_mode='same')(x)
    x = MaxPooling2D((2, 2), strides=(2, 2))(x)
    #(128*56*56)
    x = Convolution2D(256, 3, 3, activation='relu',border_mode='same')(x)
    x = Convolution2D(256, 3, 3, activation='relu',border_mode='same')(x)
    x = MaxPooling2D((2, 2), strides=(2, 2))(x)
    #(256*56*56)

    #split layer
    p3 = x
    p3 = Convolution2D(FCN_CLASSES, 1, 1,activation='relu')(p3)
    #(21*28*28)

    x = Convolution2D(512, 3, 3, activation='relu',border_mode='same')(x)
    x = Convolution2D(512, 3, 3, activation='relu',border_mode='same')(x)
    x = MaxPooling2D((2, 2), strides=(2, 2))(x)
    #(512*14*14)

    #split layer
    p4 = x
    p4 = Convolution2D(FCN_CLASSES, 1, 1, activation='relu')(p4)
    p4 = Deconvolution2D(FCN_CLASSES, 4, 4,
            output_shape=(None, FCN_CLASSES, 30, 30),
            subsample=(2, 2),
            border_mode='valid')(p4)
    p4 = Cropping2D(cropping=((1, 1), (1, 1)))(p4)

    #(21*28*28)

    x = Convolution2D(512, 3, 3, activation='relu',border_mode='same')(x)
    x = Convolution2D(512, 3, 3, activation='relu',border_mode='same')(x)
    x = MaxPooling2D((2, 2), strides=(2, 2))(x)
    #(512*7*7)

    p5 = x
    p5 = Convolution2D(FCN_CLASSES, 1, 1, activation='relu')(p5)
    p5 = Deconvolution2D(FCN_CLASSES, 8, 8,
            output_shape=(None, FCN_CLASSES, 32, 32),
            subsample=(4, 4),
            border_mode='valid')(p5)
    p5 = Cropping2D(cropping=((2, 2), (2, 2)))(p5)
    #(21*28*28)

    # merge scores
    merged = merge([p3, p4, p5], mode='sum')
    x = Deconvolution2D(FCN_CLASSES, 16, 16,
            output_shape=(None, FCN_CLASSES, 232, 232),
            subsample=(8, 8),
            border_mode='valid')(merged)
    x = Cropping2D(cropping=((4, 4), (4, 4)))(x)
    x = Reshape((FCN_CLASSES,224*224))(x)
    x = Permute((2,1))(x)
    out = Activation("softmax")(x)
    #(21,224,224)
    model = Model(input_img, out)
    return model
Exemplo n.º 20
0
def model_EED(input_col, input_row):
    _input = Input(shape=(input_col, input_row, 1), name='input')

    Feature = Conv2D(nb_filter=64,
                     nb_row=3,
                     nb_col=3,
                     init='glorot_uniform',
                     activation='relu',
                     border_mode='same',
                     bias=True)(_input)
    Feature = Conv2D(nb_filter=64,
                     nb_row=3,
                     nb_col=3,
                     init='glorot_uniform',
                     activation='relu',
                     border_mode='same',
                     bias=True)(Feature)
    Feature3 = Conv2D(nb_filter=64,
                      nb_row=3,
                      nb_col=3,
                      init='glorot_uniform',
                      activation='relu',
                      border_mode='same',
                      bias=True)(Feature)
    Feature_out = merge(inputs=[Feature, Feature3], mode='sum')

    # Upsampling
    Upsampling1 = Conv2D(nb_filter=8,
                         nb_row=1,
                         nb_col=1,
                         init='glorot_uniform',
                         activation='relu',
                         border_mode='same',
                         bias=True)(Feature_out)
    Upsampling2 = Deconvolution2D(nb_filter=8,
                                  nb_row=14,
                                  nb_col=14,
                                  output_shape=(None, input_col * 2,
                                                input_row * 2, 8),
                                  subsample=(2, 2),
                                  border_mode='same',
                                  init='glorot_uniform',
                                  activation='relu')(Upsampling1)
    Upsampling3 = Conv2D(nb_filter=64,
                         nb_row=1,
                         nb_col=1,
                         init='glorot_uniform',
                         activation='relu',
                         border_mode='same',
                         bias=True)(Upsampling2)

    # Mulyi-scale Reconstruction
    Reslayer1 = Conv2D(nb_filter=64,
                       nb_row=3,
                       nb_col=3,
                       init='glorot_uniform',
                       activation='relu',
                       border_mode='same',
                       bias=True)(Upsampling3)
    Reslayer2 = Conv2D(nb_filter=64,
                       nb_row=3,
                       nb_col=3,
                       init='glorot_uniform',
                       activation='relu',
                       border_mode='same',
                       bias=True)(Reslayer1)
    Block1 = merge(inputs=[Reslayer1, Reslayer2], mode='sum')

    Reslayer3 = Conv2D(nb_filter=64,
                       nb_row=3,
                       nb_col=3,
                       init='glorot_uniform',
                       activation='relu',
                       border_mode='same',
                       bias=True)(Block1)
    Reslayer4 = Conv2D(nb_filter=64,
                       nb_row=3,
                       nb_col=3,
                       init='glorot_uniform',
                       activation='relu',
                       border_mode='same',
                       bias=True)(Reslayer3)
    Block2 = merge(inputs=[Reslayer3, Reslayer4], mode='sum')

    # ***************//
    Multi_scale1 = Conv2D(nb_filter=16,
                          nb_row=1,
                          nb_col=1,
                          init='glorot_uniform',
                          activation='relu',
                          border_mode='same',
                          bias=True)(Block2)
    Multi_scale2a = Conv2D(nb_filter=16,
                           nb_row=1,
                           nb_col=1,
                           init='glorot_uniform',
                           activation='relu',
                           border_mode='same',
                           bias=True)(Multi_scale1)
    Multi_scale2b = Conv2D(nb_filter=16,
                           nb_row=3,
                           nb_col=3,
                           init='glorot_uniform',
                           activation='relu',
                           border_mode='same',
                           bias=True)(Multi_scale1)
    Multi_scale2c = Conv2D(nb_filter=16,
                           nb_row=5,
                           nb_col=5,
                           init='glorot_uniform',
                           activation='relu',
                           border_mode='same',
                           bias=True)(Multi_scale1)
    Multi_scale2d = Conv2D(nb_filter=16,
                           nb_row=7,
                           nb_col=7,
                           init='glorot_uniform',
                           activation='relu',
                           border_mode='same',
                           bias=True)(Multi_scale1)
    Multi_scale2 = merge(
        inputs=[Multi_scale2a, Multi_scale2b, Multi_scale2c, Multi_scale2d],
        mode='concat')

    out = Conv2D(nb_filter=1,
                 nb_row=1,
                 nb_col=1,
                 init='glorot_uniform',
                 activation='relu',
                 border_mode='same',
                 bias=True)(Multi_scale2)
    model = Model(input=_input, output=out)

    Adam = adam(lr=0.001)
    model.compile(optimizer=Adam,
                  loss='mean_squared_error',
                  metrics=['mean_squared_error'])

    return model
Exemplo n.º 21
0
def fcn32_blank(image_size=512):
    withDO = False  # no effect during evaluation but usefull for fine-tuning

    if True:
        mdl = Sequential()

        # First layer is a dummy-permutation = Identity to specify input shape
        mdl.add(Permute((1, 2, 3),
                        input_shape=(image_size, image_size,
                                     3)))  # WARNING : axis 0 is the sample dim

        for l in convblock(64, 1, bits=2):
            mdl.add(l)

        for l in convblock(128, 2, bits=2):
            mdl.add(l)

        for l in convblock(256, 3, bits=3):
            mdl.add(l)

        for l in convblock(512, 4, bits=3):
            mdl.add(l)

        for l in convblock(512, 5, bits=3):
            mdl.add(l)

        mdl.add(
            Convolution2D(4096,
                          kernel_size=(7, 7),
                          padding='same',
                          activation='relu',
                          name='fc6'))  # WARNING border
        if withDO:
            mdl.add(Dropout(0.5))
        mdl.add(
            Convolution2D(4096,
                          kernel_size=(1, 1),
                          padding='same',
                          activation='relu',
                          name='fc7'))  # WARNING border
        if withDO:
            mdl.add(Dropout(0.5))

        # WARNING : model decapitation i.e. remove the classifier step of VGG16 (usually named fc8)

        mdl.add(
            Convolution2D(21,
                          kernel_size=(1, 1),
                          padding='same',
                          activation='relu',
                          name='score_fr'))

        convsize = mdl.layers[-1].output_shape[2]
        deconv_output_size = (convsize -
                              1) * 2 + 4  # INFO: =34 when images are 512x512
        # WARNING : valid, same or full ?
        mdl.add(
            Deconvolution2D(21,
                            kernel_size=(4, 4),
                            strides=(2, 2),
                            padding='valid',
                            activation=None,
                            name='score2'))

        extra_margin = deconv_output_size - convsize * 2  # INFO: =2 when images are 512x512
        assert (extra_margin > 0)
        assert (extra_margin % 2 == 0)
        # INFO : cropping as deconv gained pixels
        # print(extra_margin)
        c = ((0, extra_margin), (0, extra_margin))
        # print(c)
        mdl.add(Cropping2D(cropping=c))
        # print(mdl.summary())

        return mdl

    else:
        # See following link for a version based on Keras functional API :
        # gist.github.com/EncodeTS/6bbe8cb8bebad7a672f0d872561782d9
        raise ValueError('not implemented')
Exemplo n.º 22
0
def autoencoder_CNN(input_img):
    dropRate = 0.5

    #drop_input=Dropout(dropRate)(input_img)
    conv1 = Conv2D(64, (3, 3), activation='relu',
                   padding='same')(input_img)  #28 x 28 x 32
    #norm1=BatchNormalization()(conv1)
    pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)  #14 x 14 x 32
    #drop1=Dropout(dropRate)(pool1)

    conv2 = Conv2D(128, (3, 3), activation='relu',
                   padding='same')(pool1)  #14 x 14 x 64
    #norm2=BatchNormalization()(conv2)
    pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)  #7 x 7 x 64
    #drop2=Dropout(dropRate)(pool2)

    conv3 = Conv2D(256, (3, 3), activation='relu',
                   padding='same')(pool2)  #7 x 7 x 128 (small and thick)
    pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)  #7 x 7 x 64
    # # #norm3=BatchNormalization()(conv3)

    conv4 = Conv2D(512, (3, 3), activation='relu',
                   padding='same')(pool3)  #7 x 7 x 128 (small and thick)
    pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)  #7 x 7 x 64

    conv5 = Conv2D(1024, (3, 3), activation='relu',
                   padding='same')(pool4)  #7 x 7 x 128 (small and thick)
    pool5 = MaxPooling2D(pool_size=(2, 2))(conv5)  #7 x 7 x 64

    conv6 = Conv2D(4096, (4, 4),
                   activation='relu')(pool5)  #7 x 7 x 128 (small and thick)

    # #decoder

    # de_conv1 = Deconvolution2D(512, (3, 3), activation='relu',output_shape=(None,1,14,14))(conv4) #7 x 7 x 128

    # up1 = UpSampling2D((2,2))(de_conv1)
    de_conv1 = Deconvolution2D(1024, (4, 4),
                               activation='relu',
                               output_shape=(None, 1, 4,
                                             4))(conv6)  #7 x 7 x 128

    up2 = UpSampling2D((2, 2))(de_conv1)
    de_conv3 = Conv2D(512, (3, 3), activation='relu',
                      padding='same')(up2)  #7 x 7 x 128

    up3 = UpSampling2D((2, 2))(de_conv3)
    de_conv4 = Conv2D(256, (3, 3), activation='relu',
                      padding='same')(up3)  #7 x 7 x 128

    up4 = UpSampling2D((2, 2))(de_conv4)
    de_conv5 = Conv2D(128, (3, 3), activation='relu',
                      padding='same')(up4)  #7 x 7 x 128

    up5 = UpSampling2D((2, 2))(de_conv5)
    de_conv6 = Conv2D(64, (3, 3), activation='relu',
                      padding='same')(up5)  #7 x 7 x 128

    up6 = UpSampling2D((2, 2))(de_conv6)
    de_conv7 = Conv2D(1, (3, 3), activation='relu',
                      padding='same')(up6)  #7 x 7 x 128

    decoded = de_conv7

    return decoded
Exemplo n.º 23
0
def get_model(args):
    # Dataset config
    config = data_constants[args.dataset.lower()]
    inputCNNshape = config['inputCNNshape']
    inputMLPshape = config['inputMLPshape']
    nb_classes = config['nb_classes']

    # Build the CNN
    inputCNN = Input(shape=inputCNNshape)
    inputNorm = Flatten()(inputCNN)
    inputNorm = BatchNormalization(input_shape=inputCNNshape)(inputNorm)
    inputNorm = layers.Reshape(inputCNNshape)(inputNorm)

    # conv = Convolution2D(16, 3, 3, border_mode='same', activation='relu')(inputNorm)
    # conv = Convolution2D(32, 3, 3, border_mode='same', activation='relu')(conv)
    # conv = Convolution2D(64, 3, 3, border_mode='same', activation='relu')(conv)
    # conv = BatchNormalization()(conv)
    # pool = MaxPooling2D((2, 2), strides=(2, 2))(conv)
    # Build the CNN
    inputCNN = Input(shape=inputCNNshape)
    inputNorm = Flatten()(inputCNN)
    inputNorm = BatchNormalization(input_shape=inputCNNshape)(inputNorm)
    inputNorm = layers.Reshape(inputCNNshape)(inputNorm)

    # conv1
    conv = Convolution2D(16, 3, 3, border_mode='same',
                         activation='relu')(inputNorm)
    conv = Convolution2D(16, 3, 3, border_mode='same', activation='relu')(conv)
    conv = BatchNormalization()(conv)
    pool = MaxPooling2D((2, 2), strides=(2, 2))(conv)
    pool = Dropout(0.25)(pool)

    # conv2
    conv = Convolution2D(32, 3, 3, border_mode='same', activation='relu')(pool)
    conv = Convolution2D(32, 3, 3, border_mode='same', activation='relu')(conv)
    conv = BatchNormalization()(conv)
    pool = MaxPooling2D((2, 2), strides=(2, 2))(conv)
    pool = Dropout(0.25)(pool)

    # conv3
    conv = Convolution2D(64, 3, 3, border_mode='same', activation='relu')(pool)
    conv = Convolution2D(64, 3, 3, border_mode='same', activation='relu')(conv)
    conv = BatchNormalization()(conv)
    pool = MaxPooling2D((2, 2), strides=(2, 2))(conv)
    pool = Dropout(0.25)(pool)

    # conv4
    conv = Convolution2D(128, 3, 3, border_mode='same',
                         activation='relu')(pool)
    conv = Convolution2D(128, 3, 3, border_mode='same',
                         activation='relu')(conv)
    conv = cbam_block(conv)
    conv = BatchNormalization()(conv)
    pool = MaxPooling2D((2, 2), strides=(2, 2))(conv)
    pool = Dropout(0.25)(pool)
    reshape = Flatten()(pool)
    fcCNN = Dense(256, activation='relu')(reshape)

    # Build the MLP to CNN for cross-connections
    inputMLP = Input(shape=inputMLPshape)
    fcMLP = Dense(128, activation='relu')(inputMLP)
    fcMLP = BatchNormalization()(fcMLP)
    fcMLP = Dropout(0.5)(fcMLP)

    x12 = Dense(24 * 24)(fcMLP)
    x12 = PReLU()(x12)
    x12 = BatchNormalization()(x12)
    x12 = Dropout(0.5)(x12)
    x12 = Reshape((24, 24, 1))(x12)
    x12 = Deconvolution2D(16,
                          9,
                          9,
                          output_shape=(None, 40, 30, 16),
                          border_mode='valid')(x12)
    fcMLPTOCNN = PReLU()(x12)

    try:
        # Multimodal bilinear pooling
        merged = merge([fcCNN, fcMLPTOCNN], compact_bilinear)
    except:
        merged = merge.concatenate([fcCNN, fcMLP])
    merged = BatchNormalization()(merged)
    merged = Dropout(0.5)(merged)

    fc = Dense(512, activation='relu')(merged)
    fc = Dropout(0.5)(fc)
    out = Dense(nb_classes, activation='softmax')(fc)

    # Return the model object
    model = Model(input=[inputCNN, inputMLP], output=out)
    return model
Exemplo n.º 24
0
def get_unet1():
    inputs = Input((1, img_rows, img_cols))
    conv1 = Convolution2D(32,
                          3,
                          3,
                          activation='relu',
                          border_mode='same',
                          init='he_uniform')(inputs)
    conv1 = Convolution2D(32,
                          3,
                          3,
                          activation='relu',
                          border_mode='same',
                          init='he_uniform')(conv1)
    pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)

    conv2 = Convolution2D(64,
                          3,
                          3,
                          activation='relu',
                          border_mode='same',
                          init='he_uniform')(pool1)
    conv2 = Convolution2D(64,
                          3,
                          3,
                          activation='relu',
                          border_mode='same',
                          init='he_uniform')(conv2)
    pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)

    conv3 = Convolution2D(128,
                          3,
                          3,
                          activation='relu',
                          border_mode='same',
                          init='he_uniform')(pool2)
    conv3 = Convolution2D(128,
                          3,
                          3,
                          activation='relu',
                          border_mode='same',
                          init='he_uniform')(conv3)
    pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)

    conv4 = Convolution2D(256,
                          3,
                          3,
                          activation='relu',
                          border_mode='same',
                          init='he_uniform')(pool3)
    conv4 = Convolution2D(256,
                          3,
                          3,
                          activation='relu',
                          border_mode='same',
                          init='he_uniform')(conv4)
    pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)

    conv5 = Convolution2D(512,
                          3,
                          3,
                          activation='relu',
                          border_mode='same',
                          init='he_uniform')(pool4)
    conv5 = Convolution2D(512,
                          3,
                          3,
                          activation='relu',
                          border_mode='same',
                          init='he_uniform')(conv5)

    up6 = merge([Deconvolution2D(size=(2, 2))(conv5), conv4],
                mode='concat',
                concat_axis=1)
    conv6 = Convolution2D(256,
                          3,
                          3,
                          activation='relu',
                          border_mode='same',
                          init='he_uniform')(up6)
    conv6 = Convolution2D(256,
                          3,
                          3,
                          activation='relu',
                          border_mode='same',
                          init='he_uniform')(conv6)

    up7 = merge([Deconvolution2D(size=(2, 2))(conv6), conv3],
                mode='concat',
                concat_axis=1)
    conv7 = Convolution2D(128,
                          3,
                          3,
                          activation='relu',
                          border_mode='same',
                          init='he_uniform')(up7)
    conv7 = Convolution2D(128,
                          3,
                          3,
                          activation='relu',
                          border_mode='same',
                          init='he_uniform')(conv7)

    up8 = merge([Deconvolution2D(size=(2, 2))(conv7), conv2],
                mode='concat',
                concat_axis=1)
    conv8 = Convolution2D(64,
                          3,
                          3,
                          activation='relu',
                          border_mode='same',
                          init='he_uniform')(up8)
    conv8 = Convolution2D(64,
                          3,
                          3,
                          activation='relu',
                          border_mode='same',
                          init='he_uniform')(conv8)

    up9 = merge([Deconvolution2D(size=(2, 2))(conv8), conv1],
                mode='concat',
                concat_axis=1)
    conv9 = Convolution2D(32,
                          3,
                          3,
                          activation='relu',
                          border_mode='same',
                          init='he_uniform')(up9)
    conv9 = Convolution2D(32,
                          3,
                          3,
                          activation='relu',
                          border_mode='same',
                          init='he_uniform')(conv9)

    conv10 = Convolution2D(1, 1, 1, activation='sigmoid')(conv9)

    model = Model(input=inputs, output=conv10)

    model.compile(optimizer=Adam(lr=1e-5),
                  loss=dice_coef_loss,
                  metrics=[dice_coef])

    return model
Exemplo n.º 25
0
    def create_model(self, train_flag=True):
        #(samples, channels, rows, cols)
        ip = Input(shape=(3,self.img_height, self.img_width))
        h = self.vgg16.layers[1](ip)
        h = self.vgg16.layers[2](h)
        h = self.vgg16.layers[3](h)
        h = self.vgg16.layers[4](h)
        h = self.vgg16.layers[5](h)
        h = self.vgg16.layers[6](h)
        h = self.vgg16.layers[7](h)
        h = self.vgg16.layers[8](h)
        h = self.vgg16.layers[9](h)
        h = self.vgg16.layers[10](h)

        #split layer
        p3 = h
        p3 = Convolution2D(self.FCN_CLASSES, 1, 1, activation='relu', border_mode='valid')(p3)

        #(21*28*28)
        h = self.vgg16.layers[11](h)
        h = self.vgg16.layers[12](h)
        h = self.vgg16.layers[13](h)
        h = self.vgg16.layers[14](h)
        #(512*14*14)

        #split layer
        p4 = h
        p4 = Convolution2D(self.FCN_CLASSES, 1, 1, activation='relu')(p4)
        #"""
        p4 = Deconvolution2D(self.FCN_CLASSES, 4, 4,
                output_shape=(self.batchsize, self.FCN_CLASSES, 30, 30),
                subsample=(2, 2),
                border_mode='valid')(p4)
        p4 = Cropping2D(cropping=((1, 1), (1, 1)))(p4)
        #"""
        #p4 = UpSampling2D((2,2))(p4)
        #p4 = Convolution2D(self.FCN_CLASSES, 3, 3, activation='relu', border_mode='same')(p4)

        h = self.vgg16.layers[15](h)
        h = self.vgg16.layers[16](h)
        h = self.vgg16.layers[17](h)
        h = self.vgg16.layers[18](h)

        p5 = h
        p5 = Convolution2D(self.FCN_CLASSES, 1, 1, activation='relu')(p5)
        #"""
        p5 = Deconvolution2D(self.FCN_CLASSES, 8, 8,
                output_shape=(self.batchsize, self.FCN_CLASSES, 32, 32),
                subsample=(4, 4),
                border_mode='valid')(p5)
        p5 = Cropping2D(cropping=((2, 2), (2, 2)))(p5)
        #"""
        #p5 = UpSampling2D((4, 4))(p5)
        #p5 = Convolution2D(self.FCN_CLASSES, 3, 3, activation='relu', border_mode='same')(p5)

        # merge scores
        h = merge([p3, p4, p5], mode="sum")

        #"""
        h = Deconvolution2D(self.FCN_CLASSES, 16, 16,
                output_shape=(self.batchsize, self.FCN_CLASSES, 232, 232),
                subsample=(8, 8),
                border_mode='valid')(h)
        #"""
        #h = UpSampling2D((8, 8))(h)
        
        #h = Convolution2D(self.FCN_CLASSES, 3, 3, activation='relu', border_mode='same')(h)
        
        h = Cropping2D(cropping=((4, 4), (4, 4)))(h)
        
        #model_temp = Model(ip, h)
        #model_temp.summary()

        if not train_flag:
            return Model(ip, h)

        h = Reshape((self.FCN_CLASSES,self.img_height*self.img_width))(h)
        h = Permute((2,1))(h)
        out = Activation("softmax")(h)
        train_model = Model(ip, out)
        return train_model
Exemplo n.º 26
0
def unet(img_shape):
    inputs = Input(shape=img_shape)
    conv1 = Convolution2D(32, 3, 3, activation='relu',
                          border_mode='same')(inputs)
    conv1 = Convolution2D(32, 3, 3, activation='relu',
                          border_mode='same')(conv1)
    pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)

    conv2 = Convolution2D(64, 3, 3, activation='relu',
                          border_mode='same')(pool1)
    conv2 = Convolution2D(64, 3, 3, activation='relu',
                          border_mode='same')(conv2)
    pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)

    conv3 = Convolution2D(128, 3, 3, activation='relu',
                          border_mode='same')(pool2)
    conv3 = Convolution2D(128, 3, 3, activation='relu',
                          border_mode='same')(conv3)
    pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)

    conv4 = Convolution2D(256, 3, 3, activation='relu',
                          border_mode='same')(pool3)
    conv4 = Convolution2D(256, 3, 3, activation='relu',
                          border_mode='same')(conv4)
    pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)

    conv5 = Convolution2D(512, 3, 3, activation='relu',
                          border_mode='same')(pool4)
    conv5 = Convolution2D(512, 3, 3, activation='relu',
                          border_mode='same')(conv5)
    pool5 = MaxPooling2D(pool_size=(2, 2))(conv5)

    conv_ext1 = Convolution2D(1024, 3, 3, activation='relu',
                              border_mode='same')(pool5)
    conv_ext1 = Convolution2D(1024, 3, 3, activation='relu',
                              border_mode='same')(conv_ext1)

    up_ext6 = merge([Deconvolution2D(512, 2, 2,
                                     output_shape=(None, 32, 32, 512),
                                     activation='relu', subsample=(2, 2),
                                     border_mode='same')(conv_ext1), conv5],
                    mode='concat', concat_axis=3)
    conv_ext2 = Convolution2D(512, 3, 3, activation='relu',
                              border_mode='same')(up_ext6)
    conv_ext2 = Convolution2D(512, 3, 3, activation='relu',
                              border_mode='same')(conv_ext2)

    up6 = merge([Deconvolution2D(256, 2, 2,
                                 output_shape=(None, 64, 64, 256),
                                 activation='relu', subsample=(2, 2),
                                 border_mode='same')(conv_ext2), conv4],
                mode='concat', concat_axis=3)
    # # up6 = merge([UpSampling2D(size=(2, 2))(conv5), conv_ext2],
    # #             mode='concat', concat_axis=3)
    conv6 = Convolution2D(256, 3, 3, activation='relu',
                          border_mode='same')(up6)
    conv6 = Convolution2D(256, 3, 3, activation='relu',
                          border_mode='same')(conv6)

    up7 = merge([Deconvolution2D(128, 2, 2,
                                 output_shape=(None, 128, 128, 128),
                                 activation='relu', subsample=(2, 2),
                                 border_mode='same')(conv6), conv3],
                mode='concat', concat_axis=3)
    conv7 = Convolution2D(128, 3, 3, activation='relu',
                          border_mode='same')(up7)
    conv7 = Convolution2D(128, 3, 3, activation='relu',
                          border_mode='same')(conv7)

    up8 = merge([Deconvolution2D(64, 2, 2,
                                 output_shape=(None, 256, 256, 64),
                                 activation='relu', subsample=(2, 2),
                                 border_mode='same')(conv7), conv2],
                mode='concat', concat_axis=3)
    conv8 = Convolution2D(64, 3, 3, activation='relu',
                          border_mode='same')(up8)
    conv8 = Convolution2D(64, 3, 3, activation='relu',
                          border_mode='same')(conv8)

    up9 = merge([Deconvolution2D(32, 2, 2,
                                 output_shape=(None, 512, 512, 32),
                                 activation='relu', subsample=(2, 2),
                                 border_mode='same')(conv8), conv1],
                mode='concat', concat_axis=3)
    conv9 = Convolution2D(32, 3, 3, activation='relu',
                          border_mode='same')(up9)
    conv9 = Convolution2D(32, 3, 3, activation='relu',
                          border_mode='same')(conv9)

    conv10 = Convolution2D(1, 1, 1, activation='sigmoid')(conv9)
    flat = Flatten(name='flatten')(conv9)
    dense1 = Dense(64, activation='relu', name='fc1')(flat)
    dense2 = Dense(64, activation='relu', name='fc2')(dense1)
    dense = Dense(8, activation='softmax', name='predictions')(dense2)

    model = Model(input=inputs, output=[conv10, dense])
    # model = Model(input=inputs, output=[conv10])

    model.compile(optimizer=Adam(lr=1e-5), loss=[dice_coef_loss,
                                                 'categorical_crossentropy'],
                  metrics=[dice_coef, 'accuracy'])
    # model.compile(optimizer=Adam(lr=1e-5), loss=[dice_coef_loss],
    #               metrics=[dice_coef])

    return model
Exemplo n.º 27
0
def YOLSEModel(input_shape=(128, 128, 3)):
    input_ = Input(shape=input_shape)

    x = conv_bn_relu(filters=96,
                     name={
                         'conv': 'conv1',
                         'batch_norm': 'BN1',
                         'activation': 'act1'
                     })(input_)
    x = MaxPool2D(name='pool_01')(x)

    x = conv_bn_relu(filters=96,
                     name={
                         'conv': 'conv2',
                         'batch_norm': 'BN2',
                         'activation': 'act2'
                     })(x)
    x = MaxPool2D(name='pool_02')(x)

    x = conv_bn_relu(filters=128,
                     name={
                         'conv': 'conv3',
                         'batch_norm': 'BN3',
                         'activation': 'act3'
                     })(x)

    upsample = Deconvolution2D(filters=128,
                               kernel_size=(3, 3),
                               strides=(2, 2),
                               activation='relu',
                               padding='same')(x)

    x = conv_bn_relu(filters=256,
                     name={
                         'conv': 'conv4',
                         'batch_norm': 'BN4',
                         'activation': 'act4'
                     })(upsample)

    x = conv_bn_relu(filters=256,
                     name={
                         'conv': 'conv5',
                         'batch_norm': 'BN5',
                         'activation': 'act5'
                     })(x)

    x = conv_bn_relu(filters=256,
                     name={
                         'conv': 'conv6',
                         'batch_norm': 'BN6',
                         'activation': 'act6'
                     })(x)

    x = conv_bn_relu(filters=256,
                     name={
                         'conv': 'conv7',
                         'batch_norm': 'BN7',
                         'activation': 'act7'
                     })(x)

    x = Concatenate()([x, upsample])

    x = conv_bn_relu(filters=128,
                     name={
                         'conv': 'conv8',
                         'batch_norm': 'BN8',
                         'activation': 'act8'
                     })(x)

    x = Conv2D(filters=5,
               kernel_size=(3, 3),
               padding='same',
               activation='sigmoid')(x)

    return Model(inputs=input_, outputs=x)
Exemplo n.º 28
0
def get_uplusnet():
	inputs = Input((640, 640, 1))
	nb_filters = 64

	conv1a = Convolution2D(nb_filters*1, 3, 3, activation='relu', border_mode='same')(inputs)
	conv1a = Residual(nb_filters*1, nb_filters*1, conv1a)
	conv1a = Convolution2D(nb_filters*1, 3, 3, activation='relu', border_mode='same')(conv1a)
	conv1a = Dropout(p=0.25)(conv1a)
	pool1a = MaxPooling2D(pool_size=(2, 2))(conv1a)

	conv2a = Convolution2D(nb_filters*2, 3, 3, activation='relu', border_mode='same')(pool1a)
	conv2a = Residual(nb_filters*2, nb_filters*2, conv2a)
	conv2a = Convolution2D(nb_filters*2, 3, 3, activation='relu', border_mode='same')(conv2a) 
	conv2a = Dropout(p=0.25)(conv2a)
	pool2a = MaxPooling2D(pool_size=(2, 2))(conv2a)

	conv3a = Convolution2D(nb_filters*4, 3, 3, activation='relu', border_mode='same')(pool2a)
	conv3a = Residual(nb_filters*4, nb_filters*4, conv3a)
	conv3a = Convolution2D(nb_filters*4, 3, 3, activation='relu', border_mode='same')(conv3a)
	conv3a = Dropout(p=0.25)(conv3a)
	pool3a = MaxPooling2D(pool_size=(2, 2))(conv3a)

	conv4a = Convolution2D(nb_filters*8, 3, 3, activation='relu', border_mode='same')(pool3a)
	conv4a = Residual(nb_filters*8, nb_filters*8, conv4a)
	conv4a = Convolution2D(nb_filters*8, 3, 3, activation='relu', border_mode='same')(conv4a)
	conv4a = Dropout(p=0.25)(conv4a)
	pool4a = MaxPooling2D(pool_size=(2, 2))(conv4a)

	conv5a = Convolution2D(nb_filters*16, 3, 3, activation='relu', border_mode='same')(pool4a)
	conv5a = Residual(nb_filters*16, nb_filters*16, conv5a)
	conv5a = Dropout(p=0.25)(conv5a)
	conv5a = Convolution2D(nb_filters*16, 3, 3, activation='relu', border_mode='same')(conv5a)

	up6a   = Convolution2D(nb_filters*8, 3, 3, activation='relu', border_mode='same')(conv5a)
	# up6a   = UpSampling2D(size=(2, 2))(up6a)
	up6a   = Deconvolution2D(nb_filters*8, 3, 3, 
							 input_shape=(nb_filters*8, 40, 40),
							 output_shape=(1, 80, 80, nb_filters*8), 							 
							 subsample=(2, 2), 
							 activation='relu',
							 border_mode='same')(up6a)
	conv6a = merge([up6a, conv4a], mode='sum')
	conv6a = Dropout(p=0.25)(conv6a)
	conv6a = Residual(nb_filters*8, nb_filters*8, conv6a)
	conv6a = Convolution2D(nb_filters*8, 3, 3, activation='relu', border_mode='same')(conv6a)

	up7a   = Convolution2D(nb_filters*4, 3, 3, activation='relu', border_mode='same')(conv6a)
	# up7a   = UpSampling2D(size=(2, 2))(up7a)
	up7a   = Deconvolution2D(nb_filters*4, 3, 3, 
							 input_shape=(nb_filters*4, 80, 80),
							 output_shape=(1, 160, 160, nb_filters*4), 							 
							 subsample=(2, 2), 
							 activation='relu',
							 border_mode='same')(up7a)
	conv7a = merge([up7a, conv3a], mode='sum')
	conv7a = Dropout(p=0.25)(conv7a)
	conv7a = Residual(nb_filters*4, nb_filters*4, conv7a)
	conv7a = Convolution2D(nb_filters*4, 3, 3, activation='relu', border_mode='same')(conv7a)

	up8a   = Convolution2D(nb_filters*2, 3, 3, activation='relu', border_mode='same')(conv7a)
	# up8a   = UpSampling2D(size=(2, 2))(up8a)
	up8a   = Deconvolution2D(nb_filters*2, 3, 3, 
							 input_shape=(nb_filters*4, 160, 160),
							 output_shape=(1, 320, 320, nb_filters*2), 							 
							 subsample=(2, 2), 
							 activation='relu',
							 border_mode='same')(up8a)
	conv8a = merge([up8a, conv2a], mode='sum')
	conv8a = Dropout(p=0.25)(conv8a)
	conv8a = Residual(nb_filters*2, nb_filters*2, conv8a)
	conv8a = Convolution2D(nb_filters*2, 3, 3, activation='relu', border_mode='same')(conv8a)

	up9a   = Convolution2D(nb_filters*1, 3, 3, activation='relu', border_mode='same')(conv8a)
	# up9a   = UpSampling2D(size=(2, 2))(up9a)
	up9a   = Deconvolution2D(nb_filters*1, 3, 3, 
							 input_shape=(nb_filters*1, 320, 320),
							 output_shape=(1, 640, 640, nb_filters*1), 							 
							 subsample=(2, 2), 
							 activation='relu',
							 border_mode='same')(up9a)

	conv9a = merge([up9a, conv1a], mode='sum')
	conv9a = Dropout(p=0.25)(conv9a)
	conv9a = Residual(nb_filters*1, nb_filters*1, conv9a)
	conv9a = Convolution2D(nb_filters*1, 3, 3, activation='relu', border_mode='same')(conv9a)
	
	conv10 = Convolution2D(1, 1, 1, activation='sigmoid')(conv9a) 
	
	model = Model(input=inputs, output=conv10)
	
	model.compile(optimizer=Nadam(lr=0.0001), loss='mae')
	
	model.summary()
	return model
Exemplo n.º 29
0
    def __init__(self, input_shape=(28, 28, 1), latent_dim=2, intermediate_dim=256, batch_size=100, epsilon_std=1.0,
                 dropout_p=0.1):
        # input image dimensions
        self.input_shape = input_shape
        if len(input_shape) == 3:
            self.img_rows, self.img_cols, self.img_chns = input_shape
        elif len(input_shape) == 2:
            self.img_rows, self.img_cols = input_shape
            self.img_chns = 1
        else:
            raise IndexError("Invalid shape: {}".format(input_shape))
        self.batch_size = batch_size
        self.original_dim = np.prod(input_shape)
        self.latent_dim = latent_dim
        self.intermediate_dim = intermediate_dim
        self.epsilon_std = epsilon_std

        # number of convolutional filters to use
        nb_filters = 64
        # convolution kernel size
        nb_conv = 3

        batch_size = 100
        if K_backend.image_dim_ordering() == 'th':
            self.original_img_size = (self.img_chns, self.img_rows, self.img_cols)
        else:
            self.original_img_size = (self.img_rows, self.img_cols, self.img_chns)

        x = Input(batch_shape=(batch_size,) + self.original_img_size)
        conv_1 = Convolution2D(self.img_chns, 2, 2, border_mode='same', activation='relu')(x)
        conv_2 = Convolution2D(nb_filters, 2, 2,
                               border_mode='same', activation='relu', subsample=(2, 2))(conv_1)
        conv_3 = Convolution2D(nb_filters, nb_conv, nb_conv, border_mode='same', activation='relu', subsample=(1, 1))(
            conv_2)
        for i in range(5):
            conv_3 = BatchNormalization()(conv_3)
            conv_3 = Dropout(dropout_p)(conv_3)
            conv_3 = Convolution2D(nb_filters, nb_conv, nb_conv, border_mode='same', activation='relu',
                                   subsample=(1, 1))(conv_3)

        conv_3 = BatchNormalization()(conv_3)
        conv_4 = Convolution2D(nb_filters, nb_conv, nb_conv, border_mode='same', activation='relu', subsample=(1, 1))(
            conv_3)
        flat = Flatten()(conv_4)
        hidden = Dense(intermediate_dim, activation='relu')(flat)

        self.z_mean = Dense(latent_dim)(hidden)
        self.z_log_var = Dense(latent_dim)(hidden)

        ## ==== End of encoding portion ======

        # note that "output_shape" isn't necessary with the TensorFlow backend
        # so you could write `Lambda(sampling)([z_mean, z_log_var])`
        z = Lambda(self.sampling, output_shape=(latent_dim,))([self.z_mean, self.z_log_var])

        # we instantiate these layers separately so as to reuse them later
        decoder_hid = Dense(intermediate_dim, activation='relu')
        decoder_upsample = Dense(nb_filters * 14 * 14, activation='relu')

        if K_backend.image_dim_ordering() == 'th':
            output_shape = (batch_size, nb_filters, 14, 14)
        else:
            output_shape = (batch_size, 14, 14, nb_filters)

        decoder_reshape = Reshape(output_shape[1:])
        decoder_deconv_1 = Deconvolution2D(nb_filters, nb_conv, nb_conv, output_shape,
                                           border_mode='same', subsample=(1, 1), activation='relu')
        decoder_deconv_2 = Deconvolution2D(nb_filters, nb_conv, nb_conv, output_shape,
                                           border_mode='same', subsample=(1, 1), activation='relu')
        if K_backend.image_dim_ordering() == 'th':
            output_shape = (batch_size, nb_filters, 29, 29)
        else:
            output_shape = (batch_size, 29, 29, nb_filters)
        decoder_deconv_3_upsamp = Deconvolution2D(nb_filters, 2, 2, output_shape,
                                                  border_mode='valid', subsample=(2, 2), activation='relu')
        decoder_mean_squash = Convolution2D(self.img_chns, 2, 2, border_mode='valid', activation='sigmoid')

        hid_decoded = decoder_hid(z)
        up_decoded = decoder_upsample(hid_decoded)
        reshape_decoded = decoder_reshape(up_decoded)
        deconv_1_decoded = decoder_deconv_1(reshape_decoded)
        deconv_2_decoded = decoder_deconv_2(deconv_1_decoded)
        x_decoded_relu = decoder_deconv_3_upsamp(deconv_2_decoded)
        x_decoded_mean_squash = decoder_mean_squash(x_decoded_relu)

        self.model = Model(x, x_decoded_mean_squash)
        self.model.compile(optimizer='rmsprop', loss=self.vae_loss)
        # self.model.summary()

        # build a model to project inputs on the latent space
        self.encoder = Model(x, self.z_mean)

        # build a digit generator that can sample from the learned distribution
        # todo: (un)roll this
        decoder_input = Input(shape=(latent_dim,))
        _hid_decoded = decoder_hid(decoder_input)
        _up_decoded = decoder_upsample(_hid_decoded)
        _reshape_decoded = decoder_reshape(_up_decoded)
        _deconv_1_decoded = decoder_deconv_1(_reshape_decoded)
        _deconv_2_decoded = decoder_deconv_2(_deconv_1_decoded)
        _x_decoded_relu = decoder_deconv_3_upsamp(_deconv_2_decoded)
        _x_decoded_mean_squash = decoder_mean_squash(_x_decoded_relu)
        self.generator = Model(decoder_input, _x_decoded_mean_squash)
Exemplo n.º 30
0
def get_unet(img_rows, img_cols):
    from keras.models import Model
    from keras.layers.core import Reshape, Permute, Activation
    from keras.layers import Input, merge, Convolution2D, MaxPooling2D, UpSampling2D, Deconvolution2D
    from keras.layers.normalization import BatchNormalization
    from keras import backend as K
    K.set_image_dim_ordering('th')

    inputs = Input((1, img_rows, img_cols))
    conv1 = Convolution2D(64, 3, 3, activation='relu',
                          border_mode='same')(inputs)
    conv2 = Convolution2D(64, 3, 3, activation='relu',
                          border_mode='same')(conv1)
    pool1 = MaxPooling2D(pool_size=(2, 2))(conv2)

    conv3 = Convolution2D(128, 3, 3, activation='relu',
                          border_mode='same')(pool1)
    conv4 = Convolution2D(128, 3, 3, activation='relu',
                          border_mode='same')(conv3)
    pool2 = MaxPooling2D(pool_size=(2, 2))(conv4)

    conv5 = Convolution2D(256, 3, 3, activation='relu',
                          border_mode='same')(pool2)
    conv6 = Convolution2D(256, 3, 3, activation='relu',
                          border_mode='same')(conv5)
    conv6 = Convolution2D(256, 3, 3, activation='relu',
                          border_mode='same')(conv6)
    pool3 = MaxPooling2D(pool_size=(2, 2))(conv6)

    conv7 = Convolution2D(512, 3, 3, activation='relu',
                          border_mode='same')(pool3)
    conv8 = Convolution2D(512, 3, 3, activation='relu',
                          border_mode='same')(conv7)
    conv8 = Convolution2D(512, 3, 3, activation='relu',
                          border_mode='same')(conv8)
    pool4 = MaxPooling2D(pool_size=(2, 2))(conv8)

    conv9 = Convolution2D(1024, 3, 3, activation='relu',
                          border_mode='same')(pool4)
    conv10 = Convolution2D(1024, 3, 3, activation='relu',
                           border_mode='same')(conv9)

    deconv1 = Deconvolution2D(512,
                              2,
                              2,
                              output_shape=(batch_size, 512, img_rows / 8,
                                            img_cols / 8),
                              subsample=(2, 2),
                              activation='relu')(conv10)
    #deconv1 = UpSampling2D(size=(2,2))(conv10)
    #deconv1 = Convolution2D(512, 2, 2, activation='relu', border_mode='same')(deconv1)
    merge1 = merge([deconv1, conv8], mode='concat', concat_axis=1)
    conv11 = Convolution2D(512, 3, 3, activation='relu',
                           border_mode='same')(merge1)
    conv12 = Convolution2D(512, 3, 3, activation='relu',
                           border_mode='same')(conv11)
    conv12 = Convolution2D(512, 3, 3, activation='relu',
                           border_mode='same')(conv12)

    deconv2 = Deconvolution2D(256,
                              2,
                              2,
                              output_shape=(batch_size, 256, img_rows / 4,
                                            img_cols / 4),
                              subsample=(2, 2),
                              activation='relu')(conv12)
    #deconv2 = UpSampling2D(size=(2,2))(conv12)
    #deconv2 = Convolution2D(256, 2, 2, activation='relu', border_mode='same')(deconv2)
    merge2 = merge([deconv2, conv6], mode='concat', concat_axis=1)
    conv13 = Convolution2D(256, 3, 3, activation='relu',
                           border_mode='same')(merge2)
    conv14 = Convolution2D(256, 3, 3, activation='relu',
                           border_mode='same')(conv13)
    conv14 = Convolution2D(256, 3, 3, activation='relu',
                           border_mode='same')(conv14)

    deconv3 = Deconvolution2D(128,
                              2,
                              2,
                              output_shape=(batch_size, 128, img_rows / 2,
                                            img_cols / 2),
                              subsample=(2, 2),
                              activation='relu')(conv14)
    #deconv3 = UpSampling2D(size=(2,2))(conv14)
    #deconv3 = Convolution2D(128, 2, 2, activation='relu', border_mode='same')(deconv3)
    merge3 = merge([deconv3, conv4], mode='concat', concat_axis=1)
    conv15 = Convolution2D(128, 3, 3, activation='relu',
                           border_mode='same')(merge3)
    conv16 = Convolution2D(128, 3, 3, activation='relu',
                           border_mode='same')(conv15)

    deconv4 = Deconvolution2D(64,
                              2,
                              2,
                              output_shape=(batch_size, 64, img_rows,
                                            img_cols),
                              subsample=(2, 2),
                              activation='relu')(conv16)
    #deconv4 = UpSampling2D(size=(2,2))(conv16)
    #deconv4 = Convolution2D(64, 2, 2, activation='relu', border_mode='same')(deconv4)
    merge4 = merge([deconv4, conv2], mode='concat', concat_axis=1)
    conv17 = Convolution2D(64, 3, 3, activation='relu',
                           border_mode='same')(merge4)
    conv18 = Convolution2D(64, 3, 3, activation='relu',
                           border_mode='same')(conv17)

    conv19 = Convolution2D(2, 1, 1, activation=None,
                           border_mode='same')(conv18)
    conv19 = Reshape((2, img_rows * img_cols))(conv19)
    conv19 = Permute((2, 1))(conv19)
    conv19 = Activation('softmax')(conv19)

    model = Model(input=inputs, output=conv19)
    model.summary()

    return model