Download scripts/publish.py from fatmur/dynamicworld: direct link, hf CLI and curl.
- Browser
- Download file 3.17 kB
-
https://huggingface.co/fatmur/dynamicworld/resolve/main/scripts/publish.py
- Command line
-
hf download hf://fatmur/dynamicworld/scripts/publish.py
-
curl -L -o publish.py https://huggingface.co/fatmur/dynamicworld/resolve/main/scripts/publish.py
3.17 kB
| """Publish a checkpoint and its inference pipeline to a Hugging Face model repo. | |
| python -m scripts.publish fatmur/dynamicworld-small-cnn \ | |
| artifacts/small_cnn_unet/version_0/checkpoints/last.ckpt | |
| Add --out release/dynamicworld-small-cnn to retain a local export. | |
| The run's config.yaml supplies preprocessing; the checkpoint supplies the | |
| weights and validation chip settings; the run config also freezes its label scheme. | |
| """ | |
| from pathlib import Path | |
| import typer | |
| import yaml | |
| from geosave.ml import RasterPipeline | |
| from geosave.ml.factory import BuildSpec | |
| from geosave.ml.processing.raster.ingestion.sentinel2 import Sentinel2Ingestor | |
| from geosave.ml.processing.raster.postprocessing.dense import SegmentationPostprocessor | |
| from geosave.ml.processing.raster.prediction.dense import DensePredictor | |
| from geosave.ml.processing.raster.preprocessing.base import PREPROCESSORS | |
| from geosave.ml.release import ChainModel | |
| from modules.supervised.module import Module | |
| def main( | |
| repo_id: str, | |
| checkpoint: Path, | |
| out: Path | None = typer.Option(None, help="Optional local release directory."), | |
| ) -> None: | |
| """Publish to repo_id; optionally keep the same release locally.""" | |
| # Lightning writes a run's resolved config beside its checkpoints folder. | |
| run = checkpoint.parent.parent | |
| config = yaml.safe_load((run / "config.yaml").read_text()) | |
| labels = config["data"]["train"]["init_args"]["label_scheme"] | |
| val_labels = config["data"]["val"]["init_args"]["label_scheme"] | |
| if labels != val_labels: | |
| raise ValueError("training and validation label schemes differ") | |
| module = Module.load_from_checkpoint(checkpoint, map_location="cpu") | |
| module.eval() | |
| if module.model.head.num_classes != len(labels["classes"]): | |
| raise ValueError("checkpoint head does not match the run label scheme") | |
| preprocessor_spec = config["data"]["train"]["init_args"]["preprocessor"] | |
| preprocessor = PREPROCESSORS.build(BuildSpec.model_validate(preprocessor_spec)) | |
| # The release chips and merges a raster as validation chipped the scenes. | |
| predictor = DensePredictor( | |
| size=module.chip_size, | |
| stride=module.chip_stride, | |
| mode=module.chip_mode, | |
| window=module.merge_window, | |
| batch_size=module.chip_batch_size, | |
| context=["centroid", "year", "day_of_year"], | |
| nodes=None, | |
| ) | |
| postprocessor = SegmentationPostprocessor( | |
| classes=labels["classes"], colors=labels["colors"], mask=None | |
| ) | |
| # The images were ingested at 10 m under the node the preprocessor reads. | |
| ingestor = Sentinel2Ingestor( | |
| bands=preprocessor.bands, resolution=10.0, name=preprocessor.name | |
| ) | |
| pipeline = RasterPipeline( | |
| module.model, | |
| preprocessor=preprocessor, | |
| predictor=predictor, | |
| postprocessor=postprocessor, | |
| ingestor=ingestor, | |
| ) | |
| release = ChainModel.from_pipeline(pipeline) | |
| if out is not None: | |
| release.save_pretrained(out) | |
| print(f"Saved local release to {out}") | |
| commit = release.push_to_hub(repo_id) | |
| print(f"Released {repo_id} at {commit.oid}") | |
| if __name__ == "__main__": | |
| typer.run(main) | |