File size: 15,469 Bytes
f670afc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# Copyright (C) 2021 NVIDIA CORPORATION & AFFILIATES.  All rights reserved.
#
# This work is made available under the Nvidia Source Code License-NC.
# To view a copy of this license, check out LICENSE.md
from functools import partial
from types import SimpleNamespace

import torch
from torch import nn

from imaginaire.layers import \
    (Conv2dBlock, LinearBlock, Res2dBlock, UpRes2dBlock)


class Generator(nn.Module):
    r"""Generator of the improved FUNIT baseline in the COCO-FUNIT paper.
    """

    def __init__(self, gen_cfg, data_cfg):
        super().__init__()
        self.generator = FUNITTranslator(**vars(gen_cfg))

    def forward(self, data):
        r"""In the FUNIT's forward pass, it generates a content embedding and
        a style code from the content image, and a style code from the style
        image. By mixing the content code and the style code from the content
        image, we reconstruct the input image. By mixing the content code and
        the style code from the style image, we have a translation output.

        Args:
            data (dict): Training data at the current iteration.
        """
        content_a = self.generator.content_encoder(data['images_content'])
        style_a = self.generator.style_encoder(data['images_content'])
        style_b = self.generator.style_encoder(data['images_style'])
        images_trans = self.generator.decode(content_a, style_b)
        images_recon = self.generator.decode(content_a, style_a)

        net_G_output = dict(images_trans=images_trans,
                            images_recon=images_recon)
        return net_G_output

    def inference(self, data, keep_original_size=True):
        r"""COCO-FUNIT inference.

        Args:
            data (dict): Training data at the current iteration.
              - images_content (tensor): Content images.
              - images_style (tensor): Style images.
            a2b (bool): If ``True``, translates images from domain A to B,
                otherwise from B to A.
            keep_original_size (bool): If ``True``, output image is resized
            to the input content image size.
        """
        content_a = self.generator.content_encoder(data['images_content'])
        style_b = self.generator.style_encoder(data['images_style'])
        output_images = self.generator.decode(content_a, style_b)
        if keep_original_size:
            height = data['original_h_w'][0][0]
            width = data['original_h_w'][0][1]
            # print('( H, W) = ( %d, %d)' % (height, width))
            output_images = torch.nn.functional.interpolate(
                output_images, size=[height, width])
        file_names = data['key']['images_content'][0]
        return output_images, file_names


class FUNITTranslator(nn.Module):
    r"""

    Args:
         num_filters (int): Base filter numbers.
         num_filters_mlp (int): Base filter number in the MLP module.
         style_dims (int): Dimension of the style code.
         num_res_blocks (int): Number of residual blocks at the end of the
            content encoder.
         num_mlp_blocks (int): Number of layers in the MLP module.
         num_downsamples_content (int): Number of times we reduce
            resolution by 2x2 for the content image.
         num_downsamples_style (int): Number of times we reduce
            resolution by 2x2 for the style image.
         num_image_channels (int): Number of input image channels.
         weight_norm_type (str): Type of weight normalization.
             ``'none'``, ``'spectral'``, or ``'weight'``.
    """

    def __init__(self,
                 num_filters=64,
                 num_filters_mlp=256,
                 style_dims=64,
                 num_res_blocks=2,
                 num_mlp_blocks=3,
                 num_downsamples_style=4,
                 num_downsamples_content=2,
                 num_image_channels=3,
                 weight_norm_type='',
                 **kwargs):
        super().__init__()

        self.style_encoder = StyleEncoder(num_downsamples_style,
                                          num_image_channels,
                                          num_filters,
                                          style_dims,
                                          'reflect',
                                          'none',
                                          weight_norm_type,
                                          'relu')

        self.content_encoder = ContentEncoder(num_downsamples_content,
                                              num_res_blocks,
                                              num_image_channels,
                                              num_filters,
                                              'reflect',
                                              'instance',
                                              weight_norm_type,
                                              'relu')

        self.decoder = Decoder(self.content_encoder.output_dim,
                               num_filters_mlp,
                               num_image_channels,
                               num_downsamples_content,
                               'reflect',
                               weight_norm_type,
                               'relu')

        self.mlp = MLP(style_dims,
                       num_filters_mlp,
                       num_filters_mlp,
                       num_mlp_blocks,
                       'none',
                       'relu')

    def forward(self, images):
        r"""Reconstruct the input image by combining the computer content and
        style code.

        Args:
            images (tensor): Input image tensor.
        """
        # reconstruct an image
        content, style = self.encode(images)
        images_recon = self.decode(content, style)
        return images_recon

    def encode(self, images):
        r"""Encoder images to get their content and style codes.

        Args:
            images (tensor): Input image tensor.
        """
        style = self.style_encoder(images)
        content = self.content_encoder(images)
        return content, style

    def decode(self, content, style):
        r"""Generate images by combining their content and style codes.

        Args:
            content (tensor): Content code tensor.
            style (tensor): Style code tensor.
        """
        style = self.mlp(style)
        images = self.decoder(content, style)
        return images


