detectors package

Subpackages

Submodules

detectors.config module

Configuration for the project.

It is used to set the default paths for the data, checkpoints, results, etc.

Constants:

  • DATA_DIR: The directory where the data is stored.

  • IMAGENET_ROOT: The directory where the ImageNet data is stored.

  • CHECKPOINTS_DIR: The directory where the checkpoints are stored.

  • RESULTS_DIR: The directory where the results are stored.

detectors.criterions module

class detectors.criterions.CSILoss(temperature=0.07, contrast_mode='all', base_temperature=0.07, lbd=1)[source]

Bases: SupConLoss

Contrasting shifted instances (con-SI) loss.

References

[1] https://arxiv.org/abs/2007.08176

forward(features, shift_labels, labels=None, mask=None)[source]

Compute loss for model. If both labels and mask are None, it degenerates to SimCLR unsupervised loss.

Parameters:
  • features – hidden vector of shape [bsz, n_views, …].

  • labels – ground truth of shape [bsz].

  • mask – contrastive mask of shape [bsz, bsz], mask_{i,j}=1 if sample j has the same class as sample i. Can be asymmetric.

Returns:

A loss scalar.

training: bool
class detectors.criterions.SupConLoss(temperature=0.07, contrast_mode='all', base_temperature=0.07)[source]

Bases: Module

Supervised Contrastive Learning. It also supports the unsupervised contrastive loss in SimCLR: A Simple Framework for Contrastive Learning of Visual Representations

References

[1] https://github.com/HobbitLong/SupContrast [2] https://arxiv.org/pdf/2004.11362.pdf [3] https://arxiv.org/abs/2002.05709

forward(features, labels=None, mask=None)[source]

Compute loss for model. If both labels and mask are None, it degenerates to SimCLR unsupervised loss.

Parameters:
  • features – hidden vector of shape [bsz, n_views, …].

  • labels – ground truth of shape [bsz].

  • mask – contrastive mask of shape [bsz, bsz], mask_{i,j}=1 if sample j has the same class as sample i. Can be asymmetric.

Returns:

A loss scalar.

training: bool

detectors.eval module

Module containing evaluation metrics.

detectors.eval.compute_detection_error(fpr: float, tpr: float, pos_ratio: float)[source]

Compute the detection error.

Parameters:
  • fpr (float) – False positive rate at a fixed TPR.

  • tpr (float) – True positive rate.

  • pos_ratio (float) – Ratio of positive labels.

Returns:

Detection error.

Return type:

float

detectors.eval.fpr_at_fixed_tpr(fprs: ndarray, tprs: ndarray, thresholds: ndarray, tpr_level: float = 0.95)[source]

Return the FPR at a fixed TPR level.

Parameters:
  • fprs (np.ndarray) – False positive rates.

  • tprs (np.ndarray) – True positive rates.

  • thresholds (np.ndarray) – Thresholds.

  • tpr_level (float, optional) – TPR level. Defaults to 0.95.

Returns:

FPR, TPR, threshold.

Return type:

Tuple[float, float, float]

detectors.eval.get_ood_results(in_scores: Tensor, ood_scores: Tensor) Dict[str, float][source]

Compute OOD detection metrics.

Parameters:
  • in_scores (Tensor) – In-distribution scores.

  • ood_scores (Tensor) – Out-of-distribution scores.

Returns:

OOD detection metrics.

keys: fpr_at_0.95_tpr, tnr_at_0.95_tpr, detection_error, auroc, aupr_in, aupr_out, thr.

Return type:

Dict[str, float]

detectors.eval.minimum_detection_error(fprs: ndarray, tprs: ndarray, pos_ratio: float)[source]

Compute the minimum detection error.

Parameters:
  • fprs (np.ndarray) – False positive rates.

  • tprs (np.ndarray) – True positive rates.

  • thresholds (np.ndarray) – Thresholds.

  • pos_ratio (float) – Ratio of positive labels.

Returns:

FPR, TPR, threshold.

Return type:

Tuple[float, float, float]

detectors.trainer module

Trainer for classification models.

detectors.trainer.get_criterion_cls(criterion_name: str) _Loss[source]
detectors.trainer.get_optimizer_cls(optimizer_name: str) Optimizer[source]
detectors.trainer.get_scheduler_cls(scheduler_name: str) _LRScheduler[source]
detectors.trainer.save_model(model: Module, accelerator: Accelerator, filename: str)[source]
detectors.trainer.trainer_supervised_classification(model: ~torch.nn.modules.module.Module, optimizer: ~torch.optim.optimizer.Optimizer, scheduler: ~torch.optim.lr_scheduler._LRScheduler, criterion: ~typing.Callable, train_loader: ~torch.utils.data.dataloader.DataLoader, val_loader: ~torch.utils.data.dataloader.DataLoader, save_root: str, training_function=<function training_iteration>, validation_function=<function validation_iteration>, epochs=10, validation_frequency=1, seed: int = 42)[source]
detectors.trainer.training_iteration(batch: Tuple[Tensor, Tensor], model: Module, optimizer: Optimizer, criterion: Callable, accelerator: Accelerator)[source]
detectors.trainer.validation_iteration(batch: Tuple[Tensor, Tensor], model: Module, criterion: Callable, accelerator: Accelerator)[source]

detectors.utils module

class detectors.utils.ConcatDatasetsDim1(datasets: List[Dataset])[source]

Bases: Dataset

detectors.utils.append_results_to_csv_file(results, filename='results.csv')[source]
detectors.utils.str_to_dict(string: str) Dict[str, Any][source]
detectors.utils.sync_tensor_across_gpus(t: Tensor) Tensor[source]

Gather tensor from all gpus and return a tensor with dim 0 equal to the number of gpus.

Parameters:

t (torch.Tensor) – _description_

Returns:

_description_

Return type:

torch.Tensor

References

https://discuss.pytorch.org/t/ddp-evaluation-gather-output-loss-and-stuff-how-to/130593/2

Module contents

Detectors package.