Note: This notebook requires the Wherobots RasterFlow functionality to be enabled and a GPU runtime selected in Wherobots Cloud.
Model Prerequisites
RasterFlow currently supports PyTorch models in the PT2 format with the following prerequisites:- Supported tasks: semantic segmentation and regression
- Supported models: single image batch input of shape (Batch, Channel, Height, Width), single prediction batch output (Batch, Category, Height, Width).
- Our prediction workflows assume the model returns the raw PyTorch model output without operations to determine categories, unit conversion, etc.
Resources
For examples of models and scripts you can run in RasterFlow, see our Hugging Face collection. PyTorch PT2 export documentation:- Common Challenges and Solutions — Beginner
- Export Tutorial — Advanced (complex models, accelerator optimization)
- PT2 Archive Format
Optional Setup for CPU Runtimes
You can run the following to install torch and torchvision for CPU runtimes to walk through this example.!uv pip install torch==2.8 torchvision --extra-index-url https://download.pytorch.org/whl/cpu
1. Create a Toy Model
We’ll create an example model matching the signature of the Meta/WRI Canopy Height Model: the input is a tensor of image data and the output is a tensor of continuous values (canopy height in meters). The shape of the output is the same as the input.2. PyTorch 2 Export Formats
PyTorch offers multiple export formats for different use cases: storing weights, training, edge inference, and server inference.Why not export with the .pth format?
You may be familiar with the checkpoint format saved as .pth:
state_dict exports can’t be optimized for specific accelerators, making deployment difficult.
The .pt2 Format — A Better Alternative
torch.export produces a single artifact for both training and inference with some key benefits:
- Device flexibility — Export on CPU, load parameters to GPU at runtime
- Accelerator optimization — Compile for faster execution on CUDA, AMD, or Intel GPUs
- Standardized metadata — Store hyperparameters, configuration, and transforms alongside the model
We’ll export our
ExampleModel in PT2 format. For input preprocessing, we’ll export transforms as an nn.Module in the same archive. Using torch.nn.Sequential simplifies export since its forward method takes a single input argument.
3. Define Input Shape Constraints
torch.export needs to know the expected input shape. Use Dim objects to specify:
- Dynamic dimensions — can be any value ≥1
- Static dimensions — must be a fixed size
FAQ
| Question | Answer |
|---|---|
| How do I know what shape to use? | Input shape affects runtime performance and accuracy. Check the model creator’s recommendations. |
| Should I use dynamic for all dimensions? | Typically no. Some models have data dependent control flow logic that requires fixed dimensions (e.g., object detection models). |
Dim.Auto), 3 channels, 256×256 height/width.
eval mode and configure device/dtype before export:
torch.export needs the forward function’s argument names. Parse them using Python’s inspect module:
4. Export to ExportedProgram
Now that we have the following inputs we can export to anExportedProgram, an in-memory model object that can be saved to a .pt2 archive.
model, we defined this toy model earlier as an nn.Moduleargs, a tuple of the arguments to the model’s forward pass functiondynamic_shapes, a dict mapping the argument name of the input to the tuple of dimension constraints we created earlier:dims
ExportedProgram includes the state_dict (weights) and example_inputs (useful for testing).
5. Save to .pt2 Archive
Note:torch.export.savesaves a single ExportedProgram. We’ll usetorch.export.pt2_archive._package.package_pt2to bundle both the model and transforms into one.pt2file.
6. Run the Model with RasterFlow
RasterFlow supports both Wherobots Hosted Models and custom models like the one we just exported. Steps:- Upload the
.pt2file to S3 (we’ll use Wherobots Managed Storage) - Define an
InferenceConfigto tell RasterFlow how to run the model
Build the Model Input
To run our model, we need some input imagery. We’ll test our model on National Agricultural Imagery Program (NAIP) 4 band imagery - red, blue, green, and near infrared. We’ll select an AOI over Nashua, New Hampshire that has some forest canopy for our toy canopy height model.
Note: We’re using rasterflow_version="v1.43.1" explicitly here. This version is currently being validated before becoming the default in Wherobots Cloud images.
Visualize the Input Mosaic
We will use hvplot and datashader to visualize a small subset of the mosaic’s red band.predict_mosaic method to run our model. predict_mosaic leverages RasterFlow’s powerful inference engine that scales from small to global scale areas of interest.
The inputs to this method are our input store we want to run prediction on, and our InferenceConfig object we created earlier.
Defining the InferenceConfig
With the model on S3, define the inference job configuration.
Note: Wherobots Hosted Models come with preconfigured ModelRecipes— this step is only needed for custom models.
See the InferenceConfig documentation for parameter details.
Note: This step will take approximately 10 minutes to complete when run for the first time
predict_mosaic and confirm that it is the same shape as our input store.
7. What We Learned
Congratulations on completing this guide! You’ve learned how to:- Create a PyTorch model compatible with RasterFlow’s expected input/output signature (Batch, Channel, Height, Width)
- Understand PT2 vs .pth formats — PT2 bundles weights, structure, and execution logic into a single deployable artifact
- Define input shape constraints using
torch.export.Dimfor dynamic batch sizes and fixed spatial dimensions - Export models and transforms together into a single
.pt2archive usingpackage_pt2 - Upload custom models to S3 using Wherobots Managed Storage
- Build GTI mosaics from imagery indexes with
build_gti_mosaic - Run scalable inference using
InferenceConfigandpredict_mosaic
Next Steps
Explore more RasterFlow examples:- RasterFlow Canopy Height Model — Run the real Meta/WRI canopy height model at scale
- RasterFlow ChesapeakeRSC Rural Road Segmentation — Semantic segmentation for rural road detection
- RasterFlow Tile2Net — Extract road networks from satellite imagery
- RasterFlow Documentation — Full API reference
- Wherobots AI Models on Hugging Face — Pre-trained models ready to use
- InferenceConfig Reference — All configuration options for custom models

