Ejemplo n.º 1
0
    def __init__(self, in_nc, out_nc, nf, nb, gc=32, upscale=4, norm_type=None, act_type='leakyrelu', \
            mode='CNA', res_scale=1, upsample_mode='upconv'):
        super(RRDB_Net, self).__init__()
        n_upscale = int(math.log(upscale, 2))
        if upscale == 3:
            n_upscale = 1

        fea_conv = B.conv_block(in_nc, nf, kernel_size=3, norm_type=None, act_type=None)
        rb_blocks = [B.RRDB(nf, kernel_size=3, gc=32, stride=1, bias=True, pad_type='zero', \
            norm_type=norm_type, act_type=act_type, mode='CNA') for _ in range(nb)]
        LR_conv = B.conv_block(nf, nf, kernel_size=3, norm_type=norm_type, act_type=None, mode=mode)

        if upsample_mode == 'upconv':
            upsample_block = B.upconv_blcok
        elif upsample_mode == 'pixelshuffle':
            upsample_block = B.pixelshuffle_block
        else:
            raise NotImplementedError('upsample mode [%s] is not found' % upsample_mode)
        if upscale == 3:
            upsampler = upsample_block(nf, nf, 3, act_type=act_type)
        else:
            upsampler = [upsample_block(nf, nf, act_type=act_type) for _ in range(n_upscale)]
        HR_conv0 = B.conv_block(nf, nf, kernel_size=3, norm_type=None, act_type=act_type)
        HR_conv1 = B.conv_block(nf, out_nc, kernel_size=3, norm_type=None, act_type=None)

        self.model = B.sequential(fea_conv, B.ShortcutBlock(B.sequential(*rb_blocks, LR_conv)),\
            *upsampler, HR_conv0, HR_conv1)
Ejemplo n.º 2
0
def yolov4_tiny(inputs, num_bbox=3, NUM_CLASS):
    """

    Return:
        [conv_mbbox, conv_lbbox]
    """
    route_1, x = cspdarknet53_tiny(inputs)
    x = conv_block(x, channels=256, kernel_size=(1, 1))

    conv_lobj_branch = conv_block(x, channels=512, kernel_size=(3, 3))
    conv_lbbox = conv_block(conv_lobj_branch,
                            channels=(NUM_CLASS + 4 + 1) * num_bbox,
                            kernel_size=(1, 1),
                            activation=None,
                            bn=False)

    x = conv_block(x, channels=128, kernel_size=(1, 1))
    x = upsample(x)
    x = tf.concat([x, route_1], axis=-1)

    conv_mobj_branch = conv_block(x, channels=256, kernel_size=(3, 3))
    conv_mbbox = conv_block(conv_mobj_branch,
                            channels=(NUM_CLASS + 4 + 1) * num_bbox,
                            kernel_size=(1, 1),
                            activation=None,
                            bn=False)
    return [conv_mbbox, conv_lbbox]
Ejemplo n.º 3
0
def darknet53_tiny(inputs):
    x = conv_block(inputs, channels=16, kernel_size=(3, 3))
    x = tf.keras.layers.MaxPool2D(pool_size=2, strides=2, padding='same')(x)
    x = conv_block(x, channels=32, kernel_size=(3, 3))
    x = tf.keras.layers.MaxPool2D(pool_size=2, strides=2, padding='same')(x)
    x = conv_block(x, channels=64, kernel_size=(3, 3))
    x = tf.keras.layers.MaxPool2D(pool_size=2, strides=2, padding='same')(x)
    x = conv_block(x, channels=128, kernel_size=(3, 3))
    x = tf.keras.layers.MaxPool2D(pool_size=2, strides=2, padding='same')(x)
    x = conv_block(x, channels=256, kernel_size=(3, 3))
    route_1 = x
    x = tf.keras.layers.MaxPool2D(pool_size=2, strides=2, padding='same')(x)
    x = conv_block(x, channels=512, kernel_size=(3, 3))
    x = tf.keras.layers.MaxPool2D(pool_size=2, strides=2, padding='same')(x)
    x = conv_block(x, channels=1024, kernel_size=(3, 3))

    return route_1, x
