Skip to content

Commit f8885cf

Browse files
PatBall1cursoragent
andcommitted
Replace Colab notebooks with clean set aligned to tutorials
- Delete notebooks/241106_colab_JB/ (5 notebooks with branch-specific installs) - Delete old notebooks/colab/ (5 notebooks with deprecated API calls) - Add 4 clean notebooks mirroring Sphinx tutorials 01-04: 01_getting_started: end-to-end tiling, training, prediction 02_data_preparation: RGB/MS tiling, multi-class, advanced options 03_training_and_evaluation: registration, config, training, loss plotting 04_prediction: full postprocessing pipeline including post_clean - All use `pip install detectree2` (PyPI) and current API - Source cells only, no outputs (run on Colab to populate) Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent e75399c commit f8885cf

14 files changed

Lines changed: 1078 additions & 10 deletions

notebooks/241106_colab_JB/tile_multiclass_JB.ipynb

Lines changed: 0 additions & 1 deletion
This file was deleted.

notebooks/241106_colab_JB/tilingJB.ipynb

Lines changed: 0 additions & 1 deletion
This file was deleted.

notebooks/241106_colab_JB/tiling_ms_JB.ipynb

Lines changed: 0 additions & 1 deletion
This file was deleted.

notebooks/241106_colab_JB/trainingJB.ipynb

Lines changed: 0 additions & 1 deletion
This file was deleted.

