File size: 7,660 Bytes
47c9e12
 
2b77473
 
 
 
 
 
 
 
 
 
 
 
47c9e12
 
2b77473
47c9e12
2b77473
47c9e12
 
 
 
 
2b77473
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47c9e12
 
 
 
 
 
 
2b77473
47c9e12
2b77473
47c9e12
2b77473
47c9e12
 
 
2b77473
47c9e12
 
 
2b77473
 
47c9e12
 
 
2b77473
47c9e12
 
 
2b77473
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47c9e12
 
 
 
 
2b77473
47c9e12
2b77473
 
 
 
47c9e12
2b77473
47c9e12
2b77473
47c9e12
2b77473
47c9e12
 
 
2b77473
 
 
 
 
47c9e12
2b77473
8207a64
47c9e12
 
2b77473
47c9e12
2b77473
47c9e12
2b77473
 
 
 
47c9e12
 
 
2b77473
47c9e12
2b77473
 
 
47c9e12
2b77473
47c9e12
 
 
2b77473
47c9e12
 
 
 
 
2b77473
 
 
47c9e12
 
 
2b77473
 
 
47c9e12
2b77473
47c9e12
2b77473
47c9e12
 
 
2b77473
 
 
 
 
 
 
 
47c9e12
 
 
2b77473
47c9e12
 
 
2b77473
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
---
library_name: transformers
tags:
- vision transformer
- agriculture
- plant disease detection
- smart farming
- image classification
license: mit
metrics:
- accuracy
base_model:
- WinKawaks/vit-tiny-patch16-224
pipeline_tag: image-classification
---

# Model Card for Smart Farming Disease Detection Transformer

This model is a Vision Transformer (ViT) designed to identify plant diseases in crops as part of a smart agricultural farming system. It has been trained on a diverse dataset of plant images, including different disease categories affecting crops such as corn, potato, rice, and wheat. The model aims to provide farmers and agronomists with real-time disease detection for better crop management.

## Model Details

### Model Description

This Vision Transformer model has been fine-tuned to classify various plant diseases commonly found in agricultural settings. The model can classify diseases in crops such as corn, potato, rice, and wheat, identifying diseases like rust, blight, leaf spots, and others. The goal is to enable precision farming by helping farmers detect diseases early and take appropriate actions.

