### Set up Virtual Environment and Install Dependencies Source: https://github.com/terrastackai/terratorch/blob/main/examples/custom_modules/README.md These commands set up a Python virtual environment and install the necessary packages for the project. Activate the environment before running the example. ```bash python -m venv .venv source .venv/bin/activate pip install -e . ``` ```bash source .venv/bin/activate ``` -------------------------------- ### Install Dependencies Source: https://github.com/terrastackai/terratorch/blob/main/examples/utils/prithvi_v2_eo_300_tl_unet_burnscars_tensorrt_metrics.ipynb Install the required packages for the deprecated example. ```bash !pip install terratorch==1.0.1 gdown tensorrt onnx onnxruntime polygraphy numpy pycuda numba ``` -------------------------------- ### Install Necessary Packages Source: https://github.com/terrastackai/terratorch/blob/main/docs/tutorials/using_datamodule_multitemporalclassificationModule.md Installs terratorch, gdown, and tensorboard. Output is redirected to install.log. ```python !pip install terratorch gdown tensorboard >& install.log ``` -------------------------------- ### Setup and Inspect DataModule Source: https://github.com/terrastackai/terratorch/blob/main/examples/object_detection/ft_aed_elephants_terramind_tiny.ipynb Prepares the DataModule and inspects the training dataloader. ```python dm.setup() ``` ```python tdl = dm.train_dataloader() ``` ```python len(tdl.dataset) ``` ```python tdl.dataset[0].keys() ``` -------------------------------- ### Setup Pipeline Components Source: https://github.com/terrastackai/terratorch/blob/main/examples/models/models-abstraction.ipynb Initializes the necessary components for a full training pipeline. ```python # Level 4: Working example with fake data from torch.utils.data import Dataset, DataLoader from lightning import Trainer import torch ``` -------------------------------- ### Setup Training Data and Visualize Samples Source: https://github.com/terrastackai/terratorch/blob/main/examples/weather_climate/WxCTutorialDownscalingECCC.ipynb Prepares the datamodule for the fitting stage and visualizes a sample batch. ```python datamodule.setup(stage='fit') train_dl = datamodule.train_dataloader() plot_sample(next(iter(train_dl))) ``` -------------------------------- ### Install Dependencies Source: https://github.com/terrastackai/terratorch/blob/main/examples/weather_climate/WxCTutorialDownscalingECCC.ipynb Installs necessary libraries for TerraTorch, Prithvi-WxC, and granite-wxc. ```python !pip install -q git+https://github.com/IBM/terratorch.git !pip install -q git+https://github.com/NASA-IMPACT/Prithvi-WxC.git !pip install -q git+https://github.com/IBM/granite-wxc.git ``` -------------------------------- ### Install Dependencies Source: https://github.com/terrastackai/terratorch/blob/main/examples/weather_climate/WxCTutorialGravityWave.ipynb Install the required TerraTorch, Hugging Face, and Prithvi WxC packages. ```bash !pip install terratorch==0.99.9 huggingface_hub PrithviWxC ``` ```bash !pip install git+https://github.com/romeokienzler/gravity-wave-finetuning.git ``` -------------------------------- ### Setup Test Dataset Source: https://github.com/terrastackai/terratorch/blob/main/examples/utils/prithvi_v2_eo_300_tl_unet_burnscars_tensorrt_metrics.ipynb Prepare the test dataset and check its length. ```python datamodule.setup("test") test_dataset = datamodule.test_dataset len(test_dataset) ``` -------------------------------- ### Install Dependencies Source: https://github.com/terrastackai/terratorch/blob/main/examples/utils/visualize_model.ipynb Install the necessary libraries, torchview and graphviz, using pip. ```python !pip install torchview !pip install graphviz ``` -------------------------------- ### Setup and Load Batch from DataModule Source: https://github.com/terrastackai/terratorch/blob/main/examples/object_detection/od_data_augmentation.ipynb Sets up the data module for training and then retrieves the first batch of data from the training dataloader. ```python dm.setup("fit") train_loader = dm.train_dataloader() batch = next(iter(train_loader)) ``` -------------------------------- ### Launch TensorBoard for Training Analysis Source: https://github.com/terrastackai/terratorch/blob/main/examples/weather_climate/WxCTutorialDownscalingECCC.ipynb Command to start the TensorBoard server for monitoring training logs. ```python # !tensorboard --logdir logs/ --port 9010 # add --host $(hostname -f) for clusters ``` -------------------------------- ### Run the TerraTorch Example Source: https://github.com/terrastackai/terratorch/blob/main/examples/custom_modules/README.md Execute the TerraTorch training command from the `examples/custom_modules` directory using the specified configuration file. ```bash terratorch fit -c ./config.yaml ``` -------------------------------- ### Initialization and Camera Setup Source: https://github.com/terrastackai/terratorch/blob/main/examples/webapps/prithvi.tiny.onnx.demo/model_tm_tiny_xview.html Initializes the web worker, retrieves available camera devices, and logs the initialization status. ```javascript log("Initializing..."); initWorker(); getCameras(); ``` -------------------------------- ### Setup DataModule Source: https://github.com/terrastackai/terratorch/blob/main/examples/weather_climate/InspectMerra2Data.ipynb Configure and setup the Merra2DownscaleNonGeoDataModule for prediction. ```python from terratorch.datamodules.merra2_downscale import Merra2DownscaleNonGeoDataModule dm = Merra2DownscaleNonGeoDataModule( time_range=('2001-01-01', '2001-01-02'), data_path_surface = config.data.data_path_surface, data_path_vertical = config.data.data_path_vertical, climatology_path_surface = config.data.climatology_path_surface, climatology_path_vertical = config.data.climatology_path_vertical, input_surface_vars = config.data.input_surface_vars, input_static_surface_vars = config.data.input_static_surface_vars, input_vertical_vars = config.data.input_vertical_vars, input_levels = config.data.input_levels, n_input_timestamps = config.data.n_input_timestamps, output_vars=config.data.output_vars, transforms=_get_transforms(config), ) dm.setup('predict') ``` -------------------------------- ### Download sample images via wget Source: https://github.com/terrastackai/terratorch/blob/main/examples/webapps/terramind.tiny.onnx.demo/README.md Use these commands to fetch the required example tiles for the TerraMind.tiny model demo. ```bash wget https://terramind-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/tile_0_0_0.png wget https://terramind-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/tile_0_1152_3072.png wget https://terramind-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/tile_0_768_3072.png wget https://terramind-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/tile_1260_0_2688.png wget https://terramind-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/tile_100_3840_1536.png wget https://terramind-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/tile_100_4224_1152.png wget https://terramind-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/tile_100_4224_1536.png wget https://terramind-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/tile_100_4224_768.png wget https://terramind-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/tile_100_4608_0.png wget https://terramind-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/tile_1261_4224_768.png wget https://terramind-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/tile_1013_3072_3072.png wget https://terramind-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/tile_101_3072_384.png ``` -------------------------------- ### Commented Out Execution Examples Source: https://github.com/terrastackai/terratorch/blob/main/examples/object_detection/ft_aed_elephants_terramind_tiny.ipynb Examples of how to call visualization or iteration logic, currently commented out. ```python #m.setup("fit") #show_from_datamodule(dm, n=30, split="train") ``` ```python """ train_loader = dm.train_dataloader() # Iterate and count total_elements = 0 for batch in train_loader: # 'batch' is a tensor or list of tensors depending on your dataset batch_size = len(batch) print(f"Processing a batch of size: {batch_size}") total_elements += batch_size print(f"All elements read. Total count: {total_elements}") """ ``` -------------------------------- ### Setup Data Module and Predict Source: https://github.com/terrastackai/terratorch/blob/main/examples/weather_climate/WxCTutorialDownscaling.ipynb Initializes the data module for prediction and runs the trainer to generate results. ```python dm = Merra2DownscaleNonGeoDataModule( time_range=('2020-01-01T00:00:00', '2020-01-01T23:59:59'), data_path_surface = config.data.data_path_surface, data_path_vertical = config.data.data_path_vertical, climatology_path_surface = config.data.climatology_path_surface, climatology_path_vertical = config.data.climatology_path_vertical, input_surface_vars = config.data.input_surface_vars, input_static_surface_vars = config.data.input_static_surface_vars, input_vertical_vars = config.data.input_vertical_vars, input_levels = config.data.input_levels, n_input_timestamps = config.data.n_input_timestamps, output_vars=config.data.output_vars, transforms=_get_transforms(config), ) dm.setup('predict') first_element = dm.predict_dataloader().dataset[0] ``` ```python dm = Merra2DownscaleNonGeoDataModule( time_range=('2020-01-01T00:00:00', '2020-01-01T23:59:59'), data_path_surface = config.data.data_path_surface, data_path_vertical = config.data.data_path_vertical, climatology_path_surface = config.data.climatology_path_surface, climatology_path_vertical = config.data.climatology_path_vertical, input_surface_vars = config.data.input_surface_vars, input_static_surface_vars = config.data.input_static_surface_vars, input_vertical_vars = config.data.input_vertical_vars, input_levels = config.data.input_levels, n_input_timestamps = config.data.n_input_timestamps, output_vars=config.data.output_vars, transforms=_get_transforms(config), ) dm.setup('predict') trainer = Trainer( max_epochs=1, limit_predict_batches=1, ) results = trainer.predict(model=task, datamodule=dm) ``` -------------------------------- ### Start vLLM Serving Instance Source: https://github.com/terrastackai/terratorch/blob/main/docs/guide/vllm/serving_a_model_tensor.md Use this command to start a vLLM serving instance with a specified HuggingFace model. Ensure the model is prepared for vLLM serving. Options like `--trust-remote-code`, `--dtype float16`, `--skip-tokenizer-init`, `--enforce-eager`, and `--enable-mm-embeds` are used for model loading and configuration. ```bash vllm serve \ --model='ibm-nasa-geospatial/Prithvi-EO-2.0-300M-TL-Sen1Floods11' \ --trust-remote-code \ --dtype float16 \ --skip-tokenizer-init \ --enforce-eager \ --enable-mm-embeds ``` -------------------------------- ### Install PrithviWxC and Dependencies Source: https://github.com/terrastackai/terratorch/blob/main/examples/weather_climate/WxCTutorialDownscaling.ipynb Install the PrithviWxC library and its dependencies, including granitewxc, huggingface_hub, and terratorch, using pip. ```python !pip install PrithviWxC granitewxc==0.1.1rc1 huggingface_hub terratorch==0.99.9 ``` -------------------------------- ### Local development and build commands Source: https://github.com/terrastackai/terratorch/blob/main/examples/webapps/terramind.tiny.onnx.demo/README.md Commands for installing dependencies, running the development server, and building the project. ```bash pnpm i npm install vite-plugin-pwa --save-dev pnpm dev pnpm build python3 -m http.server ``` -------------------------------- ### Execute Training Source: https://github.com/terrastackai/terratorch/blob/main/examples/classification/classification_eurosat.ipynb Start the training process using the configured trainer and datamodule. ```python # Start training trainer.fit(model, datamodule=datamodule) ``` -------------------------------- ### Multimodal Data Organization Examples Source: https://github.com/terrastackai/terratorch/blob/main/docs/guide/vllm/plugins/tm_segmentation_io_plugin.md Examples showing how to structure the data field for URL and path-based inputs, including the required directory structure. ```json { "data_format": "url", "data": { "DEM": "https://example.com/path/to/dem_file", "S1RTC": "https://example.com/path/to/S1RTC_file", "S2L2A": "https://example.com/path/to/S2L2A_file" } } ``` ```json { "data_format": "path", "data": "/path/to/input/directory/" } ``` ```text /path/to/input/directory/ ├── DEM/ │ └── FILE_NAME_DEM.tiff ├── S1RTC/ │ └── FILE_NAME_S1RTC.zarr.zip └── S2L2A/ └── FILE_NAME_S2L2A.zarr.zip ``` -------------------------------- ### Install TerraTorch from GitHub Source: https://github.com/terrastackai/terratorch/blob/main/docs/guide/quick_start.md Install the most recent version from the main branch of the GitHub repository. ```shell pip install git+https://github.com/IBM/terratorch.git ``` -------------------------------- ### Start vLLM Serving Instance Source: https://github.com/terrastackai/terratorch/blob/main/docs/guide/vllm/serving_a_model_image.md Use this command to start a vLLM serving instance for a TerraTorch model. Ensure you have prepared your model for vLLM and identified the correct IOProcessor plugin. ```bash vllm serve ibm-nasa-geospatial/Prithvi-EO-2.0-300M-TL-Sen1Floods11 \ --io-processor-plugin terratorch_segmentation \ --model-impl terratorch \ --skip-tokenizer-init \ --enforce-eager \ --enable-mm-embeds ``` -------------------------------- ### Setup Test Dataset Source: https://github.com/terrastackai/terratorch/blob/main/examples/pixelwise_regression/pixelwise_regression_agb.ipynb Configure the datamodule for testing and check the test dataset size. ```python # checking datasets testing split size datamodule.setup("test") test_dataset = datamodule.test_dataset print(f"Available samples in the test dataset: {len(test_dataset)}") ``` -------------------------------- ### Download Example Input Image Source: https://github.com/terrastackai/terratorch/blob/main/examples/embeddings/embedding_generation_manual_backbone_registry.ipynb Downloads a sample Sentinel-2 TIFF file from the Hugging Face Hub if it does not exist locally. ```python # Download example input image image_path = Path('examples/S2L2A/Santiago.tif') if not image_path.exists(): hf_hub_download(repo_id='ibm-esa-geospatial/Examples', filename='S2L2A/Santiago.tif', repo_type='dataset', local_dir='examples/') ``` -------------------------------- ### Install TerraTorch for Development Source: https://github.com/terrastackai/terratorch/blob/main/README.md Commands to clone the repository and install it in editable mode with optional dependencies. ```bash git clone https://github.com/terrastackai/terratorch.git cd terratorch pip install -e .[test] ``` ```bash pip install -e .[wxc] ``` -------------------------------- ### Setup Lightning Trainer Source: https://github.com/terrastackai/terratorch/blob/main/examples/segmentation/segmentation_sen1floods11.ipynb Configures the PyTorch Lightning Trainer with logging, checkpointing, and progress tracking callbacks. ```python import os from lightning.pytorch import Trainer from lightning.pytorch.callbacks import LearningRateMonitor, ModelCheckpoint, RichProgressBar from lightning.pytorch.loggers import TensorBoardLogger experiment = "tutorial" os.makedirs("tutorial_experiments", exist_ok=True) default_root_dir = os.path.join("tutorial_experiments", experiment) logger = TensorBoardLogger(save_dir=default_root_dir, name=experiment) checkpoint_callback = ModelCheckpoint( save_top_k=1, save_last=True, ) trainer = Trainer( accelerator="auto", # or specify cpu or gpu max_epochs=1, # for demo purposes default_root_dir=default_root_dir, logger=logger, callbacks=[ RichProgressBar(), checkpoint_callback, LearningRateMonitor(logging_interval="epoch"), ], log_every_n_steps=1, ) ``` -------------------------------- ### Initialize Trainer and Start Training Source: https://github.com/terrastackai/terratorch/blob/main/examples/datasets_and_benchmarks/forestnet_dataset_timm_model.ipynb Configure the PyTorch Lightning Trainer with callbacks and loggers, then execute the training loop. ```python from lightning.pytorch import Trainer from lightning.pytorch.callbacks import ModelCheckpoint, RichProgressBar from lightning.pytorch.loggers import TensorBoardLogger checkpoint = ModelCheckpoint(monitor=task.monitor, save_top_k=1, save_last=True) logger = TensorBoardLogger(save_dir="output", name="tutorial") trainer = Trainer( devices=1, precision="16-mixed", max_epochs=1, # demo only callbacks=[RichProgressBar(), checkpoint], logger=logger, default_root_dir="output/tutorial", log_every_n_steps=1, ) _ = trainer.fit(task, datamodule) ``` -------------------------------- ### Specify example input image file Source: https://github.com/terrastackai/terratorch/blob/main/docs/tutorials/burn_scars_inference_simplified.md Sets the path to a single image file for testing direct inference. ```python example_file = "data/examples/subsetted_512x512_HLS.S30.T10SEH.2018190.v1.4_merged.tif" ``` -------------------------------- ### Fine-tuning via CLI Source: https://github.com/terrastackai/terratorch/blob/main/examples/multitemporal_data/temporalwrapper_segementation_crop.ipynb Execute fine-tuning using a configuration file for a no-code setup. This command requires a YAML configuration file specifying the training components. ```bash !terratorch fit -c temporalwrapper_segementation_crop.yaml ``` -------------------------------- ### Start vLLM server with TerraTorch plugin Source: https://github.com/terrastackai/terratorch/blob/main/docs/guide/vllm/vllm_io_plugins.md Use the --io-processor-plugin flag to enable the terratorch_segmentation plugin during vLLM server startup. ```bash vllm serve \ --model=ibm-nasa-geospatial/Prithvi-EO-2.0-300M-TL-Sen1Floods11 \ --model-impl terratorch \ --task embed --trust-remote-code \ --skip-tokenizer-init --enforce-eager \ --io-processor-plugin terratorch_segmentation ``` -------------------------------- ### Debug and Fix Broken Model URLs Source: https://github.com/terrastackai/terratorch/blob/main/examples/models/models-abstraction.ipynb This example guides you on how to find and fix broken model URLs by locating the model's configuration file and checking for `hf_hub_id` or `url` settings. It uses Python's `inspect` module to find the file location. ```python # How to find and fix broken model URLs # # Step 1: Find where the model is defined # - Search the codebase for the model name # - For Clay: terratorch/models/backbones/clay_v1/embedder.py # # Step 2: Look for `default_cfgs` or `hf_hub_id` # - This is where HuggingFace URLs are configured import inspect from terratorch.models.backbones.clay_v1 import embedder # Find the file location print("📁 FILE TO EDIT:") print(f" {inspect.getfile(embedder)}") print() # Show the current HuggingFace configuration print("🔗 CURRENT HUGGINGFACE CONFIG:") if hasattr(embedder, 'default_cfgs'): for model_name, cfg in embedder.default_cfgs.items(): hf_url = getattr(cfg.default, 'hf_hub_id', 'Not set') print(f" {model_name}: {hf_url}") else: print(" (Check the file manually for hf_hub_id or url settings)") ``` -------------------------------- ### Install TerraTorch as a developer Source: https://github.com/terrastackai/terratorch/blob/main/docs/guide/quick_start.md Clone the repository and install in editable mode for development. ```shell git clone https://github.com//terratorch.git pip install -e . ``` -------------------------------- ### Install TerraTorch via Conda Source: https://github.com/terrastackai/terratorch/blob/main/README.md Installation command for users of the conda-forge channel. ```bash conda install -c conda-forge terratorch ``` -------------------------------- ### Initialize Environment and Device Source: https://github.com/terrastackai/terratorch/blob/main/examples/embeddings/embedding_generation_manual_backbone_registry.ipynb Sets up the necessary imports and detects the available hardware device (CUDA, MPS, or CPU). ```python from pathlib import Path import torch import rioxarray as rxr from huggingface_hub import hf_hub_download import numpy as np # Select device if torch.cuda.is_available(): device = 'cuda' elif torch.backends.mps.is_available(): device = 'mps' else: device = 'cpu' ``` -------------------------------- ### Install GDAL via Conda Source: https://github.com/terrastackai/terratorch/blob/main/README.md Recommended method for installing the required GDAL dependency. ```bash conda install -c conda-forge gdal ``` -------------------------------- ### Install TerraTorch via Pipx Source: https://github.com/terrastackai/terratorch/blob/main/README.md Installs the library in an isolated environment for CLI usage. ```bash pipx install terratorch ``` -------------------------------- ### Load and Inspect Backbones Source: https://github.com/terrastackai/terratorch/blob/main/examples/models/models-abstraction.ipynb Demonstrates how to instantiate a backbone from the registry and process an image to extract features. ```python # Level 1: Load a backbone from terratorch.registry import BACKBONE_REGISTRY import torch # Build a backbone backbone = BACKBONE_REGISTRY.build("prithvi_eo_v2_100_tl", pretrained=True) print(f"Type: {type(backbone).__name__}") ``` ```python # See how the backbone processes an image fake_image = torch.randn(1, 6, 224, 224) # [batch, channels, height, width] backbone.eval() with torch.no_grad(): features = backbone(fake_image) # Print output shapes print("Features shape:") for i, f in enumerate(features): print(f" tensor {i}: {f.shape}") ``` -------------------------------- ### Download Sample Data Assets Source: https://github.com/terrastackai/terratorch/blob/main/examples/webapps/prithvi.tiny.onnx.demo/README.md Use wget to retrieve individual sample images and binary files from the cloud storage bucket. ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_51_y.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_51_pred_mask.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_80_rgb.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_80_x.bin ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_80_y.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_80_pred_mask.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_108_rgb.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_108_x.bin ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_108_y.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_108_pred_mask.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_111_rgb.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_111_x.bin ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_111_y.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_111_pred_mask.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_112_rgb.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_112_x.bin ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_112_y.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_112_pred_mask.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_113_rgb.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_113_x.bin ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_113_y.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_113_pred_mask.png ``` -------------------------------- ### Install TerraTorch Dependencies Source: https://github.com/terrastackai/terratorch/blob/main/examples/datasets_and_benchmarks/forestnet_dataset_timm_model.ipynb Install the required packages for running TerraTorch in a Google Colab environment. ```python !pip install terratorch==1.2.1 !pip install gdown tensorboard !pip install -U jupyter ipywidgets ``` -------------------------------- ### Install TerraTorch and Dependencies Source: https://github.com/terrastackai/terratorch/blob/main/examples/classification/classification_eurosat.ipynb Install the required packages for running TerraTorch in a Google Colab environment. ```bash !pip install terratorch==1.2.1 !pip install tensorboard !pip install -U jupyter ipywidgets ``` -------------------------------- ### Install TerraTorch via Pip Source: https://github.com/terrastackai/terratorch/blob/main/README.md Commands for installing stable releases or the latest development branch. ```bash pip install terratorch== ``` ```bash pip install git+https://github.com/terrastackai/terratorch.git ``` -------------------------------- ### Local Development and Deployment Commands Source: https://github.com/terrastackai/terratorch/blob/main/examples/webapps/prithvi.tiny.onnx.demo/README.md Commands for setting up the development environment, building the project, and serving the application locally. ```bash python3 -m http.server ``` ```bash pnpm i ``` ```bash npm install vite-plugin-pwa --save-dev ``` ```bash pnpm dev ``` ```bash pnpm build ``` -------------------------------- ### Install TerraTorch using pip Source: https://github.com/terrastackai/terratorch/blob/main/docs/guide/quick_start.md Use this command to install the latest stable release of TerraTorch. ```shell pip install terratorch ``` -------------------------------- ### Request Payload Examples Source: https://github.com/terrastackai/terratorch/blob/main/docs/guide/vllm/plugins/tm_segmentation_io_plugin.md Examples of request payloads demonstrating different data formats and output configurations. ```json { "data_format": "url", "out_data_format": "b64_json", "data": { "DEM": "https://example.com/path/to/dem_file", "S1RTC": "https://example.com/path/to/S1RTC_file", "S2L2A": "https://example.com/path/to/S2L2A_file" } } ``` ```json { "data_format": "path", "out_data_format": "path", "data": "/path/to/input/directory" } ``` ```json { "data_format": "url", "out_data_format": "path", "out_path": "/custom/output/directory", "data": { "DEM": "https://example.com/path/to/dem_file", "S1RTC": "https://example.com/path/to/S1RTC_file", "S2L2A": "https://example.com/path/to/S2L2A_file" } } ``` -------------------------------- ### Download Sample Data for Prithvi.tiny Demo Source: https://github.com/terrastackai/terratorch/blob/main/examples/webapps/prithvi.tiny.onnx.demo/README.md Use these wget commands to download the necessary sample images and data files for the Prithvi.tiny web demo. Ensure you copy them to the current directory and the 'dist' directory as needed. ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_3_rgb.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_3_x.bin ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_3_y.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_3_pred_mask.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_16_rgb.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_16_x.bin ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_16_y.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_16_pred_mask.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_18_rgb.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_18_x.bin ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_18_y.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_18_pred_mask.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_21_rgb.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_21_x.bin ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_21_y.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_21_pred_mask.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_23_rgb.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_23_x.bin ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_23_y.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_23_pred_mask.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_37_rgb.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_37_x.bin ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_37_y.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_37_pred_mask.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_39_rgb.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_39_x.bin ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_39_y.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_39_pred_mask.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_40_rgb.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_40_x.bin ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_40_y.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_40_pred_mask.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_42_rgb.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_42_x.bin ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_42_y.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_42_pred_mask.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_49_rgb.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_49_x.bin ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_49_y.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_49_pred_mask.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_51_rgb.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_51_x.bin ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_51_y.png ``` ```bash wget https://prithvi-demo-onnx-mobile.s3-web.eu-de.cloud-object-storage.appdomain.cloud/sample_51_pred_mask.png ``` -------------------------------- ### Generate vLLM Config with Input Spec as File Source: https://github.com/terrastackai/terratorch/blob/main/docs/guide/vllm/prepare_your_model.md This example demonstrates generating a vLLM configuration by providing the input specification via a JSON file. First, create the input JSON file, then pass its path to the script. ```bash cat << EOF > ./input_sample.json { "target": "pixel_values", "data": { "pixel_values": { "type": "torch.Tensor", "shape": [6,512,512] }, "location_coords": { "type": "torch.Tensor", "shape": [1,2] } } } EOF python vllm_config_generator.py \ --ttconfig config.yaml \ -i ./input_sample.json ``` -------------------------------- ### Using MultiSourceRegistries for Decoders Source: https://github.com/terrastackai/terratorch/blob/main/docs/guide/registry.md Illustrates how to use `MultiSourceRegistries` like `DECODER_REGISTRY` to instantiate decoders. It shows how to specify the registry prefix for clarity or rely on the default search order. The `keys()` method can be used to inspect available prefixes. ```python from terratorch import DECODER_REGISTRY # decoder registries always take at least one extra argument, the channel list with the channel dimension of each embedding passed to it DECODER_REGISTRY.build("FCNDecoder", [32, 64, 128]) DECODER_REGISTRY.build("terratorch_FCNDecoder", [32, 64, 128]) # Find all prefixes DECODER_REGISTRY.keys() >>> odict_keys(['terratorch', 'smp', 'mmseg']) ``` -------------------------------- ### Install MMSegmentation dependencies Source: https://github.com/terrastackai/terratorch/blob/main/docs/guide/encoder_decoder_factory.md Commands to install the required MMSegmentation environment, noting the specific version constraints for mmcv and torch. ```sh pip install -U openmim mim install mmengine mim install mmcv==2.1.0 pip install regex ftfy mmsegmentation ``` -------------------------------- ### Install TerraTorch and Dependencies Source: https://github.com/terrastackai/terratorch/blob/main/examples/embeddings/embedding_generation_burnscars.ipynb Installs the TerraTorch library and other necessary packages like gdown and tensorboard. Recommended for Google Colab environments. ```python !pip install terratorch==1.2.4 !pip install gdown tensorboard !pip install -U jupyter ipywidgets ``` -------------------------------- ### Configure Training Pipeline Source: https://github.com/terrastackai/terratorch/blob/main/examples/pixelwise_regression/pixelwise_regression_agb.ipynb Set up the Lightning Trainer and ModelCheckpoint for fine-tuning the Prithvi model. ```python from terratorch.tasks import PixelwiseRegressionTask import lightning.pytorch as pl pl.seed_everything(0) checkpoint_callback = pl.callbacks.ModelCheckpoint( dirpath="output/agb/checkpoints/", monitor="val/RMSE", # metric to monitor mode="min", filename="best-{epoch:02d}", ) # Lightning Trainer trainer = pl.Trainer( accelerator="auto", strategy="auto", devices=1, # single device for notebook stability precision="16-mixed", # speed up training num_nodes=1, logger=True, # uses TensorBoard by default max_epochs=1, # demo only log_every_n_steps=1, enable_checkpointing=True, callbacks=[checkpoint_callback, pl.callbacks.RichProgressBar()], default_root_dir="output/agb", detect_anomaly=True, ) ``` -------------------------------- ### Perform Inference and Get Predictions Source: https://github.com/terrastackai/terratorch/blob/main/examples/pixelwise_regression/pixelwise_regression_agb.ipynb Loads data, applies augmentation, moves images to the model's device, and performs inference to get model predictions. Ensure the augmentation matches training. ```python import torch test_loader = datamodule.test_dataloader() with torch.no_grad(): batch = next(iter(test_loader)) # Apply the same normalization used during training batch = datamodule.aug(batch) images = batch["image"].to(model.device) outputs = model(images) preds = outputs.output ``` -------------------------------- ### Initialize Environment and Imports Source: https://github.com/terrastackai/terratorch/blob/main/examples/weather_climate/WxCTutorialGravityWave.ipynb Import necessary libraries and initialize the distributed process group for training. ```python import terratorch # this import is needed to initialize TT's factories from lightning.pytorch import Trainer import os import torch from huggingface_hub import hf_hub_download, snapshot_download from terratorch.models.wxc_model_factory import WxCModelFactory from terratorch.tasks.wxc_task import WxCTask import torch.distributed as dist ``` ```python os.environ['MASTER_ADDR'] = 'localhost' os.environ['MASTER_PORT'] = '12355' if dist.is_initialized(): dist.destroy_process_group() dist.init_process_group( backend='gloo', init_method='env://', rank=0, world_size=1 ) ``` -------------------------------- ### Model Pipeline Output Source: https://github.com/terrastackai/terratorch/blob/main/examples/multitemporal_data/temporalwrapper_segementation_crop.ipynb Example output from the pipeline inspection script. ```text Output: Model Pipeline — Diff Subsets Pooling -------------------------------------- Input shape: (1, 6, 3, 224, 224) [B, C, T, H, W] → Batch size 1, 6 channels, 3 timesteps Backbone: terramind_v1_small Temporal pooling: diff Encoder feature maps: 12 Example feature map: (1, 196, 384) The 12 intermediate layers of the TerraMind ViT-Small backbone each produce a feature map of 196 tokens with 384 dimensions. Diff subsets pooling computes the difference between the mean of two temporal subsets and returns a single feature representation with the same shape as a single timestep.This keeps the decoder size and parameter count identical to mean pooling. Model Parameters ---------------- Total parameters: 31.32M Trainable (decoder): 9.44M ``` -------------------------------- ### Fine-tune via CLI Source: https://github.com/terrastackai/terratorch/blob/main/examples/datasets_and_benchmarks/burnscars_dataset_prithvi.ipynb Executes training using configuration files via the command line interface. ```bash !terratorch fit -c burnscars_dataset_prithvi.yaml ``` ```bash !terratorch fit -c burnscars_dataset_smp_unet.yaml ``` -------------------------------- ### Using Registries to Find and Instantiate Models Source: https://github.com/terrastackai/terratorch/blob/main/docs/guide/registry.md Demonstrates how to search for available models within a registry, check for model existence, and instantiate models with optional arguments like pretrained weights or custom checkpoint paths. The registry prefix is optional during instantiation. ```python from terratorch import BACKBONE_REGISTRY # find available prithvi models print([model_name for model_name in BACKBONE_REGISTRY if "terratorch_prithvi" in model_name]) >>> ['terratorch_prithvi_eo_tiny', 'terratorch_prithvi_eo_v1_100', 'terratorch_prithvi_eo_v2_300', 'terratorch_prithvi_eo_v2_600', 'terratorch_prithvi_eo_v2_300_tl', 'terratorch_prithvi_eo_v2_600_tl'] # show all models with list(BACKBONE_REGISTRY) # check a model is in the registry "terratorch_prithvi_eo_v2_300" in BACKBONE_REGISTRY >>> True # without the prefix, all internal registries will be searched until the first match is found "prithvi_eo_v1_100" in BACKBONE_REGISTRY >>> True # instantiate your desired model # the backbone registry prefix (e.g. `terratorch` or `timm`) is optional # in this case, the underlying registry is terratorch. model = BACKBONE_REGISTRY.build("prithvi_eo_v1_100", pretrained=True) # instantiate your model with more options, for instance, passing weights from your own file model = BACKBONE_REGISTRY.build( "prithvi_eo_v2_300", num_frames=1, ckpt_path='path/to/model.pt' ) # Rest of your PyTorch / PyTorchLightning code ``` -------------------------------- ### Verify Dataset Size Source: https://github.com/terrastackai/terratorch/blob/main/examples/segmentation/segmentation_sen1floods11.ipynb Checks the number of samples available in the test dataset after setup. ```python # Check the size of the test dataset datamodule.setup("test") test_dataset = datamodule.test_dataset print(f"Available samples in the test dataset: {len(test_dataset)}") ``` -------------------------------- ### Initialize Configuration Source: https://github.com/terrastackai/terratorch/blob/main/examples/weather_climate/InspectMerra2Data.ipynb Load the configuration file required for the model factory. ```python import xarray as xr from granitewxc.utils.config import get_config from granitewxc.utils.data import _get_transforms config = get_config("../../integrationtests/test_prithvi_wxc_model_factory_config.yaml") ``` -------------------------------- ### Get Training Dataset Size Source: https://github.com/terrastackai/terratorch/blob/main/examples/multitemporal_data/temporalwrapper_segementation_crop.ipynb Retrieve the training dataset object from the datamodule and display its size. ```python train_dataset = datamodule.train_dataset len(train_dataset) ``` -------------------------------- ### Configure Model and Task Source: https://github.com/terrastackai/terratorch/blob/main/examples/weather_climate/WxCTutorialGravityWave.ipynb Define model arguments and initialize the WxCTask for evaluation or training. ```python from prithviwxc.gravitywave.datamodule import ERA5DataModule model_args = { "in_channels": 1280, "input_size_time": 1, "n_lats_px": 64, "n_lons_px": 128, "patch_size_px": [2, 2], "mask_unit_size_px": [8, 16], "mask_ratio_inputs": 0.5, "embed_dim": 2560, "n_blocks_encoder": 12, "n_blocks_decoder": 2, "mlp_multiplier": 4, "n_heads": 16, "dropout": 0.0, "drop_path": 0.05, "parameter_dropout": 0.0, "residual": "none", "masking_mode": "both", "decoder_shifting": False, "positional_encoding": "absolute", "checkpoint_encoder": [3, 6, 9, 12, 15, 18, 21, 24], "checkpoint_decoder": [1, 3], "in_channels_static": 3, "input_scalers_mu": torch.tensor([0] * 1280), "input_scalers_sigma": torch.tensor([1] * 1280), "input_scalers_epsilon": 0, "static_input_scalers_mu": torch.tensor([0] * 3), "static_input_scalers_sigma": torch.tensor([1] * 3), "static_input_scalers_epsilon": 0, "output_scalers": torch.tensor([0] * 1280), #"encoder_hidden_channels_multiplier" : [1, 2, 4, 8], #"encoder_num_encoder_blocks" : 4, #"decoder_hidden_channels_multiplier" : [(16, 8), (12, 4), (6, 2), (3, 1)], #"decoder_num_decoder_blocks" : 4, "aux_decoders": "unetpincer", "backbone": "prithviwxc", "skip_connection": "True" } task = WxCTask('WxCModelFactory', model_args=model_args, mode='eval') ``` ```python task = WxCTask('WxCModelFactory', model_args=model_args, mode='train') ``` -------------------------------- ### Specify configuration file path Source: https://github.com/terrastackai/terratorch/blob/main/docs/tutorials/burn_scars_inference_simplified.md Sets the path to the YAML configuration file required for model setup. ```python config_file = "burn_scars_config.yaml" ``` -------------------------------- ### Get Validation Dataset Size Source: https://github.com/terrastackai/terratorch/blob/main/examples/multitemporal_data/multitemporal_model_segementation_crop.ipynb Calculates and returns the total number of samples in the validation split of the dataset. ```python # checking datasets validation split size val_dataset = datamodule.val_dataset len(val_dataset) ``` -------------------------------- ### Initialise a Prithvi model Source: https://github.com/terrastackai/terratorch/blob/main/examples/segmentation/segmentation_sen1floods11.ipynb Builds a Prithvi EO v2 model using the registry. ```python model = BACKBONE_REGISTRY.build("prithvi_eo_v2_100_tl") print(model) ``` -------------------------------- ### Get Training Dataset Size Source: https://github.com/terrastackai/terratorch/blob/main/examples/multitemporal_data/multitemporal_model_segementation_crop.ipynb Calculates and returns the total number of samples in the training split of the dataset. ```python # checking datasets train split size train_dataset = datamodule.train_dataset len(train_dataset) ``` -------------------------------- ### vLLM Instance Ready Logs Source: https://github.com/terrastackai/terratorch/blob/main/docs/guide/vllm/serving_a_model_image.md These logs indicate a successfully initialized vLLM serving instance, ready to accept requests. ```log (APIServer pid=532339) INFO 10-01 09:01:06 [launcher.py:44] Route: /scale_elastic_ep, Methods: POST (APIServer pid=532339) INFO 10-01 09:01:06 [launcher.py:44] Route: /is_scaling_elastic_ep, Methods: POST (APIServer pid=532339) INFO 10-01 09:01:06 [launcher.py:44] Route: /invocations, Methods: POST (APIServer pid=532339) INFO 10-01 09:01:06 [launcher.py:44] Route: /metrics, Methods: GET (APIServer pid=532339) INFO: Started server process [532339] (APIServer pid=532339) INFO: Waiting for application startup. (APIServer pid=532339) INFO: Application startup complete. ``` -------------------------------- ### Configure and Run PyTorch Lightning Trainer Source: https://github.com/terrastackai/terratorch/blob/main/examples/weather_climate/WxCTutorialDownscalingECCC.ipynb Initializes a trainer with logging and checkpointing, then executes the training process. ```python logger = TensorBoardLogger("logs", name="eccc", log_graph=False) checkpoint_callback = CheckpointCallback(config, save_every_n_epochs=2) trainer = Trainer( max_epochs=3, check_val_every_n_epoch=1, log_every_n_steps=config.data.n_random_windows, logger=logger, callbacks=[checkpoint_callback], devices=1, precision="16-mixed", ) ``` ```python datamodule.setup(stage='val') trainer.fit(model=task, datamodule=datamodule) ``` -------------------------------- ### Example Output for Concat Pooling Inspection Source: https://github.com/terrastackai/terratorch/blob/main/examples/multitemporal_data/temporalwrapper_segementation_crop.ipynb Expected console output when running the inspection script for concat pooling. ```text Model Pipeline — Concat Pooling ------------------------------- Input shape: (1, 6, 3, 224, 224) [B, C, T, H, W] → Batch size 1, 6 channels, 3 timesteps Backbone: terramind_v1_small Temporal pooling: concat Encoder feature maps: 12 Example feature map: (1, 196, 1152) The TerraMind ViT-Small backbone returns a feature map per transformer block (12 total), each with 196 spatial tokens and 384-dimensional embeddings. Concat pooling stacks the features from all 3 timesteps along the channel dimension (now 1152-dim) before passing them to the U-Net decoder, increasing the decoder’s input size and number of parameters. Model Parameters ---------------- Total parameters: 44.81M Trainable (decoder): 22.94M ```