2020-07-07 19:32:06 +08:00
|
|
|
import torch.nn as nn
|
|
|
|
|
|
|
|
from ..builder import CLASSIFIERS, build_backbone, build_head, build_neck
|
2021-04-14 21:27:42 +08:00
|
|
|
from ..utils import BatchCutMixLayer, BatchMixupLayer
|
2020-07-07 19:32:06 +08:00
|
|
|
from .base import BaseClassifier
|
|
|
|
|
|
|
|
|
|
|
|
@CLASSIFIERS.register_module()
|
|
|
|
class ImageClassifier(BaseClassifier):
|
|
|
|
|
2021-02-25 14:06:58 +08:00
|
|
|
def __init__(self,
|
|
|
|
backbone,
|
|
|
|
neck=None,
|
|
|
|
head=None,
|
|
|
|
pretrained=None,
|
|
|
|
train_cfg=None):
|
2020-07-07 19:32:06 +08:00
|
|
|
super(ImageClassifier, self).__init__()
|
2021-02-25 14:06:58 +08:00
|
|
|
|
2020-07-07 19:32:06 +08:00
|
|
|
self.backbone = build_backbone(backbone)
|
|
|
|
|
|
|
|
if neck is not None:
|
|
|
|
self.neck = build_neck(neck)
|
|
|
|
|
|
|
|
if head is not None:
|
|
|
|
self.head = build_head(head)
|
|
|
|
|
2021-04-14 21:27:42 +08:00
|
|
|
self.mixup, self.cutmix = None, None
|
2021-02-25 14:06:58 +08:00
|
|
|
if train_cfg is not None:
|
|
|
|
mixup_cfg = train_cfg.get('mixup', None)
|
2021-04-14 21:27:42 +08:00
|
|
|
cutmix_cfg = train_cfg.get('cutmix', None)
|
|
|
|
assert mixup_cfg is None or cutmix_cfg is None, \
|
|
|
|
'Mixup and CutMix can not be set simultaneously.'
|
|
|
|
if mixup_cfg is not None:
|
|
|
|
self.mixup = BatchMixupLayer(**mixup_cfg)
|
|
|
|
if cutmix_cfg is not None:
|
|
|
|
self.cutmix = BatchCutMixLayer(**cutmix_cfg)
|
2021-02-25 14:06:58 +08:00
|
|
|
|
2020-07-07 19:32:06 +08:00
|
|
|
self.init_weights(pretrained=pretrained)
|
|
|
|
|
|
|
|
def init_weights(self, pretrained=None):
|
|
|
|
super(ImageClassifier, self).init_weights(pretrained)
|
|
|
|
self.backbone.init_weights(pretrained=pretrained)
|
|
|
|
if self.with_neck:
|
|
|
|
if isinstance(self.neck, nn.Sequential):
|
|
|
|
for m in self.neck:
|
|
|
|
m.init_weights()
|
|
|
|
else:
|
|
|
|
self.neck.init_weights()
|
|
|
|
if self.with_head:
|
|
|
|
self.head.init_weights()
|
|
|
|
|
|
|
|
def extract_feat(self, img):
|
2021-04-14 21:22:37 +08:00
|
|
|
"""Directly extract features from the backbone + neck."""
|
2020-07-07 19:32:06 +08:00
|
|
|
x = self.backbone(img)
|
|
|
|
if self.with_neck:
|
|
|
|
x = self.neck(x)
|
|
|
|
return x
|
|
|
|
|
|
|
|
def forward_train(self, img, gt_label, **kwargs):
|
|
|
|
"""Forward computation during training.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
img (Tensor): of shape (N, C, H, W) encoding input images.
|
|
|
|
Typically these should be mean centered and std scaled.
|
2021-01-25 18:10:14 +08:00
|
|
|
gt_label (Tensor): It should be of shape (N, 1) encoding the
|
|
|
|
ground-truth label of input images for single label task. It
|
|
|
|
shoulf be of shape (N, C) encoding the ground-truth label
|
|
|
|
of input images for multi-labels task.
|
2020-07-07 19:32:06 +08:00
|
|
|
Returns:
|
|
|
|
dict[str, Tensor]: a dictionary of loss components
|
|
|
|
"""
|
2021-02-25 14:06:58 +08:00
|
|
|
if self.mixup is not None:
|
|
|
|
img, gt_label = self.mixup(img, gt_label)
|
|
|
|
|
2021-04-14 21:27:42 +08:00
|
|
|
if self.cutmix is not None:
|
|
|
|
img, gt_label = self.cutmix(img, gt_label)
|
|
|
|
|
2020-07-07 19:32:06 +08:00
|
|
|
x = self.extract_feat(img)
|
|
|
|
|
|
|
|
losses = dict()
|
|
|
|
loss = self.head.forward_train(x, gt_label)
|
|
|
|
losses.update(loss)
|
|
|
|
|
|
|
|
return losses
|
|
|
|
|
2021-01-26 11:24:08 +08:00
|
|
|
def simple_test(self, img, img_metas):
|
2020-07-07 19:32:06 +08:00
|
|
|
"""Test without augmentation."""
|
|
|
|
x = self.extract_feat(img)
|
|
|
|
return self.head.simple_test(x)
|