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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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# 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
import warnings
from types import SimpleNamespace

import torch
from torch import nn
from torch.nn import Upsample as NearestUpsample

from imaginaire.layers import Conv2dBlock, LinearBlock, Res2dBlock
from imaginaire.generators.unit import ContentEncoder


class Generator(nn.Module):
    r"""Improved MUNIT generator.

    Args:
        gen_cfg (obj): Generator definition part of the yaml config file.
        data_cfg (obj): Data definition part of the yaml config file.
    """

    def __init__(self, gen_cfg, data_cfg):
        super().__init__()
        self.autoencoder_a = AutoEncoder(**vars(gen_cfg))
        self.autoencoder_b = AutoEncoder(**vars(gen_cfg))

    def forward(self, data, random_style=True, image_recon=True,
                latent_recon=True, cycle_recon=True, within_latent_recon=False):
        r"""In MUNIT's forward pass, it generates a content code and a style
        code from images in both domain. It then performs a within-domain
        reconstruction step and a cross-domain translation step.
        In within-domain reconstruction, it reconstructs an image using the
        content and style from the same image and optionally encodes the image
        back to the latent space.
        In cross-domain translation, it generates an translated image by mixing
        the content and style from images in different domains, and optionally
        encodes the image back to the latent space.

        Args:
            data (dict): Training data at the current iteration.
              - images_a (tensor): Images from domain A.
              - images_b (tensor): Images from domain B.
            random_style (bool): If ``True``, samples the style code from the
                prior distribution, otherwise uses the style code encoded from
                the input images in the other domain.
            image_recon (bool): If ``True``, also returns reconstructed images.
            latent_recon (bool): If ``True``, also returns reconstructed latent
                code during cross-domain translation.
            cycle_recon (bool): If ``True``, also returns cycle
                reconstructed images.
            within_latent_recon (bool): If ``True``, also returns reconstructed
                latent code during within-domain reconstruction.
        """

        images_a = data['images_a']
        images_b = data['images_b']
        net_G_output = dict()

        # encode input images into content and style code
        content_a, style_a = self.autoencoder_a.encode(images_a)
        content_b, style_b = self.autoencoder_b.encode(images_b)

        # decode (within domain)
        if image_recon:
            images_aa = self.autoencoder_a.decode(content_a, style_a)
            images_bb = self.autoencoder_b.decode(content_b, style_b)
            net_G_output.update(dict(images_aa=images_aa, images_bb=images_bb))

        # decode (cross domain)
        if random_style:  # use randomly sampled style code
            style_a_rand = torch.randn_like(style_a)
            style_b_rand = torch.randn_like(style_b)
        else:  # use style code encoded from the other domain
            style_a_rand = style_a
            style_b_rand = style_b
        images_ba = self.autoencoder_a.decode(content_b, style_a_rand)
        images_ab = self.autoencoder_b.decode(content_a, style_b_rand)

        # encode translated images into content and style code
        if latent_recon or cycle_recon:
            content_ba, style_ba = self.autoencoder_a.encode(images_ba)
            content_ab, style_ab = self.autoencoder_b.encode(images_ab)
            net_G_output.update(dict(content_ba=content_ba, style_ba=style_ba,
                                     content_ab=content_ab, style_ab=style_ab))

        # encode reconstructed images into content and style code
        if image_recon and within_latent_recon:
            content_aa, style_aa = self.autoencoder_a.encode(images_aa)
            content_bb, style_bb = self.autoencoder_b.encode(images_bb)
            net_G_output.update(dict(content_aa=content_aa, style_aa=style_aa,
                                     content_bb=content_bb, style_bb=style_bb))

        # cycle reconstruction
        if cycle_recon:
            images_aba = self.autoencoder_a.decode(content_ab, style_a)
            images_bab = self.autoencoder_b.decode(content_ba, style_b)
            net_G_output.update(
                dict(images_aba=images_aba, images_bab=images_bab))

        # required outputs
        net_G_output.update(dict(content_a=content_a, content_b=content_b,
                                 style_a=style_a, style_b=style_b,
                                 style_a_rand=style_a_rand,
                                 style_b_rand=style_b_rand,
                                 images_ba=images_ba, images_ab=images_ab))

        return net_G_output

    def inference(self, data, a2b=True, random_style=True):
        r"""MUNIT inference.

        Args:
            data (dict): Training data at the current iteration.
              - images_a (tensor): Images from domain A.
              - images_b (tensor): Images from domain B.
            a2b (bool): If ``True``, translates images from domain A to B,
                otherwise from B to A.
            random_style (bool): If ``True``, samples the style code from the
                prior distribution, otherwise uses the style code encoded from
                the input images in the other domain.
        """
        if a2b:
            input_key = 'images_a'
            content_encode = self.autoencoder_a.content_encoder
            style_encode = self.autoencoder_b.style_encoder
            decode = self.autoencoder_b.decode
        else:
            input_key = 'images_b'
            content_encode = self.autoencoder_b.content_encoder
            style_encode = self.autoencoder_a.style_encoder
            decode = self.autoencoder_a.decode

        content_images = data[input_key]
        content = content_encode(content_images)
        if random_style:
            style_channels = self.autoencoder_a.style_channels
            style = torch.randn(content.size(0), style_channels, 1, 1,
                                device=torch.device('cuda'))
            file_names = data['key'][input_key]['filename']
        else:
            style_key = 'images_b' if a2b else 'images_a'
            assert style_key in data.keys(), \
                "{} must be provided when 'random_style' " \
                "is set to False".format(style_key)
            style_images = data[style_key]
            style = style_encode(style_images)
            file_names = \
                [content_name + '_style_' + style_name
                 for content_name, style_name in
                    zip(data['key'][input_key]['filename'],
                        data['key'][style_key]['filename'])]

        output_images = decode(content, style)
        return output_images, file_names


