EnsureShapeMultiple
Bases: SpatialTransform
Ensure that all values in the image shape are divisible by \(n\).
Some convolutional neural network architectures need the size of the input across all spatial dimensions to be a power of 2.
For example, a 3D U-Net with 3 downsampling (pooling) operations needs all spatial dimensions to be multiples of \(2^3 = 8\).
This transform computes the nearest valid shape and delegates to
CropOrPad to reach it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_multiple
|
TargetMultipleParam
|
Tuple \((n_i, n_j, n_k)\) so that the output size along axis \(d\) is a multiple of \(n_d\). If a single value \(n\) is provided, then \(n_i = n_j = n_k = n\). |
required |
method
|
str
|
Either |
'pad'
|
padding_mode
|
str
|
Padding mode forwarded to |
'constant'
|
fill
|
float
|
Fill value when |
0
|
**kwargs
|
Any
|
See |
{}
|
Examples:
>>> import torchio as tio
>>> transform = tio.EnsureShapeMultiple(8)
>>> transform = tio.EnsureShapeMultiple(2**3, method='pad')
>>> transform = tio.EnsureShapeMultiple(16, method='crop')
>>> transform = tio.EnsureShapeMultiple((4, 8, 16))
Source code in src/torchio/transforms/spatial/ensure_shape_multiple.py
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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | |
supports_per_instance_params
property
Whether this transform can sample parameters per batch element.
Defaults to False. Transforms that implement per-instance
parameter sampling override this to return True. When False,
the transform always uses batch-shared parameters regardless of
the per_instance flag, preserving the legacy behavior.
supports_per_instance_p
property
Whether this transform can gate each batch element independently.
Defaults to False. Shape-preserving transforms that implement
per-element probability override this to return True.
Shape-changing transforms must leave it False because masked
and unmasked elements would have incompatible shapes.
invertible
property
Whether this transform can be inverted.
inverse(params)
Return a transform that undoes this one.
Override in invertible subclasses. The returned transform, when applied, reverses the effect of the forward pass with the given parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
dict[str, Any]
|
The parameters recorded in the forward pass. |
required |
Returns:
| Type | Description |
|---|---|
Transform
|
A new |
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
forward(data)
Apply the transform.
For Subject and Image inputs, delegates to CropOrPad
for lazy operation without loading data from disk.