Skip to content

Commit

Permalink
Update sample code
Browse files Browse the repository at this point in the history
  • Loading branch information
quic-zhanweiw committed Dec 30, 2024
1 parent ce210db commit 159b7db
Show file tree
Hide file tree
Showing 16 changed files with 283 additions and 186 deletions.
3 changes: 3 additions & 0 deletions samples/python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,8 @@ python stable_diffusion_v2_1\stable_diffusion_v2_1.py --prompt "spectacular view
| riffusion | 2.24 | python riffusion\riffusion.py --prompt "the prompt string ..." |
| real_esrgan_x4plus | 2.28 | python real_esrgan_x4plus\real_esrgan_x4plus.py |
| real_esrgan_general_x4v3 | 2.28 | python real_esrgan_general_x4v3\real_esrgan_general_x4v3.py |
| inception_v3 | 2.28 | python inception_v3\inception_v3.py |
| yolov8_det | 2.28 | python yolov8_det\yolov8_det.py |
| unet_segmentation | 2.28 | python unet_segmentation\unet_segmentation.py |

*More models will be supported soon!*
2 changes: 1 addition & 1 deletion samples/python/inception_v3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ This is sample code for using AppBuilder to load inception_v3 QNN model to HTP a
It can also be used as a backbone in building more complex models for specific use cases.

## Setup AppBuilder environment and prepare QNN SDK libraries by referring to the links below:
https://github.com/quic/ai-engine-direct-helper/blob/main/README.md
https://github.com/quic/ai-engine-direct-helper/blob/main/README.md <br>
https://github.com/quic/ai-engine-direct-helper/blob/main/Docs/User_Guide.md

Copy the QNN libraries from QNN SDK to below path:
Expand Down
54 changes: 46 additions & 8 deletions samples/python/inception_v3/inception_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
# SPDX-License-Identifier: BSD-3-Clause
# ---------------------------------------------------------------------

import sys
import os
sys.path.append(".")
sys.path.append("..")
import utils.install as install
import cv2
import numpy as np

import torch
import torchvision.transforms as transforms
from PIL import Image
Expand All @@ -16,12 +19,33 @@

####################################################################

MODEL_ID = "mmxeyyvyn"
MODEL_NAME = "inception_v3"
MODEL_HELP_URL = "https://github.com/quic/ai-engine-direct-helper/tree/main/samples/python/" + MODEL_NAME + "#" + MODEL_NAME + "-qnn-models"
IMAGENET_CLASSES_URL = "https://raw.githubusercontent.com/pytorch/hub/refs/heads/master/imagenet_classes.txt"
IMAGENET_CLASSES_FILE = "imagenet_classes.txt"
IMAGE_SIZE = 224

####################################################################

execution_ws = os.getcwd()
qnn_dir = execution_ws + "\\qai_libs"

if not MODEL_NAME in execution_ws:
execution_ws = execution_ws + "\\" + MODEL_NAME

model_dir = execution_ws + "\\models"
madel_path = model_dir + "\\" + MODEL_NAME + ".bin"
imagenet_classes_path = model_dir + "\\" + IMAGENET_CLASSES_FILE

####################################################################

inceptionV3 = None

def preprocess_PIL_image(image: Image) -> torch.Tensor:
preprocess = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.Resize(IMAGE_SIZE),
transforms.CenterCrop(IMAGE_SIZE),
transforms.ToTensor(),
])

Expand All @@ -31,7 +55,7 @@ def preprocess_PIL_image(image: Image) -> torch.Tensor:

def post_process(probabilities, output):
# Read the categories
with open("imagenet_classes.txt", "r") as f:
with open(imagenet_classes_path, "r") as f:
categories = [s.strip() for s in f.readlines()]
# Show top categories per image
top5_prob, top5_catid = torch.topk(probabilities, 5)
Expand All @@ -46,22 +70,36 @@ def Inference(self, input_data):
output_data = super().Inference(input_datas)[0]
return output_data

def model_download():
ret = True

if not os.path.exists(imagenet_classes_path):
ret = install.download_url(IMAGENET_CLASSES_URL, imagenet_classes_path)

desc = f"Downloading {MODEL_NAME} model... "
fail = f"\nFailed to download {MODEL_NAME} model. Please prepare the model according to the steps in below link:\n{MODEL_HELP_URL}"
ret = install.download_qai_hubmodel(MODEL_ID, madel_path, desc=desc, fail=fail)

if not ret:
exit()

def Init():
global inceptionV3

model_download()

# Config AppBuilder environment.
QNNConfig.Config(os.getcwd() + "\\qai_libs", Runtime.HTP, LogLevel.WARN, ProfilingLevel.BASIC)

# Instance for InceptionV3 objects.
inceptionV3_model = "models\\inception_v3.bin"
inceptionV3 = InceptionV3("inceptionV3", inceptionV3_model)
inceptionV3 = InceptionV3("inceptionV3", madel_path)

