File size: 3,870 Bytes
399055c
 
 
 
f956579
399055c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Corn Detection Model

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.

---

## Overview

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.

---

## Model Details

- **Model Type:** EfficientNet
- **Base Model:** EfficientNetB3
- **Weights File:** `EfficientNetB3-corn-100.0.h5`
- **License:** MIT
- **Language:** English
- **Main Metric:** Accuracy
- **Pipeline Tag:** Image Classification

### Classes

1. **Healthy corn**
   - **ID:** 0
   - **Input Size:** 224 x 224 pixels
   - **Scale Factor:** 1
2. **Infected**
   - **ID:** 1
   - **Input Size:** 224 x 224 pixels
   - **Scale Factor:** 1

### Preprocessing

- **Resize:** `[224, 224]`
- **Scale:** Images are scaled by `255` (i.e., pixel values are normalized)

---

## Installation

Ensure you have Python installed along with the necessary dependencies. You can install the required packages with pip:

```bash
pip install tensorflow huggingface_hub numpy pillow requests
```

---

## Usage

### Custom Depthwise Convolution Layer

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.

### Loading the Model

The model is downloaded from the Hugging Face Hub using the `hf_hub_download` function and loaded with the custom `DepthwiseConv2D` object:

```python
from tensorflow.keras.layers import DepthwiseConv2D as OriginalDepthwiseConv2D
from huggingface_hub import hf_hub_download
from tensorflow.keras.models import load_model

# Define a wrapper that ignores the 'groups' argument
def DepthwiseConv2D(*args, **kwargs):
    kwargs.pop('groups', None)  # Remove the groups parameter if present
    return OriginalDepthwiseConv2D(*args, **kwargs)

# Download the model weights from the Hugging Face Hub
model_path = hf_hub_download(
    repo_id="Luwayy/corn-detection",  # Your HF repository ID
    filename="EfficientNetB3-corn-100.0.h5"
)

custom_objects = {'DepthwiseConv2D': DepthwiseConv2D}
model = load_model(model_path, custom_objects=custom_objects)
```

### Preprocessing and Prediction

The code below demonstrates how to load and preprocess an image, and then perform prediction:

```python
import numpy as np
from tensorflow.keras.applications.efficientnet import preprocess_input
from PIL import Image
import requests
from io import BytesIO

# Class labels
labels = ["Healthy corn", "Infected"]

# Function to load and preprocess the image
def load_and_preprocess_image(image_url):
    response = requests.get(image_url)
    img = Image.open(BytesIO(response.content)).convert("RGB")
    img = img.resize((224, 224))  # Resize to model input dimensions
    img_array = np.array(img)
    img_array = preprocess_input(img_array)  # EfficientNet preprocessing
    img_array = np.expand_dims(img_array, axis=0)  # Add batch dimension
    return img_array

# Prediction function
def predict(image_url):
    img = load_and_preprocess_image(image_url)
    preds = model.predict(img)[0]
    pred_index = np.argmax(preds)
    confidence = preds[pred_index]
    return labels[pred_index], confidence

# Example usage
image_url = "https://www.harvestplus.org/wp-content/uploads/2021/08/Orange-maize-2.png"  # Replace with your image URL
predicted_class, confidence = predict(image_url)
print(f"Predicted: {predicted_class} (Confidence: {confidence:.2f})")
```

Upon running the example, you might see an output similar to:

```
Predicted: Healthy corn (Confidence: 0.80)
```

---