YAML Metadata Warning: empty or missing yaml metadata in repo card (https://huggingface.co/docs/hub/model-cards#model-card-metadata)

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:

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:

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:

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)

Downloads last month
6
Inference Providers NEW
This model isn't deployed by any Inference Provider. ๐Ÿ™‹ Ask for provider support