Instructions to use ramyibrahim/Gemma4-E2B-Crops-V1 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use ramyibrahim/Gemma4-E2B-Crops-V1 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-classification", model="ramyibrahim/Gemma4-E2B-Crops-V1") pipe("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/hub/parrots.png")# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("ramyibrahim/Gemma4-E2B-Crops-V1", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Gemma4-E2B-corps-v1 is a highly accurate, AI-driven image classification model fine-tuned for crop disease diagnosis. Built on top of the Gemma4-E2B-IT base model, it leverages a vision encoder and is optimized for inference on edge devices due to its compact size of 2 billion active parameters.
The model predicts structured JSON outputs, identifying both the crop type (out of 14 categories) and its specific health status (out of 67 possible conditions).
Problem Statement & Motivation
By 2050, the global population is projected to reach nearly 9.7 billion, requiring a massive surge in food production. Currently, 20% to 40% of global crop yields are lost annually to plant pests and diseases, representing over $220 billion in economic damage. Traditional disease diagnosis relies heavily on expert agricultural knowledge, which is often expensive, localized, and too slow to stop an outbreak. This model democratizes expert-level agronomy by allowing farmers to instantly detect and diagnose crop diseases from simple images, ultimately safeguarding food security and smallholder farmer livelihoods.
Dataset
The core training data was acquired from the Plant Diseases 100K Labelled Images dataset.
Dataset Characteristics
- Scale: Contains over 100,000 high-resolution images, providing a robust sample size necessary for training deep neural networks without severe overfitting.
- Diversity: Encompasses 14 distinct crop categories (e.g., Apple, Corn, Rice, Sugarcane, Strawberry, Cotton, Pepper).
- Each category includes imagery of both healthy leaves and leaves exhibiting various pathogenic states, enabling the model to learn specific visual boundaries.
- Statuses: The model categorizes 67 distinct health statuses across these crops.
Data Distribution
Class Imbalance Handling
Because the dataset is not perfectly balanced across all 67 statuses, the training process minimizes cross-entropy loss by prioritizing larger classes in samples.
Weights are calculated for each class at the status level. During training, the average loss of response tokens per sample is calculated and multiplied by the specific status weight before being averaged for backpropagation.
Usage & Prompt Engineering
The model utilizes its Instruct (IT) capabilities to output highly structured JSON data. The generation follows an autoregressive format: predicting the crop first and then generating the specific status.
Example Prompt Payload
{
"role": "user",
"content": [
{
"type": "image"
},
{
"type": "text",
"text": "Analyze the following image of a crop. Identify the type of crop and its health status. Output your findings strictly as a JSON object with 'crop' and 'status' keys."
}
]
}
Example Response
{
"role": "assistant",
"content": [
{
"type": "text",
"text": "{\"crop\": \"Strawberry\", \"status\": \"Strawberry_Leaf_scorch\"}"
}
]
}
Training Details
Quantization & Memory Management
Precision
bfloat16 sets the math precision to 16-bit to prevent mathematical overflows during training.
Quantization
4-bit Quantization was used to compress model weights and drastically reduce GPU VRAM usage. Specifically, NormalFloat4 (NF4) was utilized for better intelligence retention.
Weights temporarily de-quantize to BF16 during active math calculations.
Gradient Checkpointing
Enabled as a massive memory saver; it re-computes intermediate activations during the backward pass rather than storing them for the entire network.
Parameter Efficient Fine-Tuning (LoRA)
| Parameter | Value |
|---|---|
| Target Modules | Default layers (attention query and value projection matrices) |
| Rank (r) | 16 |
| Alpha | 32 |
| Dropout | 0.05 |
| Bias | None |
Hyperparameters
| Parameter | Value |
|---|---|
| Optimizer | paged_adamw_8bit |
| Batch Size | 32 |
| Epochs | 3 |
| Learning Rate | 2e-4 |
| Scheduler | Cosine Annealing |
| Warmup Ratio | 0.03 |
| Gradient Clipping | 0.3 |
Hyperparameter Notes
- Optimizer: Compresses AdamW states to 8-bit and offloads data to CPU RAM to prevent Out Of Memory crashes.
- Learning Rate Scheduler: Gradually decreases the learning rate following a cosine curve to help convergence.
- Warmup Ratio: Starts from 0 and reaches the target learning rate during the first 3% of training.
- Gradient Clipping: Prevents exploding gradients by scaling down excessively large updates.
Training Results
The model exhibited strong and continuous convergence throughout all three epochs.
| Step | Training Loss | Validation Loss |
|---|---|---|
| 500 | 0.403620 | 0.013751 |
| 1000 | 0.010460 | 0.008049 |
| 1500 | 0.005946 | 0.006840 |
| 2000 | 0.003942 | 0.005440 |
| 2500 | 0.002745 | 0.005093 |
| 2577 | 0.002745 | 0.005088 |
