File size: 5,857 Bytes
abca75a |
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 |
"""
Hugging Face Model Hub Integration Example
==========================================
This script demonstrates how to use the cattle breed classification model
from Hugging Face Model Hub.
"""
import onnxruntime as ort
import numpy as np
import json
from PIL import Image
from torchvision import transforms
import requests
from huggingface_hub import hf_hub_download
import os
class CattleBreedClassifier:
def __init__(self, model_name="your-username/cattle-breed-classifier"):
"""
Initialize the classifier by downloading model files from Hugging Face
Args:
model_name: HuggingFace model repository name
"""
self.model_name = model_name
self.session = None
self.prototypes = None
self.metadata = None
# Download and load model files
self._download_model_files()
self._load_model()
self._load_prototypes()
def _download_model_files(self):
"""Download model files from Hugging Face Hub"""
print("π₯ Downloading model files from Hugging Face...")
# Download ONNX model
self.model_path = hf_hub_download(
repo_id=self.model_name,
filename="model.onnx"
)
# Download prototypes
self.prototypes_path = hf_hub_download(
repo_id=self.model_name,
filename="prototypes.json"
)
# Download metadata
self.metadata_path = hf_hub_download(
repo_id=self.model_name,
filename="metadata.json"
)
print("β
Model files downloaded successfully!")
def _load_model(self):
"""Load the ONNX model"""
self.session = ort.InferenceSession(self.model_path)
print("β
ONNX model loaded")
def _load_prototypes(self):
"""Load breed prototypes"""
with open(self.prototypes_path, 'r') as f:
self.prototypes = json.load(f)
with open(self.metadata_path, 'r') as f:
self.metadata = json.load(f)
print(f"β
Loaded prototypes for {len(self.prototypes['prototypes'])} breeds")
def preprocess_image(self, image_input):
"""
Preprocess image for model inference
Args:
image_input: PIL Image, numpy array, or file path
Returns:
numpy.ndarray: Preprocessed image tensor
"""
# Handle different input types
if isinstance(image_input, str):
image = Image.open(image_input).convert('RGB')
elif isinstance(image_input, np.ndarray):
image = Image.fromarray(image_input).convert('RGB')
else:
image = image_input.convert('RGB')
# Apply preprocessing
transform = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]
)
])
tensor = transform(image).unsqueeze(0)
return tensor.numpy()
def predict(self, image_input, return_all_scores=False):
"""
Predict cattle/buffalo breed from image
Args:
image_input: Image input (PIL Image, numpy array, or file path)
return_all_scores: Whether to return scores for all breeds
Returns:
dict: Prediction results
"""
# Preprocess image
input_data = self.preprocess_image(image_input)
# Run inference
features = self.session.run(None, {'input': input_data})[0][0]
# Calculate similarities with all breed prototypes
similarities = {}
for breed, prototype in self.prototypes['prototypes'].items():
similarity = np.dot(features, np.array(prototype))
similarities[breed] = float(similarity)
# Get top prediction
predicted_breed = max(similarities, key=similarities.get)
confidence = similarities[predicted_breed]
# Determine animal type
buffalo_breeds = ['Bhadawari', 'Jaffarbadi', 'Mehsana', 'Murrah', 'Surti']
animal_type = 'Buffalo' if predicted_breed in buffalo_breeds else 'Cattle'
result = {
'predicted_breed': predicted_breed,
'confidence': confidence,
'animal_type': animal_type
}
if return_all_scores:
result['all_scores'] = similarities
return result
# Example usage
def main():
# Initialize classifier (will download model from Hugging Face)
classifier = CattleBreedClassifier("your-username/cattle-breed-classifier")
# Example 1: Predict from local image
image_path = "path/to/your/image.jpg"
if os.path.exists(image_path):
result = classifier.predict(image_path, return_all_scores=True)
print(f"\nπ Prediction Results:")
print(f"Animal Type: {result['animal_type']}")
print(f"Predicted Breed: {result['predicted_breed']}")
print(f"Confidence: {result['confidence']:.4f}")
print(f"\nπ All Breed Scores:")
for breed, score in sorted(result['all_scores'].items(),
key=lambda x: x[1], reverse=True):
print(f" {breed}: {score:.4f}")
# Example 2: Predict from PIL Image
from PIL import Image
image = Image.open(image_path)
result = classifier.predict(image)
print(f"\nDirect PIL prediction: {result['predicted_breed']} ({result['confidence']:.4f})")
if __name__ == "__main__":
main()
|