-
Notifications
You must be signed in to change notification settings - Fork 99
/
models.py
executable file
·106 lines (90 loc) · 3.68 KB
/
models.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
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.nn.functional as F
class Generator(nn.Module):
def __init__(self, input_nc=3, output_nc=4, ngf=64, norm_layer=nn.BatchNorm2d, use_dropout=False, n_blocks=6):
assert(n_blocks >= 0)
super(Generator, self).__init__()
self.input_nc = input_nc
self.output_nc = output_nc
self.ngf = ngf
model = [nn.ReflectionPad2d(3),
nn.Conv2d(input_nc, ngf, kernel_size=7, padding=0),
norm_layer(ngf),
nn.ReLU(True)]
n_downsampling = 2
for i in range(n_downsampling):
mult = 2**i
model += [nn.Conv2d(ngf * mult, ngf * mult * 2, kernel_size=3,
stride=2, padding=1),
norm_layer(ngf * mult * 2),
nn.ReLU(True)]
mult = 2**n_downsampling
for i in range(n_blocks):
model += [ResnetBlock(ngf * mult, norm_layer=norm_layer, use_dropout=use_dropout)]
for i in range(n_downsampling):
mult = 2**(n_downsampling - i)
model += [nn.ReflectionPad2d(1),
nn.Conv2d(ngf * mult, int(ngf * mult / 2),
kernel_size=3, stride=1),
norm_layer(int(ngf * mult / 2)),
nn.ReLU(True),
nn.Conv2d(int(ngf * mult / 2), int(ngf * mult / 2)*4,
kernel_size=1, stride=1),
nn.PixelShuffle(2),
norm_layer(int(ngf * mult / 2)),
nn.ReLU(True),
]
model += [nn.ReflectionPad2d(3)]
model += [nn.Conv2d(ngf, output_nc, kernel_size=7, padding=0)]
self.model = nn.Sequential(*model)
def forward(self, input):
output = self.model(input)
attention_mask = F.sigmoid(output[:, :1])
content_mask = output[:, 1:]
attention_mask = attention_mask.repeat(1, 3, 1, 1)
result = content_mask * attention_mask + input * (1 - attention_mask)
return result, attention_mask, content_mask
class ResnetBlock(nn.Module):
def __init__(self, dim, norm_layer, use_dropout):
super(ResnetBlock, self).__init__()
self.conv_block = self.build_conv_block(dim, norm_layer, use_dropout)
def build_conv_block(self, dim, norm_layer, use_dropout):
conv_block = [nn.ReflectionPad2d(1),
nn.Conv2d(dim, dim, kernel_size=3),
norm_layer(dim),
nn.ReLU(True)]
if use_dropout:
conv_block += [nn.Dropout(0.5)]
conv_block += [nn.ReflectionPad2d(1),
nn.Conv2d(dim, dim, kernel_size=3),
norm_layer(dim)]
return nn.Sequential(*conv_block)
def forward(self, x):
out = x + self.conv_block(x)
return out
class Discriminator(nn.Module):
def __init__(self):
super(Discriminator, self).__init__()
self.conv_tower = nn.Sequential(
nn.Conv2d(3, 64, 4, 2),
nn.BatchNorm2d(64),
nn.LeakyReLU(),
nn.Conv2d(64, 128, 4, 2),
nn.BatchNorm2d(128),
nn.LeakyReLU(),
nn.Conv2d(128, 256, 4, 2),
nn.BatchNorm2d(256),
nn.LeakyReLU(),
nn.Conv2d(256, 512, 4, 2),
nn.BatchNorm2d(512),
nn.LeakyReLU(),
nn.Conv2d(512, 512, 4),
nn.LeakyReLU(),
nn.AdaptiveAvgPool2d((1, 1)),
nn.Conv2d(512, 1, 1),
)
def forward(self, img):
output = self.conv_tower(img)
return output