Sorghum kernel defect classifiers: from GrainSet to GrainDet
This repository belongs to our paper on sorghum kernel defect classifiers. We train 21 models on one public dataset (GrainSet), test them on a second public dataset (GrainDet) and measure how much accuracy they lose and how much fine-tuning gives back. The repository holds the code, the lists that define our data split, the per-image predictions, the result tables, the figures and the model checkpoints. We do not release any images. Every number in the paper can be rebuilt from the prediction files, so you can check a result without retraining anything.
What is inside
| Folder | What it holds |
|---|---|
src/kernel_transfer/ |
the code: data.py (classes, trays, split lists), models.py (model families), finetune.py (the one fine-tuning recipe), metrics.py (macro-F1 and related scores), stats.py (tray-clustered intervals, McNemar test, Holm correction) |
scripts/ |
01_finetune.py (fine-tune one model and save its predictions), 02_rebuild_table5.py (rebuild the main results table, Table 1 of the paper; the file name keeps an older table number), 03_rebuild_audit_and_session.py (rebuild the tray audit and the capture-session check) |
data/split/ |
the lists that define our split of the 8,640 GrainDet crops into fine-tuning, validation and held-out parts, and the tray table |
data/predictions/ |
per-image predictions: locked_21_models/ (the 21 models, before and after fine-tuning) and tray_audit/ (predictions of the tray audit) |
all_results/ |
every result table produced for the paper (CSV), including the seed and label-efficiency runs |
figures/ |
the final figures |
checkpoints/ |
the trained model weights; see checkpoints/MANIFEST.md for which file is which model |
tests/ |
test_rebuild_numbers.py, which rebuilds the main numbers of the paper from the predictions and checks them |
The folder results/ is created when you run the scripts; the rebuilt tables are written there.
How to rebuild the numbers
You need Python and the packages in requirements.txt. No images and no GPU are needed for the first three commands.
pip install -r requirements.txt
python tests/test_rebuild_numbers.py # rebuilds the main numbers of the paper and checks them
python scripts/02_rebuild_table5.py # prints the main table (Table 1 of the paper)
python scripts/03_rebuild_audit_and_session.py # prints the tray audit and the capture-session check
To fine-tune a model yourself you need the GrainDet images (see the next section):
python scripts/01_finetune.py --model vit_small --checkpoint best_vit_small_whole.pt --images-dir /path/to/graindet/rec_crops/test --out preds.csv
python scripts/01_finetune.py --model efficientnet_b0 --images-dir /path/to/graindet/rec_crops/test --smoke --out smoke.csv # tiny test that the code runs; a CPU is enough
The images (not included)
We did not create the images and we do not redistribute them. Get them from the original sources and point the scripts to them:
- GrainSet (Fan et al., Scientific Data 2023), sorghum part on Figshare: DOI 10.6084/m9.figshare.22988981.v2
- GrainDet (Fan et al., IEEE Robotics and Automation Letters 2024): github.com/hellodfan/GrainDet. We use the pre-cropped sorghum single-kernel images (
rec_crops/test): 8,640 crops from 47 trays, one folder per class (the class "F&S" is stored in a folder calledFS).
Choices behind the code
- One recipe for every model. All models use the same learning rate, number of epochs and augmentation, so differences between models are not recipe differences.
- Trays, not crops, carry the uncertainty. The crops of one tray were photographed together, so confidence intervals resample whole trays inside each class.
- Class and capture session overlap. Three classes were photographed on dates of their own and five classes share one date (paper, Sections 3.3 and 3.10). The code reads the date from the file name.
What is not released
The code that trained the GrainSet-side adversarial and instance-normalisation variants is not part of this release. The resulting checkpoints are included and listed in checkpoints/MANIFEST.md. The scripts here therefore reproduce all tables and checks from the predictions, but they do not retrain those variants.
Licence
The code in this repository is released under the MIT licence (see LICENSE). The images of GrainSet and GrainDet are not included and keep the terms of their original sources; please check those terms before reuse. The split lists and prediction files are derived from GrainDet file names and labels and are provided so that the paper's numbers can be reproduced.