Ejemplo n.º 4
0
def cspdarknet53_tiny(inputs):
    x = conv_block(inputs, channels=32, downsample=True)
    x = conv_block(x, channels=64, downsample=True)
    #### block start #####
    x = conv_block(x, channels=64)
    route = x
    x = route_group(x, num_groups=2, group_id=1)
    x = conv_block(x, channels=32)
    route_1 = x
    x = conv_block(x, channels=32)
    x = tf.concat([x, route_1], axis=-1)
    x = conv_block(x, channels=64, kernel_size=(1, 1))
    x = tf.concat([route, x], axis=-1)
    x = tf.keras.layers.MaxPool2D(pool_size=2, strides=2, padding='same')(x)
    #### block start #####
    x = conv_block(x, channels=128)
    route = x
    x = route_group(x, num_groups=2, group_id=1)
    x = conv_block(x, channels=64)
    route_1 = x
    x = conv_block(x, channels=64)
    x = tf.concat([x, route_1], axis=-1)
    x = conv_block(x, channels=128, kernel_size=(1, 1))
    x = tf.concat([route, x], axis=-1)
    x = tf.keras.layers.MaxPool2D(pool_size=2, strides=2, padding='same')(x)
    #### block start #####
    x = conv_block(x, channels=256)
    route = x
    x = route_group(x, num_groups=2, group_id=1)
    x = conv_block(x, 128)
    route_1 = x
    x = conv_block(x, channels=128)
    x = tf.concat([x, route_1], axis=-1)
    x = conv_bloack(x, channels=256, kernel_size=(1, 1))
    x = tf.concat([route, x], axis=-1)
    x = tf.keras.layers.MaxPool2D(pool_size=2, strides=2, padding='same')(x)

    x = conv_block(x, channels=512)
    return route_1, x
Ejemplo n.º 5
0
    def __init__(
        self,
        in_nc,
        out_nc,
        nf,
        nb,
        gc=32,
        upscale=4,
        norm_type=None,
        act_type="leakyrelu",
        mode="CNA",
        upsample_mode="upconv",
        convtype="Conv2D",
        finalact=None,
        plus=False,
    ):
        super(RRDBNet, self).__init__()
        n_upscale = int(math.log(upscale, 2))
        if upscale == 3:
            n_upscale = 1

        fea_conv = B.conv_block(in_nc,
                                nf,
                                kernel_size=3,
                                norm_type=None,
                                act_type=None)
        rb_blocks = [
            B.RRDB(
                nf,
                kernel_size=3,
                gc=32,
                stride=1,
                bias=1,
                pad_type="zero",
                norm_type=norm_type,
                act_type=act_type,
                mode="CNA",
                convtype=convtype,
                plus=plus,
            ) for _ in range(nb)
        ]
        LR_conv = B.conv_block(nf,
                               nf,
                               kernel_size=3,
                               norm_type=norm_type,
                               act_type=None,
                               mode=mode)

        if upsample_mode == "upconv":
            upsample_block = B.upconv_block
        elif upsample_mode == "pixelshuffle":
            upsample_block = B.pixelshuffle_block
        else:
            raise NotImplementedError(
                "upsample mode [{:s}] is not found".format(upsample_mode))
        if upscale == 3:
            upsampler = upsample_block(nf, nf, 3, act_type=act_type)
        else:
            upsampler = [
                upsample_block(nf, nf, act_type=act_type)
                for _ in range(n_upscale)
            ]
        HR_conv0 = B.conv_block(nf,
                                nf,
                                kernel_size=3,
                                norm_type=None,
                                act_type=act_type)
        HR_conv1 = B.conv_block(nf,
                                out_nc,
                                kernel_size=3,
                                norm_type=None,
                                act_type=None)

        # Note: this option adds new parameters to the architecture, another option is to use "outm" in the forward
        outact = B.act(finalact) if finalact else None

        self.model = B.sequential(
            fea_conv, B.ShortcutBlock(B.sequential(*rb_blocks, LR_conv)),
            *upsampler, HR_conv0, HR_conv1, outact)
