Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Running inference and inspecting raw Mask R-CNN predictions

1. Motivation

You trained a model on the previous page. This page applies it to new imagery and works through exactly what geoai hands back, before the next page cleans and vectorizes that output into something GIS-ready.

Instance segmentation inference returns more than a single raster. Knowing what each piece of the output actually represents, rather than treating the whole result as one opaque object, is what lets you debug a poor prediction and decide which parts of the output your project actually needs.


2. Core idea

geoai.instance_segmentation() runs sliding-window inference the same way detection and semantic segmentation did in earlier lessons, and returns a dictionary with four related but distinct outputs: an instance raster, a class-label raster, a confidence-score raster, and, optionally, a vectorized set of polygons.


3. Workflow

Once model training produces a saved checkpoint (best_model.pth), the next step is Inference—applying the trained network to unseen test imagery. Instance segmentation produces multi-layered outputs containing spatial geometry, class identities, unique instance IDs, and prediction confidence. Inspecting raw model predictions before applying post-processing or cleaning filters is essential for understanding true model behavior and diagnosing failure modes.


A. Test image selection

We re-establish the dataset environment and select an independent test image from the held-out test split:

Using sorted() ensures file order remains deterministic and reproducible across execution environments.


B. Executing instance segmentation

Satellite scenes are frequently larger than the fixed 256×256256 \times 256 pixel input size used during training. We apply the model using Tiled Inference, sliding a window across the target image and stitching predictions together:

Tiled inference parameters dictate both prediction quality and computational efficiency:


C. Deconstructing the result dictionary

The instance_segmentation() function returns a structured dictionary containing four distinct analytical representations:

Output KeyData StructureDescription & Analytical Purpose
"instance"Raster (uint16)A label matrix where background pixels equal 0 and every detected field is assigned a unique positive integer ID (1,2,3…1, 2, 3 \dots).
"class_label"Raster (uint8)A semantic mask storing binary classification results (0 = background, 1 = field).
"score"Raster (float32)A continuous spatial surface of per-pixel confidence values (0.0 to 1.00.0 \text{ to } 1.0).
"vector"GeoDataFrameVector polygons generated when vectorize=True, containing geometries attributed with instance_id, class_name, and score.

D. Visualizing raw raster predictions

Displaying raw raster outputs provides immediate visual feedback on model performance before any post-processing smoothing or polygon filtering occurs:

When inspecting raw rasters, evaluate predictions against these active diagnostic questions:


E. Inspecting raw vectorized outputs

When vectorize=True, the pipeline polygonizes raster instance masks into a GeoPandas GeoDataFrame. Interactive vector visualization allows styling polygons by model confidence:

Styling vector outputs by score immediately highlights low-confidence candidate polygons (e.g., scores between 0.50 and 0.65). These low-certainty features represent prime candidates for targeted manual QA/QC or post-processing threshold adjustments.


F. Diagnosing failure modes

Instance segmentation models exhibit structural and spatial errors beyond standard misclassification. Evaluating raw test outputs requires identifying four common error categories:

1. Missed Instance (False Negative)        2. Spurious Instance (False Positive)
Reference:  [ Field A ] [ Field B ]        Reference:  [ Field A ] [  Background  ]
Prediction: [ Field A ] [    ---   ]        Prediction: [ Field A ] [ Ghost Parcel ]

3. Merged Instances (Structural Error)    4. Split Instance (Structural Error)
Reference:  [ Field A ] [ Field B ]        Reference:  [     Field A     ]
Prediction: [   Combined Polygon  ]        Prediction: [ Part 1 ][ Part 2 ]

G. Quantitative instance evaluation

While visual inspection isolates specific spatial failures, rigorous evaluation requires quantitative performance metrics. Instance segmentation metrics evaluate both bounding box detection and mask geometry overlap:

IoU(A,B)=∣A∩B∣∣A∪B∣\text{IoU}(A, B) = \frac{\vert{}A \cap B\vert{}}{\vert{}A \cup B\vert{}}

A predicted instance is typically counted as a True Positive if its IoU≥0.50\text{IoU} \ge 0.50.


4. Python reactivation

result["instance"], result["class_label"], result["score"], and result["vector"] are all dictionary lookups by key, the same pattern used for data["images_dir"] on the previous page, just applied here to a function’s return value instead of a prepared dataset.


5. Common pitfalls


6. Mini task

You inspect result["instance"] and see 12 distinct non-zero values, but result["vector"] contains only 10 rows. What is the most likely explanation, and how would you check it?

Sample solution

The most likely explanation is that the vectorization step (or a filtering step applied to it) dropped two very small or otherwise invalid detections, for example instances too small to form a valid polygon, rather than an error in the count. You would check this by comparing the actual instance ID values present in the raster (via numpy.unique()) against the instance IDs listed in the result["vector"] GeoDataFrame, and inspect the two missing IDs directly in the raster to see whether they correspond to tiny, likely spurious detections.


7. Key takeaways

Further reading