Training¶
Training creates a samstars model bundle for later crown segmentation.
The bundle stores the trained proposal decoder, polygon-refinement model files, and run defaults needed by samstars.segment(...).
Training uses the same input and prepared-tile products described in Data Preparation: imagery, canopy height, first-return density, seed points, and cost rasters. It also requires crown polygons for supervision and a base SAM checkpoint.
Inputs¶
Training requires:
- imagery raster
- canopy height model raster
- first-return density raster
- crown polygons in a GeoPackage or other vector file readable by GeoPandas
- base SAM checkpoint, such as
sam_vit_l_0b3195.pth
You can train from one labeled scene or from a directory of labeled tiles.
Train From One Labeled Area¶
Use samstars.train(...) with img, chm, den, and crowns when the labeled training area is a single set of aligned rasters and crown polygons.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
During training, samstars prepares a tile-style training directory, builds seed points and cost rasters, trains the proposal decoder, generates proposal rasters, builds refinement records, and trains the polygon-refinement model.
Train From Labeled Tiles¶
Use tiles_dir when the training area is already split into tile directories.
1 2 3 4 5 6 7 8 9 10 11 | |
Each tile can be fully prepared:
img.tifchm.tifcost.tifseeds.gpkgcrowns.gpkg
or raw labeled:
img.tifchm.tifden.tifcrowns.gpkg
For raw labeled tiles, train(...) derives cost.tif and seeds.gpkg before training.
Do not mix fully prepared and raw labeled tile layouts in the same tiles_dir.
Model Bundle¶
A trained bundle contains:
manifest.jsondecoder.pthconfig.jsonweights.h5thresholds.json, when threshold optimization produced one
manifest.json records the bundle format, artifact filenames, and defaults used later by segment(...).
The bundle directory is the path passed to segment(model=...).
Persistent Training Outputs¶
With storage="persistent", training intermediates are kept under:
1 | |
These files are useful for inspection, debugging, and partial reruns. They include:
- prepared training tiles
- training chips
- proposal rasters and proposal records
- refinement training records
- CHM scale stats
- temporary refinement model training outputs
Use storage="temp" when you only want the final model bundle.
Training Chips¶
Decoder training uses chip arrays derived from labeled tiles:
1 2 3 | |
Each input tile should contain:
img.tifchm.tifcrowns.gpkg
The output contains:
images/: image chips as.npymasks/: binary crown masks as.npychm/: CHM chips as.npyprompts/: prompt metadata per chip
The top-level train(...) function builds training chips automatically.
Use this command when you want to inspect or rebuild only the chip arrays.
Training Controls¶
Common controls include:
rgb_bands: image bands used for the three-band proposal image.chip_sizeandchip_stride: training chip size and stride in pixels.sam_epochs,sam_min_iters, andsam_lr: proposal-decoder training controls.stardist_epochsandstardist_min_iters: polygon-refinement training controls.val_fraction: validation fraction for refinement training records.seed_d_min,seed_min_dist_px,seed_gauss_sigma,seed_merge_radius, andseed_min_canopy_height: seed generation controls.stardist_input_key: primary refinement input, usuallyindex.stardist_use_aux: whether to provide CHM as an auxiliary refinement channel.stardist_cpu: run refinement training on CPU.
See the API reference for samstars.api for the full function signature.
Use the Trained Bundle¶
After training, pass the bundle to segment(...):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
For runnable walkthroughs, see Train and Segment One Area and Train and Segment a Tiled Area. For component commands, see CLI Scripts.