Ejemplo n.º 6
0
    def __init__(
        self,
        in_nc,
        out_nc,
        nf,
        nb,
        gc=32,
        upscale=4,
        norm_type=None,
        act_type="leakyrelu",
        mode="CNA",
        upsample_mode="upconv",
    ):
        super(SPSRNet, self).__init__()

        n_upscale = int(math.log(upscale, 2))
        if upscale == 3:
            n_upscale = 1

        fea_conv = B.conv_block(in_nc,
                                nf,
                                kernel_size=3,
                                norm_type=None,
                                act_type=None)
        rb_blocks = [
            B.RRDB(
                nf,
                kernel_size=3,
                gc=32,
                stride=1,
                bias=True,
                pad_type="zero",
                norm_type=norm_type,
                act_type=act_type,
                mode="CNA",
            ) for _ in range(nb)
        ]
        LR_conv = B.conv_block(nf,
                               nf,
                               kernel_size=3,
                               norm_type=norm_type,
                               act_type=None,
                               mode=mode)

        if upsample_mode == "upconv":
            upsample_block = B.upconv_blcok
        elif upsample_mode == "pixelshuffle":
            upsample_block = B.pixelshuffle_block
        else:
            raise NotImplementedError(
                "upsample mode [{:s}] is not found".format(upsample_mode))
        if upscale == 3:
            upsampler = upsample_block(nf, nf, 3, act_type=act_type)
        else:
            upsampler = [
                upsample_block(nf, nf, act_type=act_type)
                for _ in range(n_upscale)
            ]
        self.HR_conv0_new = B.conv_block(nf,
                                         nf,
                                         kernel_size=3,
                                         norm_type=None,
                                         act_type=act_type)
        self.HR_conv1_new = B.conv_block(nf,
                                         nf,
                                         kernel_size=3,
                                         norm_type=None,
                                         act_type=None)

        self.model = B.sequential(
            fea_conv, B.ShortcutBlockSPSR(B.sequential(*rb_blocks, LR_conv)),
            *upsampler, self.HR_conv0_new)

        self.get_g_nopadding = Get_gradient_nopadding()

        self.b_fea_conv = B.conv_block(in_nc,
                                       nf,
                                       kernel_size=3,
                                       norm_type=None,
                                       act_type=None)

        self.b_concat_1 = B.conv_block(2 * nf,
                                       nf,
                                       kernel_size=3,
                                       norm_type=None,
                                       act_type=None)
        self.b_block_1 = B.RRDB(
            nf * 2,
            kernel_size=3,
            gc=32,
            stride=1,
            bias=True,
            pad_type="zero",
            norm_type=norm_type,
            act_type=act_type,
            mode="CNA",
        )

        self.b_concat_2 = B.conv_block(2 * nf,
                                       nf,
                                       kernel_size=3,
                                       norm_type=None,
                                       act_type=None)
        self.b_block_2 = B.RRDB(
            nf * 2,
            kernel_size=3,
            gc=32,
            stride=1,
            bias=True,
            pad_type="zero",
            norm_type=norm_type,
            act_type=act_type,
            mode="CNA",
        )

        self.b_concat_3 = B.conv_block(2 * nf,
                                       nf,
                                       kernel_size=3,
                                       norm_type=None,
                                       act_type=None)
        self.b_block_3 = B.RRDB(
            nf * 2,
            kernel_size=3,
            gc=32,
            stride=1,
            bias=True,
            pad_type="zero",
            norm_type=norm_type,
            act_type=act_type,
            mode="CNA",
        )

        self.b_concat_4 = B.conv_block(2 * nf,
                                       nf,
                                       kernel_size=3,
                                       norm_type=None,
                                       act_type=None)
        self.b_block_4 = B.RRDB(
            nf * 2,
            kernel_size=3,
            gc=32,
            stride=1,
            bias=True,
            pad_type="zero",
            norm_type=norm_type,
            act_type=act_type,
            mode="CNA",
        )

        self.b_LR_conv = B.conv_block(nf,
                                      nf,
                                      kernel_size=3,
                                      norm_type=norm_type,
                                      act_type=None,
                                      mode=mode)

        if upsample_mode == "upconv":
            upsample_block = B.upconv_blcok
        elif upsample_mode == "pixelshuffle":
            upsample_block = B.pixelshuffle_block
        else:
            raise NotImplementedError(
                "upsample mode [{:s}] is not found".format(upsample_mode))
        if upscale == 3:
            b_upsampler = upsample_block(nf, nf, 3, act_type=act_type)
        else:
            b_upsampler = [
                upsample_block(nf, nf, act_type=act_type)
                for _ in range(n_upscale)
            ]

        b_HR_conv0 = B.conv_block(nf,
                                  nf,
                                  kernel_size=3,
                                  norm_type=None,
                                  act_type=act_type)
        b_HR_conv1 = B.conv_block(nf,
                                  nf,
                                  kernel_size=3,
                                  norm_type=None,
                                  act_type=None)

        self.b_module = B.sequential(*b_upsampler, b_HR_conv0, b_HR_conv1)

        self.conv_w = B.conv_block(nf,
                                   out_nc,
                                   kernel_size=1,
                                   norm_type=None,
                                   act_type=None)

        self.f_concat = B.conv_block(nf * 2,
                                     nf,
                                     kernel_size=3,
                                     norm_type=None,
                                     act_type=None)

        self.f_block = B.RRDB(
            nf * 2,
            kernel_size=3,
            gc=32,
            stride=1,
            bias=True,
            pad_type="zero",
            norm_type=norm_type,
            act_type=act_type,
            mode="CNA",
        )

        self.f_HR_conv0 = B.conv_block(nf,
                                       nf,
                                       kernel_size=3,
                                       norm_type=None,
                                       act_type=act_type)
        self.f_HR_conv1 = B.conv_block(nf,
                                       out_nc,
                                       kernel_size=3,
                                       norm_type=None,
                                       act_type=None)