OneHot#
- class deeptrack.features.OneHot(num_classes: int | Callable[[...], int], **kwargs: Any)#
Bases:
FeatureConvert the input to a one-hot encoded array.
This feature takes an input array of integer class labels and converts it into a one-hot encoded array. The last dimension of the input is replaced by the one-hot encoding.
Parameters#
- num_classes: PropertyLike[int]
The total number of classes for the one-hot encoding.
- **kwargs: Any
Additional keyword arguments passed to the parent Feature class.
Methods#
- get(inputs, num_classes, **kwargs) -> array or tensor
Convert the input array of class labels into a one-hot encoded array. The input and output can be NumPy arrays or PyTorch tensors.
Examples#
>>> import deeptrack as dt
Create an input array of class labels:
>>> import numpy as np >>> >>> input_data = np.array([0, 1, 2])
Apply a OneHot feature:
>>> one_hot_feature = dt.OneHot(num_classes=3) >>> one_hot_encoded = one_hot_feature.get(input_data, num_classes=3) >>> one_hot_encoded array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]], dtype=float32)
Methods Summary
get(image, num_classes, **kwargs)Convert the input array of labels into a one-hot encoded array.
Methods Documentation
- get(image: ndarray | Tensor, num_classes: int, **kwargs: Any) ndarray | Tensor#
Convert the input array of labels into a one-hot encoded array.
Parameters#
- image: array or tensor
The input array of class labels. The last dimension should contain integers representing class indices. The input can be a NumPy array or a PyTorch tensor.
- num_classes: int
The total number of classes for the one-hot encoding.
- **kwargs: Any
Additional keyword arguments (unused here).
Returns#
- array or tensor
The one-hot encoded array. The last dimension is replaced with one-hot vectors of length num_classes. The output can be a NumPy array or a PyTorch tensor. In all cases, it is of data type float32 (e.g., np.float32 or torch.float32).