File size: 7,887 Bytes
5f6152d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import torch

from gfpgan.archs.gfpganv1_arch import FacialComponentDiscriminator, GFPGANv1, StyleGAN2GeneratorSFT
from gfpgan.archs.gfpganv1_clean_arch import GFPGANv1Clean, StyleGAN2GeneratorCSFT


def test_stylegan2generatorsft():
    """Test arch: StyleGAN2GeneratorSFT."""

    # model init and forward (gpu)
    if torch.cuda.is_available():
        net = StyleGAN2GeneratorSFT(
            out_size=32,
            num_style_feat=512,
            num_mlp=8,
            channel_multiplier=1,
            resample_kernel=(1, 3, 3, 1),
            lr_mlp=0.01,
            narrow=1,
            sft_half=False).cuda().eval()
        style = torch.rand((1, 512), dtype=torch.float32).cuda()
        condition1 = torch.rand((1, 512, 8, 8), dtype=torch.float32).cuda()
        condition2 = torch.rand((1, 512, 16, 16), dtype=torch.float32).cuda()
        condition3 = torch.rand((1, 512, 32, 32), dtype=torch.float32).cuda()
        conditions = [condition1, condition1, condition2, condition2, condition3, condition3]
        output = net([style], conditions)
        assert output[0].shape == (1, 3, 32, 32)
        assert output[1] is None

        # -------------------- with return_latents ----------------------- #
        output = net([style], conditions, return_latents=True)
        assert output[0].shape == (1, 3, 32, 32)
        assert len(output[1]) == 1
        # check latent
        assert output[1][0].shape == (8, 512)

        # -------------------- with randomize_noise = False ----------------------- #
        output = net([style], conditions, randomize_noise=False)
        assert output[0].shape == (1, 3, 32, 32)
        assert output[1] is None

        # -------------------- with truncation = 0.5 and mixing----------------------- #
        output = net([style, style], conditions, truncation=0.5, truncation_latent=style)
        assert output[0].shape == (1, 3, 32, 32)
        assert output[1] is None


def test_gfpganv1():
    """Test arch: GFPGANv1."""

    # model init and forward (gpu)
    if torch.cuda.is_available():
        net = GFPGANv1(
            out_size=32,
            num_style_feat=512,
            channel_multiplier=1,
            resample_kernel=(1, 3, 3, 1),
            decoder_load_path=None,
            fix_decoder=True,
            # for stylegan decoder
            num_mlp=8,
            lr_mlp=0.01,
            input_is_latent=False,
            different_w=False,
            narrow=1,
            sft_half=True).cuda().eval()
        img = torch.rand((1, 3, 32, 32), dtype=torch.float32).cuda()
        output = net(img)
        assert output[0].shape == (1, 3, 32, 32)
        assert len(output[1]) == 3
        # check out_rgbs for intermediate loss
        assert output[1][0].shape == (1, 3, 8, 8)
        assert output[1][1].shape == (1, 3, 16, 16)
        assert output[1][2].shape == (1, 3, 32, 32)

        # -------------------- with different_w = True ----------------------- #
        net = GFPGANv1(
            out_size=32,
            num_style_feat=512,
            channel_multiplier=1,
            resample_kernel=(1, 3, 3, 1),
            decoder_load_path=None,
            fix_decoder=True,
            # for stylegan decoder
            num_mlp=8,
            lr_mlp=0.01,
            input_is_latent=False,
            different_w=True,
            narrow=1,
            sft_half=True).cuda().eval()
        img = torch.rand((1, 3, 32, 32), dtype=torch.float32).cuda()
        output = net(img)
        assert output[0].shape == (1, 3, 32, 32)
        assert len(output[1]) == 3
        # check out_rgbs for intermediate loss
        assert output[1][0].shape == (1, 3, 8, 8)
        assert output[1][1].shape == (1, 3, 16, 16)
        assert output[1][2].shape == (1, 3, 32, 32)


