返回模型
说明文档
演示
https://huggingface.co/spaces/jerilseb/quickdraw-small
使用方法
import torch
from torch import nn
import torchvision.transforms as transforms
import torch.nn.functional as F
from pathlib import Path
LABELS = Path("classes.txt").read_text().splitlines()
num_classes = len(LABELS)
model = nn.Sequential(
nn.Conv2d(1, 64, 3, padding="same"),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Conv2d(64, 128, 3, padding="same"),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Conv2d(128, 256, 3, padding="same"),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Flatten(),
nn.Linear(2304, 512),
nn.ReLU(),
nn.Linear(512, num_classes),
)
state_dict = torch.load("model.pth", map_location="cpu")
model.load_state_dict(state_dict)
model.eval()
transform = transforms.Compose(
[
transforms.Resize((28, 28)),
transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,)),
]
)
def predict(image):
image = image['composite']
tensor = transform(image).unsqueeze(0)
with torch.no_grad():
out = model(tensor)
probabilities = F.softmax(out[0], dim=0)
values, indices = torch.topk(probabilities, 5)
print(values, indices)
jerilseb/quickdraw-small
作者 jerilseb
↓ 0
♥ 0
创建时间: 2024-08-24 03:07:35+00:00
更新时间: 2024-08-24 12:05:04+00:00
在 Hugging Face 上查看文件 (5)
.gitattributes
README.md
classes.txt
model.onnx
ONNX
model.pth