-
Notifications
You must be signed in to change notification settings - Fork 84
/
demo.py
80 lines (69 loc) · 2.33 KB
/
demo.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
# ---------------------------------------------------------------------
# Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved.
# SPDX-License-Identifier: BSD-3-Clause
# ---------------------------------------------------------------------
import torch
from qai_hub_models.models._shared.imagenet_classifier.test_utils import (
TEST_IMAGENET_IMAGE,
)
from qai_hub_models.models.mobile_vit.app import MobileVITApp
from qai_hub_models.models.mobile_vit.model import (
MODEL_ASSET_VERSION,
MODEL_ID,
MobileVIT,
)
from qai_hub_models.utils.args import (
demo_model_from_cli_args,
get_model_cli_parser,
get_on_device_demo_parser,
validate_on_device_demo_args,
)
from qai_hub_models.utils.asset_loaders import (
CachedWebModelAsset,
load_image,
load_json,
)
from qai_hub_models.utils.base_model import TargetRuntime
IMAGENET_LABELS_ASSET = CachedWebModelAsset(
"https://raw.githubusercontent.com/anishathalye/imagenet-simple-labels/master/imagenet-simple-labels.json",
MODEL_ID,
MODEL_ASSET_VERSION,
"imagenet_labels.json",
)
# Run Imagenet Classifier end-to-end on a sample image.
# The demo will print the predicted class to terminal.
def demo(
model_cls: type[MobileVIT] = MobileVIT,
model_id: str = MODEL_ID,
is_test: bool = False,
available_target_runtimes: list[TargetRuntime] = list(
TargetRuntime.__members__.values()
),
):
# Demo parameters
parser = get_model_cli_parser(model_cls)
parser = get_on_device_demo_parser(
parser, available_target_runtimes=available_target_runtimes
)
parser.add_argument(
"--image",
type=str,
default=TEST_IMAGENET_IMAGE,
help="test image file path or URL",
)
args = parser.parse_args([] if is_test else None)
validate_on_device_demo_args(args, model_id)
model = demo_model_from_cli_args(model_cls, model_id, args)
app = MobileVITApp(model)
print("Model Loaded")
image = load_image(args.image)
# Run app
probabilities = app.predict(image)
top5 = torch.topk(probabilities, 5)
if not is_test:
labels = load_json(IMAGENET_LABELS_ASSET)
print("Top 5 predictions for image:\n")
for i in range(5):
print(f"{labels[top5.indices[i]]}: {100 * top5.values[i]:.3g}%\n")
if __name__ == "__main__":
demo()