sr module¶
Super-resolution utilities using OpenSR latent diffusion models.
This module provides functions to perform super-resolution on multispectral GeoTIFF images using the latent diffusion models from the ESA OpenSR project:
1 | |
load_image_tensor(image_path, device, bands, window=None, scale_factor=10000.0)
¶
Load specified bands of a multispectral GeoTIFF as a PyTorch tensor.
The pixel values are divided by scale_factor to normalize them to the
[0, 1] range expected by the OpenSR model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_path
|
str
|
Path to input GeoTIFF. |
required |
device
|
str
|
Device to move the tensor to ('cpu' or 'cuda'). |
required |
bands
|
list[int]
|
List of 1-based band indices to read. |
required |
window
|
tuple
|
Region of interest as
|
None
|
scale_factor
|
float
|
Divisor to normalize pixel values to [0, 1]. Default is 10000.0 for Sentinel-2 L2A BOA reflectance. |
10000.0
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Tuple[torch.Tensor, dict]: Tensor of shape (1, C, H, W) and rasterio |
dict
|
profile adjusted for the window (if provided). |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If input image does not exist. |
ValueError
|
If any band index is out of range. |
Source code in geoai/tools/sr.py
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 | |
plot_sr_comparison(lr_path, sr_path, bands=[1, 2, 3], lr_vmax=None, sr_vmax=None, figsize=(14, 7), **kwargs)
¶
Plot a side-by-side comparison of low-resolution and super-resolution images.
Displays RGB composites of the LR input and SR output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lr_path
|
str
|
Path to the low-resolution GeoTIFF. |
required |
sr_path
|
str
|
Path to the super-resolution GeoTIFF. |
required |
bands
|
list[int]
|
Three 1-based band indices for the RGB composite.
Default is |
[1, 2, 3]
|
lr_vmax
|
float
|
Maximum value for LR image contrast stretch. If None, the 98th percentile is used. |
None
|
sr_vmax
|
float
|
Maximum value for SR image contrast stretch. If None, the 98th percentile is used. |
None
|
figsize
|
tuple
|
Figure size. Default is |
(14, 7)
|
**kwargs
|
Additional keyword arguments passed to
|
{}
|
Returns:
| Type | Description |
|---|---|
|
matplotlib.figure.Figure: The matplotlib figure object. |
Source code in geoai/tools/sr.py
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 | |
plot_sr_uncertainty(uncertainty_path, cmap='RdYlGn_r', normalize=True, figsize=(8, 8), **kwargs)
¶
Plot the uncertainty map from super-resolution inference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uncertainty_path
|
str
|
Path to the uncertainty GeoTIFF. |
required |
cmap
|
str
|
Matplotlib colormap name. Default is |
'RdYlGn_r'
|
normalize
|
bool
|
Whether to normalize values to [0, 1]. Default is True. |
True
|
figsize
|
tuple
|
Figure size. Default is |
(8, 8)
|
**kwargs
|
Additional keyword arguments passed to
|
{}
|
Returns:
| Type | Description |
|---|---|
|
matplotlib.figure.Figure: The matplotlib figure object. |
Source code in geoai/tools/sr.py
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 | |
save_geotiff(data, reference_profile, output_path, scale=4)
¶
Save a 2D or 3D NumPy array as a GeoTIFF with super-resolution scaling and corrected georeference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
ndarray
|
Image array to save. Can be: - 2D array (H, W) for a single-band image - 3D array (C, H, W) for multi-band images (e.g., RGB+NIR) |
required |
reference_profile
|
dict
|
Rasterio metadata from a reference GeoTIFF. Used to preserve CRS, transform, and other metadata. |
required |
output_path
|
str
|
Path to save the output GeoTIFF. |
required |
scale
|
int
|
Super-resolution scale factor. Default is 4. This adjusts the affine transform to ensure georeference matches the original image. |
4
|
Source code in geoai/tools/sr.py
248 249 250 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 | |
super_resolution(input_lr_path, output_sr_path, output_uncertainty_path=None, rgb_nir_bands=[1, 2, 3, 4], sampling_steps=100, n_variations=25, scale=4, compute_uncertainty=False, window=None, scale_factor=10000.0, patch_size=128, overlap=16)
¶
Perform super-resolution on RGB+NIR bands of a multispectral GeoTIFF using OpenSR latent diffusion.
The model enhances Sentinel-2 imagery from 10m to 2.5m spatial resolution
(4x upsampling) using the LDSR-S2 latent diffusion model from the ESA
OpenSR project. For images larger than patch_size, the input is
automatically tiled into overlapping patches, each patch is super-resolved,
and the results are stitched back together with linear blending.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_lr_path
|
str
|
Path to the input low-resolution GeoTIFF. |
required |
output_sr_path
|
str
|
Path to save the super-resolution GeoTIFF. |
required |
output_uncertainty_path
|
str
|
Path to save the uncertainty
map GeoTIFF. Required when |
None
|
rgb_nir_bands
|
list[int]
|
List of 4 one-based band indices
corresponding to [R, G, B, NIR] in the input file. Default is
|
[1, 2, 3, 4]
|
sampling_steps
|
int
|
Number of diffusion sampling steps. Higher values produce better results but are slower. Default is 100. |
100
|
n_variations
|
int
|
Number of stochastic forward passes used to estimate uncertainty. Default is 25. |
25
|
scale
|
int
|
Super-resolution scale factor. Default is 4. |
4
|
compute_uncertainty
|
bool
|
Whether to compute an uncertainty map via multiple stochastic forward passes. Default is False. |
False
|
window
|
tuple
|
Region of interest as
|
None
|
scale_factor
|
float
|
Divisor to normalize pixel values to the [0, 1] range. For Sentinel-2 L2A BOA reflectance, use 10000.0 (the default). Set to 1.0 if the data is already normalized. |
10000.0
|
patch_size
|
int
|
Tile size for patched inference. The model expects 128x128 input patches. Default is 128. |
128
|
overlap
|
int
|
Number of overlapping pixels between adjacent patches to reduce tiling artifacts. Default is 16. |
16
|
Returns:
| Type | Description |
|---|---|
Tuple[ndarray, Optional[ndarray]]
|
Tuple[np.ndarray, Optional[np.ndarray]]: Tuple containing:
- sr_image: Super-resolution image as a NumPy array (4, H, W).
- uncertainty: Uncertainty map as a NumPy array (H, W), or None
if |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
ImportError
|
If the |
Example
import geoai sr_image, uncertainty = geoai.super_resolution( ... input_lr_path="sentinel2.tif", ... output_sr_path="sr_output.tif", ... rgb_nir_bands=[1, 2, 3, 4], ... window=(500, 500, 128, 128), ... sampling_steps=50, ... )
Source code in geoai/tools/sr.py
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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | |