Representation
- class torchok.tasks.pairwise_task.PairwiseLearnTask(hparams: DictConfig, num_classes: int, backbone_name: str, pooling_name: Optional[str] = None, head_name: Optional[str] = None, neck_name: Optional[str] = None, backbone_params: Optional[dict] = None, neck_params: Optional[dict] = None, pooling_params: Optional[dict] = None, head_params: Optional[dict] = None, inputs: Optional[dict] = None)
Bases:
ClassificationTaskDeep Metric Learning task for pairwise losses.
This task use ClassificationDataset in multilabel mode, to detect similar images. An example config for this task is torchok/examples/configs/pairwise_sop.yaml.
- __init__(hparams: DictConfig, num_classes: int, backbone_name: str, pooling_name: Optional[str] = None, head_name: Optional[str] = None, neck_name: Optional[str] = None, backbone_params: Optional[dict] = None, neck_params: Optional[dict] = None, pooling_params: Optional[dict] = None, head_params: Optional[dict] = None, inputs: Optional[dict] = None)
Init PairwiseLearnTask.
- Parameters
hparams – Hyperparameters that set in yaml file.
num_classes – number of all classes in train multilabel dataset.
backbone_name – name of the backbone architecture in the BACKBONES registry.
pooling_name – name of the backbone architecture in the POOLINGS registry.
head_name – name of the neck architecture in the HEADS registry.
neck_name – if present, name of the head architecture in the NECKS registry. Otherwise, model will be created without neck.
backbone_params – parameters for backbone constructor.
neck_params – parameters for neck constructor. in_channels will be set automatically based on backbone.
pooling_params – parameters for neck constructor. in_channels will be set automatically based on neck or backbone if neck is absent.
head_params – parameters for head constructor. in_channels will be set automatically based on neck.
inputs – information about input model shapes and dtypes.
- forward_with_gt(batch: Dict[str, Tensor]) Dict[str, Tensor]
Forward with ground truth labels.
- Parameters
batch –
Dictionary with the following keys and values:
- image (torch.Tensor):
tensor of shape (B, C, H, W), representing input images.
- target (torch.Tensor):
tensor of shape (B), target class or labels per each image.
- Returns
Dictionary with the following keys and values
’emb1’: model forward method output.
’emb2’: model forward method output same as emb1.
’target’: target value batch if key target in batch, otherwise this key not in output dictionary.
’R’: calculated relevance matrix if key target in batch, otherwise this key not in output dictionary.
- calc_relevance_matrix(y: Tensor) Tensor
Calculates binary relevance matrix given multi-label matrix y.
- Parameters
y – Multi-label matrix of shape (N, L) representing labels for N samples, where L - number of classes.
1 (Values are either 0 or) –
y1[i (where) –
class. (k] = 1 indicate that i-th sample belongs to k-th) –
- Returns
Binary relevance matrix R of shape (N, M) where R[i, j] = 1 means that samples i and j are relevant to each other, dtype=float32.
- class torchok.tasks.triplet_task.TripletLearnTask(hparams: DictConfig, **kwargs)
Bases:
ClassificationTaskA class for triplet learning task.
- __init__(hparams: DictConfig, **kwargs)
Init TripletLearnTask.
- Parameters
hparams – Hyperparameters that set in yaml file.
- forward_with_gt(batch: Dict[str, Union[Tensor, int]]) Dict[str, Tensor]
Forward with ground truth labels.
- validation_step(batch: Dict[str, Union[Tensor, int]], batch_idx: int) Dict[str, Tensor]
Complete validation loop.