ArcFaceClsHead¶
- class mmpretrain.models.heads.ArcFaceClsHead(num_classes, in_channels, num_subcenters=1, scale=64.0, margins=0.5, easy_margin=False, loss={'loss_weight': 1.0, 'type': 'CrossEntropyLoss'}, init_cfg=None)[source]¶
ArcFace classifier head.
A PyTorch implementation of paper ArcFace: Additive Angular Margin Loss for Deep Face Recognition and Sub-center ArcFace: Boosting Face Recognition by Large-Scale Noisy Web Faces
Example
To use ArcFace in config files.
use vanilla ArcFace
mode = dict( backbone = xxx, neck = xxxx, head=dict( type='ArcFaceClsHead', num_classes=5000, in_channels=1024, loss = dict(type='CrossEntropyLoss', loss_weight=1.0), init_cfg=None), )
use SubCenterArcFace with 3 sub-centers
mode = dict( backbone = xxx, neck = xxxx, head=dict( type='ArcFaceClsHead', num_classes=5000, in_channels=1024, num_subcenters=3, loss = dict(type='CrossEntropyLoss', loss_weight=1.0), init_cfg=None), )
use SubCenterArcFace With CountPowerAdaptiveMargins
mode = dict( backbone = xxx, neck = xxxx, head=dict( type='ArcFaceClsHead', num_classes=5000, in_channels=1024, num_subcenters=3, loss = dict(type='CrossEntropyLoss', loss_weight=1.0), init_cfg=None), ) custom_hooks = [dict(type='SetAdaptiveMarginsHook')]
- Parameters:
num_classes (int) – Number of categories excluding the background category.
in_channels (int) – Number of channels in the input feature map.
num_subcenters (int) – Number of subcenters. Defaults to 1.
scale (float) – Scale factor of output logit. Defaults to 64.0.
margins (float) –
The penalty margin. Could be the fllowing formats:
float: The margin, would be same for all the categories.
Sequence[float]: The category-based margins list.
str: A ‘.txt’ file path which contains a list. Each line represents the margin of a category, and the number in the i-th row indicates the margin of the i-th class.
Defaults to 0.5.
easy_margin (bool) – Avoid theta + m >= PI. Defaults to False.
loss (dict) – Config of classification loss. Defaults to
dict(type='CrossEntropyLoss', loss_weight=1.0)
.init_cfg (dict, optional) – the config to control the initialization. Defaults to None.
- loss(feats, data_samples, **kwargs)[source]¶
Calculate losses from the classification score.
- Parameters:
feats (tuple[Tensor]) – The features extracted from the backbone. Multiple stage inputs are acceptable but only the last stage will be used to classify. The shape of every item should be
(num_samples, num_classes)
.data_samples (List[DataSample]) – The annotation data of every samples.
**kwargs – Other keyword arguments to forward the loss module.
- Returns:
a dictionary of loss components
- Return type: