-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGridNet.py
171 lines (144 loc) · 5.93 KB
/
GridNet.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import torch
import torch.nn as nn
from torch.nn.functional import interpolate
class GridNet(nn.Module):
def __init__(self, dim=32, act=nn.ReLU):
super(GridNet, self).__init__()
self.lateral = nn.ModuleList([
nn.ModuleList([
LateralBlock(dim, act=act),
LateralBlock(dim, act=act),
LateralBlock(dim, act=act),
LateralBlock(dim, act=act),
LateralBlock(dim, act=act),
LateralBlock(dim, act=act),
]),
nn.ModuleList([
LateralBlock(dim*2, act=act),
LateralBlock(dim*2, act=act),
LateralBlock(dim*2, act=act),
LateralBlock(dim*2, act=act),
LateralBlock(dim*2, act=act),
]),
nn.ModuleList([
LateralBlock(dim*3, act=act),
LateralBlock(dim*3, act=act),
LateralBlock(dim*3, act=act),
LateralBlock(dim*3, act=act),
LateralBlock(dim*3, act=act),
])
])
self.down = nn.ModuleList([
nn.ModuleList([
DownBlock(dim, dim*2, act=act),
DownBlock(dim, dim*2, act=act),
DownBlock(dim, dim*2, act=act),
]),
nn.ModuleList([
DownBlock(dim*2, dim*3, act=act),
DownBlock(dim*2, dim*3, act=act),
DownBlock(dim*2, dim*3, act=act),
])
])
self.up = nn.ModuleList([
nn.ModuleList([
UpBlock(dim*2, dim, act=act),
UpBlock(dim*2, dim, act=act),
UpBlock(dim*2, dim, act=act),
]),
nn.ModuleList([
UpBlock(dim*3, dim*2, act=act),
UpBlock(dim*3, dim*2, act=act),
UpBlock(dim*3, dim*2, act=act),
])
])
self.compress = nn.ModuleList([
nn.Conv2d((dim + 3) * 2, dim, 3, 1, 1),
nn.Conv2d(dim*4, dim*2, 3, 1, 1),
nn.Conv2d(dim*6, dim*3, 3, 1, 1)
])
self.to_RGB = nn.Sequential(
act(),
nn.Conv2d(dim, 3, 3, 1, 1)
)
def forward(self, pyramid):
x_orig, x_0_0, x_1_0, x_2_0 = pyramid
'''
compress dim first. (32 x 2 -> 32)
this channel reduction / compression process is not explicitly mentioned in the Softsplat paper,
but only says it adopted the GridNet of CtxSyn, which has a dimension of 32, 64, 96 at the three levels.
Thus this dimension compression part may be different from the original implementation.
'''
x_0_0 = torch.cat([x_orig, x_0_0], dim=1)
x_0_0 = self.compress[0](x_0_0)
x_1_0 = self.compress[1](x_1_0)
x_2_0 = self.compress[2](x_2_0)
# first half: down & lateral
x_0_1 = self.lateral[0][0](x_0_0)
x_0_2 = self.lateral[0][1](x_0_1)
x_0_3 = self.lateral[0][2](x_0_2)
x_1_0 = x_1_0 + self.down[0][0](x_0_0)
x_2_0 = x_2_0 + self.down[1][0](x_1_0)
x_1_1 = self.lateral[1][0](x_1_0)
x_2_1 = self.lateral[2][0](x_2_0)
x_1_1 = x_1_1 + self.down[0][1](x_0_1)
x_2_1 = x_2_1 + self.down[1][1](x_1_1)
x_1_2 = self.lateral[1][1](x_1_1)
x_2_2 = self.lateral[2][1](x_2_1)
x_1_2 = x_1_2 + self.down[0][2](x_0_2)
x_2_2 = x_2_2 + self.down[1][2](x_1_2)
x_1_3 = self.lateral[1][2](x_1_2)
x_2_3 = self.lateral[2][2](x_2_2)
# second half: up & lateral
x_2_4 = self.lateral[2][3](x_2_3)
x_2_5 = self.lateral[2][4](x_2_4)
x_1_3 = x_1_3 + interpolate(self.up[1][0](x_2_3), size=x_1_3.shape[-2:], mode='bilinear', align_corners=False)
x_0_3 = x_0_3 + interpolate(self.up[0][0](x_1_3), size=x_0_3.shape[-2:], mode='bilinear', align_corners=False)
x_1_4 = self.lateral[1][3](x_1_3)
x_0_4 = self.lateral[0][3](x_0_3)
x_1_4 = x_1_4 + interpolate(self.up[1][1](x_2_4), size=x_1_4.shape[-2:], mode='bilinear', align_corners=False)
x_0_4 = x_0_4 + interpolate(self.up[0][1](x_1_4), size=x_0_4.shape[-2:], mode='bilinear', align_corners=False)
x_1_5 = self.lateral[1][4](x_1_4)
x_0_5 = self.lateral[0][4](x_0_4)
x_1_5 = x_1_5 + interpolate(self.up[1][2](x_2_5), size=x_1_5.shape[-2:], mode='bilinear', align_corners=False)
x_0_5 = x_0_5 + interpolate(self.up[0][2](x_1_5), size=x_0_5.shape[-2:], mode='bilinear', align_corners=False)
# final synthesis
output = self.lateral[0][5](x_0_5)
output = self.to_RGB(output)
return output
class LateralBlock(nn.Module):
def __init__(self, dim, act=nn.ReLU):
super(LateralBlock, self).__init__()
self.layers = nn.Sequential(
act(),
nn.Conv2d(dim, dim, 3, 1, 1),
act(),
nn.Conv2d(dim, dim, 3, 1, 1)
)
def forward(self, x):
res = x
x = self.layers(x)
return x + res
class DownBlock(nn.Module):
def __init__(self, in_dim, out_dim, act=nn.ReLU):
super(DownBlock, self).__init__()
self.layers = nn.Sequential(
act(),
nn.Conv2d(in_dim, out_dim, 3, 2, 1),
act(),
nn.Conv2d(out_dim, out_dim, 3, 1, 1)
)
def forward(self, x):
return self.layers(x)
class UpBlock(nn.Module):
def __init__(self, in_dim, out_dim, act=nn.ReLU):
super(UpBlock, self).__init__()
self.layers = nn.Sequential(
act(),
nn.Conv2d(in_dim, out_dim, 3, 1, 1),
act(),
nn.Conv2d(out_dim, out_dim, 3, 1, 1)
)
def forward(self, x):
x = interpolate(x, scale_factor=2.0, mode='bilinear', align_corners=False)
return self.layers(x)