class AutoEncoder(nn.Module):
    r"""Improved MUNIT autoencoder.

    Args:
        num_filters (int): Base filter numbers.
        max_num_filters (int): Maximum number of filters in the encoder.
        num_filters_mlp (int): Base filter number in the MLP module.
        latent_dim (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_style (int): Number of times we reduce
            resolution by 2x2 for the style image.
        num_downsamples_content (int): Number of times we reduce
            resolution by 2x2 for the content image.
        num_image_channels (int): Number of input image channels.
        content_norm_type (str): Type of activation normalization in the
            content encoder.
        style_norm_type (str): Type of activation normalization in the
            style encoder.
        decoder_norm_type (str): Type of activation normalization in the
            decoder.
        weight_norm_type (str): Type of weight normalization.
        decoder_norm_params (obj): Parameters of activation normalization in the
            decoder. If not ``None``, decoder_norm_params.__dict__ will be used
            as keyword arguments when initializing activation normalization.
        output_nonlinearity (str): Type of nonlinearity before final output,
            ``'tanh'`` or ``'none'``.
        pre_act (bool): If ``True``, uses pre-activation residual blocks.
        apply_noise (bool): If ``True``, injects Gaussian noise in the decoder.
    """

    def __init__(self,
                 num_filters=64,
                 max_num_filters=256,
                 num_filters_mlp=256,
                 latent_dim=8,
                 num_res_blocks=4,
                 num_mlp_blocks=2,
                 num_downsamples_style=4,
                 num_downsamples_content=2,
                 num_image_channels=3,
                 content_norm_type='instance',
                 style_norm_type='',
                 decoder_norm_type='instance',
                 weight_norm_type='',
                 decoder_norm_params=SimpleNamespace(affine=False),
                 output_nonlinearity='',
                 pre_act=False,
                 apply_noise=False,
                 **kwargs):
        super().__init__()
        for key in kwargs:
            if key != 'type':
                warnings.warn(
                    "Generator argument '{}' is not used.".format(key))
        self.style_encoder = StyleEncoder(num_downsamples_style,
                                          num_image_channels,
                                          num_filters,
                                          latent_dim,
                                          'reflect',
                                          style_norm_type,
                                          weight_norm_type,
                                          'relu')
        self.content_encoder = ContentEncoder(num_downsamples_content,
                                              num_res_blocks,
                                              num_image_channels,
                                              num_filters,
                                              max_num_filters,
                                              'reflect',
                                              content_norm_type,
                                              weight_norm_type,
                                              'relu',
                                              pre_act)
        self.decoder = Decoder(num_downsamples_content,
                               num_res_blocks,
                               self.content_encoder.output_dim,
                               num_image_channels,
                               num_filters_mlp,
                               'reflect',
                               decoder_norm_type,
                               decoder_norm_params,
                               weight_norm_type,
                               'relu',
                               output_nonlinearity,
                               pre_act,
                               apply_noise)
        self.mlp = MLP(latent_dim,
                       num_filters_mlp,
                       num_filters_mlp,
                       num_mlp_blocks,
                       'none',
                       'relu')
        self.style_channels = latent_dim

    def forward(self, images):
        r"""Reconstruct an image.

        Args:
            images (Tensor): Input images.
        Returns:
            images_recon (Tensor): Reconstructed images.
        """
        content, style = self.encode(images)
        images_recon = self.decode(content, style)
        return images_recon

    def encode(self, images):
        r"""Encode an image to content and style code.

        Args:
            images (Tensor): Input images.
        Returns:
            (tuple):
              - content (Tensor): Content code.
              - style (Tensor): Style code.
        """
        style = self.style_encoder(images)
        content = self.content_encoder(images)
        return content, style

    def decode(self, content, style):
        r"""Decode content and style code to an image.

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


class StyleEncoder(nn.Module):
    r"""MUNIT style encoder.

    Args:
        num_downsamples (int): Number of times we reduce
            resolution by 2x2.
        num_image_channels (int): Number of input image channels.
        num_filters (int): Base filter numbers.
        style_channels (int): Dimension of the style code.
        padding_mode (string): Type of padding.
        activation_norm_type (str): Type of activation normalization.
        weight_norm_type (str): Type of weight normalization.
        nonlinearity (str): Type of nonlinear activation function.
    """

    def __init__(self, num_downsamples, num_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(num_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 Decoder(nn.Module):
    r"""Improved MUNIT decoder. The network consists of

    - $(num_res_blocks) residual blocks.
    - $(num_upsamples) residual blocks or convolutional blocks
    - output layer.

    Args:
        num_upsamples (int): Number of times we increase resolution by 2x2.
        num_res_blocks (int): Number of residual blocks.
        num_filters (int): Base filter numbers.
        num_image_channels (int): Number of input image channels.
        style_channels (int): Dimension of the style code.
        padding_mode (string): Type of padding.
        activation_norm_type (str): Type of activation normalization.
        activation_norm_params (obj): Parameters of activation normalization.
            If not ``None``, decoder_norm_params.__dict__ will be used
            as keyword arguments when initializing activation normalization.
        weight_norm_type (str): Type of weight normalization.
        nonlinearity (str): Type of nonlinear activation function.
        output_nonlinearity (str): Type of nonlinearity before final output,
            ``'tanh'`` or ``'none'``.
        pre_act (bool): If ``True``, uses pre-activation residual blocks.
        apply_noise (bool): If ``True``, injects Gaussian noise.
    """

    def __init__(self,
                 num_upsamples,
                 num_res_blocks,
                 num_filters,
                 num_image_channels,
                 style_channels,
                 padding_mode,
                 activation_norm_type,
                 activation_norm_params,
                 weight_norm_type,
                 nonlinearity,
                 output_nonlinearity,
                 pre_act=False,
                 apply_noise=False):
        super().__init__()
        adain_params = SimpleNamespace(
            activation_norm_type=activation_norm_type,
            activation_norm_params=activation_norm_params,
            cond_dims=style_channels)
        conv_params = dict(padding_mode=padding_mode,
                           nonlinearity=nonlinearity,
                           inplace_nonlinearity=True,
                           apply_noise=apply_noise,
                           weight_norm_type=weight_norm_type,
                           activation_norm_type='adaptive',
                           activation_norm_params=adain_params)

        # The order of operations in residual blocks.
        order = 'pre_act' if pre_act else 'CNACNA'

        # Residual blocks with AdaIN.
        self.decoder = nn.ModuleList()
        for _ in range(num_res_blocks):
            self.decoder += [Res2dBlock(num_filters, num_filters,
                                        **conv_params,
                                        order=order)]

        # Convolutional blocks with upsampling.
        for i in range(num_upsamples):
            self.decoder += [NearestUpsample(scale_factor=2)]
            self.decoder += [Conv2dBlock(num_filters, num_filters // 2,
                                         5, 1, 2, **conv_params)]
            num_filters //= 2
        self.decoder += [Conv2dBlock(num_filters, num_image_channels, 7, 1, 3,
                                     nonlinearity=output_nonlinearity,
                                     padding_mode=padding_mode)]

    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 MLP(nn.Module):
    r"""The multi-layer perceptron (MLP) that maps Gaussian style code to a
    feature vector that is given as the conditional input to AdaIN.

    Args:
        input_dim (int): Number of channels in the input tensor.
        output_dim (int): Number of channels in the output tensor.
        latent_dim (int): Number of channels in the latent features.
        num_layers (int): Number of layers in the MLP.
        norm (str): Type of activation normalization.
        nonlinearity (str): Type of nonlinear activation function.
    """

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

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

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