-
Notifications
You must be signed in to change notification settings - Fork 0
/
resnet.py
52 lines (48 loc) · 1.28 KB
/
resnet.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
import torch.nn as nn
from normalization import AdaILN
class ResnetBlock(nn.Module):
def __init__(self, channels: int, bias: bool, adaptive_norm: bool):
super(ResnetBlock, self).__init__()
self.adaptive_norm = adaptive_norm
self.pad1 = nn.ReflectionPad2d(1)
self.conv1 = nn.Conv2d(
in_channels=channels,
out_channels=channels,
kernel_size=3,
stride=1,
padding=0,
bias=bias
)
self.norm1 = (
AdaILN(channels) if adaptive_norm else
nn.InstanceNorm2d(channels)
)
self.relu1 = nn.ReLU(True)
self.pad2 = nn.ReflectionPad2d(1)
self.conv2 = nn.Conv2d(
in_channels=channels,
out_channels=channels,
kernel_size=3,
stride=1,
padding=0,
bias=bias
)
self.norm2 = (
AdaILN(channels) if adaptive_norm else
nn.InstanceNorm2d(channels)
)
def forward(self, x, gamma=None, beta=None):
out = self.pad1(x)
out = self.conv1(out)
if self.adaptive_norm:
out = self.norm1(out, gamma, beta)
else:
out = self.norm1(out)
out = self.relu1(out)
out = self.pad2(out)
out = self.conv2(out)
if self.adaptive_norm:
out = self.norm2(out, gamma, beta)
else:
out = self.norm2(out)
return out + x