-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcyclegan.py
118 lines (98 loc) · 3.82 KB
/
cyclegan.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
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
import torch
import torch.nn as nn
def weights_init(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
nn.init.normal_(m.weight.data, 0.0, 0.02)
if hasattr(m, "bias") and m.bias is not None:
torch.nn.init.constant_(m.bias.data, 0.0)
elif classname.find('BatchNorm2d') != -1:
nn.init.normal_(m.weight.data, 1.0, 0.02)
nn.init.constant_(m.bias.data, 0.0)
class ResidualBlock(nn.Module):
def __init__(self, features: int, kernel_size: int) -> None:
super().__init__()
self.reflectpad = nn.ReflectionPad2d(1)
self.conv = nn.Conv2d(features, features, kernel_size)
self.instnorm = nn.InstanceNorm2d(features)
self.relu = nn.ReLU(inplace=True)
def block(self, x):
x = self.reflectpad(x)
x = self.conv(x)
x = self.instnorm(x)
x = self.relu(x)
x = self.reflectpad(x)
x = self.conv(x)
x = self.instnorm(x)
return x
def forward(self, x: torch.Tensor):
return x + self.block(x)
class SamplingBlock(nn.Module):
def __init__(self, in_features: int, out_features: int, kernel_size: int, stride: int, padding: int) -> None:
super().__init__()
self.conv = nn.Conv2d(in_features, out_features, kernel_size=kernel_size, stride=stride, padding=padding)
self.instnorm = nn.InstanceNorm2d(out_features)
self.relu = nn.ReLU(inplace=True)
def forward(self, x: torch.Tensor):
x = self.conv(x)
x = self.instnorm(x)
x = self.relu(x)
return x
class Generator(nn.Module):
def __init__(self, input_shape: int) -> None:
super().__init__()
self.model = nn.Sequential(
# Encoder
nn.ReflectionPad2d(input_shape),
nn.Conv2d(input_shape, 64, kernel_size=7),
nn.InstanceNorm2d(64),
nn.ReLU(inplace=True),
SamplingBlock(64, 128, 3, 2, 1),
SamplingBlock(128, 256, 3, 2, 1),
# Transformer/9 blocks
ResidualBlock(256, 3),
ResidualBlock(256, 3),
ResidualBlock(256, 3),
ResidualBlock(256, 3),
ResidualBlock(256, 3),
ResidualBlock(256, 3),
ResidualBlock(256, 3),
ResidualBlock(256, 3),
ResidualBlock(256, 3),
# Decoder/transpose conv
nn.Upsample(scale_factor=2),
SamplingBlock(256, 128, 3, 1, 1),
nn.Upsample(scale_factor=2),
SamplingBlock(128, 64, 3, 1, 1),
nn.ReflectionPad2d(input_shape),
nn.Conv2d(64, input_shape, kernel_size=7),
nn.Tanh(),
)
def forward(self, x):
return self.model(x)
class DiscBlock(nn.Module):
def __init__(self, in_features: int, out_features: int, kernel_size: int, stride: int, padding: int, norm=True) -> None:
super().__init__()
self.norm = norm
self.conv = nn.Conv2d(in_features, out_features, kernel_size=kernel_size, stride=stride, padding=padding)
self.instnorm = nn.InstanceNorm2d(out_features)
self.leakyrelu = nn.LeakyReLU(0.2, inplace=True)
def forward(self, x: torch.Tensor):
x = self.conv(x)
if self.norm:
x = self.instnorm(x)
x = self.leakyrelu(x)
return x
class Discriminator(nn.Module):
def __init__(self, input_shape: int) -> None:
super().__init__()
self.model = nn.Sequential(
DiscBlock(input_shape, 64, 4, 2, 1, norm=False),
DiscBlock(64, 128, 4, 2, 1),
DiscBlock(128, 256, 4, 2, 1),
DiscBlock(256, 512, 4, 2, 1),
nn.ZeroPad2d((1, 0, 1, 0)), # 4,1,16,16 // 4,1,15,15
nn.Conv2d(512, 1, kernel_size=4, padding=1)
)
def forward(self, x: torch.Tensor):
return self.model(x)