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

Class EfficientNetB3

script/feature/model.py:454–509  ·  view source on GitHub ↗

EfficientNet-B3 backbone, model ref: https://github.com/lukemelas/EfficientNet-PyTorch/blob/master/efficientnet_pytorch/model.py

Source from the content-addressed store, hash-verified

452 return feat_out, predict
453
454class EfficientNetB3(nn.Module):
455 ''' EfficientNet-B3 backbone,
456 model ref: https://github.com/lukemelas/EfficientNet-PyTorch/blob/master/efficientnet_pytorch/model.py
457 '''
458 def __init__(self, feat_dim=12, feature_block=6):
459 super(EfficientNetB3, self).__init__()
460 self.backbone_net = EfficientNet.from_pretrained('efficientnet-b3')
461 self.feature_block = feature_block # determine which block's feature to use, max=6
462 if self.feature_block == 6:
463 self.feature_extractor = self.backbone_net.extract_features
464 else:
465 self.feature_extractor = self.backbone_net.extract_endpoints
466
467 # self.feature_extractor = self.backbone_net.extract_endpoints # it can restore middle layer
468 self.avgpool = nn.AdaptiveAvgPool2d(1)
469 self.fc_pose = nn.Linear(1536, feat_dim) # 1280 for efficientnet-b0, 1536 for efficientnet-b3
470
471 def _aggregate_feature2(self, x):
472 '''
473 assume target and nerf rgb are inferenced at the same time,
474 slice target batch and nerf batch and output stacked features
475 :param x: image blob (2B x C x H x W)
476 :return feature: (2 x B x C x H x W)
477 '''
478 batch = x.shape[0] # should be target batch_size + rgb batch_size
479 feature_t = x[:batch//2]
480 feature_r = x[batch//2:]
481 feature = torch.stack([feature_t, feature_r])
482 return feature
483
484 def forward(self, x, return_feature=False, isSingleStream=False):
485 '''
486 Currently under dev.
487 :param x: image blob ()
488 :param return_feature: True to extract features, False only return pose prediction. Really should be isExtractFeature
489 :param isSingleStream: True to inference single img, False to inference two imgs in siemese network fashion
490 '''
491 # pdb.set_trace()
492 feat_out = [] # we only use high level features
493 if self.feature_block == 6:
494 x = self.feature_extractor(x)
495 fe = x.clone() # features to save
496 else:
497 list_x = self.feature_extractor(x)
498 fe = list_x['reduction_'+str(self.feature_block)]
499 x = list_x['reduction_6'] # features to save
500 if return_feature:
501 if isSingleStream:
502 feature = torch.stack([fe])
503 else:
504 feature = self._aggregate_feature2(fe)
505 feat_out.append(feature)
506 x = self.avgpool(x)
507 x = x.reshape(x.size(0), -1)
508 predict = self.fc_pose(x)
509 return feat_out, predict

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected