Views
No views yet
brand)1import torch, torch.nn as nn
2from huggingface_hub import hf_hub_download
3from torchvision.models import resnet50
4from torchvision import transforms
5from PIL import Image
6
7path = hf_hub_download("ryosiswand/resnet50-stanford-cars-brand", "model.pth")
8ckpt = torch.load(path, map_location="cpu", weights_only=False)
9
10model = resnet50(weights=None)
11model.fc = nn.Linear(model.fc.in_features, ckpt["num_classes"])
12model.load_state_dict(ckpt["state_dict"])
13model.eval()
14
15tf = transforms.Compose([
16 transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(),
17 transforms.Normalize(ckpt["mean"], ckpt["std"]),
18])
19img = Image.open("mobil.jpg").convert("RGB")
20probs = model(tf(img).unsqueeze(0)).softmax(-1)[0]
21print(ckpt["class_names"][int(probs.argmax())], float(probs.max()))