Configures weights initialization when transfer learning or fine-tuning models. Args: snapshot_path: The path to the snapshot used to initialize pose model weights when training a model. detector_snapshot_path: The path to the snapshot used to initialize detector
| 21 | |
| 22 | @dataclass |
| 23 | class WeightInitialization: |
| 24 | """Configures weights initialization when transfer learning or fine-tuning models. |
| 25 | |
| 26 | Args: |
| 27 | snapshot_path: The path to the snapshot used to initialize pose model weights |
| 28 | when training a model. |
| 29 | detector_snapshot_path: The path to the snapshot used to initialize detector |
| 30 | weights when training a model. |
| 31 | dataset: Optionally, the dataset on which the snapshots were trained. Required |
| 32 | when fine-tuning SuperAnimal models. |
| 33 | with_decoder: Whether to load the decoder weights as well. |
| 34 | memory_replay: Only when ``with_decoder=True``. Whether to train the model with |
| 35 | memory replay, so that it predicts all SuperAnimal (or previous project) |
| 36 | bodyparts. |
| 37 | conversion_array: The mapping from SuperAnimal (or other project, on which the |
| 38 | weights were trained) to project bodyparts. Required when |
| 39 | `with_decoder=True`. |
| 40 | An array [7, 0, 1] means the project has 3 bodyparts, where the 1st bodypart |
| 41 | corresponds to the 8th bodypart in the pretrained model, the 2nd to the 1st |
| 42 | and the 3rd to the 2nd (as arrays are 0-indexed). |
| 43 | bodyparts: Optionally, the name of each bodypart entry in the conversion array. |
| 44 | """ |
| 45 | |
| 46 | snapshot_path: Path |
| 47 | detector_snapshot_path: Path | None = None |
| 48 | dataset: str | None = None |
| 49 | with_decoder: bool = False |
| 50 | memory_replay: bool = False |
| 51 | conversion_array: np.ndarray | None = None |
| 52 | bodyparts: list[str] | None = None |
| 53 | |
| 54 | def __post_init__(self): |
| 55 | if self.memory_replay and not self.with_decoder: |
| 56 | raise ValueError( |
| 57 | "You cannot train a model with memory replay if you do not keep the " |
| 58 | "decoder layers (``with_decoder=True``), but you passed " |
| 59 | "`memory_replay=True` and `with_decoder=False`. Please change your " |
| 60 | "WeightInitialization parameters." |
| 61 | ) |
| 62 | |
| 63 | if self.with_decoder and self.conversion_array is None: |
| 64 | raise ValueError( |
| 65 | "You must specify a conversion_array to initialize decoder weights (``with_decoder=True``)." |
| 66 | ) |
| 67 | |
| 68 | if self.bodyparts is not None and self.conversion_array is None: |
| 69 | raise ValueError( |
| 70 | "Specifying bodyparts should only be done when `with_decoder=True` and" |
| 71 | " the conversion array is specified." |
| 72 | ) |
| 73 | |
| 74 | if self.conversion_array is not None and self.bodyparts is not None: |
| 75 | if not len(self.conversion_array) == len(self.bodyparts): |
| 76 | raise ValueError( |
| 77 | "There must be the same number of elements in the bodyparts list " |
| 78 | "and conv. array; found {self.bodyparts}, {self.conversion_array}" |
| 79 | ) |
| 80 |
no outgoing calls
no test coverage detected