|
from functools import partial |
|
import timm |
|
from optimum.amd.ryzenai import ( |
|
AutoQuantizationConfig, |
|
RyzenAIOnnxQuantizer, |
|
) |
|
from optimum.exporters.onnx import main_export |
|
from transformers import PretrainedConfig |
|
|
|
|
|
export_dir = "resnet_onnx" |
|
quantization_dir = "resnet_onnx_quantized" |
|
|
|
|
|
model_id = "timm/resnet18.a1_in1k" |
|
|
|
|
|
main_export( |
|
model_name_or_path=model_id, |
|
output=export_dir, |
|
task="image-classification", |
|
opset=13, |
|
batch_size=1, |
|
no_dynamic_axes=True, |
|
) |
|
|
|
|
|
config = PretrainedConfig.from_pretrained(export_dir) |
|
data_config = timm.data.resolve_data_config(pretrained_cfg=config.pretrained_cfg) |
|
transforms = timm.data.create_transform(**data_config, is_training=False) |
|
|
|
def preprocess_fn(ex, transforms): |
|
image = ex["image"] |
|
if image.mode == "L": |
|
|
|
print("WARNING: converting greyscale to RGB") |
|
image = image.convert("RGB") |
|
pixel_values = transforms(image) |
|
return {"pixel_values": pixel_values} |
|
|
|
|
|
quantizer = RyzenAIOnnxQuantizer.from_pretrained(export_dir) |
|
|
|
|
|
quantization_config = AutoQuantizationConfig.ipu_cnn_config() |
|
|
|
|
|
train_calibration_dataset = quantizer.get_calibration_dataset( |
|
"imagenet-1k", |
|
preprocess_function=partial(preprocess_fn, transforms=transforms), |
|
num_samples=100, |
|
dataset_split="train", |
|
preprocess_batch=False, |
|
streaming=True, |
|
) |
|
|
|
|
|
quantizer.quantize( |
|
quantization_config=quantization_config, |
|
dataset=train_calibration_dataset, |
|
save_dir=quantization_dir |
|
) |
|
|
|
|