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

45 lines
1.3 KiB
Python
Raw Normal View History

2020-03-25 10:58:26 +08:00
# encoding: utf-8
"""
@author: liaoxingyu
@contact: sherlockliao01@gmail.com
"""
from .build import REID_HEADS_REGISTRY
from ...layers import *
2020-03-25 10:58:26 +08:00
@REID_HEADS_REGISTRY.register()
class LinearHead(nn.Module):
def __init__(self, cfg, in_feat, pool_layer=nn.AdaptiveAvgPool2d(1)):
super().__init__()
self._num_classes = cfg.MODEL.HEADS.NUM_CLASSES
self.pool_layer = nn.Sequential(
pool_layer,
Flatten()
)
if cfg.MODEL.HEADS.CLS_LAYER == 'linear':
self.classifier = nn.Linear(in_feat, self._num_classes, bias=False)
elif cfg.MODEL.HEADS.CLS_LAYER == 'arcface':
self.classifier = Arcface(cfg, in_feat)
elif cfg.MODEL.HEADS.CLS_LAYER == 'circle':
self.classifier = Circle(cfg, in_feat)
else:
self.classifier = nn.Linear(in_feat, self._num_classes, bias=False)
2020-03-25 10:58:26 +08:00
def forward(self, features, targets=None):
"""
See :class:`ReIDHeads.forward`.
"""
global_feat = self.pool_layer(features)
if not self.training:
return global_feat
# training
try:
pred_class_logits = self.classifier(global_feat)
except TypeError:
pred_class_logits = self.classifier(global_feat, targets)
2020-03-25 10:58:26 +08:00
return pred_class_logits, global_feat