はじめに
インストール
Pythonのバージョンと環境を管理するためにMinicondaの使用をお勧めします。
- ステップ1: Python環境
マシンに miniconda をインストールします。
Windows: インストールが完了したら、スタートメニューからAnaconda Promptを開きます。macOS/Linux: インストールが完了したら、新しいシェルウィンドウを開きますSet up an environment for Qualcomm® AI Hub:
conda create python=3.10 -n qai_hub conda activate qai_hub
警告
これらのステップのいずれかが SSL: CERTIFICATE_VERIFY_FAILED
エラーで失敗する場合、SSLインターセプションおよびトラフィック検査ツールがインストールされています。Python pip
およびPython Requests
ライブラリの証明書を設定する方法については、IT部門に問い合わせてください。新しい証明書ファイルを提供してもらう必要があります。環境を作成する前に、この証明書をMinicondaに追加する必要があります。これは conda config --set ssl_verify <path to certificate>
で行うことができます。
- ステップ2: Pythonクライアントのインストール
注釈
ARM用Windows: 依存関係のサポートが不足しているため、AI HubはARM Pythonのネイティブをサポートしていません。PythonエコシステムがARM Windowsに追いつくまで、x64バージョンのPythonをインストールしてください(エミュレーション を通じて可能です)。Pythonダウンロードページ からPython 3.10 "64-bit" をお勧めします。
pip3 install qai-hub
- ステップ3: サインイン
Go to Qualcomm® AI Hub and sign in with your Qualcomm ID to view information about jobs you create.
サインインしたら、Account -> Settings -> API Token に移動します。これにより、クライアントを設定するために使用できるAPIトークンが提供されます。
- Step 4, Option A (recommended): Configure Persistent API Token
Configure the client by persisting the the API token on your workstation. You only need to do this once. This is the recommended way of using AI Hub.
Run the following command in your terminal:
qai-hub configure --api_token INSERT_API_TOKEN
You can check that your API token is installed correctly by fetching a list of available devices. To do that, you can type the following in a Python terminal:
import qai_hub as hub hub.get_devices()
If you go this route, you do not need to follow Option B below.
- Step 4, Option B (for ephemeral environments): Configure Session API Token
In ephemeral customer environments, such as Amazon SageMaker, where API tokens cannot be persistently stored on disk, you will need to configure the AI Hub client on every use.
Provide your API token after you import the AI Hub client. For example:
import qai_hub as hub hub.set_session_token("INSERT_API_TOKEN")
You can check that the API token is working correctly by fetching a list of available devices. Add the following code to your Python script, after initializing the client:
hub.get_devices()
You must provide the API token every time you initialize a session using this token configuration method.
これでQualcomm AI Hubでジョブを送信する準備が整いました。問題が発生した場合は、お問い合わせ ください。以下の例を試して、希望するデバイスでプロファイルジョブを実行してみてください。
簡単な例(PyTorch)
Once you have set up your Qualcomm® AI Hub environment, the next step is to submit a profiling job. First, install the dependencies of this example:
pip3 install "qai-hub[torch]"
警告
スニペットがAPI認証エラーで失敗する場合、有効なAPIトークンがインストールされていないことを意味します。設定方法については、インストール を参照してください。
選択したデバイスタイプでMobileNet v2ネットワークの分析を送信します。上記のリストから任意のデバイスを使用して、以下のオプションとは異なるデバイスを指定できます。
モバイル
import numpy as np
import requests
import torch
from PIL import Image
from torchvision.models import mobilenet_v2
import qai_hub as hub
# Using pre-trained MobileNet
torch_model = mobilenet_v2(pretrained=True)
torch_model.eval()
# Step 1: Trace model
input_shape = (1, 3, 224, 224)
example_input = torch.rand(input_shape)
traced_torch_model = torch.jit.trace(torch_model, example_input)
# Step 2: Compile model
compile_job = hub.submit_compile_job(
model=traced_torch_model,
device=hub.Device("Samsung Galaxy S24 (Family)"),
input_specs=dict(image=input_shape),
options="--target_runtime tflite",
)
assert isinstance(compile_job, hub.CompileJob)
# Step 3: Profile on cloud-hosted device
target_model = compile_job.get_target_model()
assert isinstance(target_model, hub.Model)
profile_job = hub.submit_profile_job(
model=target_model,
device=hub.Device("Samsung Galaxy S24 (Family)"),
)
assert isinstance(profile_job, hub.ProfileJob)
# Step 4: Run inference on cloud-hosted device
sample_image_url = (
"https://qaihub-public-assets.s3.us-west-2.amazonaws.com/apidoc/input_image1.jpg"
)
response = requests.get(sample_image_url, stream=True)
response.raw.decode_content = True
image = Image.open(response.raw).resize((224, 224))
input_array = np.expand_dims(
np.transpose(np.array(image, dtype=np.float32) / 255.0, (2, 0, 1)), axis=0
)
# Run inference using the on-device model on the input image
inference_job = hub.submit_inference_job(
model=target_model,
device=hub.Device("Samsung Galaxy S24 (Family)"),
inputs=dict(image=[input_array]),
)
assert isinstance(inference_job, hub.InferenceJob)
on_device_output = inference_job.download_output_data()
assert isinstance(on_device_output, dict)
# Step 5: Post-processing the on-device output
output_name = list(on_device_output.keys())[0]
out = on_device_output[output_name][0]
on_device_probabilities = np.exp(out) / np.sum(np.exp(out), axis=1)
# Read the class labels for imagenet
sample_classes = "https://qaihub-public-assets.s3.us-west-2.amazonaws.com/apidoc/imagenet_classes.txt"
response = requests.get(sample_classes, stream=True)
response.raw.decode_content = True
categories = [str(s.strip()) for s in response.raw]
# Print top five predictions for the on-device model
print("Top-5 On-Device predictions:")
top5_classes = np.argsort(on_device_probabilities[0], axis=0)[-5:]
for c in reversed(top5_classes):
print(f"{c} {categories[c]:20s} {on_device_probabilities[0][c]:>6.1%}")
# Step 6: Download model
target_model = compile_job.get_target_model()
assert isinstance(target_model, hub.Model)
target_model.download("mobilenet_v2.tflite")
IOT
import numpy as np
import requests
import torch
from PIL import Image
from torchvision.models import mobilenet_v2
import qai_hub as hub
# Using pre-trained MobileNet
torch_model = mobilenet_v2(pretrained=True)
torch_model.eval()
# Step 1: Trace model
input_shape = (1, 3, 224, 224)
example_input = torch.rand(input_shape)
traced_torch_model = torch.jit.trace(torch_model, example_input)
# Step 2: Compile model
compile_job = hub.submit_compile_job(
model=traced_torch_model,
device=hub.Device("RB3 Gen 2 (Proxy)"),
input_specs=dict(image=input_shape),
options="--target_runtime tflite",
)
assert isinstance(compile_job, hub.CompileJob)
# Step 3: Profile on cloud-hosted device
target_model = compile_job.get_target_model()
assert isinstance(target_model, hub.Model)
profile_job = hub.submit_profile_job(
model=target_model,
device=hub.Device("RB3 Gen 2 (Proxy)"),
)
assert isinstance(profile_job, hub.ProfileJob)
# Step 4: Run inference on cloud-hosted device
sample_image_url = (
"https://qaihub-public-assets.s3.us-west-2.amazonaws.com/apidoc/input_image1.jpg"
)
response = requests.get(sample_image_url, stream=True)
response.raw.decode_content = True
image = Image.open(response.raw).resize((224, 224))
input_array = np.expand_dims(
np.transpose(np.array(image, dtype=np.float32) / 255.0, (2, 0, 1)), axis=0
)
# Run inference using the on-device model on the input image
inference_job = hub.submit_inference_job(
model=target_model,
device=hub.Device("RB3 Gen 2 (Proxy)"),
inputs=dict(image=[input_array]),
)
assert isinstance(inference_job, hub.InferenceJob)
on_device_output = inference_job.download_output_data()
assert isinstance(on_device_output, dict)
# Step 5: Post-processing the on-device output
output_name = list(on_device_output.keys())[0]
out = on_device_output[output_name][0]
on_device_probabilities = np.exp(out) / np.sum(np.exp(out), axis=1)
# Read the class labels for imagenet
sample_classes = "https://qaihub-public-assets.s3.us-west-2.amazonaws.com/apidoc/imagenet_classes.txt"
response = requests.get(sample_classes, stream=True)
response.raw.decode_content = True
categories = [str(s.strip()) for s in response.raw]
# Print top five predictions for the on-device model
print("Top-5 On-Device predictions:")
top5_classes = np.argsort(on_device_probabilities[0], axis=0)[-5:]
for c in reversed(top5_classes):
print(f"{c} {categories[c]:20s} {on_device_probabilities[0][c]:>6.1%}")
# Step 6: Download model
target_model = compile_job.get_target_model()
assert isinstance(target_model, hub.Model)
target_model.download("mobilenet_v2.tflite")
コンピュート
import numpy as np
import requests
import torch
from PIL import Image
from torchvision.models import mobilenet_v2
import qai_hub as hub
# Using pre-trained MobileNet
torch_model = mobilenet_v2(pretrained=True)
torch_model.eval()
# Step 1: Trace model
input_shape = (1, 3, 224, 224)
example_input = torch.rand(input_shape)
traced_torch_model = torch.jit.trace(torch_model, example_input)
# Step 2: Compile model
compile_job = hub.submit_compile_job(
model=traced_torch_model,
device=hub.Device("Snapdragon X Elite CRD"),
input_specs=dict(image=input_shape),
options="--target_runtime onnx",
)
assert isinstance(compile_job, hub.CompileJob)
# Step 3: Profile on cloud-hosted device
target_model = compile_job.get_target_model()
assert isinstance(target_model, hub.Model)
profile_job = hub.submit_profile_job(
model=target_model,
device=hub.Device("Snapdragon X Elite CRD"),
)
assert isinstance(profile_job, hub.ProfileJob)
# Step 4: Run inference on cloud-hosted device
sample_image_url = (
"https://qaihub-public-assets.s3.us-west-2.amazonaws.com/apidoc/input_image1.jpg"
)
response = requests.get(sample_image_url, stream=True)
response.raw.decode_content = True
image = Image.open(response.raw).resize((224, 224))
input_array = np.expand_dims(
np.transpose(np.array(image, dtype=np.float32) / 255.0, (2, 0, 1)), axis=0
)
# Run inference using the on-device model on the input image
inference_job = hub.submit_inference_job(
model=target_model,
device=hub.Device("Snapdragon X Elite CRD"),
inputs=dict(image=[input_array]),
)
assert isinstance(inference_job, hub.InferenceJob)
on_device_output = inference_job.download_output_data()
assert isinstance(on_device_output, dict)
# Step 5: Post-processing the on-device output
output_name = list(on_device_output.keys())[0]
out = on_device_output[output_name][0]
on_device_probabilities = np.exp(out) / np.sum(np.exp(out), axis=1)
# Read the class labels for imagenet
sample_classes = "https://qaihub-public-assets.s3.us-west-2.amazonaws.com/apidoc/imagenet_classes.txt"
response = requests.get(sample_classes, stream=True)
response.raw.decode_content = True
categories = [str(s.strip()) for s in response.raw]
# Print top five predictions for the on-device model
print("Top-5 On-Device predictions:")
top5_classes = np.argsort(on_device_probabilities[0], axis=0)[-5:]
for c in reversed(top5_classes):
print(f"{c} {categories[c]:20s} {on_device_probabilities[0][c]:>6.1%}")
# Step 6: Download model
target_model = compile_job.get_target_model()
assert isinstance(target_model, hub.Model)
target_model.download("mobilenet_v2.onnx")
自動車
import numpy as np
import requests
import torch
from PIL import Image
from torchvision.models import mobilenet_v2
import qai_hub as hub
# Using pre-trained MobileNet
torch_model = mobilenet_v2(pretrained=True)
torch_model.eval()
# Step 1: Trace model
input_shape = (1, 3, 224, 224)
example_input = torch.rand(input_shape)
traced_torch_model = torch.jit.trace(torch_model, example_input)
# Step 2: Compile model
compile_job = hub.submit_compile_job(
model=traced_torch_model,
device=hub.Device("SA8650 (Proxy)"),
input_specs=dict(image=input_shape),
options="--target_runtime qnn_lib_aarch64_android",
)
assert isinstance(compile_job, hub.CompileJob)
# Step 3: Profile on cloud-hosted device
target_model = compile_job.get_target_model()
assert isinstance(target_model, hub.Model)
profile_job = hub.submit_profile_job(
model=target_model,
device=hub.Device("SA8650 (Proxy)"),
)
assert isinstance(profile_job, hub.ProfileJob)
# Step 4: Run inference on cloud-hosted device
sample_image_url = (
"https://qaihub-public-assets.s3.us-west-2.amazonaws.com/apidoc/input_image1.jpg"
)
response = requests.get(sample_image_url, stream=True)
response.raw.decode_content = True
image = Image.open(response.raw).resize((224, 224))
input_array = np.expand_dims(
np.transpose(np.array(image, dtype=np.float32) / 255.0, (2, 0, 1)), axis=0
)
# Run inference using the on-device model on the input image
inference_job = hub.submit_inference_job(
model=target_model,
device=hub.Device("SA8650 (Proxy)"),
inputs=dict(image=[input_array]),
)
assert isinstance(inference_job, hub.InferenceJob)
on_device_output = inference_job.download_output_data()
assert isinstance(on_device_output, dict)
# Step 5: Post-processing the on-device output
output_name = list(on_device_output.keys())[0]
out = on_device_output[output_name][0]
on_device_probabilities = np.exp(out) / np.sum(np.exp(out), axis=1)
# Read the class labels for imagenet
sample_classes = "https://qaihub-public-assets.s3.us-west-2.amazonaws.com/apidoc/imagenet_classes.txt"
response = requests.get(sample_classes, stream=True)
response.raw.decode_content = True
categories = [str(s.strip()) for s in response.raw]
# Print top five predictions for the on-device model
print("Top-5 On-Device predictions:")
top5_classes = np.argsort(on_device_probabilities[0], axis=0)[-5:]
for c in reversed(top5_classes):
print(f"{c} {categories[c]:20s} {on_device_probabilities[0][c]:>6.1%}")
# Step 6: Download model
target_model = compile_job.get_target_model()
assert isinstance(target_model, hub.Model)
target_model.download("mobilenet_v2.so")
これにより、コンパイルジョブとプロファイルジョブが送信され、両方のジョブのURLが表示されます。最後に、コードはサンプルデータで推論ジョブを実行します。すべてのジョブは /jobs/ で表示できます。
ジョブのステータスはプログラムで照会できます:
status = profile_job.get_status()
print(status)
以下のスニペットを使用してジョブの結果にアクセスできます。主に3つの部分があります
プロファイル: JSON形式のプロファイリング結果。
ターゲットモデル: デプロイメントの準備が整った最適化済みモデル。
結果: ジョブのすべてのアーティファクト(ログを含む)を含むフォルダー。
これらはジョブが終了するまで待機するブロッキングAPI呼び出しであることに注意してください:
プロファイル結果を JSON 形式でダウンロード(ブロッキング呼び出し)
profile = profile_job.download_profile()
print(profile)
最適化されたモデルをダウンロード(ブロッキング呼び出し)
compile_job.model.download("model_filename")
結果を現在のディレクトリにダウンロード(ブロッキング呼び出し)
profile_job.download_results(".")
詳細については、モデルのプロファイリング または API documentation を参照してください。