Rotate an array by 90 degrees in the plane specified by `axes`. See `torch.rot90` for additional details: https://pytorch.org/docs/stable/generated/torch.rot90.html#torch-rot90. This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic ` f
| 1179 | |
| 1180 | |
| 1181 | class Rotate90(InvertibleTransform, LazyTransform): |
| 1182 | """ |
| 1183 | Rotate an array by 90 degrees in the plane specified by `axes`. |
| 1184 | See `torch.rot90` for additional details: |
| 1185 | https://pytorch.org/docs/stable/generated/torch.rot90.html#torch-rot90. |
| 1186 | |
| 1187 | This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic<lazy_resampling>` |
| 1188 | for more information. |
| 1189 | """ |
| 1190 | |
| 1191 | backend = [TransformBackends.TORCH] |
| 1192 | |
| 1193 | def __init__(self, k: int = 1, spatial_axes: tuple[int, int] = (0, 1), lazy: bool = False) -> None: |
| 1194 | """ |
| 1195 | Args: |
| 1196 | k: number of times to rotate by 90 degrees. |
| 1197 | spatial_axes: 2 int numbers, defines the plane to rotate with 2 spatial axes. |
| 1198 | Default: (0, 1), this is the first two axis in spatial dimensions. |
| 1199 | If axis is negative it counts from the last to the first axis. |
| 1200 | lazy: a flag to indicate whether this transform should execute lazily or not. |
| 1201 | Defaults to False |
| 1202 | """ |
| 1203 | LazyTransform.__init__(self, lazy=lazy) |
| 1204 | self.k = (4 + (k % 4)) % 4 # 0, 1, 2, 3 |
| 1205 | spatial_axes_: tuple[int, int] = ensure_tuple(spatial_axes) |
| 1206 | if len(spatial_axes_) != 2: |
| 1207 | raise ValueError(f"spatial_axes must be 2 numbers to define the plane to rotate, got {spatial_axes_}.") |
| 1208 | self.spatial_axes = spatial_axes_ |
| 1209 | |
| 1210 | def __call__(self, img: torch.Tensor, lazy: bool | None = None) -> torch.Tensor: |
| 1211 | """ |
| 1212 | Args: |
| 1213 | img: channel first array, must have shape: (num_channels, H[, W, ..., ]), |
| 1214 | lazy: a flag to indicate whether this transform should execute lazily or not |
| 1215 | during this call. Setting this to False or True overrides the ``lazy`` flag set |
| 1216 | during initialization for this call. Defaults to None. |
| 1217 | """ |
| 1218 | img = convert_to_tensor(img, track_meta=get_track_meta()) |
| 1219 | axes = map_spatial_axes(img.ndim, self.spatial_axes) |
| 1220 | lazy_ = self.lazy if lazy is None else lazy |
| 1221 | return rotate90(img, axes, self.k, lazy=lazy_, transform_info=self.get_transform_info()) # type: ignore |
| 1222 | |
| 1223 | def inverse(self, data: torch.Tensor) -> torch.Tensor: |
| 1224 | transform = self.pop_transform(data) |
| 1225 | return self.inverse_transform(data, transform) |
| 1226 | |
| 1227 | def inverse_transform(self, data: torch.Tensor, transform) -> torch.Tensor: |
| 1228 | axes = transform[TraceKeys.EXTRA_INFO]["axes"] |
| 1229 | k = transform[TraceKeys.EXTRA_INFO]["k"] |
| 1230 | inv_k = 4 - k % 4 |
| 1231 | xform = Rotate90(k=inv_k, spatial_axes=axes) |
| 1232 | with xform.trace_transform(False): |
| 1233 | return xform(data) |
| 1234 | |
| 1235 | |
| 1236 | class RandRotate90(RandomizableTransform, InvertibleTransform, LazyTransform): |
no outgoing calls
searching dependent graphs…