Ejemplo n.º 1
0
def get_G(a_shape=(None, flags.za_dim), c_shape=(None, flags.c_shape[0], flags.c_shape[1], flags.c_shape[2]), \
          name=None):
    ndf = 256
    na = Input(a_shape)
    nc = Input(c_shape)
    #z = Concat(-1)([na, nt])
    z = na
    nz = ExpandDims(1)(z)
    nz = ExpandDims(1)(nz)
    nz = Tile([1, c_shape[1], c_shape[2], 1])(nz)

    # res block
    nn = Conv2d(ndf, (3, 3), (1, 1), act=None, W_init=w_init, b_init=None)(nc)
    nn = InstanceNorm2d(act=tf.nn.relu, gamma_init=g_init)(nn)
    nn = Conv2d(ndf, (3, 3), (1, 1), act=None, W_init=w_init, b_init=None)(nn)
    nn = InstanceNorm2d(act=None, gamma_init=g_init)(nn)
    n = Elementwise(tf.add)([nc, nn])

    nd_tmp = flags.za_dim
    ndf = ndf + nd_tmp
    n = Concat(-1)([n, nz])

    # res block *3
    for i in range(1, 4):
        nn = Conv2d(ndf, (3, 3), (1, 1), act=None, W_init=w_init,
                    b_init=None)(n)
        nn = InstanceNorm2d(act=tf.nn.relu, gamma_init=g_init)(nn)
        nn = Conv2d(ndf, (3, 3), (1, 1), act=None, W_init=w_init,
                    b_init=None)(nn)
        nn = InstanceNorm2d(act=None, gamma_init=g_init)(nn)
        n = Elementwise(tf.add)([n, nn])

    for i in range(2):
        ndf = ndf + nd_tmp
        n = Concat(-1)([n, nz])
        nz = Tile([1, 2, 2, 1])(nz)

        n = DeConv2d(ndf // 2, (3, 3), (2, 2),
                     act=tf.nn.relu,
                     W_init=w_init,
                     b_init=None)(n)
        n = InstanceNorm2d(act=tf.nn.relu, gamma_init=g_init)(n)

        ndf = ndf // 2

    n = Concat(-1)([n, nz])
    n = DeConv2d(3, (1, 1), (1, 1), act=tf.nn.tanh, W_init=w_init)(n)

    M = Model(inputs=[na, nc], outputs=n, name=name)
    return M
Ejemplo n.º 2
0
def get_discriminator(latent_shape, image_shape, df_dim=64):

    w_init = tf.random_normal_initializer(stddev=0.02)
    gamma_init = tf.random_normal_initializer(1., 0.02)
    lrelu = lambda x: tf.nn.leaky_relu(x, 0.2)

    n1i = Input(image_shape)
    n1 = Conv2d(df_dim, (5, 5), (2, 2), act=lrelu, W_init=w_init)(n1i)
    n1 = Conv2d(df_dim * 2, (5, 5), (2, 2), W_init=w_init, b_init=None)(n1)
    n1 = BatchNorm2d(decay=0.9, act=lrelu, gamma_init=gamma_init)(n1)
    n1 = Dropout(keep=0.8)(n1)
    n1 = Conv2d(df_dim * 4, (5, 5), (2, 2), W_init=w_init, b_init=None)(n1)
    n1 = BatchNorm2d(decay=0.9, act=lrelu, gamma_init=gamma_init)(n1)
    n1 = Dropout(keep=0.8)(n1)
    n1 = Conv2d(df_dim * 8, (5, 5), (2, 2), W_init=w_init, b_init=None)(n1)
    n1 = BatchNorm2d(decay=0.9, act=lrelu, gamma_init=gamma_init)(n1)
    n1 = Dropout(keep=0.8)(n1)
    n1 = Flatten()(n1)  # [-1,4*4*df_dim*8]

    n2i = Input(latent_shape)
    n2 = Dense(n_units=4 * 4 * df_dim * 8, W_init=w_init, b_init=None)(n2i)
    n2 = Dropout(keep=0.8)(n2)
    nn = Concat()([n1, n2])

    nn = Dense(n_units=1, W_init=w_init, b_init=None)(nn)

    return tl.models.Model(inputs=[n1i, n2i], outputs=nn, name='discriminator')
Ejemplo n.º 3
0
    def tam_net(self):
        inputs = Input(self.in_shape, name='inputs')

        e_in = inputs
        for i in range(0, 5):
            e_out = Conv2d(self.f_size * (2**i), (3, 3), (2, 2),
                           act=tf.nn.relu,
                           name=f'e{i+1}_con')(e_in)
            e_in = self.residual_block(i, e=True)(e_out)
            self.__setattr__(f'e{i+1}', e_in)

        d_in = e_in
        for i in range(4, 0, -1):
            d_out = DeConv2d(self.f_size * (2**(i - 1)), (3, 3), (2, 2),
                             name=f'd{i}_con')(d_in)
            encoder = self.__getattribute__(f'e{i}')
            d_out = Concat(concat_dim=3, name=f'concat{i}')([encoder, d_out])
            d_out = Conv2d(self.f_size * (2**(i - 1)), (1, 1), (1, 1),
                           name=f'fusion{i}')(d_out)
            d_in = self.residual_block(i - 1, e=False)(d_out)
            self.__setattr__(f'd{i + 1}', d_in)

        outs = DeConv2d(3, (3, 3), (2, 2), name='d_con_out')(d_in)
        outs = Conv2d(3, (1, 1), (1, 1), act=tf.nn.sigmoid, name='outs')(outs)
        return Model(inputs=inputs, outputs=outs, name="TAM_Net")
