discopat.nn_training.detr package

Submodules

discopat.nn_training.detr.collate module

discopat.nn_training.detr.collate.collate_fn(batch)[source]

discopat.nn_training.detr.criterion module

DETR criterion class.

class discopat.nn_training.detr.criterion.SetCriterion(num_classes, matcher, weight_dict, eos_coef, losses)[source]

Bases: Module

Compute the loss for DETR.

The process happens in two steps:
  1. we compute hungarian assignment between ground truth boxes and the outputs of the model

  2. we supervise each pair of matched ground-truth / prediction (supervise class and box)

forward(outputs, targets)[source]

Perform the loss computation.

Parameters:
  • outputs (dict of tensors, see the output specification of the model for the format)

  • targets (list of dicts, such that len(targets) == batch_size.) – The expected keys in each dict depends on the losses applied, see each loss’ doc

get_loss(loss, outputs, targets, indices, num_boxes, **kwargs)[source]
loss_boxes(outputs, targets, indices, num_boxes)[source]

Compute the losses related to the bounding boxes, the L1 regression loss and the GIoU loss.

targets dicts must contain the key “boxes” containing a tensor of dim [nb_target_boxes, 4] The target boxes are expected in format (center_x, center_y, w, h), normalized by the image size.

loss_cardinality(outputs, targets, indices, num_boxes)[source]

Compute the cardinality error, ie the absolute error in the number of predicted non-empty boxes.

This is not really a loss, it is intended for logging purposes only. It doesn’t propagate gradients

loss_labels(outputs, targets, indices, num_boxes, log=True)[source]

Classification loss (NLL).

targets dicts must contain the key “labels” containing a tensor of dim [nb_target_boxes]

discopat.nn_training.detr.criterion.accuracy(output, target, topk=(1,))[source]

Compute the precision@k for the specified values of k.

discopat.nn_training.detr.detr_trainer module

class discopat.nn_training.detr.detr_trainer.DetrTrainer(net, dataset, val_dataset, parameters, device, callbacks=None, num_classes=0)[source]

Bases: NNTrainer

Parameters:
set_default_lr_scheduler()[source]
Return type:

LRScheduler

set_default_optimiser()[source]
Return type:

Optimizer

train(num_epochs)[source]
Parameters:

num_epochs (int)

discopat.nn_training.detr.engine module

Train and eval functions used in main.py.

discopat.nn_training.detr.engine.evaluate(model, criterion, postprocessors, data_loader, base_ds, device, output_dir)[source]
discopat.nn_training.detr.engine.train_one_epoch(model, criterion, data_loader, optimizer, device, epoch, max_norm=0)[source]
Parameters:
  • model (Module)

  • criterion (Module)

  • data_loader (Iterable)

  • optimizer (Optimizer)

  • device (device)

  • epoch (int)

  • max_norm (float)

discopat.nn_training.detr.matcher module

Compute the matching cost and solve the corresponding LSAP.

class discopat.nn_training.detr.matcher.HungarianMatcher(cost_class=1, cost_bbox=1, cost_giou=1)[source]

Bases: Module

Compute an assignment between the targets and the predictions of the network.

For efficiency reasons, the targets don’t include the no_object. Because of this, in general, there are more predictions than targets. In this case, we do a 1-to-1 matching of the best predictions, while the others are un-matched (and thus treated as non-objects).

Parameters:
  • cost_class (float)

  • cost_bbox (float)

  • cost_giou (float)

forward(outputs, targets)[source]

Perform the matching.

Params:
outputs: This is a dict that contains at least these entries:

“pred_logits”: Tensor of dim [batch_size, num_queries, num_classes] with the classification logits “pred_boxes”: Tensor of dim [batch_size, num_queries, 4] with the predicted box coordinates

targets: This is a list of targets (len(targets) = batch_size), where each target is a dict containing:
“labels”: Tensor of dim [num_target_boxes] (where num_target_boxes is the number of ground-truth

objects in the target) containing the class labels

“boxes”: Tensor of dim [num_target_boxes, 4] containing the target box coordinates

Returns:

  • index_i is the indices of the selected predictions (in order)
    • index_j is the indices of the corresponding selected targets (in order)

For each batch element, it holds:

len(index_i) = len(index_j) = min(num_queries, num_target_boxes)

Return type:

A list of size batch_size, containing tuples of (index_i, index_j) where

discopat.nn_training.detr.nested_tensor module

class discopat.nn_training.detr.nested_tensor.NestedTensor(tensors, mask)[source]

Bases: object

Parameters:

mask (Tensor | None)

decompose()[source]
to(device)[source]
Parameters:

device (Device)

Return type:

NestedTensor

discopat.nn_training.detr.nested_tensor.nested_tensor_from_tensor_list(tensor_list)[source]
Parameters:

tensor_list (list[Tensor])

Module contents