Update README.md
Browse files
README.md
CHANGED
@@ -1,8 +1,126 @@
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Corn Detection Model
|
2 |
+
|
3 |
+
This repository contains an implementation of a corn detection model using the EfficientNet architecture. The model distinguishes between "Healthy corn" and "Infected" corn based on input images.
|
4 |
+
|
5 |
---
|
6 |
+
|
7 |
+
## Overview
|
8 |
+
|
9 |
+
The project uses **EfficientNetB3** as the base model and is fine-tuned for corn health detection. It supports image classification by preprocessing input images to the required dimensions and scale, and then outputs predictions with associated confidence scores.
|
10 |
+
|
11 |
+
---
|
12 |
+
|
13 |
+
## Model Details
|
14 |
+
|
15 |
+
- **Model Type:** EfficientNet
|
16 |
+
- **Base Model:** EfficientNetB3
|
17 |
+
- **Weights File:** `EfficientNetB3-corn-100.0.h5`
|
18 |
+
- **License:** MIT
|
19 |
+
- **Language:** English
|
20 |
+
- **Main Metric:** Accuracy
|
21 |
+
- **Pipeline Tag:** Image Classification
|
22 |
+
|
23 |
+
### Classes
|
24 |
+
|
25 |
+
1. **Healthy corn**
|
26 |
+
- **ID:** 0
|
27 |
+
- **Input Size:** 224 x 224 pixels
|
28 |
+
- **Scale Factor:** 1
|
29 |
+
2. **Infected**
|
30 |
+
- **ID:** 1
|
31 |
+
- **Input Size:** 224 x 224 pixels
|
32 |
+
- **Scale Factor:** 1
|
33 |
+
|
34 |
+
### Preprocessing
|
35 |
+
|
36 |
+
- **Resize:** `[224, 224]`
|
37 |
+
- **Scale:** Images are scaled by `255` (i.e., pixel values are normalized)
|
38 |
+
|
39 |
+
---
|
40 |
+
|
41 |
+
## Installation
|
42 |
+
|
43 |
+
Ensure you have Python installed along with the necessary dependencies. You can install the required packages with pip:
|
44 |
+
|
45 |
+
```bash
|
46 |
+
pip install tensorflow huggingface_hub numpy pillow requests
|
47 |
+
```
|
48 |
+
|
49 |
+
---
|
50 |
+
|
51 |
+
## Usage
|
52 |
+
|
53 |
+
### Custom Depthwise Convolution Layer
|
54 |
+
|
55 |
+
Due to a potential mismatch with the default Keras implementation, a custom wrapper for the `DepthwiseConv2D` layer is provided that ignores the `groups` parameter. This wrapper is then used when loading the model.
|
56 |
+
|
57 |
+
### Loading the Model
|
58 |
+
|
59 |
+
The model is downloaded from the Hugging Face Hub using the `hf_hub_download` function and loaded with the custom `DepthwiseConv2D` object:
|
60 |
+
|
61 |
+
```python
|
62 |
+
from tensorflow.keras.layers import DepthwiseConv2D as OriginalDepthwiseConv2D
|
63 |
+
from huggingface_hub import hf_hub_download
|
64 |
+
from tensorflow.keras.models import load_model
|
65 |
+
|
66 |
+
# Define a wrapper that ignores the 'groups' argument
|
67 |
+
def DepthwiseConv2D(*args, **kwargs):
|
68 |
+
kwargs.pop('groups', None) # Remove the groups parameter if present
|
69 |
+
return OriginalDepthwiseConv2D(*args, **kwargs)
|
70 |
+
|
71 |
+
# Download the model weights from the Hugging Face Hub
|
72 |
+
model_path = hf_hub_download(
|
73 |
+
repo_id="Luwayy/corn-detection", # Your HF repository ID
|
74 |
+
filename="EfficientNetB3-corn-100.0.h5"
|
75 |
+
)
|
76 |
+
|
77 |
+
custom_objects = {'DepthwiseConv2D': DepthwiseConv2D}
|
78 |
+
model = load_model(model_path, custom_objects=custom_objects)
|
79 |
+
```
|
80 |
+
|
81 |
+
### Preprocessing and Prediction
|
82 |
+
|
83 |
+
The code below demonstrates how to load and preprocess an image, and then perform prediction:
|
84 |
+
|
85 |
+
```python
|
86 |
+
import numpy as np
|
87 |
+
from tensorflow.keras.applications.efficientnet import preprocess_input
|
88 |
+
from PIL import Image
|
89 |
+
import requests
|
90 |
+
from io import BytesIO
|
91 |
+
|
92 |
+
# Class labels
|
93 |
+
labels = ["Healthy corn", "Infected"]
|
94 |
+
|
95 |
+
# Function to load and preprocess the image
|
96 |
+
def load_and_preprocess_image(image_url):
|
97 |
+
response = requests.get(image_url)
|
98 |
+
img = Image.open(BytesIO(response.content)).convert("RGB")
|
99 |
+
img = img.resize((224, 224)) # Resize to model input dimensions
|
100 |
+
img_array = np.array(img)
|
101 |
+
img_array = preprocess_input(img_array) # EfficientNet preprocessing
|
102 |
+
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
|
103 |
+
return img_array
|
104 |
+
|
105 |
+
# Prediction function
|
106 |
+
def predict(image_url):
|
107 |
+
img = load_and_preprocess_image(image_url)
|
108 |
+
preds = model.predict(img)[0]
|
109 |
+
pred_index = np.argmax(preds)
|
110 |
+
confidence = preds[pred_index]
|
111 |
+
return labels[pred_index], confidence
|
112 |
+
|
113 |
+
# Example usage
|
114 |
+
image_url = "https://www.harvestplus.org/wp-content/uploads/2021/08/Orange-maize-2.png" # Replace with your image URL
|
115 |
+
predicted_class, confidence = predict(image_url)
|
116 |
+
print(f"Predicted: {predicted_class} (Confidence: {confidence:.2f})")
|
117 |
+
```
|
118 |
+
|
119 |
+
Upon running the example, you might see an output similar to:
|
120 |
+
|
121 |
+
```
|
122 |
+
Predicted: Healthy corn (Confidence: 0.80)
|
123 |
+
```
|
124 |
+
|
125 |
+
---
|
126 |
+
|