Beans ViT Disease Classifier
ViT-B/16 fine-tuned to classify bean leaf images into three classes:
angular_leaf_spotbean_rusthealthy
Model
- Architecture: Torchvision ViT-B/16
- Initialization: ImageNet-1K pretrained weights
- Input: RGB image
- Evaluation preprocessing: resize to 256 pixels and center crop to 224 x 224
- Total parameters: 85,800,963
- Parameters trainable during fine-tuning: 14,179,587
Training occurred in two stages:
- Training only the classification head.
- Fine-tuning Transformer blocks 10 and 11, the final normalization layer, and the classification head.
The model was selected exclusively using validation macro-F1.
Results
| Split | Accuracy | Macro-F1 | Balanced accuracy |
|---|---|---|---|
| Validation | 0.9925 | 0.9925 | - |
| Test | 0.9688 | 0.9690 | 0.9690 |
Test set:
- Images: 128
- Correct predictions: 124
- Errors: 4
All healthy images were classified correctly. The four errors occurred between angular_leaf_spot and bean_rust.
Comparison with the custom CNN
| Model | Parameters | Test accuracy | Test macro-F1 |
|---|---|---|---|
| Custom CNN | 1,206,499 | 0.9141 | 0.9114 |
| ViT-B/16 | 85,800,963 | 0.9688 | 0.9690 |
The ViT obtained higher test performance, but it is approximately 71 times larger than the custom CNN. The CNN may remain preferable for resource-constrained deployment.
Usage
Download the repository and use inference.py.
Example:
from PIL import Image
from inference import load_model, predict
model, config, device = load_model(".")
image = Image.open("bean_leaf.jpg")
results = predict(
image,
model,
config,
device
)
print(results)
Limitations
- The test split contains only 128 images.
- The model was evaluated on a single dataset.
- Mild or visually ambiguous symptoms may cause confusion between the two diseases.
- Key diagnostic evidence such as leaf underside, raised pustules, and field context may not be visible.
- The comparison with the earlier CNN is descriptive and was performed after the test split had already been inspected.
- This model is an experimental image classifier and must not replace diagnosis by a qualified agricultural specialist.