-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathmodel.py
156 lines (124 loc) · 5.07 KB
/
model.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
147
148
149
150
151
152
153
154
155
156
# Copyright 2022 Lorna. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
import torch
from torch import nn, Tensor
from torch.nn import functional as F_torch
from torchvision.transforms import functional as F_vision
__all__ = [
"PathDiscriminator", "CycleNet",
"path_discriminator", "cyclenet",
]
class PathDiscriminator(nn.Module):
def __init__(
self,
in_channels: int,
out_channels: int,
channels: int,
) -> None:
super(PathDiscriminator, self).__init__()
self.main = nn.Sequential(
nn.Conv2d(in_channels, channels, (4, 4), (2, 2), (1, 1)),
nn.LeakyReLU(0.2, True),
nn.Conv2d(channels, int(channels * 2), (4, 4), (2, 2), (1, 1)),
nn.InstanceNorm2d(int(channels * 2)),
nn.LeakyReLU(0.2, True),
nn.Conv2d(int(channels * 2), int(channels * 4), (4, 4), (2, 2), (1, 1)),
nn.InstanceNorm2d(int(channels * 4)),
nn.LeakyReLU(0.2, True),
nn.Conv2d(int(channels * 4), int(channels * 8), (4, 4), (1, 1), (1, 1)),
nn.InstanceNorm2d(int(channels * 8)),
nn.LeakyReLU(0.2, True),
nn.Conv2d(int(channels * 8), out_channels, (4, 4), (1, 1), (1, 1)),
)
def forward(self, x: Tensor) -> Tensor:
x = self.main(x)
return x
class CycleNet(nn.Module):
def __init__(
self,
in_channels: int,
out_channels: int,
channels: int,
) -> None:
super(CycleNet, self).__init__()
self.main = nn.Sequential(
# Initial convolution block
nn.ReflectionPad2d(3),
nn.Conv2d(in_channels, channels, (7, 7), (1, 1), (0, 0)),
nn.InstanceNorm2d(channels, track_running_stats=True),
nn.ReLU(True),
# Downsampling
nn.Conv2d(channels, int(channels * 2), (3, 3), (2, 2), (1, 1)),
nn.InstanceNorm2d(int(channels * 2), track_running_stats=True),
nn.ReLU(True),
nn.Conv2d(int(channels * 2), int(channels * 4), (3, 3), (2, 2), (1, 1)),
nn.InstanceNorm2d(int(channels * 4), track_running_stats=True),
nn.ReLU(True),
# Residual blocks
_ResidualBlock(int(channels * 4)),
_ResidualBlock(int(channels * 4)),
_ResidualBlock(int(channels * 4)),
_ResidualBlock(int(channels * 4)),
_ResidualBlock(int(channels * 4)),
_ResidualBlock(int(channels * 4)),
_ResidualBlock(int(channels * 4)),
_ResidualBlock(int(channels * 4)),
_ResidualBlock(int(channels * 4)),
# Upsampling
nn.ConvTranspose2d(int(channels * 4), int(channels * 2), (3, 3), (2, 2), (1, 1), (1, 1)),
nn.InstanceNorm2d(int(channels * 2), track_running_stats=True),
nn.ReLU(True),
nn.ConvTranspose2d(int(channels * 2), channels, (3, 3), (2, 2), (1, 1), (1, 1)),
nn.InstanceNorm2d(channels, track_running_stats=True),
nn.ReLU(True),
# Output layer
nn.ReflectionPad2d(3),
nn.Conv2d(channels, out_channels, (7, 7), (1, 1), (0, 0)),
nn.Tanh(),
)
def forward(self, x: Tensor) -> Tensor:
x = self.main(x)
return x
class _ResidualBlock(nn.Module):
def __init__(self, channels: int):
super(_ResidualBlock, self).__init__()
self.res = nn.Sequential(
nn.ReflectionPad2d(1),
nn.Conv2d(channels, channels, (3, 3), (1, 1), (0, 0)),
nn.InstanceNorm2d(channels, track_running_stats=True),
nn.ReLU(True),
nn.ReflectionPad2d(1),
nn.Conv2d(channels, channels, (3, 3), (1, 1), (0, 0)),
nn.InstanceNorm2d(channels, track_running_stats=True),
)
def forward(self, x: Tensor) -> Tensor:
identity = x
x = self.res(x)
x = torch.add(x, identity)
return x
def _weights_init(m):
classname = m.__class__.__name__
if classname.find("Conv") != -1:
torch.nn.init.normal_(m.weight, 0.0, 0.02)
elif classname.find("BatchNorm") != -1:
torch.nn.init.normal_(m.weight, 1.0, 0.02)
torch.nn.init.zeros_(m.bias)
def path_discriminator(**kwargs) -> PathDiscriminator:
model = PathDiscriminator(**kwargs)
model.apply(_weights_init)
return model
def cyclenet(**kwargs) -> CycleNet:
model = CycleNet(**kwargs)
model.apply(_weights_init)
return model