-
Notifications
You must be signed in to change notification settings - Fork 16
/
torchmodule.py
117 lines (95 loc) · 5.57 KB
/
torchmodule.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
import torch.nn as nn
# Hout=(Hin−1)stride[0]−2padding[0]+kernelsize[0]+outputpadding[0]
class Conv2d_BN_AC2(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3, padding=0, stride=1, padding_mode='zeros'):
super(Conv2d_BN_AC2, self).__init__()
self.conv = nn.Conv2d(in_channels=in_channels, out_channels=out_channels,
kernel_size=kernel_size, stride=stride, padding=padding, padding_mode=padding_mode, bias=False)
self.bn = nn.BatchNorm2d(out_channels, eps=0.001, momentum=0.5)
self.ac = nn.ReLU(inplace=True)
def forward(self, x):
print('\n', self.bn.running_mean[0].cpu().detach().numpy(), self.bn.running_var[0].cpu().detach().numpy(), self.bn.running_mean.shape)
print(self.bn.weight[0].cpu().detach().numpy(), self.bn.bias[0].cpu().detach().numpy(), self.bn.weight.shape)
print('mean:', x[0, 0].cpu().detach().numpy().mean(), ' var', x[0, 0].cpu().detach().numpy().var())
x = self.conv(x)
print('minibatch_mean', x[:, 0].cpu().detach().numpy().mean(), ' minibatch_var', x[:, 0].cpu().detach().numpy().var())
print('mean:', x[0, 0].cpu().detach().numpy().mean(), ' var', x[0, 0].cpu().detach().numpy().var())
x = self.bn(x)
print('mean', x[0, 0].cpu().detach().numpy().mean(), ' var', x[0, 0].cpu().detach().numpy().var())
out = self.ac(x)
return out
class Conv2d_BN_AC(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3, padding=0, stride=1, padding_mode='zeros'):
super(Conv2d_BN_AC, self).__init__()
self.pipe = nn.Sequential(
nn.Conv2d(in_channels=in_channels, out_channels=out_channels,
kernel_size=kernel_size, stride=stride, padding=padding, padding_mode=padding_mode, bias=False),
nn.BatchNorm2d(out_channels, eps=0.001, momentum=0.5),
nn.ReLU(inplace=True))
def forward(self, x):
out = self.pipe(x)
return out
class ConvTranspose2d_BN_AC(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, activation=nn.ReLU(inplace=True)):
super(ConvTranspose2d_BN_AC, self).__init__()
self.deconv = nn.ConvTranspose2d(in_channels=in_channels, out_channels=out_channels,
kernel_size=kernel_size, stride=stride, padding=(kernel_size - 1) // 2, output_padding=stride - 1, bias=False)
self.BN_AC = nn.Sequential(
nn.BatchNorm2d(out_channels, eps=0.001, momentum=0.5),
activation)
self.crop_size = (kernel_size + 1) % 2
def forward(self, x):
out = self.deconv(x)
out2 = out[:, :, self.crop_size:out.shape[2], self.crop_size:out.shape[3]].clone()
out2 = self.BN_AC(out2)
return out2
class ConvTranspose2d_BN_AC2(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=4, stride=1, activation=nn.ReLU(inplace=True)):
super(ConvTranspose2d_BN_AC2, self).__init__()
if stride % 2 == 0:
self.deconv = nn.ConvTranspose2d(in_channels=in_channels, out_channels=out_channels,
kernel_size=kernel_size, stride=stride, padding=(kernel_size - 1) // 2, bias=False)
else:
self.deconv = nn.Sequential(nn.ConstantPad2d((2, 1, 2, 1), 0),
nn.ConvTranspose2d(in_channels=in_channels, out_channels=out_channels,
kernel_size=kernel_size, stride=stride, padding=3, bias=False))
self.BN_AC = nn.Sequential(
nn.BatchNorm2d(out_channels, eps=0.001, momentum=0.5),
activation)
def forward(self, x):
out = self.deconv(x)
out2 = self.BN_AC(out)
return out2
class PRNResBlock(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, with_conv_shortcut=False, activation=nn.ReLU(inplace=True)):
super(PRNResBlock, self).__init__()
if kernel_size % 2 == 1:
self.pipe = nn.Sequential(
Conv2d_BN_AC(in_channels=in_channels, out_channels=out_channels // 2, stride=1, kernel_size=1),
Conv2d_BN_AC(in_channels=out_channels // 2, out_channels=out_channels // 2, stride=stride,
kernel_size=kernel_size, padding=(kernel_size - 1) // 2),
nn.Conv2d(in_channels=out_channels // 2, out_channels=out_channels, stride=1, kernel_size=1, bias=False),
)
else:
self.pipe = nn.Sequential(
Conv2d_BN_AC(in_channels=in_channels, out_channels=out_channels // 2, stride=1, kernel_size=1),
Conv2d_BN_AC(in_channels=out_channels // 2, out_channels=out_channels // 2, stride=stride,
kernel_size=kernel_size, padding=kernel_size - 1, padding_mode='circular'),
nn.Conv2d(in_channels=out_channels // 2, out_channels=out_channels, stride=1, kernel_size=1, bias=False)
)
self.shortcut = nn.Sequential()
if with_conv_shortcut:
self.shortcut = nn.Sequential(
nn.Conv2d(in_channels=in_channels, out_channels=out_channels, stride=stride, kernel_size=1, bias=False),
)
self.BN_AC = nn.Sequential(
nn.BatchNorm2d(out_channels, eps=0.001, momentum=0.5),
activation
)
def forward(self, x):
out = self.pipe(x)
s = self.shortcut(x)
assert (s.shape == out.shape)
out = out + s
out = self.BN_AC(out)
return out