mmclassification#

Adapters of classification - mmcls.

class otx.algorithms.classification.adapters.mmcls.BYOL(backbone: Dict[str, Any], neck: Optional[Dict[str, Any]] = None, head: Optional[Dict[str, Any]] = None, pretrained: Optional[str] = None, base_momentum: float = 0.996, **kwargs)#

Implementation of ‘Bootstrap Your Own Latent: A New Approach to Self-Supervised Learning (https://arxiv.org/abs/2006.07733)’.

Args:

backbone (dict): Config dict for module of backbone ConvNet. neck (dict): Config dict for module of deep features to compact feature vectors.

Default: None.

head (dict): Config dict for module of loss functions. Default: None. pretrained (str, optional): Path to pre-trained weights. Default: None. base_momentum (float): The base momentum coefficient for the target network.

Default: 0.996.

forward(img1: Tensor, img2: Tensor, **kwargs)#

Forward computation during training.

Args:
img1 (Tensor): Input of two concatenated images of shape (N, C, H, W).

Typically these should be mean centered and std scaled.

img2 (Tensor): Input of two concatenated images of shape (N, C, H, W).

Typically these should be mean centered and std scaled.

Returns:

dict[str, Tensor]: A dictionary of loss components.

init_weights(pretrained: Optional[str] = None)#

Initialize the weights of model.

Args:
pretrained (str, optional): Path to pre-trained weights.

Default: None.

momentum_update()#

Momentum update of the target network.

static state_dict_hook(module, state_dict, prefix, *args, **kwargs)#

Save only online backbone as output state_dict.

train_step(data: Dict[str, Any], optimizer)#

The iteration step during training.

This method defines an iteration step during training, except for the back propagation and optimizer updating, which are done in an optimizer hook. Note that in some complicated cases or models, the whole process including back propagation and optimizer updating are also defined in this method, such as GAN.

Args:

data (dict): The output of dataloader. optimizer (torch.optim.Optimizer | dict): The optimizer of

runner is passed to train_step(). This argument is unused and reserved.

Returns:
dict: It should contain at least 3 keys: loss, log_vars,

num_samples. loss is a tensor for back propagation, which can be a weighted sum of multiple losses. log_vars contains all the variables to be sent to the logger. num_samples indicates the batch size (when the model is DDP, it means the batch size on each GPU), which is used for averaging the logs.

training: bool#
val_step(*args)#

Disable validation step during self-supervised learning.

class otx.algorithms.classification.adapters.mmcls.ConstrastiveHead(predictor: Dict[str, Any], size_average: bool = True, **kwargs)#

Head for contrastive learning.

Args:

predictor (dict): configurations for predictor. size_average (bool): whether averaging loss using batch size. Default value is True.

forward(inputs: Tensor, targets: Tensor)#

Forward head.

Args:

inputs (Tensor): NxC input features. targets (Tensor): NxC target features.

Returns:

dict[str, Tensor]: A dictionary of loss components.

init_weights(init_linear: str = 'normal')#

Initialize predictor weights.

Args:

init_linear (str): Option to initialize weights. Default: ‘normal’

training: bool#
class otx.algorithms.classification.adapters.mmcls.LARS(params, lr=<required parameter>, momentum=0, dampening=0, weight_decay=0, eta=0.001, nesterov=False, mode=None, exclude_bn_from_weight_decay=False)#

Implements layer-wise adaptive rate scaling for SGD.

Args:
params (iterable): iterable of parameters to optimize or dicts defining

parameter groups

lr (float): base learning rate (gamma_0) momentum (float, optional): momentum factor (default: 0) (“m”) weight_decay (float, optional): weight decay (L2 penalty) (default: 0)

(”beta”)

dampening (float, optional): dampening for momentum (default: 0) eta (float, optional): LARS coefficient nesterov (bool, optional): enables Nesterov momentum (default: False)

Based on Algorithm 1 of the following paper by You, Gitman, and Ginsburg. Large Batch Training of Convolutional Networks:

Example:
>>> optimizer = LARS(model.parameters(), lr=0.1, momentum=0.9,
>>>                  weight_decay=1e-4, eta=1e-3)
>>> optimizer.zero_grad()
>>> loss_fn(model(input), target).backward()
>>> optimizer.step()
step(closure=None)#

Performs a single optimization step.

Args:
closure (callable, optional): A closure that reevaluates the model

and returns the loss.

class otx.algorithms.classification.adapters.mmcls.OTXClsDataset(otx_dataset: DatasetEntity, labels: List[LabelEntity], empty_label=None, **kwargs)#

Multi-class classification dataset class.

class_accuracy(results, gt_labels)#

Return per-class accuracy.

evaluate(results, metric='accuracy', metric_options=None, logger=None)#

Evaluate the dataset with new metric class_accuracy.

Args:

results (list): Testing results of the dataset. metric (str | list[str]): Metrics to be evaluated.

Default value is accuracy. ‘accuracy’, ‘precision’, ‘recall’, ‘f1_score’, ‘support’, ‘class_accuracy’

metric_options (dict, optional): Options for calculating metrics.

Allowed keys are ‘topk’, ‘thrs’ and ‘average_mode’. Defaults to None.

logger (logging.Logger | str, optional): Logger used for printing

related information during evaluation. Defaults to None.

Returns:

dict: evaluation results

get_gt_labels()#

Get all ground-truth labels (categories).

Returns:

list[int]: categories for all images.

get_indices(*args)#

Get indices.

load_annotations()#

Load annotations.

class otx.algorithms.classification.adapters.mmcls.SelfSLDataset(otx_dataset: DatasetEntity, pipeline: Dict[str, Any], **kwargs)#

SelfSL dataset that training with two pipelines and no label.

CLASSES = None#
class otx.algorithms.classification.adapters.mmcls.SelfSLMLP(in_channels: int, hid_channels: int, out_channels: int, norm_cfg: Optional[Dict[str, Any]] = None, use_conv: bool = False, with_avg_pool: bool = True)#

The SelfSLMLP neck: fc/conv-bn-relu-fc/conv.

Args:

in_channels (int): The number of feature output channels from backbone. hid_channels (int): The number of channels for a hidden layer. out_channels (int): The number of output channels of SelfSLMLP. norm_cfg (dict): Normalize configuration. Default: dict(type=”BN1d”). use_conv (bool): Whether using conv instead of fc. Default: False. with_avg_pool (bool): Whether using average pooling before passing MLP.

Default: True.

forward(x: Union[Tensor, Tuple, List])#

Forward SelfSLMLP.

Args:
x (Tensor, tuple, list): Inputs to pass MLP.

If a type of the inputs is tuple or list, just use the last index.

Return:

Tensor: Features passed SelfSLMLP.

init_weights(init_linear: str = 'normal', std: float = 0.01, bias: float = 0.0)#

Initialize SelfSLMLP weights.

Args:

init_linear (str): Option to initialize weights. Default: “normal”. std (float): Standard deviation for normal initialization. Default: 0.01. bias (float): Bias for normal initialization. Default: 0.

training: bool#