This repository hosts the TreeLens tree detection model, built on Detectron2 and integrated with the SAHI (Slicing Aided Hyper Inference) library. It is designed to detect and map tree crowns in high-resolution aerial orthomosaics and estimate tree biophysical parameters like crown width, height, and biomass (using digital elevation models).
Developed by Farmers for Forests, this model is optimized to work with SAHI sliced inference.
How to Run the Inference Script
This guide explains how to run the treelens_ortho_inference.py script step-by-step. The script is structured with # %% cell markers, meaning you can easily open it in Google Colab (by renaming it to .ipynb or uploading it directly) or run it in VS Code as interactive cells.
Step 1: Upload and Organize Your Files
Place your orthomosaic (.tif format) and digital elevation model (.tif format) in your Google Drive.
By default, the script looks for your files at:
Orthomosaic:/content/drive/MyDrive/test.tif
DEM (Elevation):/content/drive/MyDrive/test.tif
Output Directory:/content/drive/MyDrive(If you want to use different paths, edit lines 6 to 11 in Cell 2 of the script).
Step 2: Open and Run the Script in Google Colab
You can run this code by uploading the script to a Google Colab notebook:
Cell 1: Mount Google Drive
This cell links your Google Drive to the runtime environment to access your orthomosaic and DEM images.
Define your file inputs, output folder, confidence threshold, and average tree crown width.
aveg_width = 13 The script will automatically calculate the best slicing patch size based on this width and the ground resolution (GSD) of your .tif file. aveg_width/GSD = sahi slice size (pixel)
Cell 3: Download Model Files from Hugging Face
Downloads the Detectron2 weights (Treelens_model.pth) and the training configuration (config.yaml) from the Hugging Face hub automatically.
Spatial coordinates of each tree center (lon, lat).
Calculated Crown (m) width of each tree.
{basename}_pred_with_heights.csv: Same as above, updated with Height (m) for each tree derived from the DEM file.
{basename}_pred_with_biomass.csv: (Optional) Estimates DBH (cm) and AGB (Above Ground Biomass in kg) for each tree. To enable this, set calculate_biomass = True in Cell 9.
Local Terminal/CLI Execution (Optional)
If you wish to run this script outside of Colab (e.g. locally in a terminal):