MCPcopy Create free account
hub / github.com/ActiveVisionLab/DFNet / EfficientNetB0

Class EfficientNetB0

script/feature/efficientnet.py:181–271  ·  view source on GitHub ↗

DFNet with EB0 backbone, feature levels can be customized

Source from the content-addressed store, hash-verified

179 return features
180
181class EfficientNetB0(nn.Module):
182 ''' DFNet with EB0 backbone, feature levels can be customized '''
183 default_conf = {
184 # 'hypercolumn_layers': ["reduction_1", "reduction_3", "reduction_6"],
185 'hypercolumn_layers': ["reduction_1", "reduction_3", "reduction_5"],
186 # 'hypercolumn_layers': ["reduction_2", "reduction_4", "reduction_6"],
187 # 'hypercolumn_layers': ["reduction_1"],
188 'output_dim': 128,
189 }
190 mean = [0.485, 0.456, 0.406]
191 std = [0.229, 0.224, 0.225]
192
193 def __init__(self, feat_dim=12, places365_model_path=''):
194 super(EfficientNetB0, self).__init__()
195 # Initialize architecture
196 self.backbone_net = EfficientNet.from_pretrained('efficientnet-b0')
197 self.feature_extractor = self.backbone_net.extract_endpoints
198
199 # self.feature_block_index = [1, 3, 6] # same as the 'hypercolumn_layers'
200 self.feature_block_index = [1, 3, 5] # same as the 'hypercolumn_layers'
201 # self.feature_block_index = [2, 4, 6] # same as the 'hypercolumn_layers'
202 # self.feature_block_index = [1]
203
204 ## adaptation layers, see off branches from fig.3 in S2DNet paper
205 self.adaptation_layers = AdaptLayers2(self.default_conf['hypercolumn_layers'], self.default_conf['output_dim'])
206
207 # pose regression layers
208 self.avgpool = nn.AdaptiveAvgPool2d(1)
209 self.fc_pose = nn.Linear(1280, feat_dim)
210
211 def forward(self, x, return_feature=False, isSingleStream=False, return_pose=False, upsampleH=120, upsampleW=213):
212 '''
213 inference DFNet. It can regress camera pose as well as extract intermediate layer features.
214 :param x: image blob (2B x C x H x W) two stream or (B x C x H x W) single stream
215 :param return_feature: whether to return features as output
216 :param isSingleStream: whether it's an single stream inference or siamese network inference
217 :param return_pose: TODO: if only return_pose, we don't need to compute return_feature part
218 :param upsampleH: feature upsample size H
219 :param upsampleW: feature upsample size W
220 :return feature_maps: (2, [B, C, H, W]) or (1, [B, C, H, W]) or None
221 :return predict: [2B, 12] or [B, 12]
222 '''
223 # normalize input data
224 mean, std = x.new_tensor(self.mean), x.new_tensor(self.std)
225 x = (x - mean[:, None, None]) / std[:, None, None]
226
227 ### encoder ###
228 feature_maps = []
229 list_x = self.feature_extractor(x)
230
231 x = list_x['reduction_6'] # features to save
232 for i in self.feature_block_index:
233 fe = list_x['reduction_'+str(i)].clone()
234 feature_maps.append(fe)
235
236 ### extract and process intermediate features ###
237 if return_feature:
238 feature_maps = self.adaptation_layers(feature_maps) # (3, [B, C, H', W']), H', W' are different in each layer

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected