File size: 2,394 Bytes
7e4e601
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import torch.nn as nn
from models.conv_blocks import InvertedResBlock
from models.conv_blocks import ConvBlock
from models.conv_blocks import UpConvLNormLReLU
from utils.common import initialize_weights


class GeneratorV2(nn.Module):
    def __init__(self, dataset=''):
        super(GeneratorV2, self).__init__()
        self.name = f'{self.__class__.__name__}_{dataset}'
        bias = False

        self.conv_block1 = nn.Sequential(
            ConvBlock(3, 32, kernel_size=7, stride=1, norm_type="layer", bias=bias),
            ConvBlock(32, 64, kernel_size=3, stride=2, norm_type="layer", bias=bias),
            ConvBlock(64, 64, kernel_size=3, stride=1, norm_type="layer", bias=bias),
        )

        self.conv_block2 = nn.Sequential(
            ConvBlock(64, 128, kernel_size=3, stride=2, norm_type="layer", bias=bias),
            ConvBlock(128, 128, kernel_size=3, stride=1, norm_type="layer", bias=bias),
        )

        self.res_blocks = nn.Sequential(
            ConvBlock(128, 128, kernel_size=3, stride=1, norm_type="layer", bias=bias),
            InvertedResBlock(128, 256, expand_ratio=2, norm_type="layer", bias=bias),
            InvertedResBlock(256, 256, expand_ratio=2, norm_type="layer", bias=bias),
            InvertedResBlock(256, 256, expand_ratio=2, norm_type="layer", bias=bias),
            InvertedResBlock(256, 256, expand_ratio=2, norm_type="layer", bias=bias),
            ConvBlock(256, 128, kernel_size=3, stride=1, norm_type="layer", bias=bias),
        )

        self.upsample1 = nn.Sequential(
            UpConvLNormLReLU(128, 128),
            ConvBlock(128, 128, kernel_size=3, stride=1, norm_type="layer", bias=bias),
        )

        self.upsample2 = nn.Sequential(
            UpConvLNormLReLU(128, 64),
            ConvBlock(64, 64, kernel_size=3, stride=1, norm_type="layer", bias=bias),
            ConvBlock(64, 32, kernel_size=7, stride=1, norm_type="layer", bias=bias),
        )

        self.decode_blocks = nn.Sequential(
            nn.Conv2d(32, 3, kernel_size=1, stride=1, padding=0, bias=bias),
            nn.Tanh(),
        )

        initialize_weights(self)

    def forward(self, x):
        out = self.conv_block1(x)
        out = self.conv_block2(out)
        out = self.res_blocks(out)
        out = self.upsample1(out)
        out = self.upsample2(out)
        img = self.decode_blocks(out)

        return img