2020-04-19 12:54:01 +08:00
|
|
|
# encoding: utf-8
|
|
|
|
"""
|
|
|
|
@author: liaoxingyu
|
|
|
|
@contact: sherlockliao01@gmail.com
|
|
|
|
"""
|
|
|
|
|
2020-05-01 09:02:46 +08:00
|
|
|
from fastreid.layers import *
|
|
|
|
from fastreid.utils.weight_init import weights_init_kaiming
|
2020-04-19 12:54:01 +08:00
|
|
|
from .build import REID_HEADS_REGISTRY
|
|
|
|
|
|
|
|
|
|
|
|
@REID_HEADS_REGISTRY.register()
|
|
|
|
class ReductionHead(nn.Module):
|
2020-04-29 21:29:48 +08:00
|
|
|
def __init__(self, cfg, in_feat, num_classes, pool_layer=nn.AdaptiveAvgPool2d(1)):
|
2020-04-19 12:54:01 +08:00
|
|
|
super().__init__()
|
2020-05-01 09:02:46 +08:00
|
|
|
|
2020-04-19 12:54:01 +08:00
|
|
|
reduction_dim = cfg.MODEL.HEADS.REDUCTION_DIM
|
|
|
|
|
2020-05-01 09:02:46 +08:00
|
|
|
self.pool_layer = pool_layer
|
2020-04-19 12:54:01 +08:00
|
|
|
|
|
|
|
self.bottleneck = nn.Sequential(
|
2020-05-01 09:02:46 +08:00
|
|
|
nn.Conv2d(in_feat, reduction_dim, 1, 1, bias=False),
|
2020-05-14 11:36:28 +08:00
|
|
|
get_norm(cfg.MODEL.HEADS.NORM, reduction_dim, cfg.MODEL.HEADS.NORM_SPLIT, bias_freeze=True),
|
2020-04-19 12:54:01 +08:00
|
|
|
nn.LeakyReLU(0.1),
|
2020-05-01 09:02:46 +08:00
|
|
|
nn.Dropout2d(0.5),
|
2020-04-19 12:54:01 +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)
|
2020-04-19 12:54:01 +08:00
|
|
|
|
|
|
|
self.bottleneck.apply(weights_init_kaiming)
|
|
|
|
self.bnneck.apply(weights_init_kaiming)
|
|
|
|
|
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(reduction_dim, num_classes, bias=False)
|
2020-04-19 12:54:01 +08:00
|
|
|
elif cfg.MODEL.HEADS.CLS_LAYER == 'arcface':
|
|
|
|
self.classifier = Arcface(cfg, reduction_dim)
|
|
|
|
elif cfg.MODEL.HEADS.CLS_LAYER == 'circle':
|
|
|
|
self.classifier = Circle(cfg, reduction_dim)
|
|
|
|
else:
|
2020-04-29 21:29:48 +08:00
|
|
|
self.classifier = nn.Linear(reduction_dim, num_classes, bias=False)
|
2020-04-19 12:54:01 +08:00
|
|
|
|
|
|
|
def forward(self, features, targets=None):
|
|
|
|
"""
|
|
|
|
See :class:`ReIDHeads.forward`.
|
|
|
|
"""
|
|
|
|
global_feat = self.pool_layer(features)
|
|
|
|
global_feat = self.bottleneck(global_feat)
|
|
|
|
bn_feat = self.bnneck(global_feat)
|
2020-05-01 09:02:46 +08:00
|
|
|
bn_feat = Flatten()(bn_feat)
|
|
|
|
# Evaluation
|
2020-04-19 12:54:01 +08:00
|
|
|
if not self.training:
|
|
|
|
return bn_feat
|
2020-05-01 09:02:46 +08:00
|
|
|
# Training
|
2020-04-19 12:54:01 +08:00
|
|
|
try:
|
|
|
|
pred_class_logits = self.classifier(bn_feat)
|
|
|
|
except TypeError:
|
|
|
|
pred_class_logits = self.classifier(bn_feat, targets)
|
2020-05-01 09:02:46 +08:00
|
|
|
|
|
|
|
if self.neck_feat == "before":
|
|
|
|
feat = Flatten()(global_feat)
|
|
|
|
elif self.neck_feat == "after":
|
|
|
|
feat = bn_feat
|
|
|
|
else:
|
|
|
|
raise KeyError("MODEL.HEADS.NECK_FEAT value is invalid, must choose from ('after' & 'before')")
|
|
|
|
return pred_class_logits, feat, targets
|