def test_facialcomponentdiscriminator():
    """Test arch: FacialComponentDiscriminator."""

    # model init and forward (gpu)
    if torch.cuda.is_available():
        net = FacialComponentDiscriminator().cuda().eval()
        img = torch.rand((1, 3, 32, 32), dtype=torch.float32).cuda()
        output = net(img)
        assert len(output) == 2
        assert output[0].shape == (1, 1, 8, 8)
        assert output[1] is None

        # -------------------- return intermediate features ----------------------- #
        output = net(img, return_feats=True)
        assert len(output) == 2
        assert output[0].shape == (1, 1, 8, 8)
        assert len(output[1]) == 2
        assert output[1][0].shape == (1, 128, 16, 16)
        assert output[1][1].shape == (1, 256, 8, 8)


def test_stylegan2generatorcsft():
    """Test arch: StyleGAN2GeneratorCSFT."""

    # model init and forward (gpu)
    if torch.cuda.is_available():
        net = StyleGAN2GeneratorCSFT(
            out_size=32, num_style_feat=512, num_mlp=8, channel_multiplier=1, narrow=1, sft_half=False).cuda().eval()
        style = torch.rand((1, 512), dtype=torch.float32).cuda()
        condition1 = torch.rand((1, 512, 8, 8), dtype=torch.float32).cuda()
        condition2 = torch.rand((1, 512, 16, 16), dtype=torch.float32).cuda()
        condition3 = torch.rand((1, 512, 32, 32), dtype=torch.float32).cuda()
        conditions = [condition1, condition1, condition2, condition2, condition3, condition3]
        output = net([style], conditions)
        assert output[0].shape == (1, 3, 32, 32)
        assert output[1] is None

        # -------------------- with return_latents ----------------------- #
        output = net([style], conditions, return_latents=True)
        assert output[0].shape == (1, 3, 32, 32)
        assert len(output[1]) == 1
        # check latent
        assert output[1][0].shape == (8, 512)

        # -------------------- with randomize_noise = False ----------------------- #
        output = net([style], conditions, randomize_noise=False)
        assert output[0].shape == (1, 3, 32, 32)
        assert output[1] is None

        # -------------------- with truncation = 0.5 and mixing----------------------- #
        output = net([style, style], conditions, truncation=0.5, truncation_latent=style)
        assert output[0].shape == (1, 3, 32, 32)
        assert output[1] is None


def test_gfpganv1clean():
    """Test arch: GFPGANv1Clean."""

    # model init and forward (gpu)
    if torch.cuda.is_available():
        net = GFPGANv1Clean(
            out_size=32,
            num_style_feat=512,
            channel_multiplier=1,
            decoder_load_path=None,
            fix_decoder=True,
            # for stylegan decoder
            num_mlp=8,
            input_is_latent=False,
            different_w=False,
            narrow=1,
            sft_half=True).cuda().eval()

        img = torch.rand((1, 3, 32, 32), dtype=torch.float32).cuda()
        output = net(img)
        assert output[0].shape == (1, 3, 32, 32)
        assert len(output[1]) == 3
        # check out_rgbs for intermediate loss
        assert output[1][0].shape == (1, 3, 8, 8)
        assert output[1][1].shape == (1, 3, 16, 16)
        assert output[1][2].shape == (1, 3, 32, 32)

        # -------------------- with different_w = True ----------------------- #
        net = GFPGANv1Clean(
            out_size=32,
            num_style_feat=512,
            channel_multiplier=1,
            decoder_load_path=None,
            fix_decoder=True,
            # for stylegan decoder
            num_mlp=8,
            input_is_latent=False,
            different_w=True,
            narrow=1,
            sft_half=True).cuda().eval()
        img = torch.rand((1, 3, 32, 32), dtype=torch.float32).cuda()
        output = net(img)
        assert output[0].shape == (1, 3, 32, 32)
        assert len(output[1]) == 3
        # check out_rgbs for intermediate loss
        assert output[1][0].shape == (1, 3, 8, 8)
        assert output[1][1].shape == (1, 3, 16, 16)
        assert output[1][2].shape == (1, 3, 32, 32)