-
Notifications
You must be signed in to change notification settings - Fork 42
/
ace_network.py
270 lines (199 loc) · 9.77 KB
/
ace_network.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# Copyright © Niantic, Inc. 2022.
import logging
import math
import re
import torch
import torch.nn as nn
import torch.nn.functional as F
_logger = logging.getLogger(__name__)
class Encoder(nn.Module):
"""
FCN encoder, used to extract features from the input images.
The number of output channels is configurable, the default used in the paper is 512.
"""
def __init__(self, out_channels=512):
super(Encoder, self).__init__()
self.out_channels = out_channels
self.conv1 = nn.Conv2d(1, 32, 3, 1, 1)
self.conv2 = nn.Conv2d(32, 64, 3, 2, 1)
self.conv3 = nn.Conv2d(64, 128, 3, 2, 1)
self.conv4 = nn.Conv2d(128, 256, 3, 2, 1)
self.res1_conv1 = nn.Conv2d(256, 256, 3, 1, 1)
self.res1_conv2 = nn.Conv2d(256, 256, 1, 1, 0)
self.res1_conv3 = nn.Conv2d(256, 256, 3, 1, 1)
self.res2_conv1 = nn.Conv2d(256, 512, 3, 1, 1)
self.res2_conv2 = nn.Conv2d(512, 512, 1, 1, 0)
self.res2_conv3 = nn.Conv2d(512, self.out_channels, 3, 1, 1)
self.res2_skip = nn.Conv2d(256, self.out_channels, 1, 1, 0)
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.relu(self.conv2(x))
x = F.relu(self.conv3(x))
res = F.relu(self.conv4(x))
x = F.relu(self.res1_conv1(res))
x = F.relu(self.res1_conv2(x))
x = F.relu(self.res1_conv3(x))
res = res + x
x = F.relu(self.res2_conv1(res))
x = F.relu(self.res2_conv2(x))
x = F.relu(self.res2_conv3(x))
x = self.res2_skip(res) + x
return x
class Head(nn.Module):
"""
MLP network predicting per-pixel scene coordinates given a feature vector. All layers are 1x1 convolutions.
"""
def __init__(self,
mean,
num_head_blocks,
use_homogeneous,
homogeneous_min_scale=0.01,
homogeneous_max_scale=4.0,
in_channels=512):
super(Head, self).__init__()
self.use_homogeneous = use_homogeneous
self.in_channels = in_channels # Number of encoder features.
self.head_channels = 512 # Hardcoded.
# We may need a skip layer if the number of features output by the encoder is different.
self.head_skip = nn.Identity() if self.in_channels == self.head_channels else nn.Conv2d(self.in_channels,
self.head_channels, 1,
1, 0)
self.res3_conv1 = nn.Conv2d(self.in_channels, self.head_channels, 1, 1, 0)
self.res3_conv2 = nn.Conv2d(self.head_channels, self.head_channels, 1, 1, 0)
self.res3_conv3 = nn.Conv2d(self.head_channels, self.head_channels, 1, 1, 0)
self.res_blocks = []
for block in range(num_head_blocks):
self.res_blocks.append((
nn.Conv2d(self.head_channels, self.head_channels, 1, 1, 0),
nn.Conv2d(self.head_channels, self.head_channels, 1, 1, 0),
nn.Conv2d(self.head_channels, self.head_channels, 1, 1, 0),
))
super(Head, self).add_module(str(block) + 'c0', self.res_blocks[block][0])
super(Head, self).add_module(str(block) + 'c1', self.res_blocks[block][1])
super(Head, self).add_module(str(block) + 'c2', self.res_blocks[block][2])
self.fc1 = nn.Conv2d(self.head_channels, self.head_channels, 1, 1, 0)
self.fc2 = nn.Conv2d(self.head_channels, self.head_channels, 1, 1, 0)
if self.use_homogeneous:
self.fc3 = nn.Conv2d(self.head_channels, 4, 1, 1, 0)
# Use buffers because they need to be saved in the state dict.
self.register_buffer("max_scale", torch.tensor([homogeneous_max_scale]))
self.register_buffer("min_scale", torch.tensor([homogeneous_min_scale]))
self.register_buffer("max_inv_scale", 1. / self.max_scale)
self.register_buffer("h_beta", math.log(2) / (1. - self.max_inv_scale))
self.register_buffer("min_inv_scale", 1. / self.min_scale)
else:
self.fc3 = nn.Conv2d(self.head_channels, 3, 1, 1, 0)
# Learn scene coordinates relative to a mean coordinate (e.g. center of the scene).
self.register_buffer("mean", mean.clone().detach().view(1, 3, 1, 1))
def forward(self, res):
x = F.relu(self.res3_conv1(res))
x = F.relu(self.res3_conv2(x))
x = F.relu(self.res3_conv3(x))
res = self.head_skip(res) + x
for res_block in self.res_blocks:
x = F.relu(res_block[0](res))
x = F.relu(res_block[1](x))
x = F.relu(res_block[2](x))
res = res + x
sc = F.relu(self.fc1(res))
sc = F.relu(self.fc2(sc))
sc = self.fc3(sc)
if self.use_homogeneous:
# Dehomogenize coords:
# Softplus ensures we have a smooth homogeneous parameter with a minimum value = self.max_inv_scale.
h_slice = F.softplus(sc[:, 3, :, :].unsqueeze(1), beta=self.h_beta.item()) + self.max_inv_scale
h_slice.clamp_(max=self.min_inv_scale)
sc = sc[:, :3] / h_slice
# Add the mean to the predicted coordinates.
sc += self.mean
return sc
class Regressor(nn.Module):
"""
FCN architecture for scene coordinate regression.
The network predicts a 3d scene coordinates, the output is subsampled by a factor of 8 compared to the input.
"""
OUTPUT_SUBSAMPLE = 8
def __init__(self, mean, num_head_blocks, use_homogeneous, num_encoder_features=512):
"""
Constructor.
mean: Learn scene coordinates relative to a mean coordinate (e.g. the center of the scene).
num_head_blocks: How many extra residual blocks to use in the head (one is always used).
use_homogeneous: Whether to learn homogeneous or 3D coordinates.
num_encoder_features: Number of channels output of the encoder network.
"""
super(Regressor, self).__init__()
self.feature_dim = num_encoder_features
self.encoder = Encoder(out_channels=self.feature_dim)
self.heads = Head(mean, num_head_blocks, use_homogeneous, in_channels=self.feature_dim)
@classmethod
def create_from_encoder(cls, encoder_state_dict, mean, num_head_blocks, use_homogeneous):
"""
Create a regressor using a pretrained encoder, loading encoder-specific parameters from the state dict.
encoder_state_dict: pretrained encoder state dictionary.
mean: Learn scene coordinates relative to a mean coordinate (e.g. the center of the scene).
num_head_blocks: How many extra residual blocks to use in the head (one is always used).
use_homogeneous: Whether to learn homogeneous or 3D coordinates.
"""
# Number of output channels of the last encoder layer.
num_encoder_features = encoder_state_dict['res2_conv3.weight'].shape[0]
# Create a regressor.
_logger.info(f"Creating Regressor using pretrained encoder with {num_encoder_features} feature size.")
regressor = cls(mean, num_head_blocks, use_homogeneous, num_encoder_features)
# Load encoder weights.
regressor.encoder.load_state_dict(encoder_state_dict)
# Done.
return regressor
@classmethod
def create_from_state_dict(cls, state_dict):
"""
Instantiate a regressor from a pretrained state dictionary.
state_dict: pretrained state dictionary.
"""
# Mean is zero (will be loaded from the state dict).
mean = torch.zeros((3,))
# Count how many head blocks are in the dictionary.
pattern = re.compile(r"^heads\.\d+c0\.weight$")
num_head_blocks = sum(1 for k in state_dict.keys() if pattern.match(k))
# Whether the network uses homogeneous coordinates.
use_homogeneous = state_dict["heads.fc3.weight"].shape[0] == 4
# Number of output channels of the last encoder layer.
num_encoder_features = state_dict['encoder.res2_conv3.weight'].shape[0]
# Create a regressor.
_logger.info(f"Creating regressor from pretrained state_dict:"
f"\n\tNum head blocks: {num_head_blocks}"
f"\n\tHomogeneous coordinates: {use_homogeneous}"
f"\n\tEncoder feature size: {num_encoder_features}")
regressor = cls(mean, num_head_blocks, use_homogeneous, num_encoder_features)
# Load all weights.
regressor.load_state_dict(state_dict)
# Done.
return regressor
@classmethod
def create_from_split_state_dict(cls, encoder_state_dict, head_state_dict):
"""
Instantiate a regressor from a pretrained encoder (scene-agnostic) and a scene-specific head.
encoder_state_dict: encoder state dictionary
head_state_dict: scene-specific head state dictionary
"""
# We simply merge the dictionaries and call the other constructor.
merged_state_dict = {}
for k, v in encoder_state_dict.items():
merged_state_dict[f"encoder.{k}"] = v
for k, v in head_state_dict.items():
merged_state_dict[f"heads.{k}"] = v
return cls.create_from_state_dict(merged_state_dict)
def load_encoder(self, encoder_dict_file):
"""
Load weights into the encoder network.
"""
self.encoder.load_state_dict(torch.load(encoder_dict_file))
def get_features(self, inputs):
return self.encoder(inputs)
def get_scene_coordinates(self, features):
return self.heads(features)
def forward(self, inputs):
"""
Forward pass.
"""
features = self.get_features(inputs)
return self.get_scene_coordinates(features)