Ejemplo n.º 4
0
def fire_block(n, n_filter, max_pool=False, name='fire_block'):
    n = Conv2d(n_filter, (1, 1), (1, 1), tf.nn.relu, 'SAME', name=name + '.squeeze1x1')(n)
    n1 = Conv2d(n_filter * 4, (1, 1), (1, 1), tf.nn.relu, 'SAME', name=name + '.expand1x1')(n)
    n2 = Conv2d(n_filter * 4, (3, 3), (1, 1), tf.nn.relu, 'SAME', name=name + '.expand3x3')(n)
    n = Concat(-1, name=name + '.concat')([n1, n2])
    if max_pool:
        n = MaxPool2d((3, 3), (2, 2), 'VALID', name=name + '.max')(n)
    return n
Ejemplo n.º 5
0
    def u_net(self):
        inputs = Input(self.in_shape, name='inputs')

        conv1 = Conv2d(64, (3, 3), act=tf.nn.relu, name='conv1_1')(inputs)
        conv1 = Conv2d(64, (3, 3), act=tf.nn.relu, name='conv1_2')(conv1)
        pool1 = MaxPool2d((2, 2), name='pool1')(conv1)

        conv2 = Conv2d(128, (3, 3), act=tf.nn.relu, name='conv2_1')(pool1)
        conv2 = Conv2d(128, (3, 3), act=tf.nn.relu, name='conv2_2')(conv2)
        pool2 = MaxPool2d((2, 2), name='pool2')(conv2)

        conv3 = Conv2d(256, (3, 3), act=tf.nn.relu, name='conv3_1')(pool2)
        conv3 = Conv2d(256, (3, 3), act=tf.nn.relu, name='conv3_2')(conv3)
        pool3 = MaxPool2d((2, 2), name='pool3')(conv3)

        conv4 = Conv2d(512, (3, 3), act=tf.nn.relu, name='conv4_1')(pool3)
        conv4 = Conv2d(512, (3, 3), act=tf.nn.relu, name='conv4_2')(conv4)
        pool4 = MaxPool2d((2, 2), name='pool4')(conv4)

        conv5 = Conv2d(1024, (3, 3), act=tf.nn.relu, name='conv5_1')(pool4)
        conv5 = Conv2d(1024, (3, 3), act=tf.nn.relu, name='conv5_2')(conv5)

        up4 = DeConv2d(512, (3, 3), (2, 2), name='deconv4')(conv5)
        up4 = Concat(3, name='concat4')([up4, conv4])
        conv4 = Conv2d(512, (3, 3), act=tf.nn.relu, name='uconv4_1')(up4)
        conv4 = Conv2d(512, (3, 3), act=tf.nn.relu, name='uconv4_2')(conv4)

        up3 = DeConv2d(256, (3, 3), (2, 2), name='deconv3')(conv4)
        up3 = Concat(3, name='concat3')([up3, conv3])
        conv3 = Conv2d(256, (3, 3), act=tf.nn.relu, name='uconv3_1')(up3)
        conv3 = Conv2d(256, (3, 3), act=tf.nn.relu, name='uconv3_2')(conv3)

        up2 = DeConv2d(128, (3, 3), (2, 2), name='deconv2')(conv3)
        up2 = Concat(3, name='concat2')([up2, conv2])
        conv2 = Conv2d(128, (3, 3), act=tf.nn.relu, name='uconv2_1')(up2)
        conv2 = Conv2d(128, (3, 3), act=tf.nn.relu, name='uconv2_2')(conv2)

        up1 = DeConv2d(64, (3, 3), (2, 2), name='deconv1')(conv2)
        up1 = Concat(3, name='concat1')([up1, conv1])
        conv1 = Conv2d(64, (3, 3), act=tf.nn.relu, name='uconv1_1')(up1)
        conv1 = Conv2d(64, (3, 3), act=tf.nn.relu, name='uconv1_2')(conv1)

        outs = Conv2d(3, (1, 1), act=tf.nn.sigmoid, name='uconv1')(conv1)
        return Model(inputs=inputs, outputs=outs, name="U_Net")
Ejemplo n.º 6
0
def model_G2():  ##Phase2 Generator

    gamma_init = tf1.random_normal_initializer(1., 0.02)
    w_init = tf1.random_normal_initializer(stddev=0.02)
    fn = tf1.nn.relu

    ##	Input layers
    lr_image = Input(
        (None, 128, 128, 3))  ##	(batch_size, height, width, channel)
    hr_image = Input((None, 512, 512, 3))

    ## 	Feature extracting layers from LR image
    lr_feature_layer_1 = Conv2d(64, (3, 3), (1, 1),
                                act=fn,
                                padding='SAME',
                                W_init=w_init)(lr_image)  # Shape(1,256,256,64)
    lr_feature_layer_1 = BatchNorm2d(gamma_init=gamma_init)(lr_feature_layer_1)

    lr_feature_layer_2 = SubpixelConv2d(scale=4, act=fn)(
        lr_feature_layer_1)  # Shape(1,256,256,16)

    ##	Feature extracting layers from HR image

    hr_feature_layer_1 = Conv2d(64, (3, 3), (1, 1),
                                act=fn,
                                padding='SAME',
                                W_init=w_init)(hr_image)  # Shape(1,256,256,64)
    hr_feature_layer_1 = BatchNorm2d(gamma_init=gamma_init)(hr_feature_layer_1)

    ##	Features Merging layers

    merge_layer = Concat(concat_dim=-1)(
        [lr_feature_layer_2, hr_feature_layer_1])  # Shape(1,256,256,128)

    non_linearity_layer_1 = Conv2d(64, (5, 5), (1, 1),
                                   act=fn,
                                   padding='SAME',
                                   W_init=w_init)(
                                       merge_layer)  # Shape(1,256,256,256)
    non_linearity_layer_1 = BatchNorm2d(
        gamma_init=gamma_init)(non_linearity_layer_1)

    ## 	Reconstruction layers
    Recon_layer_1 = Conv2d(3, (5, 5), (1, 1),
                           act=fn,
                           padding='SAME',
                           W_init=w_init)(
                               non_linearity_layer_1)  # Shape(1,256,256,1)
    Recon_layer_2 = Elementwise(combine_fn=tf1.add)([Recon_layer_1, hr_image
                                                     ])  # Shape(1,256,256,1)

    return Model(inputs=[lr_image, hr_image], outputs=Recon_layer_2)