notebooks/241106_colab_JB/training_ms_JB.ipynb

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"provenance": []
7+
},
8+
"kernelspec": {
9+
"name": "python3",
10+
"display_name": "Python 3"
11+
},
12+
"language_info": {
13+
"name": "python"
14+
},
15+
"accelerator": "GPU"
16+
},
17+
"cells": [
18+
{
19+
"cell_type": "markdown",
20+
"metadata": {},
21+
"source": [
22+
"# Getting Started: Your First Prediction\n",
23+
"\n",
24+
"This notebook provides a concise, end-to-end walkthrough to get you from an orthomosaic to a final crown prediction map using **detectree2**.\n",
25+
"\n",
26+
"The key steps are:\n",
27+
"1. Preparing data (tiling)\n",
28+
"2. Training a model\n",
29+
"3. Making landscape-level predictions\n",
30+
"\n",
31+
"For the full tutorial, see the [documentation](https://patball1.github.io/detectree2/tutorials/01_getting_started.html).\n",
32+
"\n",
33+
"Example data is available on [Zenodo](https://zenodo.org/records/8136161)."
34+
]
35+
},
36+
{
37+
"cell_type": "markdown",
38+
"metadata": {},
39+
"source": [
40+
"## Setup"
41+
]
42+
},
43+
{
44+
"cell_type": "code",
45+
"execution_count": null,
46+
"metadata": {},
47+
"outputs": [],
48+
"source": [
49+
"!pip install torch torchvision torchaudio\n",
50+
"!pip install 'git+https://github.com/facebookresearch/detectron2.git'\n",
51+
"!pip install detectree2"
52+
]
53+
},
54+
{
55+
"cell_type": "code",
56+
"execution_count": null,
57+
"metadata": {},
58+
"outputs": [],
59+
"source": [
60+
"from google.colab import drive\n",
61+
"drive.mount('/content/drive')"
62+
]
63+
},
64+
{
65+
"cell_type": "markdown",
66+
"metadata": {},
67+
"source": [
68+
"## 1. Preparing Data\n",
69+
"\n",
70+
"First, we tile our large orthomosaic and crown data into smaller images suitable for training.\n",
71+
"\n",
72+
"You will need:\n",
73+
"- An orthomosaic (`.tif`)\n",
74+
"- Corresponding tree crown polygons (`.gpkg` or `.shp`)\n",
75+
"\n",
76+
"For best results, manual crowns should be supplied as dense clusters rather than sparsely scattered across the landscape."
77+
]
78+
},
79+
{
80+
"cell_type": "code",
81+
"execution_count": null,
82+
"metadata": {},
83+
"outputs": [],
84+
"source": [
85+
"from detectree2.preprocessing.tiling import tile_data, to_traintest_folders\n",
86+
"import geopandas as gpd\n",
87+
"import rasterio"
88+
]
89+
},
90+
{
91+
"cell_type": "code",
92+
"execution_count": null,
93+
"metadata": {},
94+
"outputs": [],
95+
"source": [
96+
"# Set up input paths\n",
97+
"site_path = \"./Paracou\" # Example path\n",
98+
"img_path = site_path + \"/rgb/Paracou_RGB_2016_10cm.tif\"\n",
99+
"crown_path = site_path + \"/crowns/UpdatedCrowns8.gpkg\"\n",
100+
"\n",
101+
"# Read in crowns and match CRS to the image\n",
102+
"data = rasterio.open(img_path)\n",
103+
"crowns = gpd.read_file(crown_path)\n",
104+
"crowns = crowns.to_crs(data.crs.data)"
105+
]
106+
},
107+
{
108+
"cell_type": "code",
109+
"execution_count": null,
110+
"metadata": {},
111+
"outputs": [],
112+
"source": [
113+
"# Set tiling parameters\n",
114+
"buffer = 30\n",
115+
"tile_width = 40\n",
116+
"tile_height = 40\n",
117+
"threshold = 0.6\n",
118+
"out_dir = site_path + \"/tiles/\"\n",
119+
"\n",
120+
"# Tile the data for training\n",
121+
"tile_data(img_path, out_dir, buffer, tile_width, tile_height, crowns, threshold, mode=\"rgb\")"
122+
]
123+
},
124+
{
125+
"cell_type": "code",
126+
"execution_count": null,
127+
"metadata": {},
128+
"outputs": [],
129+
"source": [
130+
"# Create train/test folders\n",
131+
"to_traintest_folders(out_dir, out_dir, test_frac=0.15)"
132+
]
133+
},
134+
{
135+
"cell_type": "markdown",
136+
"metadata": {},
137+
"source": [
138+
"## 2. Training a Model\n",
139+
"\n",
140+
"Register the training data, configure the model, and train."
141+
]
142+
},
143+
{
144+
"cell_type": "code",
145+
"execution_count": null,
146+
"metadata": {},
147+
"outputs": [],
148+
"source": [
149+
"from detectree2.models.train import register_train_data, MyTrainer, setup_cfg\n",
150+
"\n",
151+
"train_location = out_dir + \"/train/\"\n",
152+
"register_train_data(train_location, 'Paracou', val_fold=5)"
153+
]
154+
},
155+
{
156+
"cell_type": "code",
157+
"execution_count": null,
158+
"metadata": {},
159+
"outputs": [],
160+
"source": [
161+
"# Set the base (pre-trained) model from the detectron2 model_zoo\n",
162+
"base_model = \"COCO-InstanceSegmentation/mask_rcnn_R_101_FPN_3x.yaml\"\n",
163+
"\n",
164+
"trains = (\"Paracou_train\",) # Registered train data\n",
165+
"tests = (\"Paracou_val\",) # Registered validation data\n",
166+
"\n",
167+
"model_output_dir = \"./train_outputs\"\n",
168+
"\n",
169+
"cfg = setup_cfg(base_model, trains, tests, workers=4, eval_period=100, max_iter=3000, out_dir=model_output_dir)"
170+
]
171+
},
172+
{
173+
"cell_type": "code",
174+
"execution_count": null,
175+
"metadata": {},
176+
"outputs": [],
177+
"source": [
178+
"trainer = MyTrainer(cfg, patience=5)\n",
179+
"trainer.resume_or_load(resume=False)\n",
180+
"trainer.train()"
181+
]
182+
},
183+
{
184+
"cell_type": "markdown",
185+
"metadata": {},
186+
"source": [
187+
"## 3. Making Landscape-Level Predictions\n",
188+
"\n",
189+
"Tile the full orthomosaic, run predictions, then project back to geographic coordinates."
190+
]
191+
},
192+
{
193+
"cell_type": "code",
194+
"execution_count": null,
195+
"metadata": {},
196+
"outputs": [],
197+
"source": [
198+
"from detectree2.models.predict import predict_on_data\n",
199+
"from detectree2.models.outputs import project_to_geojson, stitch_crowns, clean_crowns\n",
200+
"from detectron2.engine import DefaultPredictor\n",
201+
"\n",
202+
"# Path to the full orthomosaic\n",
203+
"img_path = site_path + \"/rgb/Paracou_RGB_2016_10cm.tif\"\n",
204+
"pred_tiles_path = site_path + \"/tiles_pred/\"\n",
205+
"\n",
206+
"# Specify tiling parameters (should be similar to training)\n",
207+
"buffer = 30\n",
208+
"tile_width = 40\n",
209+
"tile_height = 40\n",
210+
"tile_data(img_path, pred_tiles_path, buffer, tile_width, tile_height)"
211+
]
212+
},
213+
{
214+
"cell_type": "code",
215+
"execution_count": null,
216+
"metadata": {},
217+
"outputs": [],
218+
"source": [
219+
"# You can use your own trained model or download a pre-trained one\n",
220+
"# !wget https://zenodo.org/records/15863800/files/250312_flexi.pth\n",
221+
"\n",
222+
"trained_model = \"./230103_randresize_full.pth\"\n",
223+
"cfg = setup_cfg(update_model=trained_model)\n",
224+
"predictor = DefaultPredictor(cfg)\n",
225+
"predict_on_data(pred_tiles_path, predictor)"
226+
]
227+
},
228+
{
229+
"cell_type": "code",
230+
"execution_count": null,
231+
"metadata": {},
232+
"outputs": [],
233+
"source": [
234+
"# Project tile predictions to geo-referenced crowns\n",
235+
"project_to_geojson(pred_tiles_path, pred_tiles_path + \"predictions/\", pred_tiles_path + \"predictions_geo/\")\n",
236+
"\n",
237+
"# Stitch and clean crowns\n",
238+
"crowns = stitch_crowns(pred_tiles_path + \"predictions_geo/\")\n",
239+
"clean = clean_crowns(crowns, 0.6, confidence=0.5) # Filter low-confidence and overlapping crowns"
240+
]
241+
},
242+
{
243+
"cell_type": "markdown",
244+
"metadata": {},
245+
"source": [
246+
"## 4. Saving and Visualizing\n",
247+
"\n",
248+
"Save the cleaned crown map. You can view the output in QGIS or ArcGIS."
249+
]
250+
},
251+
{
252+
"cell_type": "code",
253+
"execution_count": null,
254+
"metadata": {},
255+
"outputs": [],
256+
"source": [
257+
"# Simplify geometries for easier editing in GIS software\n",
258+
"clean = clean.set_geometry(clean.simplify(0.3))\n",
259+
"\n",
260+
"# Save to file\n",
261+
"clean.to_file(site_path + \"/crowns_out.gpkg\", driver=\"GPKG\")"
262+
]
263+
}
264+
]
265+
}

0 commit comments

Comments
 (0)