Views
No views yet
1from transformers import AutoModelForImageClassification, AutoImageProcessor
2from PIL import Image
3import torch
4
5# Load model
6model = AutoModelForImageClassification.from_pretrained("mesabo/agri-plant-disease-resnet50")
7processor = AutoImageProcessor.from_pretrained("mesabo/agri-plant-disease-resnet50")
8
9# Inference
10image = Image.open("plant_leaf.jpg").convert("RGB")
11inputs = processor(images=image, return_tensors="pt")
12
13with torch.no_grad():
14 outputs = model(**inputs)
15 probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
16 predicted_idx = probs.argmax(-1).item()
17 confidence = probs[0][predicted_idx].item()
18
19print(f"Disease: {model.config.id2label[predicted_idx]}")
20print(f"Confidence: {confidence * 100:.2f}%")1from fastapi import FastAPI, File, UploadFile
2from transformers import AutoModelForImageClassification, AutoImageProcessor
3from PIL import Image
4import torch
5import io
6
7app = FastAPI()
8
9# Load model at startup
10model = AutoModelForImageClassification.from_pretrained("mesabo/agri-plant-disease-resnet50")
11processor = AutoImageProcessor.from_pretrained("mesabo/agri-plant-disease-resnet50")
12model.eval()
13
14@app.post("/predict")
15async def predict(file: UploadFile = File(...)):
16 image = Image.open(io.BytesIO(await file.read())).convert("RGB")
17 inputs = processor(images=image, return_tensors="pt")
18
19 with torch.no_grad():
20 outputs = model(**inputs)
21 probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
22 predicted_idx = probs.argmax(-1).item()
23 confidence = probs[0][predicted_idx].item()
24
25 return {
26 "disease": model.config.id2label[predicted_idx],
27 "confidence": round(confidence * 100, 2),
28 "status": "success"
29 }| Metric | Value |
|---|---|
| Accuracy | 95%+ |
| Inference Time | < 100ms (CPU) |
| Memory Usage | ~400 MB |
| Input Size | 224x224 RGB |
pip install transformers torch pillow1@misc{agri-plant-disease-resnet50,
2 author = {mesabo},
3 title = {Plant Disease Detection - ResNet50},
4 year = {2024},
5 publisher = {Hugging Face},
6 url = {https://huggingface.co/mesabo/agri-plant-disease-resnet50}
7}