Views
No views yet
1import torch
2from torchvision import models, transforms
3from huggingface_hub import hf_hub_download
4from PIL import Image
5
6CLASSES = ("airplane", "automobile", "bird", "cat", "deer",
7 "dog", "frog", "horse", "ship", "truck")
8
9# Unduh model
10model_path = hf_hub_download(repo_id="rifda83/cifar10-resnet18-classifier", filename="best_model.pth")
11
12model = models.resnet18(weights=None)
13model.fc = torch.nn.Linear(model.fc.in_features, 10)
14model.load_state_dict(torch.load(model_path, map_location="cpu"))
15model.eval()
16
17transform = transforms.Compose([
18 transforms.Resize((32, 32)),
19 transforms.ToTensor(),
20 transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
21])
22
23image = Image.open("your_image.jpg").convert("RGB")
24tensor = transform(image).unsqueeze(0)
25with torch.no_grad():
26 probs = torch.softmax(model(tensor), dim=1)[0]
27print(CLASSES[probs.argmax()], f"100.0%")| Split | Accuracy |
|---|---|
| Test | ~88% |