Views
No views yet
1from huggingface_hub import hf_hub_download
2import torch
3from torchvision import models, transforms
4from PIL import Image
5
6repo_id = "Arko007/agromind-plant-disease-mobilenet"
7ckpt = hf_hub_download(repo_id, "newplant_model_final.pth")
8labels_path = hf_hub_download(repo_id, "labels.txt")
9
10with open(labels_path) as f:
11 labels = [l.strip() for l in f if l.strip()]
12
13model = models.mobilenet_v2(pretrained=False)
14model.classifier[1] = torch.nn.Linear(model.classifier[1].in_features, len(labels))
15state = torch.load(ckpt, map_location="cpu")
16model.load_state_dict(state)
17model.eval()
18
19transform = transforms.Compose([
20 transforms.Resize(256),
21 transforms.CenterCrop(224),
22 transforms.ToTensor(),
23 transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
24])
25
26img = Image.open("leaf.jpg").convert("RGB")
27with torch.no_grad():
28 logits = model(transform(img).unsqueeze(0))
29 print(labels[logits.argmax(dim=1).item()])labels.txt for class names.