Walnut Crown Detection: Training a Custom YOLOv8-seg Model
The Problem
Everything in PlantFarm Scan rests on one step: finding the crown of every tree in the orthophoto. If the crown is wrong, so are that tree's NDVI and its trend. This post covers how we trained the model for that step, which we call Walnut AI.
Why Instance Segmentation?
There were three options:
- Object detection (bounding boxes): finds the tree with a box, but the box corners include soil and grass and spoil per-tree NDVI.
- Semantic segmentation: labels each pixel "tree" or "not tree", but cannot separate two touching trees.
- Instance segmentation: finds each tree as a separate object with its own pixel mask.
We needed the third. We chose YOLOv8-seg: fast, a good starting point on small datasets, and trainable with a single command. We started with the small model (yolov8s-seg); moving to the medium one is an option as the data grows.
Data: Which Frames We Picked
The model's success depends more on data diversity than on architecture. When selecting frames from flights we followed these rules:
- Diversity: dense and sparse plantings, different corners of the orchard, shaded and sunny frames.
- Tree-free frames: empty field, bare soil, grass only. Essential to teach the model "green, but not a tree". Without them the model calls the first patch of grass a tree.
- Trees that look diseased: today we only find trees, but these frames will be the most valuable data for disease classification later.
- Leaving out blurry and motion-smeared frames.
Labelling was done on the colour (_D.JPG) image, with the matching red, green and NIR band files kept alongside. There are two classes: walnut and almond.
Large Orthophoto, Small Model Input
The model expects a 640×640 image; an orchard orthophoto is tens of thousands of pixels wide. So inference goes through a pipeline:
- The orthophoto is scanned in 640-pixel tiles with about 25% overlap.
- In overlapping areas the same tree is found twice; duplicates are removed with IoU-based NMS.
- Some crowns are cut in half at a tile edge: the left half in one tile, the right half in the next. A stitching step joins them into one crown.
- Finally a polygon-overlap NMS removes polygons that genuinely overlap.
Without these steps, trees at tile edges were either counted twice or measured by half.
How to Read the Results
After training we measure the model on a test set never used in training. We look at three numbers:
- Precision: how often the model is right when it says "tree". Low means many false alarms.
- Recall: how many of the real trees it finds. Low means missed trees.
- mAP50: a summary of both; the most practical single number for comparing models.
On the validation set, crown-mask recall is 0.75 and mAP50 is around 0.61. These numbers score pixel-accurate masks before tile stitching; for tree counting in the field the picture is better. To be honest, though, the bottleneck is data, not the model: there are far fewer almond examples than walnut, so almond scores are lower. That is class imbalance, not a model fault, and the fix is more almond frames.
We used early stopping: if the validation score does not improve for 30 epochs, training stops on its own. Our latest model stopped at epoch 105, with the best weights from epoch 75.
Training on an Older GPU
Our training machine has a GTX 1060 (Pascal generation). Current PyTorch releases no longer support this GPU properly: torch.cuda.is_available() returns True, but the first real operation fails. So we set up a separate training environment (PyTorch 2.1.2 + CUDA 11.8). The Ultralytics version in that environment does not know YOLO11, so we stayed on YOLOv8.
We mention this because many sites do not have the latest GPUs. Instead of trusting is_available(), we wrote a check that runs a real matrix multiplication. We do the same on customer hardware.
Next Steps
- Balance the dataset with more almond and dense-planting examples
- Label diseased trees as a separate class
- Measure the speed/accuracy trade-off of a larger model
Conclusion
A good detection model is the sum of well-chosen data and the engineering around the model (tiling, stitching, measurement). If you have a similar imaging need, see our Industrial AI solution.