Exemplo n.º 1
0
    def __init__(self,
                 inp_c,
                 oup_c,
                 code,
                 final=False,
                 blur_downsample=False,
                 fused_scale=True,
                 learn_blur=False):
        super().__init__()

        self.final = final
        self.blur_downsample = blur_downsample
        self.learn_blur = learn_blur

        self.learned_affine = AffineTransform(learnable=True,
                                              scale=oup_c if final else inp_c)

        self.in1 = nn.InstanceNorm2d(inp_c, affine=False)
        self.in2 = nn.InstanceNorm2d(oup_c, affine=False)

        self.conv1 = lreq.Conv2d(inp_c,
                                 inp_c,
                                 kernel_size=3,
                                 stride=1,
                                 padding=1)
        self.style_mapping1 = lreq.Linear(2 * inp_c, code)

        if final:
            self.fc = lreq.Linear(inp_c * 4 * 4, oup_c)
            self.style_mapping2 = lreq.Linear(oup_c, code)
        else:
            self.conv2 = lreq.Conv2d(inp_c,
                                     oup_c,
                                     kernel_size=3,
                                     stride=1,
                                     padding=1)
            self.style_mapping2 = lreq.Linear(2 * oup_c, code)

        self.act = nn.LeakyReLU(0.2)
        self.downsample = nn.AvgPool2d(2, 2)

        self.blur = Blur(inp_c)
Exemplo n.º 2
0
    def __init__(self, inputs, outputs, last=False, fused_scale=False, dense=False):
        super().__init__()
        self.conv_1 = lreq.Conv2d(inputs + (1 if last else 0), inputs, 3, 1, 1, bias=False)
        self.bias_1 = nn.Parameter(torch.Tensor(1, inputs, 1, 1))
        self.blur = BlurSimple(inputs)
        self.last = last
        self.dense_ = dense
        self.fused_scale = fused_scale
        if self.dense_:
            self.dense = lreq.Linear(inputs * 4 * 4, outputs)
        else:
            if fused_scale:
                self.conv_2 = lreq.Conv2d(inputs, outputs, 3, 2, 1, bias=False, transform_kernel=True)
            else:
                self.conv_2 = lreq.Conv2d(inputs, outputs, 3, 1, 1, bias=False)

        self.bias_2 = nn.Parameter(torch.Tensor(1, outputs, 1, 1))

        with torch.no_grad():
            self.bias_1.zero_()
            self.bias_2.zero_()
Exemplo n.º 3
0
    def __init__(self,
                 inp_c,
                 oup_c,
                 code,
                 initial=False,
                 blur_upsample=False,
                 fused_scale=True,
                 learn_blur=False,
                 scale=4):
        super().__init__()

        self.initial = initial
        self.blur_upsample = blur_upsample
        self.learn_blur = learn_blur

        # learnable affine transform to correct blur
        if self.learn_blur:
            self.learned_affine = AffineTransform(learnable=True, scale=inp_c)

        # Learnable noise coefficients
        self.B1 = set_scale(IntermediateNoise(inp_c))
        self.B2 = set_scale(IntermediateNoise(oup_c))

        # Each Ada IN contains learnable parameters A
        self.ada_in1 = AdaIN(inp_c, code)
        self.ada_in2 = AdaIN(oup_c, code)

        # In case if it is the initial block, learnable constant is created
        if self.initial:
            self.constant = nn.Parameter(torch.randn(1, inp_c, 4, 4),
                                         requires_grad=True)
        else:
            self.conv1 = lreq.Conv2d(inp_c, inp_c, kernel_size=3, padding=1)

        self.conv2 = lreq.Conv2d(inp_c, oup_c, kernel_size=3, padding=1)

        self.upsample = nn.UpsamplingBilinear2d(scale_factor=2)
        self.blur = Blur(inp_c)
        self.activation = nn.LeakyReLU(0.2)
Exemplo n.º 4
0
 def __init__(self, inp_c, oup_c):
     super(ToRGB, self).__init__()
     self.to_rgb = lreq.Conv2d(inp_c, oup_c, 1, 1, 0)
     self.upsample = nn.UpsamplingBilinear2d(scale_factor=2)
Exemplo n.º 5
0
 def __init__(self, inp_c, oup_c):
     super(FromRGB, self).__init__()
     self.from_rgb = nn.Sequential(lreq.Conv2d(inp_c, oup_c, 1, 1, 0),
                                   nn.LeakyReLU(0.2))
     self.downsample = nn.AvgPool2d(2)