fast-reid/fastreid/modeling/heads/reduction_head.py

70 lines
2.6 KiB
Python
Raw Normal View History

# encoding: utf-8
"""
@author: liaoxingyu
@contact: sherlockliao01@gmail.com
"""
from fastreid.layers import *
2020-05-30 16:50:02 +08:00
from fastreid.utils.weight_init import weights_init_kaiming, weights_init_classifier
from .build import REID_HEADS_REGISTRY
@REID_HEADS_REGISTRY.register()
class ReductionHead(nn.Module):
def __init__(self, cfg, in_feat, num_classes, pool_layer):
super().__init__()
self._cfg = cfg
reduction_dim = cfg.MODEL.HEADS.REDUCTION_DIM
self.neck_feat = cfg.MODEL.HEADS.NECK_FEAT
self.pool_layer = pool_layer
2020-05-30 16:50:02 +08:00
self.bottleneck = nn.Sequential(
nn.Conv2d(in_feat, reduction_dim, 1, 1, bias=False),
2020-05-30 16:50:02 +08:00
get_norm(cfg.MODEL.HEADS.NORM, reduction_dim, cfg.MODEL.HEADS.NORM_SPLIT),
nn.LeakyReLU(0.1, inplace=True),
)
2020-05-30 16:50:02 +08:00
2020-05-14 11:36:28 +08:00
self.bnneck = get_norm(cfg.MODEL.HEADS.NORM, reduction_dim, cfg.MODEL.HEADS.NORM_SPLIT, bias_freeze=True)
self.bottleneck.apply(weights_init_kaiming)
self.bnneck.apply(weights_init_kaiming)
# identity classification layer
2020-05-30 16:50:02 +08:00
cls_type = cfg.MODEL.HEADS.CLS_LAYER
if cls_type == 'linear': self.classifier = nn.Linear(reduction_dim, num_classes, bias=False)
elif cls_type == 'arcSoftmax': self.classifier = ArcSoftmax(cfg, reduction_dim, num_classes)
elif cls_type == 'circleSoftmax': self.classifier = CircleSoftmax(cfg, reduction_dim, num_classes)
elif cls_type == 'amSoftmax': self.classifier = AMSoftmax(cfg, reduction_dim, num_classes)
else:
2020-05-30 16:50:02 +08:00
raise KeyError(f"{cls_type} is invalid, please choose from "
f"'linear', 'arcSoftmax', 'amSoftmax' and 'circleSoftmax'.")
2020-05-30 16:50:02 +08:00
self.classifier.apply(weights_init_classifier)
def forward(self, features, targets=None):
"""
See :class:`ReIDHeads.forward`.
"""
2020-05-30 16:50:02 +08:00
features = self.pool_layer(features)
global_feat = self.bottleneck(features)
bn_feat = self.bnneck(global_feat)
bn_feat = bn_feat[..., 0, 0]
# Evaluation
2020-05-30 16:50:02 +08:00
if not self.training: return bn_feat
# Training
2020-07-14 11:58:06 +08:00
try: cls_outputs = self.classifier(bn_feat)
except TypeError: cls_outputs = self.classifier(bn_feat, targets)
pred_class_logits = F.linear(bn_feat, self.classifier.weight)
2020-05-30 16:50:02 +08:00
if self.neck_feat == "before": feat = global_feat[..., 0, 0]
elif self.neck_feat == "after": feat = bn_feat
else:
raise KeyError("MODEL.HEADS.NECK_FEAT value is invalid, must choose from ('after' & 'before')")
2020-07-14 11:58:06 +08:00
return cls_outputs, pred_class_logits, feat