-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmodels.py
146 lines (116 loc) · 4.66 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import torch.nn as nn
import torch.nn.functional as F
import torch
def weights_init_normal(m):
classname = m.__class__.__name__
if classname.find("Conv") != -1:
torch.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:
torch.nn.init.normal_(m.weight.data, 1.0, 0.02)
torch.nn.init.constant_(m.bias.data, 0.0)
class ResidualBlock(nn.Module):
def __init__(self, in_features):
super(ResidualBlock, self).__init__()
self.block = nn.Sequential(
#nn.ReflectionPad2d(1),
nn.Conv2d(in_features, in_features, 3, stride=1, padding=1),
nn.InstanceNorm2d(in_features),
nn.ReLU(inplace=True),
#nn.ReflectionPad2d(1),
nn.Conv2d(in_features, in_features, 3, stride=1, padding=1),
nn.InstanceNorm2d(in_features),
nn.ReLU(inplace=True),
)
def forward(self, x):
return x + self.block(x)
class GeneratorResNet(nn.Module):
def __init__(self, num_residual_blocks=8, in_features=256):
super(GeneratorResNet, self).__init__()
out_features = in_features
model = []
# Residual blocks
for _ in range(num_residual_blocks):
model += [ResidualBlock(out_features)]
# Upsampling
for _ in range(2):
out_features //= 2
model += [
nn.Upsample(scale_factor=2),
nn.Conv2d(in_features, out_features, 3, stride=1, padding=1),
nn.InstanceNorm2d(out_features),
nn.ReLU(inplace=True),
nn.Conv2d(out_features, out_features, 3, stride=1, padding=1),
nn.InstanceNorm2d(out_features),
nn.ReLU(inplace=True),
nn.Conv2d(out_features, out_features, 3, stride=1, padding=1),
nn.InstanceNorm2d(out_features),
nn.ReLU(inplace=True),
]
in_features = out_features
# Output layer
#model += [nn.ReflectionPad2d(2), nn.Conv2d(out_features, 2, 7), nn.Softmax()]
model += [nn.Conv2d(out_features, 2, 7, stride=1, padding=3), nn.Sigmoid()]
self.model = nn.Sequential(*model)
def forward(self, feature_map):
x = self.model(feature_map)
return x
class Encoder(nn.Module):
def __init__(self, channels=3+2):
super(Encoder, self).__init__()
# Initial convolution block
out_features = 64
model = [
nn.Conv2d(channels, out_features, 7, stride=1, padding=3),
nn.InstanceNorm2d(out_features),
nn.ReLU(inplace=True),
]
in_features = out_features
# Downsampling
for _ in range(2):
out_features *= 2
model += [
nn.Conv2d(in_features, out_features, 3, stride=1, padding=1),
nn.InstanceNorm2d(out_features),
nn.ReLU(inplace=True),
nn.Conv2d(out_features, out_features, 3, stride=1, padding=1),
nn.InstanceNorm2d(out_features),
nn.ReLU(inplace=True),
nn.MaxPool2d(2, stride=2),
]
in_features = out_features
self.model = nn.Sequential(*model)
def forward(self, arguments):
x = torch.cat(arguments, dim=1)
x = self.model(x)
return x
class Discriminator(nn.Module):
def __init__(self):
super(Discriminator, self).__init__()
channels = 2
out_channels = 2
def discriminator_block(in_filters, out_filters, normalize=True):
"""Returns downsampling layers of each discriminator block"""
layers = [nn.Conv2d(in_filters, out_filters, 3, stride=1, padding=1)]
if normalize:
layers.append(nn.InstanceNorm2d(out_filters))
layers.append(nn.ReLU())
layers.append(nn.Conv2d(out_filters, out_filters, 3, stride=1, padding=1))
if normalize:
layers.append(nn.InstanceNorm2d(out_filters))
layers.append(nn.ReLU())
layers.append(nn.MaxPool2d(2, stride=2))
return layers
self.model = nn.Sequential(
*discriminator_block(channels, 64, normalize=False),
*discriminator_block(64, 128),
*discriminator_block(128, 256),
*discriminator_block(256, 512),
nn.Conv2d(512, out_channels, 3, padding=1),
nn.Sigmoid()
)
def forward(self, img):
#img = torch.cat((rgb, mask), dim=1)
img = self.model(img)
return img