-
Notifications
You must be signed in to change notification settings - Fork 0
/
pix2pixhd_generator.py
77 lines (67 loc) · 2.4 KB
/
pix2pixhd_generator.py
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
import torch.nn as nn
from residual_block import ResidualBlock
from global_generator import GlobalGenerator
class Pix2PixHDGenerator(nn.Module):
def __init__(
self,
in_chan,
out_chan,
base_chan=32,
global_frontend_blocks=3,
global_residual_blocks=9,
local_residual_blocks=3,
):
super().__init__()
global_base_chan = 2 * base_chan
self.downsample = nn.AvgPool2d(3, stride=2, padding=1, count_include_pad=False)
# Global Generator without final output layer.
self.g1 = GlobalGenerator(
in_chan,
out_chan,
global_base_chan,
global_frontend_blocks,
global_residual_blocks,
).g1
self.g2 = nn.ModuleList()
# Local Enhancer
# Frontend Block
self.g2.append(
nn.Sequential(
nn.ReflectionPad2d(3),
nn.Conv2d(in_chan, base_chan, kernel_size=7, padding=0),
nn.InstanceNorm2d(base_chan, affine=False),
nn.ReLU(inplace=True),
nn.Conv2d(base_chan, 2 * base_chan, kernel_size=3, stride=2, padding=1),
nn.InstanceNorm2d(2 * base_chan, affine=False),
nn.ReLU(inplace=True),
)
)
self.g2.append(
nn.Sequential(
# Residual Blocks
*[ResidualBlock(2 * base_chan) for _ in range(local_residual_blocks)],
# Backend Blocks
nn.ConvTranspose2d(
2 * base_chan,
base_chan,
kernel_size=3,
stride=2,
padding=1,
output_padding=1,
),
nn.InstanceNorm2d(base_chan, affine=False),
nn.ReLU(inplace=True),
# Output Convolutional Layer
nn.ReflectionPad2d(3),
nn.Conv2d(base_chan, out_chan, kernel_size=7, padding=0),
nn.Tanh(),
)
)
def forward(self, x):
x_g1 = self.downsample(x)
# Output Features of Global Generator
x_g1 = self.g1(x_g1)
# Local Enhancer's Encoding of High Res input image
x_g2 = self.g2[0](x)
# Element wise sum of both
return self.g2[1](x_g1 + x_g2)