Export Reference¶
darts_segmentation
¶
Image segmentation of thaw-slumps for the DARTS dataset.
SMPSegmenter
¶
An actor that keeps a model as its state and segments tiles.
Source code in darts-segmentation/src/darts_segmentation/segment.py
48 49 50 51 52 53 54 55 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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
|
config: SMPSegmenterConfig = validate_config(ckpt['config'])
instance-attribute
¶
device: torch.device = torch.device('cpu') if not torch.cuda.is_available() else torch.device('cuda')
instance-attribute
¶
model: nn.Module = smp.create_model(**self.config['model'], encoder_weights=None)
instance-attribute
¶
__call__(input, patch_size=1024, overlap=16, batch_size=8, reflection=0)
¶
Run inference on a single tile or a list of tiles.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input
|
Dataset | list[Dataset]
|
A single tile or a list of tiles. |
required |
patch_size
|
int
|
The size of the patches. Defaults to 1024. |
1024
|
overlap
|
int
|
The size of the overlap. Defaults to 16. |
16
|
batch_size
|
int
|
The batch size for the prediction, NOT the batch_size of input tiles. |
8
|
reflection
|
int
|
Reflection-Padding which will be applied to the edges of the tensor. Defaults to 0. |
0
|
Returns:
Type | Description |
---|---|
Dataset | list[Dataset]
|
A single tile or a list of tiles augmented by a predicted |
Dataset | list[Dataset]
|
Each |
Raises:
Type | Description |
---|---|
ValueError
|
in case the input is not an xr.Dataset or a list of xr.Dataset |
Source code in darts-segmentation/src/darts_segmentation/segment.py
__init__(model_checkpoint)
¶
Initialize the segmenter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_checkpoint
|
Path
|
The path to the model checkpoint. |
required |
Source code in darts-segmentation/src/darts_segmentation/segment.py
segment_tile(tile, patch_size=1024, overlap=16, batch_size=8, reflection=0)
¶
Run inference on a tile.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tile
|
Dataset
|
The input tile, containing preprocessed, harmonized data. |
required |
patch_size
|
int
|
The size of the patches. Defaults to 1024. |
1024
|
overlap
|
int
|
The size of the overlap. Defaults to 16. |
16
|
batch_size
|
int
|
The batch size for the prediction, NOT the batch_size of input tiles. |
8
|
reflection
|
int
|
Reflection-Padding which will be applied to the edges of the tensor. Defaults to 0. |
0
|
Returns:
Type | Description |
---|---|
Dataset
|
Input tile augmented by a predicted |
Source code in darts-segmentation/src/darts_segmentation/segment.py
segment_tile_batched(tiles, patch_size=1024, overlap=16, batch_size=8, reflection=0)
¶
Run inference on a list of tiles.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tiles
|
list[Dataset]
|
The input tiles, containing preprocessed, harmonized data. |
required |
patch_size
|
int
|
The size of the patches. Defaults to 1024. |
1024
|
overlap
|
int
|
The size of the overlap. Defaults to 16. |
16
|
batch_size
|
int
|
The batch size for the prediction, NOT the batch_size of input tiles. |
8
|
reflection
|
int
|
Reflection-Padding which will be applied to the edges of the tensor. Defaults to 0. |
0
|
Returns:
Type | Description |
---|---|
list[Dataset]
|
A list of input tiles augmented by a predicted |
Source code in darts-segmentation/src/darts_segmentation/segment.py
tile2tensor(tile)
¶
Take a tile and convert it to a pytorch tensor.
Respects the input combination from the config.
Returns:
Type | Description |
---|---|
Tensor
|
A torch tensor for the full tile consisting of the bands specified in |
Source code in darts-segmentation/src/darts_segmentation/segment.py
tile2tensor_batched(tiles)
¶
Take a list of tiles and convert them to a pytorch tensor.
Respects the the input combination from the config.
Returns:
Type | Description |
---|---|
Tensor
|
A torch tensor for the full tile consisting of the bands specified in |
Source code in darts-segmentation/src/darts_segmentation/segment.py
SMPSegmenterConfig
¶
Bases: TypedDict
Configuration for the segmentor.
Source code in darts-segmentation/src/darts_segmentation/segment.py
create_patches(tensor_tiles, patch_size, overlap, return_coords=False)
¶
Create patches from a tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor_tiles
|
Tensor
|
The input tensor. Shape: (BS, C, H, W). |
required |
patch_size
|
int
|
The size of the patches. |
required |
overlap
|
int
|
The size of the overlap. |
required |
return_coords
|
bool
|
Whether to return the coordinates of the patches. Can be used for debugging. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: The patches. Shape: (BS, N_h, N_w, C, patch_size, patch_size). |
Source code in darts-segmentation/src/darts_segmentation/utils.py
patch_coords(h, w, patch_size, overlap)
¶
Yield patch coordinates based on height, width, patch size and margin size.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
h
|
int
|
Height of the image. |
required |
w
|
int
|
Width of the image. |
required |
patch_size
|
int
|
Patch size. |
required |
overlap
|
int
|
Margin size. |
required |
Yields:
Type | Description |
---|---|
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(model, tensor_tiles, patch_size, overlap, batch_size, reflection, device=torch.device, return_weights=False)
¶
Predict on a tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
Module
|
The model to use for prediction. |
required |
tensor_tiles
|
Tensor
|
The input tensor. Shape: (BS, C, H, W). |
required |
patch_size
|
int
|
The size of the patches. |
required |
overlap
|
int
|
The size of the overlap. |
required |
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. |
required |
reflection
|
int
|
Reflection-Padding which will be applied to the edges of the tensor. |
required |
device
|
device
|
The device to use for the prediction. |
device
|
return_weights
|
bool
|
Whether to return the weights. Can be used for debugging. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
Tensor
|
The predicted tensor. |
Source code in darts-segmentation/src/darts_segmentation/utils.py
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 176 177 178 179 180 181 182 183 184 185 |
|