segmentation module¶
CustomDataset
¶
Bases: Dataset
Custom Dataset for loading images and masks.
Source code in geoai/segmentation.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 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 |
|
__getitem__(idx)
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
idx
|
int
|
Index of the sample to fetch. |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
A dictionary with 'pixel_values' and 'labels'. |
Source code in geoai/segmentation.py
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 |
|
__init__(images_dir, masks_dir, transform=None, target_size=(256, 256), num_classes=2)
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
images_dir
|
str
|
Directory containing images. |
required |
masks_dir
|
str
|
Directory containing masks. |
required |
transform
|
Compose
|
Transformations to be applied on the images and masks. |
None
|
target_size
|
tuple
|
Target size for resizing images and masks. |
(256, 256)
|
num_classes
|
int
|
Number of classes in the masks. |
2
|
Source code in geoai/segmentation.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
__len__()
¶
Returns the total number of samples.
Source code in geoai/segmentation.py
47 48 49 |
|
get_transform()
¶
Returns:
Type | Description |
---|---|
Compose
|
A.Compose: A composition of image transformations. |
Source code in geoai/segmentation.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
|
load_model(model_path, device)
¶
Loads the fine-tuned model from the specified path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_path
|
str
|
Path to the model. |
required |
device
|
device
|
Device to load the model on. |
required |
Returns:
Name | Type | Description |
---|---|---|
SegformerForSemanticSegmentation |
SegformerForSemanticSegmentation
|
Loaded model. |
Source code in geoai/segmentation.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
|
predict_image(model, image_tensor, original_size, device)
¶
Predicts the segmentation mask for the input image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
SegformerForSemanticSegmentation
|
Fine-tuned model. |
required |
image_tensor
|
Tensor
|
Preprocessed image tensor. |
required |
original_size
|
tuple
|
Original size of the image (width, height). |
required |
device
|
device
|
Device to perform inference on. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: Predicted segmentation mask. |
Source code in geoai/segmentation.py
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
|
prepare_datasets(images_dir, masks_dir, transform, test_size=0.2, random_state=42)
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
images_dir
|
str
|
Directory containing images. |
required |
masks_dir
|
str
|
Directory containing masks. |
required |
transform
|
Compose
|
Transformations to be applied. |
required |
test_size
|
float
|
Proportion of the dataset to include in the validation split. |
0.2
|
random_state
|
int
|
Random seed for shuffling the dataset. |
42
|
Returns:
Name | Type | Description |
---|---|---|
tuple |
tuple
|
Training and validation datasets. |
Source code in geoai/segmentation.py
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 |
|
preprocess_image(image_path, target_size=(256, 256))
¶
Preprocesses the input image for prediction.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image_path
|
str
|
Path to the input image. |
required |
target_size
|
tuple
|
Target size for resizing the image. |
(256, 256)
|
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: Preprocessed image tensor. |
Source code in geoai/segmentation.py
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
|
segment_image(image_path, model_path, target_size=(256, 256), device=torch.device('cuda' if torch.cuda.is_available() else 'cpu'))
¶
Segments the input image using the fine-tuned model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image_path
|
str
|
Path to the input image. |
required |
model_path
|
str
|
Path to the fine-tuned model. |
required |
target_size
|
tuple
|
Target size for resizing the image. |
(256, 256)
|
device
|
device
|
Device to perform inference on. |
device('cuda' if is_available() else 'cpu')
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: Predicted segmentation mask. |
Source code in geoai/segmentation.py
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
|
train_model(train_dataset, val_dataset, pretrained_model='nvidia/segformer-b0-finetuned-ade-512-512', model_save_path='./model', output_dir='./results', num_epochs=10, batch_size=8, learning_rate=5e-05)
¶
Trains the model and saves the fine-tuned model to the specified path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
train_dataset
|
Dataset
|
Training dataset. |
required |
val_dataset
|
Dataset
|
Validation dataset. |
required |
pretrained_model
|
str
|
Pretrained model to fine-tune. |
'nvidia/segformer-b0-finetuned-ade-512-512'
|
model_save_path
|
str
|
Path to save the fine-tuned model. Defaults to './model'. |
'./model'
|
output_dir
|
str
|
Directory to save training outputs. |
'./results'
|
num_epochs
|
int
|
Number of training epochs. |
10
|
batch_size
|
int
|
Batch size for training and evaluation. |
8
|
learning_rate
|
float
|
Learning rate for training. |
5e-05
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
Path to the saved fine-tuned model. |
Source code in geoai/segmentation.py
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 |
|
visualize_predictions(image_path, segmented_mask, target_size=(256, 256), reference_image_path=None)
¶
Visualizes the original image, segmented mask, and optionally the reference image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image_path
|
str
|
Path to the original image. |
required |
segmented_mask
|
ndarray
|
Predicted segmentation mask. |
required |
target_size
|
tuple
|
Target size for resizing images. |
(256, 256)
|
reference_image_path
|
str
|
Path to the reference image. |
None
|
Source code in geoai/segmentation.py
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
|