-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtransformer.py
149 lines (130 loc) · 4.97 KB
/
transformer.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
import torch
import torch.nn as nn
class ConvLayer(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride, norm='instance'):
"""
Params:
- in_channels: (int) Number of channels in the input image
- out_channels: (int) Number of channels produced by the convolution
- kernel_size: (int or tuple) Size of the convolving kernel
- stride: (int or tuple) Stride of the convolution
- norm: (string, optional) Applies normalization. Accepted values: ['instance'(default), 'batch', 'None']
"""
super(ConvLayer, self).__init__()
# Add padding
padding_size = kernel_size//2
self.reflection_pad = nn.ReflectionPad2d(padding_size)
# Convolution Layer
self.conv_layer = nn.Conv2d(
in_channels, out_channels, kernel_size, stride)
# Normalization Layer
self.norm_type = norm
if(norm == 'instance'):
self.norm_layer = nn.InstanceNorm2d(out_channels, affine=True)
elif(norm == 'batch'):
self.norm_layer = nn.BatchNorm2d(out_channels, affine=True)
assert(norm in ['instance', 'batch', 'None']
), 'Accepted values must belong to: "instance", "batch", "None"'
def forward(self, x):
x = self.reflection_pad(x)
x = self.conv_layer(x)
if(self.norm_type == 'None'):
out = x
else:
out = self.norm_layer(x)
return(out)
class ResidualLayer(nn.Module):
"""
Residual block that hops one layer.
"""
def __init__(self, channels=128, kernel_size=3):
"""
Params:
- channels: (int, optional) Number of channels. Default: 128
- kernel_size: (int or tuple, optional) Size of the convolving kernel. Default: 3
"""
super(ResidualLayer, self).__init__()
self.conv1 = ConvLayer(channels, channels, kernel_size, 1)
self.relu = nn.ReLU()
self.conv2 = ConvLayer(channels, channels, kernel_size, 1)
def forward(self, x):
# preserve the residue
residue = x
# layer1 output + activation
out = self.relu(self.conv1(x))
# layer2 output
out = self.conv2(x)
# add residue to this output
out = out + residue
return(out)
class DeConvLayer(nn.Module):
"""
Fractionally strided convolution layer or Deconvolution layer.
"""
def __init__(self, in_channels, out_channels, kernel_size, stride, output_padding, norm="instance"):
"""
Params:
- in_channels: (int) Number of channels in the input image
- out_channels: (int) Number of channels produced by the convolution
- kernel_size: (int or tuple) Size of the convolving kernel
- stride: (int or tuple) Stride of the convolution
- output_padding: (int or tuple) Additional size added to one side of the output shape
- norm: (string, optional) Applies normalization. Accepted values: ['instance'(default), 'batch', 'None']
"""
super(DeConvLayer, self).__init__()
# Transposed Convolution or Fractional Convolution
padding_size = kernel_size // 2
self.conv_transpose = nn.ConvTranspose2d(
in_channels, out_channels, kernel_size, stride, padding_size, output_padding)
# Normalization Layer
self.norm_type = norm
if (norm == "instance"):
self.norm_layer = nn.InstanceNorm2d(out_channels, affine=True)
elif (norm == "batch"):
self.norm_layer = nn.BatchNorm2d(out_channels, affine=True)
assert(norm in ['instance', 'batch', 'None']
), 'Accepted values must belong to: "instance", "batch", "None"'
def forward(self, x):
x = self.conv_transpose(x)
if(self.norm_type == 'None'):
out = x
else:
out = self.norm_layer(x)
return(out)
class TransformNet(nn.Module):
"""
Image Transform Net as described in Johnson et al
paper: https://arxiv.org/abs/1603.08155
"""
def __init__(self):
"""
Conv Block -> Residual Block -> DeConv Block
"""
super(TransformNet, self).__init__()
self.ConvBlock = nn.Sequential(
ConvLayer(3, 32, 9, 1),
nn.ReLU(),
ConvLayer(32, 64, 3, 2),
nn.ReLU(),
ConvLayer(64, 128, 3, 2),
nn.ReLU()
)
self.ResidualBlock = nn.Sequential(
ResidualLayer(128, 3),
ResidualLayer(128, 3),
ResidualLayer(128, 3),
ResidualLayer(128, 3),
ResidualLayer(128, 3)
)
self.DeConvBlock = nn.Sequential(
DeConvLayer(128, 64, 3, 2, 1),
nn.ReLU(),
DeConvLayer(64, 32, 3, 2, 1),
nn.ReLU(),
ConvLayer(32, 3, 9, 1, norm='None')
)
def forward(self, x):
x = self.ConvBlock(x)
x = self.ResidualBlock(x)
out = self.DeConvBlock(x)
return(out)