Upload 2 files
Browse files- inference.py +23 -0
- quantize.py +61 -0
inference.py
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
from PIL import Image
|
3 |
+
|
4 |
+
from optimum.amd.ryzenai import RyzenAIModelForImageClassification
|
5 |
+
from transformers import AutoFeatureExtractor, pipeline
|
6 |
+
|
7 |
+
|
8 |
+
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
|
9 |
+
image = Image.open(requests.get(url, stream=True).raw)
|
10 |
+
|
11 |
+
quantized_model_path = "mohitsha/transformers-resnet18-onnx-quantized-ryzen"
|
12 |
+
|
13 |
+
# The path and name of the runtime configuration file. A default version of this file can be
|
14 |
+
# found in the voe-4.0-win_amd64 folder of the Ryzen AI software installation package under
|
15 |
+
# the name vaip_config.json
|
16 |
+
vaip_config = ".\\vaip_config.json"
|
17 |
+
|
18 |
+
model = RyzenAIModelForImageClassification.from_pretrained(quantized_model_path, vaip_config=vaip_config)
|
19 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained(quantized_model_path)
|
20 |
+
|
21 |
+
cls_pipe = pipeline("image-classification", model=model, feature_extractor=feature_extractor)
|
22 |
+
outputs = cls_pipe(image)
|
23 |
+
print(outputs)
|
quantize.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from functools import partial
|
2 |
+
|
3 |
+
from optimum.amd.ryzenai import (
|
4 |
+
AutoQuantizationConfig,
|
5 |
+
RyzenAIOnnxQuantizer,
|
6 |
+
)
|
7 |
+
from optimum.exporters.onnx import main_export
|
8 |
+
from transformers import AutoFeatureExtractor
|
9 |
+
|
10 |
+
|
11 |
+
# Define paths for exporting ONNX model and saving quantized model
|
12 |
+
export_dir = "resnet_onnx"
|
13 |
+
quantization_dir = "resnet_onnx_quantized"
|
14 |
+
|
15 |
+
# Specify the model ID from Transformers
|
16 |
+
model_id = "microsoft/resnet-18"
|
17 |
+
|
18 |
+
# Step 1: Export the model to ONNX format using Optimum Exporters
|
19 |
+
main_export(
|
20 |
+
model_name_or_path=model_id,
|
21 |
+
output=export_dir,
|
22 |
+
task="image-classification",
|
23 |
+
opset=13,
|
24 |
+
batch_size=1,
|
25 |
+
height=224,
|
26 |
+
width=224,
|
27 |
+
no_dynamic_axes=True,
|
28 |
+
)
|
29 |
+
|
30 |
+
# Step 2: Preprocess configuration and data transformations
|
31 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained(model_id)
|
32 |
+
|
33 |
+
|
34 |
+
def preprocess_fn(ex, feature_extractor):
|
35 |
+
image = ex["image"]
|
36 |
+
if image.mode == "L":
|
37 |
+
image = image.convert("RGB")
|
38 |
+
pixel_values = feature_extractor(image).pixel_values[0]
|
39 |
+
return {"pixel_values": pixel_values}
|
40 |
+
|
41 |
+
|
42 |
+
# Step 3: Initialize the RyzenAIOnnxQuantizer with the exported model
|
43 |
+
quantizer = RyzenAIOnnxQuantizer.from_pretrained(export_dir)
|
44 |
+
|
45 |
+
# Step 4: Load recommended quantization config for model
|
46 |
+
quantization_config = AutoQuantizationConfig.ipu_cnn_config()
|
47 |
+
|
48 |
+
# Step 5: Obtain a calibration dataset for computing quantization parameters
|
49 |
+
train_calibration_dataset = quantizer.get_calibration_dataset(
|
50 |
+
"imagenet-1k",
|
51 |
+
preprocess_function=partial(preprocess_fn, feature_extractor=feature_extractor),
|
52 |
+
num_samples=100,
|
53 |
+
dataset_split="train",
|
54 |
+
preprocess_batch=False,
|
55 |
+
streaming=True,
|
56 |
+
)
|
57 |
+
|
58 |
+
# Step 6: Run the quantizer with the specified configuration and calibration data
|
59 |
+
quantizer.quantize(
|
60 |
+
quantization_config=quantization_config, dataset=train_calibration_dataset, save_dir=quantization_dir
|
61 |
+
)
|