Ejemplo n.º 7
0
def get_dwG(
        shape_z=(None, 100),
        shape_h=(0, 16)):  # Dimension of gen filters in first conv layer. [64]
    s16 = flags.img_size_h // 16
    gf_dim = 64  # Dimension of gen filters in first conv layer. [64]
    w_init = tf.random_normal_initializer(stddev=0.02)
    gamma_init = tf.random_normal_initializer(1., 0.02)

    n_z = Input(shape_z)
    n_h = Input(shape_h)
    n = Concat(-1)([n_z, n_h])

    n = Dense(n_units=(gf_dim * 8 * s16 * s16),
              W_init=w_init,
              act=tf.identity,
              b_init=None)(n)

    n = Reshape(shape=[-1, s16, s16, gf_dim * 8])(n)

    n = BatchNorm(decay=0.9, act=tf.nn.relu, gamma_init=gamma_init)(n)

    n = DeConv2d(gf_dim * 4, (5, 5),
                 strides=(2, 2),
                 act=None,
                 W_init=w_init,
                 b_init=None)(n)
    n = BatchNorm(decay=0.9, act=tf.nn.relu, gamma_init=gamma_init)(n)

    n = DeConv2d(gf_dim * 2, (5, 5),
                 strides=(2, 2),
                 act=None,
                 W_init=w_init,
                 b_init=None)(n)
    n = BatchNorm(decay=0.9, act=tf.nn.relu, gamma_init=gamma_init)(n)

    n = DeConv2d(gf_dim, (5, 5),
                 strides=(2, 2),
                 act=None,
                 W_init=w_init,
                 b_init=None)(n)
    n = BatchNorm(decay=0.9, act=tf.nn.relu, gamma_init=gamma_init)(n)

    n = DeConv2d(flags.c_dim, (5, 5),
                 strides=(2, 2),
                 act=tf.nn.tanh,
                 W_init=w_init)(n)
    return tl.models.Model(inputs=[n_z, n_h], outputs=n, name='generator')