def Inference(input_image_path, output_image_path):
# Read and preprocess the image.
image = Image.open(input_image_path)
image = preprocess_PIL_image(image).numpy()
image = np.transpose(image, (0, 2, 3, 1))

# Burst the HTP.
PerfProfile.SetPerfProfileGlobal(PerfProfile.BURST)

Expand All @@ -86,7 +124,7 @@ def Release():

Init()

Inference("input.jpg", "output.jpg")
Inference(execution_ws + "\\input.jpg", "output.jpg")

Release()

2 changes: 1 addition & 1 deletion samples/python/real_esrgan_general_x4v3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
This is sample code for using AppBuilder to load real_esrgan_general_x4v3 QNN model to HTP and execute inference to upscale an image with minimal loss in quality.

## Setup AppBuilder environment and prepare QNN SDK libraries by referring to the links below:
https://github.com/quic/ai-engine-direct-helper/blob/main/README.md
https://github.com/quic/ai-engine-direct-helper/blob/main/README.md <br>
https://github.com/quic/ai-engine-direct-helper/blob/main/Docs/User_Guide.md

Copy the QNN libraries from QNN SDK to below path:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
model_dir = execution_ws + "\\models"
madel_path = model_dir + "\\" + MODEL_NAME + ".bin"

####################################################################

image_buffer = None
realesrgan = None

Expand Down
2 changes: 1 addition & 1 deletion samples/python/real_esrgan_x4plus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
This is sample code for using AppBuilder to load real_esrgan_x4plus QNN model to HTP and execute inference to generate image.

## Setup AppBuilder environment and prepare QNN SDK libraries by referring to the links below:
https://github.com/quic/ai-engine-direct-helper/blob/main/README.md
https://github.com/quic/ai-engine-direct-helper/blob/main/README.md <br>
https://github.com/quic/ai-engine-direct-helper/blob/main/Docs/User_Guide.md

Copy the QNN libraries from QNN SDK to below path:
Expand Down
2 changes: 2 additions & 0 deletions samples/python/real_esrgan_x4plus/real_esrgan_x4plus.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
model_dir = execution_ws + "\\models"
madel_path = model_dir + "\\" + MODEL_NAME + ".bin"

####################################################################

image_buffer = None
realesrgan = None

Expand Down
2 changes: 1 addition & 1 deletion samples/python/riffusion/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
This is sample code for using AppBuilder to load Riffusion QNN models to HTP and execute inference.

## Setup AppBuilder environment and prepare QNN SDK libraries by referring to the links below:
https://github.com/quic/ai-engine-direct-helper/blob/main/README.md
https://github.com/quic/ai-engine-direct-helper/blob/main/README.md <br>
https://github.com/quic/ai-engine-direct-helper/blob/main/Docs/User_Guide.md

Copy the QNN libraries from QNN SDK to below path:
Expand Down
8 changes: 6 additions & 2 deletions samples/python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--qnn-sdk-version", default="2.24", type=str)
parser.add_argument("--lib-version", default="arm64x-windows-msvc", type=str)
parser.add_argument("--dsp-arch", default=73, type=int)
args = parser.parse_args()

qnn_sdk_version = args.qnn_sdk_version
lib_version = args.lib_version
dsp_arch = args.dsp_arch

start_number = 140
print()
Expand All @@ -23,8 +27,8 @@
try:
install.install_tools()
install.install_qai_sdk(qnn_sdk_version)
install.install_qai_appbuilder(qnn_sdk_version)
install.setup_qai_env(qnn_sdk_version)
install.install_qai_appbuilder(qnn_sdk_version, lib_version)
install.setup_qai_env(qnn_sdk_version, lib_version, dsp_arch)

print()
print(start_number * "*")
Expand Down
2 changes: 1 addition & 1 deletion samples/python/stable_diffusion_v1_5/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
This is sample code for using AppBuilder to load Stable Diffusion 1.5 QNN models to HTP and execute inference to generate image.

## Setup AppBuilder environment and prepare QNN SDK libraries by referring to the links below:
https://github.com/quic/ai-engine-direct-helper/blob/main/README.md
https://github.com/quic/ai-engine-direct-helper/blob/main/README.md <br>
https://github.com/quic/ai-engine-direct-helper/blob/main/Docs/User_Guide.md

Copy the QNN libraries from QNN SDK to below path:
Expand Down
2 changes: 1 addition & 1 deletion samples/python/stable_diffusion_v2_1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
This is sample code for using AppBuilder to load Stable Diffusion 2.1 QNN models to HTP and execute inference to generate image.

## Setup AppBuilder environment and prepare QNN SDK libraries by referring to the links below:
https://github.com/quic/ai-engine-direct-helper/blob/main/README.md
https://github.com/quic/ai-engine-direct-helper/blob/main/README.md <br>
https://github.com/quic/ai-engine-direct-helper/blob/main/Docs/User_Guide.md

Copy the QNN libraries from QNN SDK to below path:
Expand Down
Loading

0 comments on commit 159b7db

Please sign in to comment.