tanganke/stanford_cars
Viewer • Updated • 72.5k • 16.9k • 29
Model klasifikasi gambar mobil hasil fine-tuning convnext_tiny (pretrained ImageNet) pada dataset tanganke/stanford_cars.
brand)import torch, torch.nn as nn
from huggingface_hub import hf_hub_download
from torchvision import transforms
from torchvision.models import resnet50, convnext_tiny
from PIL import Image
path = hf_hub_download("ryosiswand/convnext_tiny-stanford-cars-brand", "model.pth")
ckpt = torch.load(path, map_location="cpu", weights_only=False)
# bangun arsitektur sesuai ckpt["arch"], lalu muat bobotnya
if ckpt["arch"] == "convnext_tiny":
model = convnext_tiny(weights=None)
model.classifier[2] = nn.Linear(model.classifier[2].in_features, ckpt["num_classes"])
else:
model = resnet50(weights=None)
model.fc = nn.Linear(model.fc.in_features, ckpt["num_classes"])
model.load_state_dict(ckpt["state_dict"])
model.eval()
tf = transforms.Compose([
transforms.Resize(int(ckpt["img_size"]*1.14)), transforms.CenterCrop(ckpt["img_size"]), transforms.ToTensor(),
transforms.Normalize(ckpt["mean"], ckpt["std"]),
])
img = Image.open("mobil.jpg").convert("RGB")
probs = model(tf(img).unsqueeze(0)).softmax(-1)[0]
print(ckpt["class_names"][int(probs.argmax())], float(probs.max()))
Dibuat sebagai proyek Computer Vision (CNN) - Ruangguru AI Bootcamp.