Ejemplo n.º 8
0
def unet(ni, out_channel, is_tanh, out_size=flags.img_size_h):
    w_init = tf.random_normal_initializer(stddev=0.02)
    gamma_init = tf.random_normal_initializer(1., 0.02)
    ngf = 64
    conv1 = Conv2d(ngf, (3, 3), (1, 1), W_init=w_init, act=lrelu)(ni)

    conv2 = Conv2d(ngf, (4, 4), (2, 2), W_init=w_init, act=None,
                   b_init=None)(conv1)
    conv2 = BatchNorm2d(decay=0.9, act=lrelu, gamma_init=gamma_init,
                        name=None)(conv2)

    conv3 = Conv2d(ngf * 2, (4, 4), (1, 1),
                   W_init=w_init,
                   act=None,
                   b_init=None)(conv2)
    conv3 = BatchNorm2d(decay=0.9, act=lrelu, gamma_init=gamma_init,
                        name=None)(conv3)
    conv4 = Conv2d(ngf * 2, (4, 4), (2, 2),
                   W_init=w_init,
                   act=None,
                   b_init=None)(conv3)
    conv4 = BatchNorm2d(decay=0.9, act=lrelu, gamma_init=gamma_init,
                        name=None)(conv4)

    conv5 = Conv2d(ngf * 4, (4, 4), (1, 1),
                   W_init=w_init,
                   act=None,
                   b_init=None)(conv4)
    conv5 = BatchNorm2d(decay=0.9, act=lrelu, gamma_init=gamma_init,
                        name=None)(conv5)
    conv6 = Conv2d(ngf * 4, (4, 4), (2, 2),
                   W_init=w_init,
                   act=None,
                   b_init=None)(conv5)
    conv6 = BatchNorm2d(decay=0.9, act=lrelu, gamma_init=gamma_init,
                        name=None)(conv6)

    conv7 = Conv2d(ngf * 8, (4, 4), (1, 1),
                   W_init=w_init,
                   act=None,
                   b_init=None)(conv6)
    conv7 = BatchNorm2d(decay=0.9, act=lrelu, gamma_init=gamma_init,
                        name=None)(conv7)
    conv8 = Conv2d(ngf * 8, (4, 4), (2, 2),
                   act=lrelu,
                   W_init=w_init,
                   b_init=None)(conv7)
    # 8 8 512 now start upsample

    c_size = conv8.shape[-2]
    ##############################################################################################
    no = None
    for _ in range(1):
        up8 = DeConv2d(ngf * 8, (4, 4), (2, 2), W_init=w_init,
                       b_init=None)(conv8)
        up8 = BatchNorm2d(decay=0.9,
                          act=lrelu,
                          gamma_init=gamma_init,
                          name=None)(up8)
        up7 = Concat(concat_dim=3)([up8, conv7])
        up7 = DeConv2d(ngf * 8, (4, 4), (1, 1), W_init=w_init,
                       b_init=None)(up7)
        up7 = BatchNorm2d(decay=0.9,
                          act=lrelu,
                          gamma_init=gamma_init,
                          name=None)(up7)
        c_size = c_size * 2
        if c_size == out_size:
            no = up7
            break
        up6 = Concat(concat_dim=3)([up7, conv6])
        up6 = DeConv2d(ngf * 4, (4, 4), (2, 2), W_init=w_init,
                       b_init=None)(up6)
        up6 = BatchNorm2d(decay=0.9,
                          act=lrelu,
                          gamma_init=gamma_init,
                          name=None)(up6)
        up5 = Concat(concat_dim=3)([up6, conv5])
        up5 = DeConv2d(ngf * 4, (4, 4), (1, 1), W_init=w_init,
                       b_init=None)(up5)
        up5 = BatchNorm2d(decay=0.9,
                          act=lrelu,
                          gamma_init=gamma_init,
                          name=None)(up5)
        c_size = c_size * 2
        if c_size == out_size:
            no = up5
            break
        up4 = Concat(concat_dim=3)([up5, conv4])
        up4 = DeConv2d(ngf * 2, (4, 4), (2, 2), W_init=w_init,
                       b_init=None)(up4)
        up4 = BatchNorm2d(decay=0.9,
                          act=lrelu,
                          gamma_init=gamma_init,
                          name=None)(up4)
        up3 = Concat(concat_dim=3)([up4, conv3])
        up3 = DeConv2d(ngf * 2, (4, 4), (1, 1), W_init=w_init,
                       b_init=None)(up3)
        up3 = BatchNorm2d(decay=0.9,
                          act=lrelu,
                          gamma_init=gamma_init,
                          name=None)(up3)
        c_size = c_size * 2
        if c_size == out_size:
            no = up3
            break
        up2 = Concat(concat_dim=3)([up3, conv2])
        up2 = DeConv2d(ngf * 1, (4, 4), (2, 2), W_init=w_init,
                       b_init=None)(up2)
        up2 = BatchNorm2d(decay=0.9,
                          act=lrelu,
                          gamma_init=gamma_init,
                          name=None)(up2)
        up1 = Concat(concat_dim=3)([up2, conv1])
        up1 = DeConv2d(ngf * 1, (4, 4), (1, 1), W_init=w_init,
                       b_init=None)(up1)
        up1 = BatchNorm2d(decay=0.9,
                          act=lrelu,
                          gamma_init=gamma_init,
                          name=None)(up1)
        c_size = c_size * 2
        if c_size == out_size:
            no = up1
            break
    if is_tanh:
        up0 = DeConv2d(out_channel, (3, 3), (1, 1),
                       W_init=w_init,
                       act=tf.nn.tanh)(no)
    else:
        up0 = DeConv2d(out_channel, (3, 3), (1, 1),
                       W_init=w_init,
                       b_init=None,
                       act=None)(no)

    return up0
