deeplabcut.pose_estimation_pytorch.models.heads.base
Classes:
| Name | Description |
|---|---|
BaseHead |
A head for pose estimation models. |
WeightConversionMixin |
A mixin for heads that can re-order and/or filter the output channels. |
BaseHead
A head for pose estimation models.
Attributes:
| Name | Type | Description |
|---|---|---|
stride |
The stride for the head (or neck + head pair), where positive values
indicate an increase in resolution while negative values a decrease.
Assuming that H and W are divisible by |
|
predictor |
an object to generate predictions from the head outputs |
|
target_generator |
a target generator which must output a target for each output key of this module (i.e. if forward returns a "heatmap" tensor and an "offset" tensor, then targets must be generated for both) |
|
criterion |
either a single criterion (e.g. if this head only outputs heatmaps) or a dictionary mapping the outputs of this head to the criterion to use (e.g. a "heatmap" criterion and an "offset" criterion for DEKR). |
|
aggregator |
if the criterion is a dictionary, cannot be none. used to combine the individual losses from this head into one "total_loss" |
Methods:
| Name | Description |
|---|---|
forward |
Given the feature maps for an image () |
get_loss |
Computes the loss for this head. |
Source code in deeplabcut/pose_estimation_pytorch/models/heads/base.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | |
forward
abstractmethod
get_loss
get_loss(outputs: dict[str, Tensor], targets: dict[str, dict[str, Tensor]]) -> dict[str, torch.Tensor]
Computes the loss for this head.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict[str, Tensor]
|
the outputs of this head |
required |
|
dict[str, dict[str, Tensor]]
|
the targets for this head |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
A dictionary containing minimally "total_loss" key mapping to the total loss for this head (from which backwards() should be called). Can contain other keys containing losses that can be logged for informational purposes. |
Source code in deeplabcut/pose_estimation_pytorch/models/heads/base.py
WeightConversionMixin
Bases: ABC
A mixin for heads that can re-order and/or filter the output channels.
This mixin is useful to convert SuperAnimal model weights such that they can be used in downstream projects (either existing or new), where only a subset of keypoints are available (and where they might be re-ordered).
Methods:
| Name | Description |
|---|---|
convert_weights |
Converts pre-trained weights to be fine-tuned on another dataset. |
Source code in deeplabcut/pose_estimation_pytorch/models/heads/base.py
convert_weights
abstractmethod
staticmethod
convert_weights(state_dict: dict[str, Tensor], module_prefix: str, conversion: Tensor) -> dict[str, torch.Tensor]
Converts pre-trained weights to be fine-tuned on another dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict[str, Tensor]
|
the state dict for the pre-trained model |
required |
|
str
|
the prefix for weights in this head (e.g., 'heads.bodypart.') |
required |
|
Tensor
|
the mapping of old indices to new indices |
required |
Examples:
A SuperAnimal model was trained on the keypoints ["ear_left", "ear_right", "eye_left", "eye_right", "nose"]. A down-stream project has the bodyparts labeled ["nose", "eye_left", "eye_right"]. The SuperAnimal weights can be converted (to be used with the downstream project) with the following code:
``
state_dict = torch.load(
snapshot_path, map_location=torch.device('cpu')
)["model"]
state_dict = HeadClass.convert_weights(
state_dict,
"heads.bodypart",
[4, 2, 3]
)
``