-
Notifications
You must be signed in to change notification settings - Fork 3
/
model_cartoongan.py
124 lines (99 loc) · 3.58 KB
/
model_cartoongan.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
119
120
121
122
123
124
import torch
from torch import nn
import torchvision
class Encoder1(nn.Module):
def __init__(self):
super(Encoder1, self).__init__()
self.layers = nn.Sequential(
nn.Conv2d(3, 64, 7, 1, 3, bias=False),
nn.InstanceNorm2d(64),
nn.ReLU(inplace=True)
)
def forward(self, x):
return self.layers(x)
class Encoder2(nn.Module):
def __init__(self, channel_input):
super(Encoder2, self).__init__()
self.layers = nn.Sequential(
nn.Conv2d(channel_input, channel_input * 2, 3, 2, 1),
nn.Conv2d(channel_input * 2, channel_input * 2, 3, 1, 1, bias=False),
nn.InstanceNorm2d(channel_input * 2),
nn.ReLU(inplace=True)
)
def forward(self, x):
return self.layers(x)
class ResidualBlock(nn.Module):
def __init__(self):
super(ResidualBlock, self).__init__()
self.layers = nn.Sequential(
nn.Conv2d(256, 256, 3, 1, 1, bias=False),
nn.InstanceNorm2d(256),
nn.ReLU(inplace=True),
nn.Conv2d(256, 256, 3, 1, 1, bias=False),
nn.InstanceNorm2d(256)
)
def forward(self, x):
return x + self.layers(x)
class Decoder(nn.Module):
def __init__(self, channel_input):
super(Decoder, self).__init__()
self.layers = nn.Sequential(
nn.ConvTranspose2d(channel_input, channel_input // 2, 3, 2, 1, output_padding=1),
nn.Conv2d(channel_input // 2, channel_input // 2, 3, 1, 1, bias=False),
nn.InstanceNorm2d(channel_input / 2),
nn.ReLU(inplace=True)
)
def forward(self, x):
return self.layers(x)
class DiscriminatorLayer(nn.Module):
def __init__(self, channel_input, channel_middle):
super(DiscriminatorLayer, self).__init__()
self.layers = nn.Sequential(
nn.Conv2d(channel_input, channel_middle, 3, 2, 1),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(channel_middle, channel_middle * 2, 3, 1, 1, bias=False),
nn.BatchNorm2d(channel_middle * 2),
nn.LeakyReLU(0.2, inplace=True)
)
def forward(self, x):
return self.layers(x)
class CartoonGANGenerator(nn.Module):
def __init__(self):
super(CartoonGANGenerator, self).__init__()
self.layers = nn.Sequential(
Encoder1(),
Encoder2(64),
Encoder2(128),
*[ResidualBlock() for i in range(8)],
Decoder(256),
Decoder(128),
nn.Conv2d(64, 3, 7, 1, 3),
nn.Tanh()
)
def forward(self, x):
return self.layers(x)
class CartoonGANDiscriminator(nn.Module):
def __init__(self):
super(CartoonGANDiscriminator, self).__init__()
self.layers = nn.Sequential(
nn.Conv2d(3, 32, 3, 1, 1),
nn.LeakyReLU(0.2, inplace=True),
DiscriminatorLayer(32, 64),
DiscriminatorLayer(128, 128),
nn.Conv2d(256, 256, 3, 1, 1),
nn.BatchNorm2d(256),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(256, 1, 3, 1, 1)
)
def forward(self, x):
return self.layers(x)
class VGG19(nn.Module):
def __init__(self):
super().__init__()
vgg = torchvision.models.vgg19_bn(pretrained=True)
self.feature_extractor = vgg.features[:37]
for child in self.feature_extractor.children():
for param in child.parameters():
param.requires_grad = False
def forward(self, input):
return self.feature_extractor(input)