Ejemplo n.º 9
0
def cspdarknet53(input_data=None):

    input_data = convolutional(input_data, (3, 3, 3, 32), activate_type='mish')
    input_data = convolutional(input_data, (3, 3, 32, 64),
                               downsample=True,
                               activate_type='mish')

    route = input_data
    route = convolutional(route, (1, 1, 64, 64),
                          activate_type='mish',
                          name='conv_rote_block_1')
    input_data = convolutional(input_data, (1, 1, 64, 64),
                               activate_type='mish')

    for i in range(1):
        input_data = residual_block(input_data,
                                    64,
                                    32,
                                    64,
                                    activate_type="mish")
    input_data = convolutional(input_data, (1, 1, 64, 64),
                               activate_type='mish')

    input_data = Concat()([input_data, route])
    input_data = convolutional(input_data, (1, 1, 128, 64),
                               activate_type='mish')
    input_data = convolutional(input_data, (3, 3, 64, 128),
                               downsample=True,
                               activate_type='mish')
    route = input_data
    route = convolutional(route, (1, 1, 128, 64),
                          activate_type='mish',
                          name='conv_rote_block_2')
    input_data = convolutional(input_data, (1, 1, 128, 64),
                               activate_type='mish')
    for i in range(2):
        input_data = residual_block(input_data,
                                    64,
                                    64,
                                    64,
                                    activate_type="mish")
    input_data = convolutional(input_data, (1, 1, 64, 64),
                               activate_type='mish')
    input_data = Concat()([input_data, route])

    input_data = convolutional(input_data, (1, 1, 128, 128),
                               activate_type='mish')
    input_data = convolutional(input_data, (3, 3, 128, 256),
                               downsample=True,
                               activate_type='mish')
    route = input_data
    route = convolutional(route, (1, 1, 256, 128),
                          activate_type='mish',
                          name='conv_rote_block_3')
    input_data = convolutional(input_data, (1, 1, 256, 128),
                               activate_type='mish')
    for i in range(8):
        input_data = residual_block(input_data,
                                    128,
                                    128,
                                    128,
                                    activate_type="mish")
    input_data = convolutional(input_data, (1, 1, 128, 128),
                               activate_type='mish')
    input_data = Concat()([input_data, route])

    input_data = convolutional(input_data, (1, 1, 256, 256),
                               activate_type='mish')
    route_1 = input_data
    input_data = convolutional(input_data, (3, 3, 256, 512),
                               downsample=True,
                               activate_type='mish')
    route = input_data
    route = convolutional(route, (1, 1, 512, 256),
                          activate_type='mish',
                          name='conv_rote_block_4')
    input_data = convolutional(input_data, (1, 1, 512, 256),
                               activate_type='mish')
    for i in range(8):
        input_data = residual_block(input_data,
                                    256,
                                    256,
                                    256,
                                    activate_type="mish")
    input_data = convolutional(input_data, (1, 1, 256, 256),
                               activate_type='mish')
    input_data = Concat()([input_data, route])

    input_data = convolutional(input_data, (1, 1, 512, 512),
                               activate_type='mish')
    route_2 = input_data
    input_data = convolutional(input_data, (3, 3, 512, 1024),
                               downsample=True,
                               activate_type='mish')
    route = input_data
    route = convolutional(route, (1, 1, 1024, 512),
                          activate_type='mish',
                          name='conv_rote_block_5')
    input_data = convolutional(input_data, (1, 1, 1024, 512),
                               activate_type='mish')
    for i in range(4):
        input_data = residual_block(input_data,
                                    512,
                                    512,
                                    512,
                                    activate_type="mish")
    input_data = convolutional(input_data, (1, 1, 512, 512),
                               activate_type='mish')
    input_data = Concat()([input_data, route])

    input_data = convolutional(input_data, (1, 1, 1024, 1024),
                               activate_type='mish')
    input_data = convolutional(input_data, (1, 1, 1024, 512))
    input_data = convolutional(input_data, (3, 3, 512, 1024))
    input_data = convolutional(input_data, (1, 1, 1024, 512))

    maxpool1 = MaxPool2d(filter_size=(13, 13), strides=(1, 1))(input_data)
    maxpool2 = MaxPool2d(filter_size=(9, 9), strides=(1, 1))(input_data)
    maxpool3 = MaxPool2d(filter_size=(5, 5), strides=(1, 1))(input_data)
    input_data = Concat()([maxpool1, maxpool2, maxpool3, input_data])

    input_data = convolutional(input_data, (1, 1, 2048, 512))
    input_data = convolutional(input_data, (3, 3, 512, 1024))
    input_data = convolutional(input_data, (1, 1, 1024, 512))

    return route_1, route_2, input_data
Ejemplo n.º 10
0
def YOLOv4(NUM_CLASS, pretrained=False):

    input_layer = Input([None, INPUT_SIZE, INPUT_SIZE, 3])
    route_1, route_2, conv = cspdarknet53(input_layer)

    route = conv
    conv = convolutional(conv, (1, 1, 512, 256))
    conv = upsample(conv)
    route_2 = convolutional(route_2, (1, 1, 512, 256), name='conv_yolo_1')
    conv = Concat()([route_2, conv])

    conv = convolutional(conv, (1, 1, 512, 256))
    conv = convolutional(conv, (3, 3, 256, 512))
    conv = convolutional(conv, (1, 1, 512, 256))
    conv = convolutional(conv, (3, 3, 256, 512))
    conv = convolutional(conv, (1, 1, 512, 256))

    route_2 = conv
    conv = convolutional(conv, (1, 1, 256, 128))
    conv = upsample(conv)
    route_1 = convolutional(route_1, (1, 1, 256, 128), name='conv_yolo_2')
    conv = Concat()([route_1, conv])

    conv = convolutional(conv, (1, 1, 256, 128))
    conv = convolutional(conv, (3, 3, 128, 256))
    conv = convolutional(conv, (1, 1, 256, 128))
    conv = convolutional(conv, (3, 3, 128, 256))
    conv = convolutional(conv, (1, 1, 256, 128))

    route_1 = conv
    conv = convolutional(conv, (3, 3, 128, 256), name='conv_route_1')
    conv_sbbox = convolutional(conv, (1, 1, 256, 3 * (NUM_CLASS + 5)),
                               activate=False,
                               bn=False)

    conv = convolutional(route_1, (3, 3, 128, 256),
                         downsample=True,
                         name='conv_route_2')
    conv = Concat()([conv, route_2])

    conv = convolutional(conv, (1, 1, 512, 256))
    conv = convolutional(conv, (3, 3, 256, 512))
    conv = convolutional(conv, (1, 1, 512, 256))
    conv = convolutional(conv, (3, 3, 256, 512))
    conv = convolutional(conv, (1, 1, 512, 256))

    route_2 = conv
    conv = convolutional(conv, (3, 3, 256, 512), name='conv_route_3')
    conv_mbbox = convolutional(conv, (1, 1, 512, 3 * (NUM_CLASS + 5)),
                               activate=False,
                               bn=False)

    conv = convolutional(route_2, (3, 3, 256, 512),
                         downsample=True,
                         name='conv_route_4')
    conv = Concat()([conv, route])

    conv = convolutional(conv, (1, 1, 1024, 512))
    conv = convolutional(conv, (3, 3, 512, 1024))
    conv = convolutional(conv, (1, 1, 1024, 512))
    conv = convolutional(conv, (3, 3, 512, 1024))
    conv = convolutional(conv, (1, 1, 1024, 512))

    conv = convolutional(conv, (3, 3, 512, 1024))
    conv_lbbox = convolutional(conv, (1, 1, 1024, 3 * (NUM_CLASS + 5)),
                               activate=False,
                               bn=False)

    network = Model(input_layer, [conv_sbbox, conv_mbbox, conv_lbbox])

    if pretrained:
        restore_params(network, model_path='model/model.npz')

    return network