class Decoder(nn.Module):
    r"""Improved FUNIT decoder.

    Args:
        num_enc_output_channels (int): Number of content feature channels.
        style_channels (int): Dimension of the style code.
        num_image_channels (int): Number of image channels.
        num_upsamples (int): How many times we are going to apply
            upsample residual block.
    """

    def __init__(self,
                 num_enc_output_channels,
                 style_channels,
                 num_image_channels=3,
                 num_upsamples=4,
                 padding_type='reflect',
                 weight_norm_type='none',
                 nonlinearity='relu'):
        super(Decoder, self).__init__()
        adain_params = SimpleNamespace(
            activation_norm_type='instance',
            activation_norm_params=SimpleNamespace(affine=False),
            cond_dims=style_channels)

        base_res_block = partial(Res2dBlock,
                                 kernel_size=3,
                                 padding=1,
                                 padding_mode=padding_type,
                                 nonlinearity=nonlinearity,
                                 activation_norm_type='adaptive',
                                 activation_norm_params=adain_params,
                                 weight_norm_type=weight_norm_type,
                                 learn_shortcut=False)

        base_up_res_block = partial(UpRes2dBlock,
                                    kernel_size=5,
                                    padding=2,
                                    padding_mode=padding_type,
                                    weight_norm_type=weight_norm_type,
                                    activation_norm_type='adaptive',
                                    activation_norm_params=adain_params,
                                    skip_activation_norm='instance',
                                    skip_nonlinearity=nonlinearity,
                                    nonlinearity=nonlinearity,
                                    hidden_channels_equal_out_channels=True,
                                    learn_shortcut=True)

        dims = num_enc_output_channels

        # Residual blocks with AdaIN.
        self.decoder = nn.ModuleList()
        self.decoder += [base_res_block(dims, dims)]
        self.decoder += [base_res_block(dims, dims)]
        for _ in range(num_upsamples):
            self.decoder += [base_up_res_block(dims, dims // 2)]
            dims = dims // 2
        self.decoder += [Conv2dBlock(dims,
                                     num_image_channels,
                                     kernel_size=7,
                                     stride=1,
                                     padding=3,
                                     padding_mode='reflect',
                                     nonlinearity='tanh')]

    def forward(self, x, style):
        r"""

        Args:
            x (tensor): Content embedding of the content image.
            style (tensor): Style embedding of the style image.
        """
        for block in self.decoder:
            if getattr(block, 'conditional', False):
                x = block(x, style)
            else:
                x = block(x)
        return x


class StyleEncoder(nn.Module):
    r"""Improved FUNIT Style Encoder. This is basically the same as the
    original FUNIT Style Encoder.

    Args:
        num_downsamples (int): Number of times we reduce resolution by
            2x2.
        image_channels (int): Number of input image channels.
        num_filters (int): Base filter number.
        style_channels (int): Style code dimension.
        padding_mode (str): Padding mode.
        activation_norm_type (str): Type of activation normalization.
        weight_norm_type (str): Type of weight normalization.
            ``'none'``, ``'spectral'``, or ``'weight'``.
        nonlinearity (str): Nonlinearity.
    """

    def __init__(self,
                 num_downsamples,
                 image_channels,
                 num_filters,
                 style_channels,
                 padding_mode,
                 activation_norm_type,
                 weight_norm_type,
                 nonlinearity):
        super().__init__()
        conv_params = dict(padding_mode=padding_mode,
                           activation_norm_type=activation_norm_type,
                           weight_norm_type=weight_norm_type,
                           nonlinearity=nonlinearity,
                           inplace_nonlinearity=True)
        model = []
        model += [Conv2dBlock(image_channels, num_filters, 7, 1, 3,
                              **conv_params)]
        for i in range(2):
            model += [Conv2dBlock(num_filters, 2 * num_filters, 4, 2, 1,
                                  **conv_params)]
            num_filters *= 2
        for i in range(num_downsamples - 2):
            model += [Conv2dBlock(num_filters, num_filters, 4, 2, 1,
                                  **conv_params)]
        model += [nn.AdaptiveAvgPool2d(1)]
        model += [nn.Conv2d(num_filters, style_channels, 1, 1, 0)]
        self.model = nn.Sequential(*model)
        self.output_dim = num_filters

    def forward(self, x):
        r"""

        Args:
            x (tensor): Input image.
        """
        return self.model(x)


class ContentEncoder(nn.Module):
    r"""Improved FUNIT Content Encoder. This is basically the same as the
    original FUNIT content encoder.

    Args:
        num_downsamples (int): Number of times we reduce resolution by
           2x2.
        num_res_blocks (int): Number of times we append residual block
           after all the downsampling modules.
        image_channels (int): Number of input image channels.
        num_filters (int): Base filter number.
        padding_mode (str): Padding mode
        activation_norm_type (str): Type of activation normalization.
        weight_norm_type (str): Type of weight normalization.
            ``'none'``, ``'spectral'``, or ``'weight'``.
        nonlinearity (str): Nonlinearity.
    """

    def __init__(self,
                 num_downsamples,
                 num_res_blocks,
                 image_channels,
                 num_filters,
                 padding_mode,
                 activation_norm_type,
                 weight_norm_type,
                 nonlinearity):
        super().__init__()
        conv_params = dict(padding_mode=padding_mode,
                           activation_norm_type=activation_norm_type,
                           weight_norm_type=weight_norm_type,
                           nonlinearity=nonlinearity,
                           inplace_nonlinearity=True,
                           order='CNACNA')
        model = []
        model += [Conv2dBlock(image_channels, num_filters, 7, 1, 3,
                              **conv_params)]
        dims = num_filters
        for i in range(num_downsamples):
            model += [Conv2dBlock(dims, dims * 2, 4, 2, 1, **conv_params)]
            dims *= 2

        for _ in range(num_res_blocks):
            model += [Res2dBlock(dims, dims, learn_shortcut=False, **conv_params)]
        self.model = nn.Sequential(*model)
        self.output_dim = dims

    def forward(self, x):
        r"""

        Args:
            x (tensor): Input image.
        """
        return self.model(x)


class MLP(nn.Module):
    r"""Improved FUNIT style decoder.

    Args:
        input_dim (int): Input dimension (style code dimension).
        output_dim (int): Output dimension (to be fed into the AdaIN
           layer).
        latent_dim (int): Latent dimension.
        num_layers (int): Number of layers in the MLP.
        activation_norm_type (str): Activation type.
        nonlinearity (str): Nonlinearity type.
    """

    def __init__(self,
                 input_dim,
                 output_dim,
                 latent_dim,
                 num_layers,
                 activation_norm_type,
                 nonlinearity):
        super().__init__()
        model = []
        model += [LinearBlock(input_dim, latent_dim,
                              activation_norm_type=activation_norm_type,
                              nonlinearity=nonlinearity)]
        # changed from num_layers - 2 to num_layers - 3.
        for i in range(num_layers - 3):
            model += [LinearBlock(latent_dim, latent_dim,
                                  activation_norm_type=activation_norm_type,
                                  nonlinearity=nonlinearity)]
        model += [LinearBlock(latent_dim, output_dim,
                              activation_norm_type=activation_norm_type,
                              nonlinearity=nonlinearity)]
        self.model = nn.Sequential(*model)

    def forward(self, x):
        r"""

        Args:
            x (tensor): Input tensor.
        """
        return self.model(x.view(x.size(0), -1))