Shortcuts

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)[源代码]

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

示例

To use ArcFace in config files.

  1. 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),
)
  1. 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),
)
  1. 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')]
参数:
  • 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.

forward(feats, target=None)[源代码]

The forward process.

loss(feats, data_samples, **kwargs)[源代码]

Calculate losses from the classification score.

参数:
  • 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.

返回:

a dictionary of loss components

返回类型:

dict[str, Tensor]

pre_logits(feats)[源代码]

The process before the final classification head.

The input feats is a tuple of tensor, and each tensor is the feature of a backbone stage. In ArcFaceHead, we just obtain the feature of the last stage.

set_margins(margins)[源代码]

set margins of arcface head.

参数:

margins (Union[Sequence[float], float]) – The marigins.

Read the Docs v: latest
Versions
latest
stable
mmcls-1.x
mmcls-0.x
dev
Downloads
epub
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.