Ejemplo n.º 11
0
    def batch_normal_u_net(self):
        # built encoder
        inputs = Input(self.in_shape, name='inputs')
        conv1 = Conv2d(64, (4, 4), (2, 2), name='conv1')(inputs)

        conv2 = Conv2d(128, (4, 4), (2, 2), name='conv2')(conv1)
        conv2 = BatchNorm(act=tl.act.lrelu, is_train=self.train_bn,
                          name='bn2')(conv2)

        conv3 = Conv2d(256, (4, 4), (2, 2), name='conv3')(conv2)
        conv3 = BatchNorm(act=tl.act.lrelu, is_train=self.train_bn,
                          name='bn3')(conv3)

        conv4 = Conv2d(512, (4, 4), (2, 2), name='conv4')(conv3)
        conv4 = BatchNorm(act=tl.act.lrelu, is_train=self.train_bn,
                          name='bn4')(conv4)

        conv5 = Conv2d(512, (4, 4), (2, 2), name='conv5')(conv4)
        conv5 = BatchNorm(act=tl.act.lrelu, is_train=self.train_bn,
                          name='bn5')(conv5)

        conv6 = Conv2d(512, (4, 4), (2, 2), name='conv6')(conv5)
        conv6 = BatchNorm(act=tl.act.lrelu, is_train=self.train_bn,
                          name='bn6')(conv6)

        conv7 = Conv2d(512, (4, 4), (2, 2), name='conv7')(conv6)
        conv7 = BatchNorm(act=tl.act.lrelu, is_train=self.train_bn,
                          name='bn7')(conv7)

        conv8 = Conv2d(512, (4, 4), (2, 2), act=tl.act.lrelu,
                       name='conv8')(conv7)

        # built decoder
        up7 = DeConv2d(512, (4, 4), name='deconv7')(conv8)
        up7 = BatchNorm(act=tf.nn.relu, is_train=self.train_bn,
                        name='dbn7')(up7)

        up6 = Concat(concat_dim=3, name='concat6')([up7, conv7])
        up6 = DeConv2d(1024, (4, 4), name='deconv6')(up6)
        up6 = BatchNorm(act=tf.nn.relu, is_train=self.train_bn,
                        name='dbn6')(up6)

        up5 = Concat(concat_dim=3, name='concat5')([up6, conv6])
        up5 = DeConv2d(1024, (4, 4), name='deconv5')(up5)
        up5 = BatchNorm(act=tf.nn.relu, is_train=self.train_bn,
                        name='dbn5')(up5)

        up4 = Concat(concat_dim=3, name='concat4')([up5, conv5])
        up4 = DeConv2d(1024, (4, 4), name='deconv4')(up4)
        up4 = BatchNorm(act=tf.nn.relu, is_train=self.train_bn,
                        name='dbn4')(up4)

        up3 = Concat(concat_dim=3, name='concat3')([up4, conv4])
        up3 = DeConv2d(256, (4, 4), name='deconv3')(up3)
        up3 = BatchNorm(act=tf.nn.relu, is_train=self.train_bn,
                        name='dbn3')(up3)

        up2 = Concat(concat_dim=3, name='concat2')([up3, conv3])
        up2 = DeConv2d(128, (4, 4), name='deconv2')(up2)
        up2 = BatchNorm(act=tf.nn.relu, is_train=self.train_bn,
                        name='dbn2')(up2)

        up1 = Concat(concat_dim=3, name='concat1')([up2, conv2])
        up1 = DeConv2d(64, (4, 4), name='deconv1')(up1)
        up1 = BatchNorm(act=tf.nn.relu, is_train=self.train_bn,
                        name='dbn1')(up1)

        up0 = Concat(concat_dim=3, name='concat0')([up1, conv1])
        up0 = DeConv2d(64, (4, 4), name='deconv0')(up0)
        up0 = BatchNorm(act=tf.nn.relu, is_train=self.train_bn,
                        name='dbn0')(up0)

        outs = Conv2d(3, (1, 1), act=tf.nn.sigmoid, name='out')(up0)
        return Model(inputs=inputs, outputs=outs, name="BN_U_Net")
