detectron2 module¶
Detectron2 integration for remote sensing image segmentation. See https://github.com/facebookresearch/detectron2 for more details.
batch_detectron2_segment(image_paths, output_dir='.', model_config='COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml', model_weights=None, score_threshold=0.5, device=None, save_masks=True, save_probability=True)
¶
Perform batch instance segmentation on multiple images.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image_paths
|
List[str]
|
List of paths to input images |
required |
output_dir
|
str
|
Directory to save output files |
'.'
|
model_config
|
str
|
Model configuration file path or name from model zoo |
'COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml'
|
model_weights
|
Optional[str]
|
Path to model weights file. If None, uses model zoo weights |
None
|
score_threshold
|
float
|
Confidence threshold for predictions |
0.5
|
device
|
Optional[str]
|
Device to use ('cpu', 'cuda', or None for auto-detection) |
None
|
save_masks
|
bool
|
Whether to save instance masks as GeoTIFF |
True
|
save_probability
|
bool
|
Whether to save probability masks as GeoTIFF |
True
|
Returns:
Type | Description |
---|---|
List[Dict]
|
List of results dictionaries for each image |
Source code in geoai/detectron2.py
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 |
|
check_detectron2()
¶
Check if detectron2 is available.
Source code in geoai/detectron2.py
42 43 44 45 46 47 |
|
create_instance_mask(masks)
¶
Create an instance mask from individual binary masks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
masks
|
ndarray
|
Array of binary masks with shape (num_instances, height, width) |
required |
Returns:
Type | Description |
---|---|
ndarray
|
Instance mask with unique ID for each instance |
Source code in geoai/detectron2.py
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
|
create_probability_mask(masks, scores)
¶
Create a probability mask from individual binary masks and their confidence scores.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
masks
|
ndarray
|
Array of binary masks with shape (num_instances, height, width) |
required |
scores
|
ndarray
|
Array of confidence scores for each mask |
required |
Returns:
Type | Description |
---|---|
ndarray
|
Probability mask with maximum confidence score for each pixel |
Source code in geoai/detectron2.py
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
|
detectron2_segment(image_path, output_dir='.', model_config='COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml', model_weights=None, score_threshold=0.5, device=None, save_masks=True, save_probability=True, mask_prefix='instance_masks', prob_prefix='probability_mask')
¶
Perform instance segmentation on a remote sensing image using Detectron2.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image_path
|
str
|
Path to input image |
required |
output_dir
|
str
|
Directory to save output files |
'.'
|
model_config
|
str
|
Model configuration file path or name from model zoo |
'COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml'
|
model_weights
|
Optional[str]
|
Path to model weights file. If None, uses model zoo weights |
None
|
score_threshold
|
float
|
Confidence threshold for predictions |
0.5
|
device
|
Optional[str]
|
Device to use ('cpu', 'cuda', or None for auto-detection) |
None
|
save_masks
|
bool
|
Whether to save instance masks as GeoTIFF |
True
|
save_probability
|
bool
|
Whether to save probability masks as GeoTIFF |
True
|
mask_prefix
|
str
|
Prefix for instance mask output file |
'instance_masks'
|
prob_prefix
|
str
|
Prefix for probability mask output file |
'probability_mask'
|
Returns:
Type | Description |
---|---|
Dict
|
Dict containing segmentation results and output file paths |
Source code in geoai/detectron2.py
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 |
|
get_class_id_name_mapping(config_path, lazy=False)
¶
Get class ID to name mapping from a Detectron2 model config.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config_path
|
str
|
Path to the config file or model_zoo config name. |
required |
lazy
|
bool
|
Whether the config is a LazyConfig (i.e., .py). |
False
|
Returns:
Name | Type | Description |
---|---|---|
dict |
Dict[int, str]
|
Mapping from class ID (int) to class name (str). |
Source code in geoai/detectron2.py
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 |
|
get_detectron2_models()
¶
Get a list of available Detectron2 models for instance segmentation.
Returns:
Type | Description |
---|---|
List[str]
|
List of model configuration names |
Source code in geoai/detectron2.py
351 352 353 354 355 356 357 358 359 360 361 362 |
|
load_detectron2_model(model_config='COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml', model_weights=None, score_threshold=0.5, device=None, num_classes=None)
¶
Load a Detectron2 model for instance segmentation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_config
|
str
|
Model configuration file path or name from model zoo |
'COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml'
|
model_weights
|
Optional[str]
|
Path to model weights file. If None, uses model zoo weights |
None
|
score_threshold
|
float
|
Confidence threshold for predictions |
0.5
|
device
|
Optional[str]
|
Device to use ('cpu', 'cuda', or None for auto-detection) |
None
|
num_classes
|
Optional[int]
|
Number of classes for custom models |
None
|
Returns:
Name | Type | Description |
---|---|---|
DefaultPredictor |
DefaultPredictor
|
Configured Detectron2 predictor |
Source code in geoai/detectron2.py
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 |
|
save_geotiff_mask(mask, output_path, transform, crs, dtype='uint16')
¶
Save a mask as a GeoTIFF file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mask
|
ndarray
|
2D numpy array representing the mask |
required |
output_path
|
str
|
Path to save the GeoTIFF file |
required |
transform
|
Affine
|
Rasterio transform for georeferencing |
required |
crs
|
CRS
|
Coordinate reference system |
required |
dtype
|
str
|
Data type for the output file |
'uint16'
|
Source code in geoai/detectron2.py
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
|
visualize_detectron2_results(image_path, results, output_path=None, show_scores=True, show_classes=True)
¶
Visualize Detectron2 segmentation results on the original image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image_path
|
str
|
Path to the original image |
required |
results
|
Dict
|
Results dictionary from detectron2_segment |
required |
output_path
|
Optional[str]
|
Path to save the visualization (optional) |
None
|
show_scores
|
bool
|
Whether to show confidence scores |
True
|
show_classes
|
bool
|
Whether to show class labels |
True
|
Returns:
Type | Description |
---|---|
ndarray
|
Visualization image as numpy array |
Source code in geoai/detectron2.py
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 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
|