preprocess_sentinel2_v2
darts.training.preprocess_sentinel2_v2
¶
PLANET preprocessing functions for training with the v2 data preprocessing.
_align_offsets
¶
_align_offsets(
tile: xarray.Dataset,
footprint: geopandas.GeoSeries,
labels: geopandas.GeoDataFrame,
) -> tuple[geopandas.GeoDataFrame, dict[str, float]]
Source code in darts/src/darts/training/preprocess_sentinel2_v2.py
_get_region_name
¶
_get_region_name(
footprint: geopandas.GeoSeries,
admin2: geopandas.GeoDataFrame,
) -> str
Source code in darts/src/darts/training/preprocess_sentinel2_v2.py
_parse_date
¶
Source code in darts/src/darts/training/preprocess_sentinel2_v2.py
_planet_legacy_path_gen
¶
Source code in darts/src/darts/training/preprocess_sentinel2_v2.py
preprocess_s2_train_data
¶
preprocess_s2_train_data(
*,
labels_dir: pathlib.Path,
train_data_dir: pathlib.Path,
arcticdem_dir: pathlib.Path,
tcvis_dir: pathlib.Path,
admin_dir: pathlib.Path,
planet_data_dir: pathlib.Path | None = None,
s2_download_cache: pathlib.Path | None = None,
preprocess_cache: pathlib.Path | None = None,
matching_cache: pathlib.Path | None = None,
force_preprocess: bool = False,
append: bool = True,
device: typing.Literal["cuda", "cpu", "auto"]
| int
| None = None,
ee_project: str | None = None,
ee_use_highvolume: bool = True,
matching_day_range: int = 7,
matching_max_cloud_cover: int = 10,
matching_min_intersects: float = 0.7,
tpi_outer_radius: int = 100,
tpi_inner_radius: int = 0,
patch_size: int = 1024,
overlap: int = 16,
exclude_nopositive: bool = False,
exclude_nan: bool = True,
save_matching_scores: bool = False,
)
Preprocess Sentinel-2 data for training.
This function preprocesses Sentinel-2 scenes matched to Planet footprints into a training-ready format by creating fixed-size patches and storing them in a zarr array for efficient random access during training. All data is stored in a single zarr group with associated metadata.
The preprocessing matches Sentinel-2 scenes to Planet footprints based on temporal and spatial criteria, optionally aligns them spatially to Planet data, and creates patches of the specified size. The data is stored as: - A zarr group containing 'x' (input data) and 'y' (labels) arrays - A geopandas dataframe with metadata including region, position, and label statistics - A configuration file with preprocessing parameters
The x dataarray contains the input data with shape (n_patches, n_bands, patch_size, patch_size). The y dataarray contains the labels with shape (n_patches, patch_size, patch_size). Both dataarrays are chunked along the n_patches dimension with chunk size 1, resulting in each patch being stored in a separate file for super fast random access.
The metadata dataframe contains information about each patch including: - sample_id: Combined identifier for the S2 scene and Planet footprint - region: Administrative region name - geometry: Spatial extent of the patch - empty: Whether the patch contains positive labeled pixels - planet_id: Original Planet scene identifier - s2_id: Sentinel-2 scene identifier - Additional alignment and matching metadata
Through exclude_nopositive
and exclude_nan
, respective patches can be excluded from the final data.
A config.toml
file is saved in the train_data_dir
containing the configuration used for the
preprocessing. Additionally, a timestamp-based CLI configuration file is saved for reproducibility.
The final directory structure of train_data_dir
will look like this:
train_data_dir/
├── config.toml
├── data.zarr/
│ ├── x/ # Input patches [n_patches, n_bands, patch_size, patch_size]
│ └── y/ # Label patches [n_patches, patch_size, patch_size]
├── metadata.parquet
├── matching-scores.parquet # Optional matching scores
└── {timestamp}.cli.json
Parameters:
-
labels_dir
(pathlib.Path
) –The directory containing the labels and footprints / extents.
-
train_data_dir
(pathlib.Path
) –The "output" directory where the tensors are written to.
-
arcticdem_dir
(pathlib.Path
) –The directory containing the ArcticDEM data (the datacube and the extent files). Will be created and downloaded if it does not exist.
-
tcvis_dir
(pathlib.Path
) –The directory containing the TCVis data.
-
admin_dir
(pathlib.Path
) –The directory containing the admin files.
-
planet_data_dir
(pathlib.Path
, default:None
) –The directory containing the Planet scenes and orthotiles. The planet data is used to align the Sentinel-2 data to the Planet data, spatially. Can be set to None if no alignment is wished. Defaults to None.
-
s2_download_cache
(pathlib.Path
, default:None
) –The directory to use for caching the raw downloaded sentinel 2 data. Defaults to None.
-
preprocess_cache
(pathlib.Path
, default:None
) –The directory to store the preprocessed data. Defaults to None.
-
matching_cache
(pathlib.Path
, default:None
) –The path to a file where the matchings are stored. Note: this is different from the matching scores.
-
force_preprocess
(bool
, default:False
) –Whether to force the preprocessing of the data. Defaults to False.
-
append
(bool
, default:True
) –Whether to append the data to the existing data. Defaults to True.
-
device
(typing.Literal['cuda', 'cpu'] | int
, default:None
) –The device to run the model on. If "cuda" take the first device (0), if int take the specified device. If "auto" try to automatically select a free GPU (<50% memory usage). Defaults to "cuda" if available, else "cpu".
-
ee_project
(str
, default:None
) –The Earth Engine project ID or number to use. May be omitted if project is defined within persistent API credentials obtained via
earthengine authenticate
. -
ee_use_highvolume
(bool
, default:True
) –Whether to use the high volume server (https://earthengine-highvolume.googleapis.com). Defaults to True.
-
matching_day_range
(int
, default:7
) –The day range to use for matching S2 scenes to Planet footprints. Defaults to 7.
-
matching_max_cloud_cover
(int
, default:10
) –The maximum cloud cover percentage to use for matching S2 scenes to Planet footprints. Defaults to 10.
-
matching_min_intersects
(float
, default:0.7
) –The minimum intersection percentage to use for matching S2 scenes to Planet footprints. Defaults to 0.7.
-
tpi_outer_radius
(int
, default:100
) –The outer radius of the annulus kernel for the tpi calculation in m. Defaults to 100m.
-
tpi_inner_radius
(int
, default:0
) –The inner radius of the annulus kernel for the tpi calculation in m. Defaults to 0.
-
patch_size
(int
, default:1024
) –The patch size to use for inference. Defaults to 1024.
-
overlap
(int
, default:16
) –The overlap to use for inference. Defaults to 16.
-
exclude_nopositive
(bool
, default:False
) –Whether to exclude patches where the labels do not contain positives. Defaults to False.
-
exclude_nan
(bool
, default:True
) –Whether to exclude patches where the input data has nan values. Defaults to True.
-
save_matching_scores
(bool
, default:False
) –Whether to save the matching scores. Defaults to False.
Source code in darts/src/darts/training/preprocess_sentinel2_v2.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 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 246 247 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 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 |
|