Ejemplo n.º 12
0
def YOLOv4(NUM_CLASS, pretrained=False):
    """Pre-trained YOLOv4 model.

    Parameters
    ------------
    NUM_CLASS : int
        Number of classes in final prediction.
    pretrained : boolean
        Whether to load pretrained weights. Default False.

    Examples
    ---------
    Object Detection with YOLOv4, see `computer_vision.py
    <https://github.com/tensorlayer/tensorlayer/blob/master/tensorlayer/app/computer_vision.py>`__
    With TensorLayer

    >>> # get the whole model, without pre-trained YOLOv4 parameters
    >>> yolov4 = tl.app.YOLOv4(NUM_CLASS=80, pretrained=False)
    >>> # get the whole model, restore pre-trained YOLOv4 parameters
    >>> yolov4 = tl.app.YOLOv4(NUM_CLASS=80, pretrained=True)
    >>> # use for inferencing
    >>> output = yolov4(img, is_train=False)

    """

    input_layer = Input([None, INPUT_SIZE, INPUT_SIZE, 3])
    route_1, route_2, conv = cspdarknet53(input_layer)

    route = conv
    conv = convolutional(conv, (1, 1, 512, 256))
    conv = upsample(conv)
    route_2 = convolutional(route_2, (1, 1, 512, 256), name='conv_yolo_1')
    conv = Concat()([route_2, conv])

    conv = convolutional(conv, (1, 1, 512, 256))
    conv = convolutional(conv, (3, 3, 256, 512))
    conv = convolutional(conv, (1, 1, 512, 256))
    conv = convolutional(conv, (3, 3, 256, 512))
    conv = convolutional(conv, (1, 1, 512, 256))

    route_2 = conv
    conv = convolutional(conv, (1, 1, 256, 128))
    conv = upsample(conv)
    route_1 = convolutional(route_1, (1, 1, 256, 128), name='conv_yolo_2')
    conv = Concat()([route_1, conv])

    conv = convolutional(conv, (1, 1, 256, 128))
    conv = convolutional(conv, (3, 3, 128, 256))
    conv = convolutional(conv, (1, 1, 256, 128))
    conv = convolutional(conv, (3, 3, 128, 256))
    conv = convolutional(conv, (1, 1, 256, 128))

    route_1 = conv
    conv = convolutional(conv, (3, 3, 128, 256), name='conv_route_1')
    conv_sbbox = convolutional(conv, (1, 1, 256, 3 * (NUM_CLASS + 5)), activate=False, bn=False)

    conv = convolutional(route_1, (3, 3, 128, 256), downsample=True, name='conv_route_2')
    conv = Concat()([conv, route_2])

    conv = convolutional(conv, (1, 1, 512, 256))
    conv = convolutional(conv, (3, 3, 256, 512))
    conv = convolutional(conv, (1, 1, 512, 256))
    conv = convolutional(conv, (3, 3, 256, 512))
    conv = convolutional(conv, (1, 1, 512, 256))

    route_2 = conv
    conv = convolutional(conv, (3, 3, 256, 512), name='conv_route_3')
    conv_mbbox = convolutional(conv, (1, 1, 512, 3 * (NUM_CLASS + 5)), activate=False, bn=False)

    conv = convolutional(route_2, (3, 3, 256, 512), downsample=True, name='conv_route_4')
    conv = Concat()([conv, route])

    conv = convolutional(conv, (1, 1, 1024, 512))
    conv = convolutional(conv, (3, 3, 512, 1024))
    conv = convolutional(conv, (1, 1, 1024, 512))
    conv = convolutional(conv, (3, 3, 512, 1024))
    conv = convolutional(conv, (1, 1, 1024, 512))

    conv = convolutional(conv, (3, 3, 512, 1024))
    conv_lbbox = convolutional(conv, (1, 1, 1024, 3 * (NUM_CLASS + 5)), activate=False, bn=False)

    network = Model(input_layer, [conv_sbbox, conv_mbbox, conv_lbbox])

    if pretrained:
        restore_params(network, model_path='model/yolov4_model.npz')

    return network
Ejemplo n.º 13
0
def u_net(inputs, refine=False):
    w_init = tf.random_normal_initializer(stddev=0.02)
    g_init = tf.random_normal_initializer(1., 0.02)
    lrelu = lambda x: tl.act.lrelu(x, 0.2)

    # ENCODER
    conv1 = Conv2d(64, (4, 4), (2, 2), padding='SAME', W_init=w_init)(inputs)
    conv1 = InstanceNorm2d(act=lrelu, gamma_init=g_init)(conv1)

    conv2 = Conv2d(128, (4, 4), (2, 2), padding='SAME', W_init=w_init)(conv1)
    conv2 = InstanceNorm2d(act=lrelu, gamma_init=g_init)(conv2)

    conv3 = Conv2d(256, (4, 4), (2, 2), padding='SAME', W_init=w_init)(conv2)
    conv3 = InstanceNorm2d(act=lrelu, gamma_init=g_init)(conv3)

    conv4 = Conv2d(512, (4, 4), (2, 2), padding='SAME', W_init=w_init)(conv3)
    conv4 = InstanceNorm2d(act=lrelu, gamma_init=g_init)(conv4)

    conv5 = Conv2d(512, (4, 4), (2, 2), padding='SAME', W_init=w_init)(conv4)
    conv5 = InstanceNorm2d(act=lrelu, gamma_init=g_init)(conv5)

    conv6 = Conv2d(512, (4, 4), (2, 2), padding='SAME', W_init=w_init)(conv5)
    conv6 = InstanceNorm2d(act=lrelu, gamma_init=g_init)(conv6)

    conv7 = Conv2d(512, (4, 4), (2, 2), padding='SAME', W_init=w_init)(conv6)
    conv7 = InstanceNorm2d(act=lrelu, gamma_init=g_init)(conv7)

    conv8 = Conv2d(512, (4, 4), (2, 2), padding='SAME', W_init=w_init)(conv7)
    conv8 = InstanceNorm2d(act=lrelu, gamma_init=g_init)(conv8)

    # DECODER
    d0 = DeConv2d(n_filter=512, filter_size=(4, 4))(conv8)
    d0 = Dropout(0.5)(d0)
    d0 = Concat()(
        [InstanceNorm2d(act=tf.nn.relu, gamma_init=g_init)(d0), conv7])

    d1 = DeConv2d(n_filter=512, filter_size=(4, 4))(d0)
    d1 = Dropout(0.5)(d1)
    d1 = Concat()(
        [InstanceNorm2d(act=tf.nn.relu, gamma_init=g_init)(d1), conv6])

    d2 = DeConv2d(n_filter=512, filter_size=(4, 4))(d1)
    d2 = Dropout(0.5)(d2)
    d2 = Concat()(
        [InstanceNorm2d(act=tf.nn.relu, gamma_init=g_init)(d2), conv5])

    d3 = DeConv2d(n_filter=512, filter_size=(4, 4))(d2)
    d3 = Concat()(
        [InstanceNorm2d(act=tf.nn.relu, gamma_init=g_init)(d3), conv4])

    d4 = DeConv2d(n_filter=256, filter_size=(4, 4))(d3)
    d4 = Concat()(
        [InstanceNorm2d(act=tf.nn.relu, gamma_init=g_init)(d4), conv3])

    d5 = DeConv2d(n_filter=128, filter_size=(4, 4))(d4)
    d5 = Concat()(
        [InstanceNorm2d(act=tf.nn.relu, gamma_init=g_init)(d5), conv2])

    d6 = DeConv2d(n_filter=64, filter_size=(4, 4))(d5)
    d6 = Concat()(
        [InstanceNorm2d(act=tf.nn.relu, gamma_init=g_init)(d6), conv1])

    d7 = DeConv2d(n_filter=64, filter_size=(4, 4))(d6)
    d7 = InstanceNorm2d(act=tf.nn.relu, gamma_init=g_init)(d7)

    nn = Conv2d(1, (1, 1), (1, 1),
                act=tf.nn.tanh,
                padding='SAME',
                W_init=w_init)(d7)

    if refine:
        nn = RampElementwise(tf.add, act=tl.act.ramp, v_min=-1)([nn, inputs])

    return nn
