OneHot
Bases: Transform
One-hot encode label maps.
Each label map with \(K\) classes (including background) is converted from shape \((1, I, J, K)\) to \((K, I, J, K)\), where channel \(k\) is 1 where the label equals \(k\) and 0 elsewhere.
Only LabelMap images are affected.
ScalarImage instances are left unchanged.
The inverse takes the argmax across channels, restoring the original single-channel label map.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_classes
|
int
|
Total number of classes. |
-1
|
**kwargs
|
Any
|
See |
{}
|
Examples:
>>> import torchio as tio
>>> transform = tio.OneHot()
>>> transform = tio.OneHot(num_classes=5)
>>> # Invert back to single-channel
>>> restored = transformed.apply_inverse_transform()
Source code in src/torchio/transforms/label/one_hot.py
invertible
property
Whether this transform can be inverted.
forward(data)
Apply the transform.
The output type always matches the input type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
Input data to transform. |
required |
Source code in src/torchio/transforms/transform.py
to_hydra()
Export as a Hydra-compatible config dict.
Returns a dict with _target_ set to the fully qualified
class name and only non-default field values included.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict suitable for |
Source code in src/torchio/transforms/transform.py
make_params(batch)
apply_transform(batch, params)
One-hot encode each label map in the batch.