forked from tenstorrent/tt-buda-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpytorch_vgg_hf.py
82 lines (67 loc) · 2.64 KB
/
pytorch_vgg_hf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# SPDX-FileCopyrightText: © 2024 Tenstorrent AI ULC
# SPDX-License-Identifier: Apache-2.0
# VGG Demo Script
import os
import urllib
import pybuda
import requests
import torch
from PIL import Image
from pybuda._C.backend_api import BackendDevice
from torchvision import transforms
from vgg_pytorch import VGG
def run_vgg_19_hf_pytorch(variant="vgg19"):
# Set PyBuda configuration parameters
compiler_cfg = pybuda.config._get_global_compiler_config()
compiler_cfg.balancer_policy = "Ribbon"
compiler_cfg.default_df_override = pybuda._C.DataFormat.Float16_b
# Device specific configurations
available_devices = pybuda.detect_available_devices()
if available_devices:
if available_devices[0] == BackendDevice.Grayskull:
os.environ["PYBUDA_FORCE_EMULATE_HARVESTED"] = "1"
else:
os.environ["TT_BACKEND_OVERLAY_MAX_EXTRA_BLOB_SIZE"] = "65536"
os.environ["PYBUDA_FORCE_SEQUENTIAL"] = "1"
"""
# https://pypi.org/project/vgg-pytorch/
# Variants:
vgg11, vgg11_bn
vgg13, vgg13_bn
vgg16, vgg16_bn
vgg19, vgg19_bn
"""
# Create PyBuda module from PyTorch model
model = VGG.from_pretrained(variant)
model.eval()
tt_model = pybuda.PyTorchModule("pt_vgg_19_hf", model)
# Image preprocessing
url = "https://raw.githubusercontent.com/pytorch/hub/master/images/dog.jpg"
input_image = Image.open(requests.get(url, stream=True).raw)
preprocess = transforms.Compose(
[
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
]
)
input_tensor = preprocess(input_image)
input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model
# Run inference on Tenstorrent device
output_q = pybuda.run_inference(tt_model, inputs=([input_batch]))
output = output_q.get()[0].value()
# The output has unnormalized scores. To get probabilities, you can run a softmax on it.
probabilities = torch.nn.functional.softmax(output[0], dim=0)
# Get ImageNet class mappings
url = "https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt"
image_classes = urllib.request.urlopen(url)
categories = [s.decode("utf-8").strip() for s in image_classes.readlines()]
# Show top categories per image
top5_prob, top5_catid = torch.topk(probabilities, 5)
result = {}
for i in range(top5_prob.size(0)):
result[categories[top5_catid[i]]] = top5_prob[i].item()
print(result)
if __name__ == "__main__":
run_vgg_19_hf_pytorch()