2020-02-10 07:38:56 +08:00
|
|
|
# encoding: utf-8
|
|
|
|
"""
|
|
|
|
@author: liaoxingyu
|
|
|
|
@contact: sherlockliao01@gmail.com
|
|
|
|
"""
|
|
|
|
|
2020-04-19 12:54:01 +08:00
|
|
|
from torch import nn
|
2020-02-10 07:38:56 +08:00
|
|
|
|
|
|
|
from .build import META_ARCH_REGISTRY
|
|
|
|
from ..backbones import build_backbone
|
|
|
|
from ..heads import build_reid_heads
|
2020-04-19 12:54:01 +08:00
|
|
|
from ..losses import reid_losses
|
2020-04-27 14:51:39 +08:00
|
|
|
from ...layers import GeneralizedMeanPoolingP
|
2020-02-10 07:38:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
@META_ARCH_REGISTRY.register()
|
|
|
|
class Baseline(nn.Module):
|
|
|
|
def __init__(self, cfg):
|
|
|
|
super().__init__()
|
2020-03-25 10:58:26 +08:00
|
|
|
self._cfg = cfg
|
|
|
|
# backbone
|
2020-02-10 07:38:56 +08:00
|
|
|
self.backbone = build_backbone(cfg)
|
|
|
|
|
2020-03-25 10:58:26 +08:00
|
|
|
# head
|
|
|
|
if cfg.MODEL.HEADS.POOL_LAYER == 'avgpool':
|
|
|
|
pool_layer = nn.AdaptiveAvgPool2d(1)
|
|
|
|
elif cfg.MODEL.HEADS.POOL_LAYER == 'maxpool':
|
|
|
|
pool_layer = nn.AdaptiveMaxPool2d(1)
|
|
|
|
elif cfg.MODEL.HEADS.POOL_LAYER == 'gempool':
|
|
|
|
pool_layer = GeneralizedMeanPoolingP()
|
|
|
|
else:
|
|
|
|
pool_layer = nn.Identity()
|
2020-04-24 12:16:18 +08:00
|
|
|
|
|
|
|
in_feat = cfg.MODEL.HEADS.IN_FEAT
|
|
|
|
self.heads = build_reid_heads(cfg, in_feat, pool_layer)
|
2020-02-18 21:01:23 +08:00
|
|
|
|
2020-03-25 10:58:26 +08:00
|
|
|
def forward(self, inputs):
|
2020-02-18 21:01:23 +08:00
|
|
|
images = inputs["images"]
|
|
|
|
targets = inputs["targets"]
|
2020-02-10 07:38:56 +08:00
|
|
|
|
2020-03-25 10:58:26 +08:00
|
|
|
if not self.training:
|
|
|
|
pred_feat = self.inference(images)
|
|
|
|
return pred_feat, targets, inputs["camid"]
|
|
|
|
|
|
|
|
# training
|
|
|
|
features = self.backbone(images) # (bs, 2048, 16, 8)
|
2020-04-24 12:16:18 +08:00
|
|
|
return self.heads(features, targets)
|
2020-03-25 10:58:26 +08:00
|
|
|
|
|
|
|
def inference(self, images):
|
2020-02-18 21:01:23 +08:00
|
|
|
assert not self.training
|
2020-03-25 10:58:26 +08:00
|
|
|
features = self.backbone(images) # (bs, 2048, 16, 8)
|
|
|
|
pred_feat = self.heads(features)
|
2020-04-27 14:51:39 +08:00
|
|
|
return pred_feat
|
2020-03-25 10:58:26 +08:00
|
|
|
|
|
|
|
def losses(self, outputs):
|
2020-04-27 14:51:39 +08:00
|
|
|
logits, feat, targets = outputs
|
|
|
|
return reid_losses(self._cfg, logits, feat, targets)
|