Downloads · 30 days
5
14% of all-time downloads
farmersforforests/TreeLens-detectron2
TreeLens-detectron2 is a object detection model from farmersforforests. Use it when you need objects located in an image. The card lists the license as mit.
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…
Downloads · 30 days
5
14% of all-time downloads
All-time downloads
35
Public
Repo size
330 MB
Likes
3
Public
Click a slice to open those files.
.pth330 MB · 100%
From the Hugging Face model README
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.
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.
.tif format) and digital elevation model (.tif format) in your Google Drive./content/drive/MyDrive/test.tif/content/drive/MyDrive/test.tif/content/drive/MyDrive
(If you want to use different paths, edit lines 6 to 11 in Cell 2 of the script).You can run this code by uploading the script to a Google Colab notebook:
This cell links your Google Drive to the runtime environment to access your orthomosaic and DEM images.
from google.colab import drive
drive.mount('/content/drive')
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)Downloads the Detectron2 weights (Treelens_model.pth) and the training configuration (config.yaml) from the Hugging Face hub automatically.
repo_id = "farmersforforests/TreeLens-detectron2"
Installs and imports the core packages:
sahi (Sliced Inference framework)detectron2 (Object Detection engine)rasterio (Geospatial metadata & projection parser)scipy & pycocotoolsExecutes function definitions for:
Loads the Detectron2 model onto your GPU (cuda:0).
Processes the orthomosaics, runs sliced inference, plots predictions, and saves the output logs.
Once the script runs successfully, the following files will be saved in your save_dir (Google Drive root by default):
{basename}_pred.png: The orthomosaic image with visual bounding boxes drawn around all detected trees.{basename}.xml: Standard Pascal VOC XML annotation file.{basename}.json: Standard COCO JSON annotation file.{basename}_pred.csv: A spreadsheet with:
xmin, ymin, xmax, ymax).lon, lat).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.If you wish to run this script outside of Colab (e.g. locally in a terminal):
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu118
python -m pip install 'git+https://github.com/facebookresearch/detectron2.git'
pip install -U sahi huggingface_hub rasterio scipy pycocotools pandas matplotlib pillow
drive.mount) in the script.python treelens_ortho_inference.py