2020-03-25 10:58:26 +08:00
|
|
|
# encoding: utf-8
|
|
|
|
"""
|
|
|
|
@author: liaoxingyu
|
|
|
|
@contact: sherlockliao01@gmail.com
|
|
|
|
"""
|
|
|
|
|
2020-05-01 09:02:46 +08:00
|
|
|
from fastreid.layers import *
|
2020-03-25 10:58:26 +08:00
|
|
|
from .build import REID_HEADS_REGISTRY
|
|
|
|
|
|
|
|
|
|
|
|
@REID_HEADS_REGISTRY.register()
|
|
|
|
class LinearHead(nn.Module):
|
2020-04-29 21:29:48 +08:00
|
|
|
def __init__(self, cfg, in_feat, num_classes, pool_layer=nn.AdaptiveAvgPool2d(1)):
|
2020-03-25 10:58:26 +08:00
|
|
|
super().__init__()
|
2020-05-01 09:02:46 +08:00
|
|
|
self.pool_layer = pool_layer
|
2020-03-25 10:58:26 +08:00
|
|
|
|
2020-04-27 14:49:58 +08:00
|
|
|
# identity classification layer
|
2020-04-19 12:54:01 +08:00
|
|
|
if cfg.MODEL.HEADS.CLS_LAYER == 'linear':
|
2020-04-29 21:29:48 +08:00
|
|
|
self.classifier = nn.Linear(in_feat, num_classes, bias=False)
|
2020-04-19 12:54:01 +08:00
|
|
|
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:
|
2020-04-29 21:29:48 +08:00
|
|
|
self.classifier = nn.Linear(in_feat, 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)
|
2020-05-26 13:18:09 +08:00
|
|
|
global_feat = global_feat[..., 0, 0]
|
2020-03-25 10:58:26 +08:00
|
|
|
if not self.training:
|
|
|
|
return global_feat
|
|
|
|
# training
|
2020-04-19 12:54:01 +08:00
|
|
|
try:
|
|
|
|
pred_class_logits = self.classifier(global_feat)
|
|
|
|
except TypeError:
|
|
|
|
pred_class_logits = self.classifier(global_feat, targets)
|
2020-04-27 14:49:58 +08:00
|
|
|
return pred_class_logits, global_feat, targets
|