Ejemplo n.º 14
0
def get_D_Conditional(input_shape):
    w_init = tf.random_normal_initializer(stddev=0.02)
    b_init = None
    gamma_init = tf.random_normal_initializer(1., 0.02)
    lrelu = lambda x: tl.act.lrelu(x, 0.2)
    #with tf.variable_scope("SRGAN_d", reuse=reuse) as vs:
    # tl.layers.set_name_reuse(reuse) # remove for TL 1.8.0+

    nin_lr = Input((input_shape[0], input_shape[1] / 8, input_shape[2] / 8,
                    input_shape[3]))
    nin_hr = Input(input_shape)

    n_hr = Conv2d(64, (3, 3), (2, 2), act=lrelu, padding='SAME',
                  W_init=w_init)(nin_hr)
    n_hr = BatchNorm2d(gamma_init=gamma_init)(n_hr)
    n_hr = Conv2d(64, (3, 3), (2, 2), act=lrelu, padding='SAME',
                  W_init=w_init)(n_hr)
    n_hr = BatchNorm2d(gamma_init=gamma_init)(n_hr)
    n_hr = Conv2d(64, (3, 3), (2, 2), act=lrelu, padding='SAME',
                  W_init=w_init)(n_hr)
    n_hr = BatchNorm2d(gamma_init=gamma_init)(n_hr)

    n_lr = Conv2d(64, (3, 3), (1, 1), act=lrelu, padding='SAME',
                  W_init=w_init)(nin_lr)
    n_lr = BatchNorm2d(gamma_init=gamma_init)(n_lr)
    n_lr = Conv2d(64, (3, 3), (1, 1), act=lrelu, padding='SAME',
                  W_init=w_init)(n_lr)
    n_lr = BatchNorm2d(gamma_init=gamma_init)(n_lr)
    n_lr = Conv2d(64, (3, 3), (1, 1), act=lrelu, padding='SAME',
                  W_init=w_init)(n_lr)
    n_lr = BatchNorm2d(gamma_init=gamma_init)(n_lr)

    n = Concat(concat_dim=-1)([n_lr, n_hr])

    n = Conv2d(128, (3, 3), (1, 1),
               act=lrelu,
               padding='SAME',
               W_init=w_init,
               b_init=b_init,
               name='n128s1/c')(n)
    n = BatchNorm2d(gamma_init=gamma_init)(n)

    n = Conv2d(128, (3, 3), (2, 2),
               act=lrelu,
               padding='SAME',
               W_init=w_init,
               b_init=b_init,
               name='n128s2/c')(n)
    n = BatchNorm2d(gamma_init=gamma_init)(n)

    n = Conv2d(256, (3, 3), (1, 1),
               act=lrelu,
               padding='SAME',
               W_init=w_init,
               b_init=b_init,
               name='n256s1/c')(n)
    n = BatchNorm2d(gamma_init=gamma_init)(n)

    n = Conv2d(256, (3, 3), (2, 2),
               act=lrelu,
               padding='SAME',
               W_init=w_init,
               b_init=b_init,
               name='n256s2/c')(n)
    n = BatchNorm2d(gamma_init=gamma_init)(n)

    n = Conv2d(512, (3, 3), (1, 1),
               act=lrelu,
               padding='SAME',
               W_init=w_init,
               b_init=b_init,
               name='n512s1/c')(n)
    n = BatchNorm2d(gamma_init=gamma_init)(n)

    n = Conv2d(512, (3, 3), (2, 2),
               act=lrelu,
               padding='SAME',
               W_init=w_init,
               b_init=b_init,
               name='n512s2/c')(n)
    n = BatchNorm2d(gamma_init=gamma_init)(n)

    n = Flatten()(n)
    n = Dense(n_units=1024, act=lrelu, name='d1024', W_init=w_init)(n)
    no = Dense(n_units=1, act=None, name='out', W_init=w_init)(n)
    D = Model(inputs=[nin_lr, nin_hr], outputs=no)
    return D