- **Developed by:** Wambugu Kinyua
- **Model type:** Vision Transformer (ViT)
- **Languages (NLP):** N/A (Computer Vision Model)
- **License:** Apache 2.0
- **Finetuned from model:** (WinKawaks/vit-tiny-patch16-224)[https://huggingface.co./WinKawaks/vit-tiny-patch16-224]
- **Input:** Images of crops (RGB format)
- **Output:** Disease classification labels (healthy or diseased categories)
## Diseases  from the  model 

| Crop   | Diseases Identified          |
|--------|------------------------------|
| Corn   | Common Rust                  |
| Corn   | Gray Leaf Spot               |
| Corn   | Healthy                      |
| Corn   | Leaf Blight                  |
| -      | Invalid                      |
| Potato | Early Blight                 |
| Potato | Healthy                      |
| Potato | Late Blight                  |
| Rice   | Brown Spot                   |
| Rice   | Healthy                      |
| Rice   | Hispa                        |
| Rice   | Leaf Blast                   |
| Wheat  | Brown Rust                   |
| Wheat  | Healthy                      |
| Wheat  | Yellow Rust                  |



## Uses

### Direct Use

This model can be used directly to classify images of crops to detect plant diseases. It is especially useful for precision farming, enabling users to monitor crop health and take early interventions based on the detected disease.

### Downstream Use

This model can be fine-tuned on other agricultural datasets for specific crops or regions to improve its performance or be integrated into larger precision farming systems that include other features like weather predictions and irrigation control.

### Out-of-Scope Use

This model is not designed for non-agricultural image classification tasks or for environments with insufficient or very noisy data. Misuse includes using the model in areas with vastly different agricultural conditions from those it was trained on.

## Bias, Risks, and Limitations

- The model may exhibit bias toward the crops and diseases present in the training dataset, leading to lower performance on unrepresented diseases or crop varieties.
- False negatives (failing to detect a disease) may result in untreated crop damage, while false positives could lead to unnecessary interventions.

### Recommendations

Users should evaluate the model on their specific crops and farming conditions. Regular updates and retraining with local data are recommended for optimal performance.

## How to Get Started with the Model

```python
import matplotlib.pyplot as plt
from PIL import Image, UnidentifiedImageError
from transformers import ViTFeatureExtractor, ViTForImageClassification

label2id= {'Corn___Common_Rust': '0',
  'Corn___Gray_Leaf_Spot': '1',
  'Corn___Healthy': '2',
  'Corn___Leaf_Blight': '3',
  'Invalid': '4',
  'Potato___Early_Blight': '5',
  'Potato___Healthy': '6',
  'Potato___Late_Blight': '7',
  'Rice___Brown_Spot': '8',
  'Rice___Healthy': '9',
  'Rice___Hispa': '10',
  'Rice___Leaf_Blast': '11',
  'Wheat___Brown_Rust': '12',
  'Wheat___Healthy': '13',
  'Wheat___Yellow_Rust': '14'},
id2label  = {'0': 'Corn___Common_Rust',
  '1': 'Corn___Gray_Leaf_Spot',
  '2': 'Corn___Healthy',
  '3': 'Corn___Leaf_Blight',
  '4': 'Invalid',
  '5': 'Potato___Early_Blight',
  '6': 'Potato___Healthy',
  '7': 'Potato___Late_Blight',
  '8': 'Rice___Brown_Spot',
  '9': 'Rice___Healthy',
  '10': 'Rice___Hispa',
  '11': 'Rice___Leaf_Blast',
  '12': 'Wheat___Brown_Rust',
  '13': 'Wheat___Healthy',
  '14': 'Wheat___Yellow_Rust'}

feature_extractor = ViTFeatureExtractor.from_pretrained('WinKawaks/vit-tiny-patch16-224')
model = ViTForImageClassification.from_pretrained(
    'wambugu1738/crop_leaf_diseases_vit',
    num_labels=15,
    label2id=label2id,
    id2label=id2label,
    ignore_mismatched_sizes=True
)

from PIL import Image
image = Image.open('path_to_image')
inputs = feature_extractor(images=image, return_tensors="pt")
outputs = model(**inputs)
logits = outputs.logits
predicted_class_idx = logits.argmax(-1).item()
print("Predicted class:", model.config.id2label[str(predicted_class_idx)])
```

## Training Details

### Training Data

The model was trained on a dataset containing images of various crops with labeled diseases, including the following categories:

- **Corn**: Common Rust, Gray Leaf Spot, Leaf Blight, Healthy
- **Potato**: Early Blight, Late Blight, Healthy
- **Rice**: Brown Spot, Hispa, Leaf Blast, Healthy
- **Wheat**: Brown Rust, Yellow Rust, Healthy

The dataset also includes images captured under various lighting conditions and angles to simulate real-world farming scenarios.

### Training Procedure

The model was fine-tuned using a vision transformer architecture pre-trained on the ImageNet dataset. The dataset was preprocessed by resizing the images and normalizing the pixel values.

#### Training Hyperparameters

- **Batch size:** 32
- **Learning rate:** 2e-5
- **Epochs:** 4
- **Optimizer:** AdamW
- **Precision:** fp16

### Evaluation
![Confusion matrix](disease_classification_metrics.png)


#### Testing Data, Factors & Metrics

The model was evaluated using a validation set consisting of 20% of the original dataset, with the following metrics:

- **Accuracy:** 98%
- **Precision:** 97%
- **Recall:** 97%
- **F1 Score:** 96%

## Environmental Impact

Carbon emissions during model training can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute).

- **Hardware Type:** NVIDIA L40S
- **Hours used:** 1 hours
- **Cloud Provider:** Lightning AI

## Technical Specifications

### Model Architecture and Objective

The model uses a Vision Transformer architecture to learn image representations and classify them into disease categories. Its self-attention mechanism enables it to capture global contextual information in the images, making it suitable for agricultural disease detection.

### Compute Infrastructure

#### Hardware

- NVIDIA L40S GPUs
- 48 GB RAM
- SSD storage for fast I/O

#### Software

- Python 3.9
- PyTorch 2.4.1+cu121
- Transformers library by Hugging Face

## Citation

If you use this model in your research or applications, please cite it as:

**BibTeX:**

```
@misc{kinyua2024smartfarming,
  title={Smart Farming Disease Detection Transformer},
  author={Wambugu Kinyua},
  year={2024},
  publisher={Hugging Face},
}
```

**APA:**

Kinyua, W. (2024). Smart Farming Disease Detection Transformer. Hugging Face.

## Model Card Contact

For further inquiries, contact: [email protected]
```