inference module¶
Memory-efficient tiled inference with blending and test-time augmentation.
Provides a generic sliding-window inference pipeline that works with any PyTorch segmentation or regression model on GeoTIFF rasters. Key features:
- Windowed I/O -- reads tiles directly via rasterio windows, avoiding full-image input memory allocation.
- Multiple blending strategies -- linear ramp, raised cosine, and spline windows for seamless tile stitching.
- D4 test-time augmentation -- optional 8-fold augmentation using the dihedral group (identity, 3 rotations, horizontal flip, vertical flip, 2 diagonal flips).
References
- Spline blending: https://github.com/Vooban/Smoothly-Blend-Image-Patches
- GitHub issue: https://github.com/opengeos/geoai/issues/87
BlendMode
¶
Bases: str, Enum
Blending strategy for overlapping tile predictions.
Attributes:
| Name | Type | Description |
|---|---|---|
NONE |
Uniform averaging -- all pixels are weighted equally (1.0), so overlapping tiles are simply averaged without tapering. |
|
LINEAR |
Linear ramp from 0 at edges to 1 at center. |
|
COSINE |
Raised-cosine (Hann) taper in the overlap region. |
|
SPLINE |
Powered raised-cosine taper for smooth transitions in
the overlap zones. Requires |
Source code in geoai/inference.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | |
create_weight_mask(tile_size, overlap, mode=BlendMode.SPLINE, power=2)
¶
Create a 2D weight mask for blending overlapping tiles.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tile_size
|
int
|
Size of each square tile in pixels. |
required |
overlap
|
int
|
Number of pixels of overlap between adjacent tiles. |
required |
mode
|
Union[str, BlendMode]
|
Blending strategy. One of |
SPLINE
|
power
|
int
|
Exponent for spline mode. Higher values concentrate weight toward the center. Ignored for other modes. |
2
|
Returns:
| Type | Description |
|---|---|
ndarray
|
numpy.ndarray: Float32 array of shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If mode is not a recognized blending strategy, or if overlap is negative or >= tile_size. |
Example
from geoai.inference import create_weight_mask mask = create_weight_mask(256, 64, mode="spline") mask.shape (256, 256)
Source code in geoai/inference.py
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 | |
d4_forward(tensor)
¶
Apply all 8 D4 dihedral group transforms to a batch of images.
The D4 group consists of the identity, three 90-degree rotations, horizontal flip, vertical flip, and two diagonal flips.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tensor
|
'torch.Tensor'
|
Input tensor of shape |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
List['torch.Tensor']
|
List of 8 tensors, each of shape |
Source code in geoai/inference.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | |
d4_inverse(tensors)
¶
Apply the inverse D4 transforms to undo :func:d4_forward.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tensors
|
List['torch.Tensor']
|
List of 8 tensors from :func: |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
List['torch.Tensor']
|
List of 8 tensors, each transformed back to the original orientation. |
Source code in geoai/inference.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | |
d4_tta_forward(model, batch)
¶
Run inference with D4 test-time augmentation and average results.
Applies all 8 D4 transforms, runs the model on each, inverts the transforms, and averages the predictions. This can improve prediction quality at the cost of 8x compute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
'torch.nn.Module'
|
PyTorch model that accepts |
required |
batch
|
'torch.Tensor'
|
Input tensor of shape |
required |
Returns:
| Type | Description |
|---|---|
'torch.Tensor'
|
torch.Tensor: Averaged prediction tensor of shape
|
Source code in geoai/inference.py
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |
predict_geotiff(model, input_raster, output_raster, tile_size=256, overlap=64, batch_size=4, input_bands=None, num_classes=1, output_dtype='float32', output_nodata=-9999.0, blend_mode='spline', blend_power=2, tta=False, preprocess_fn=None, postprocess_fn=None, device=None, compress='lzw', verbose=True)
¶
Run tiled inference on a GeoTIFF with blending and optional TTA.
Reads tiles from input_raster using rasterio windowed I/O, runs
each batch through model, blends overlapping predictions with the
chosen weight mask, and writes results to output_raster. Memory
usage is proportional to batch_size * tile_size**2 for input
reads rather than the full image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
'torch.nn.Module'
|
PyTorch model accepting |
required |
input_raster
|
str
|
Path to the input GeoTIFF file. |
required |
output_raster
|
str
|
Path to save the output GeoTIFF. |
required |
tile_size
|
int
|
Size of square tiles in pixels. |
256
|
overlap
|
int
|
Overlap between adjacent tiles in pixels. Using overlap with blending weights eliminates tile-boundary artefacts. Higher values give smoother results at the cost of more computation. |
64
|
batch_size
|
int
|
Number of tiles per forward pass. |
4
|
input_bands
|
Optional[List[int]]
|
1-based band indices to read. If None, reads all bands. |
None
|
num_classes
|
int
|
Number of output channels/classes from the model. Use 1 for regression or binary segmentation. |
1
|
output_dtype
|
str
|
NumPy dtype string for the output raster (e.g.,
|
'float32'
|
output_nodata
|
float
|
NoData value for the output raster. |
-9999.0
|
blend_mode
|
Union[str, BlendMode]
|
Blending strategy: |
'spline'
|
blend_power
|
int
|
Exponent for spline blending (ignored for other modes). |
2
|
tta
|
bool
|
If True, apply D4 test-time augmentation. Increases compute by 8x but can improve prediction quality. |
False
|
preprocess_fn
|
Optional[Callable[..., ndarray]]
|
Optional callable |
None
|
postprocess_fn
|
Optional[Callable[..., ndarray]]
|
Optional callable |
None
|
device
|
Optional[str]
|
PyTorch device string (e.g., |
None
|
compress
|
str
|
Compression for the output GeoTIFF. |
'lzw'
|
verbose
|
bool
|
Print progress information. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Path to the output raster. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If input_raster does not exist. |
ValueError
|
If overlap >= tile_size or overlap < 0. |
Example
from geoai.inference import predict_geotiff predict_geotiff( ... model=my_model, ... input_raster="input.tif", ... output_raster="output.tif", ... tile_size=256, ... overlap=64, ... blend_mode="spline", ... tta=False, ... ) 'output.tif'
Source code in geoai/inference.py
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 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 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 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 434 435 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 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 | |