File size: 3,519 Bytes
8cc9b23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from torch import tanh, Tensor
import torch.nn as nn
from omegaconf import DictConfig
from abc import ABC, abstractmethod


class BaseGenerator(ABC, nn.Module):
    def __init__(self, channels: int = 3):
        super().__init__()
        self.channels = channels

    @abstractmethod
    def forward(self, x: Tensor) -> Tensor:
        pass


class Generator(BaseGenerator):
    def __init__(self, cfg: DictConfig):
        super().__init__(cfg.channels)
        self.cfg = cfg
        self.model = self._construct_model()

    def _construct_model(self):
        initial_layer = nn.Sequential(
            nn.Conv2d(
                self.cfg.channels,
                self.cfg.num_features,
                kernel_size=7,
                stride=1,
                padding=3,
                padding_mode="reflect",
            ),
            nn.ReLU(inplace=True),
        )

        down_blocks = nn.Sequential(
            ConvBlock(
                self.cfg.num_features,
                self.cfg.num_features * 2,
                kernel_size=3,
                stride=2,
                padding=1,
            ),
            ConvBlock(
                self.cfg.num_features * 2,
                self.cfg.num_features * 4,
                kernel_size=3,
                stride=2,
                padding=1,
            ),
        )

        residual_blocks = nn.Sequential(
            *[
                ResidualBlock(self.cfg.num_features * 4)
                for _ in range(self.cfg.num_residuals)
            ]
        )

        up_blocks = nn.Sequential(
            ConvBlock(
                self.cfg.num_features * 4,
                self.cfg.num_features * 2,
                down=False,
                kernel_size=3,
                stride=2,
                padding=1,
                output_padding=1,
            ),
            ConvBlock(
                self.cfg.num_features * 2,
                self.cfg.num_features,
                down=False,
                kernel_size=3,
                stride=2,
                padding=1,
                output_padding=1,
            ),
        )

        last_layer = nn.Conv2d(
            self.cfg.num_features,
            self.cfg.channels,
            kernel_size=7,
            stride=1,
            padding=3,
            padding_mode="reflect",
        )

        return nn.Sequential(
            initial_layer, down_blocks, residual_blocks, up_blocks, last_layer
        )

    def forward(self, x: Tensor) -> Tensor:
        return tanh(self.model(x))


class ConvBlock(nn.Module):
    def __init__(
        self, in_channels, out_channels, down=True, use_activation=True, **kwargs
    ):
        super().__init__()
        self.conv = nn.Sequential(
            nn.Conv2d(in_channels, out_channels, padding_mode="reflect", **kwargs)
            if down
            else nn.ConvTranspose2d(in_channels, out_channels, **kwargs),
            nn.InstanceNorm2d(out_channels),
            nn.ReLU(inplace=True) if use_activation else nn.Identity(),
        )

    def forward(self, x: Tensor) -> Tensor:
        return self.conv(x)


class ResidualBlock(nn.Module):
    def __init__(self, channels: int):
        super().__init__()
        self.block = nn.Sequential(
            ConvBlock(channels, channels, kernel_size=3, padding=1),
            ConvBlock(
                channels, channels, use_activation=False, kernel_size=3, padding=1
            ),
        )

    def forward(self, x: Tensor) -> Tensor:
        return x + self.block(x)