Model training is orchestrated byDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/Dhruv2012/Autonomous-Farm-Robot/llms.txt
Use this file to discover all available pages before exploring further.
main.py, which selects the model architecture and dataset, builds data generators, configures callbacks, and runs training with weighted categorical cross-entropy loss to handle class imbalance between weed, crop, and soil pixels. The script also supports reloading pre-trained weights to resume training or perform evaluation without restarting from scratch.
Training Configuration
The following variables at the top ofmain.py control the training run:
reload_weights = True, the model is initialised with architecture weights and then load_weights() is called with save_weights_path. Set it to False to start a fresh training run.
Dataset Paths
BoniRob
main.py uses data_gen() from utils.py — a Python generator that yields batches of 10-channel input arrays and flattened one-hot masks on demand, keeping memory usage constant regardless of dataset size.
CWFID
load_cwfid_withyaml() reads the YAML split, loads all images into memory, and returns NumPy arrays directly — acceptable for this smaller dataset.
Loss Function
Weed pixels represent a small fraction of most farm images. Plain categorical cross-entropy would therefore be dominated by the soil class. AGRIBOT uses weighted categorical cross-entropy defined inutils.py to upweight the rare weed class:
iou_coef metric computes mean Intersection-over-Union across the batch, giving a more spatially meaningful measure of segmentation quality than pixel accuracy alone.
For CWFID, use class_weights = [0.15, 0.75, 0.10] instead, reflecting the different class distribution of carrot crop imagery.
Callbacks
| Callback | Purpose |
|---|---|
ModelCheckpoint | Saves weights every 20 epochs when validation loss improves |
CSVLogger | Appends per-epoch metrics to training.csv for offline analysis |
TensorBoard | Writes event files to graphs/ for loss and accuracy visualisation |
EarlyStopping | Halts training if val_loss does not improve by ≥ 0.003 for one epoch |
callbacks_list before calling fit_generator to activate them during training.
Running Training
fit_generator line and set the appropriate steps_per_epoch and validation_steps values:
Evaluation Only
To evaluate an already-trained model without re-running training, keepreload_weights = True and comment out the fit_generator call. The script will run evaluate_generator on the test split and print per-class metrics:
Documents/readme-images/bonnet-metrics.png.