2020-09-17 18:17:53 +08:00
|
|
|
# encoding: utf-8
|
|
|
|
"""
|
|
|
|
@author: liaoxingyu
|
|
|
|
@contact: sherlockliao01@gmail.com
|
|
|
|
"""
|
|
|
|
|
2020-09-23 19:45:13 +08:00
|
|
|
import torch
|
2021-03-30 15:47:14 +08:00
|
|
|
import torch.nn.functional as F
|
2020-09-17 18:17:53 +08:00
|
|
|
from torch import nn
|
|
|
|
|
2021-03-30 15:47:14 +08:00
|
|
|
from fastreid.modeling.heads import EmbeddingHead
|
2021-01-18 11:36:38 +08:00
|
|
|
from fastreid.modeling.heads.build import REID_HEADS_REGISTRY
|
2021-05-31 17:27:14 +08:00
|
|
|
from fastreid.layers.weight_init import weights_init_kaiming
|
2020-09-17 18:17:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
@REID_HEADS_REGISTRY.register()
|
2021-03-30 15:47:14 +08:00
|
|
|
class AttrHead(EmbeddingHead):
|
2020-09-17 18:17:53 +08:00
|
|
|
def __init__(self, cfg):
|
2021-03-30 15:47:14 +08:00
|
|
|
super().__init__(cfg)
|
|
|
|
num_classes = cfg.MODEL.HEADS.NUM_CLASSES
|
2020-09-17 18:17:53 +08:00
|
|
|
|
2021-03-30 15:47:14 +08:00
|
|
|
self.bnneck = nn.BatchNorm1d(num_classes)
|
|
|
|
self.bnneck.apply(weights_init_kaiming)
|
2020-09-17 18:17:53 +08:00
|
|
|
|
|
|
|
def forward(self, features, targets=None):
|
|
|
|
"""
|
|
|
|
See :class:`ReIDHeads.forward`.
|
|
|
|
"""
|
2021-03-30 15:47:14 +08:00
|
|
|
pool_feat = self.pool_layer(features)
|
|
|
|
neck_feat = self.bottleneck(pool_feat)
|
2021-03-31 17:07:19 +08:00
|
|
|
neck_feat = neck_feat.view(neck_feat.size(0), -1)
|
2020-09-17 18:17:53 +08:00
|
|
|
|
2021-03-31 17:07:19 +08:00
|
|
|
logits = F.linear(neck_feat, self.weight)
|
|
|
|
logits = self.bnneck(logits)
|
2020-09-23 19:45:13 +08:00
|
|
|
|
2021-03-30 15:47:14 +08:00
|
|
|
# Evaluation
|
2021-01-18 11:36:38 +08:00
|
|
|
if not self.training:
|
2021-03-30 15:47:14 +08:00
|
|
|
cls_outptus = torch.sigmoid(logits)
|
|
|
|
return cls_outptus
|
|
|
|
|
|
|
|
return {
|
2021-03-31 17:07:19 +08:00
|
|
|
"cls_outputs": logits,
|
2021-03-30 15:47:14 +08:00
|
|
|
}
|