utils
darts_segmentation.utils
¶
Shared utilities for the inference modules.
Band
dataclass
¶
Bands
¶
Bases: collections.UserList[darts_segmentation.utils.Band]
Wrapper for the list of bands.
factors
property
¶
names
property
¶
offsets
property
¶
__reduce__
¶
Source code in darts-segmentation/src/darts_segmentation/utils.py
filter
¶
filter(
band_names: list[str],
) -> darts_segmentation.utils.Bands
Filter the bands by name.
Parameters:
Returns:
-
Bands
(darts_segmentation.utils.Bands
) –The filtered Bands object.
Source code in darts-segmentation/src/darts_segmentation/utils.py
from_config
classmethod
¶
from_config(
config: dict[
typing.Literal[
"bands", "band_factors", "band_offsets"
],
list,
]
| dict[str, tuple[float, float]],
) -> darts_segmentation.utils.Bands
Create a Bands object from a config dictionary.
Parameters:
-
config
(dict
) –The config dictionary containing the band information. Expects config to be a dictionary with keys "bands", "band_factors" and "band_offsets", with the values to be lists of the same length.
Returns:
-
Bands
(darts_segmentation.utils.Bands
) –The Bands object.
Source code in darts-segmentation/src/darts_segmentation/utils.py
from_dict
classmethod
¶
Create a Bands object from a dictionary.
Parameters:
-
config
(dict[str, tuple[float, float]]
) –The dictionary containing the band information. Expects the keys to be the band names and the values to be tuples of (factor, offset). Example: {"band1": (1.0, 0.0), "band2": (2.0, 1.0)}
Returns:
-
Bands
(darts_segmentation.utils.Bands
) –The Bands object.
Source code in darts-segmentation/src/darts_segmentation/utils.py
to_config
¶
Convert the Bands object to a config dictionary.
Returns:
-
dict
(dict[typing.Literal['bands', 'band_factors', 'band_offsets'], list]
) –The config dictionary containing the band information.
Source code in darts-segmentation/src/darts_segmentation/utils.py
create_patches
¶
create_patches(
tensor_tiles: torch.Tensor,
patch_size: int,
overlap: int,
return_coords: bool = False,
) -> torch.Tensor
Create patches from a tensor.
Parameters:
-
tensor_tiles
(torch.Tensor
) –The input tensor. Shape: (BS, C, H, W).
-
patch_size
(int
) –The size of the patches.
-
overlap
(int
) –The size of the overlap.
-
return_coords
(bool
, default:False
) –Whether to return the coordinates of the patches. Can be used for debugging. Defaults to False.
Returns:
Source code in darts-segmentation/src/darts_segmentation/utils.py
patch_coords
¶
patch_coords(
h: int, w: int, patch_size: int, overlap: int
) -> collections.abc.Generator[
tuple[int, int, int, int], None, None
]
Yield patch coordinates based on height, width, patch size and margin size.
Parameters:
-
h
(int
) –Height of the image.
-
w
(int
) –Width of the image.
-
patch_size
(int
) –Patch size.
-
overlap
(int
) –Margin size.
Yields:
-
tuple[int, int, int, int]
–tuple[int, int, int, int]: The patch coordinates y, x, patch_idx_y and patch_idx_x.
Source code in darts-segmentation/src/darts_segmentation/utils.py
predict_in_patches
¶
predict_in_patches(
model: torch.nn.Module,
tensor_tiles: torch.Tensor,
patch_size: int,
overlap: int,
batch_size: int,
reflection: int,
device=torch.device,
return_weights: bool = False,
) -> torch.Tensor
Predict on a tensor.
Parameters:
-
model
(torch.nn.Module
) –The model to use for prediction.
-
tensor_tiles
(torch.Tensor
) –The input tensor. Shape: (BS, C, H, W).
-
patch_size
(int
) –The size of the patches.
-
overlap
(int
) –The size of the overlap.
-
batch_size
(int
) –The batch size for the prediction, NOT the batch_size of input tiles. Tensor will be sliced into patches and these again will be infered in batches.
-
reflection
(int
) –Reflection-Padding which will be applied to the edges of the tensor.
-
device
(torch.device
, default:torch.device
) –The device to use for the prediction.
-
return_weights
(bool
, default:False
) –Whether to return the weights. Can be used for debugging. Defaults to False.
Returns:
Source code in darts-segmentation/src/darts_segmentation/utils.py
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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
|