Views
No views yet
EfficientNetB3-corn-100.0.h5[224, 224]255 (i.e., pixel values are normalized)pip install tensorflow huggingface_hub numpy pillow requestsDepthwiseConv2D layer is provided that ignores the groups parameter. This wrapper is then used when loading the model.hf_hub_download function and loaded with the custom DepthwiseConv2D object:1from tensorflow.keras.layers import DepthwiseConv2D as OriginalDepthwiseConv2D
2from huggingface_hub import hf_hub_download
3from tensorflow.keras.models import load_model
4
5# Define a wrapper that ignores the 'groups' argument
6def DepthwiseConv2D(*args, **kwargs):
7 kwargs.pop('groups', None) # Remove the groups parameter if present
8 return OriginalDepthwiseConv2D(*args, **kwargs)
9
10# Download the model weights from the Hugging Face Hub
11model_path = hf_hub_download(
12 repo_id="Luwayy/corn-detection", # Your HF repository ID
13 filename="EfficientNetB3-corn-100.0.h5"
14)
15
16custom_objects = {'DepthwiseConv2D': DepthwiseConv2D}
17model = load_model(model_path, custom_objects=custom_objects)1import numpy as np
2from tensorflow.keras.applications.efficientnet import preprocess_input
3from PIL import Image
4import requests
5from io import BytesIO
6
7# Class labels
8labels = ["Healthy corn", "Infected"]
9
10# Function to load and preprocess the image
11def load_and_preprocess_image(image_url):
12 response = requests.get(image_url)
13 img = Image.open(BytesIO(response.content)).convert("RGB")
14 img = img.resize((224, 224)) # Resize to model input dimensions
15 img_array = np.array(img)
16 img_array = preprocess_input(img_array) # EfficientNet preprocessing
17 img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
18 return img_array
19
20# Prediction function
21def predict(image_url):
22 img = load_and_preprocess_image(image_url)
23 preds = model.predict(img)[0]
24 pred_index = np.argmax(preds)
25 confidence = preds[pred_index]
26 return labels[pred_index], confidence
27
28# Example usage
29image_url = "https://www.harvestplus.org/wp-content/uploads/2021/08/Orange-maize-2.png" # Replace with your image URL
30predicted_class, confidence = predict(image_url)
31print(f"Predicted: {predicted_class} (Confidence: {confidence:.2f})")Predicted: Healthy corn (Confidence: 0.80)