Skip to content

Commit

Permalink
fix CI issue
Browse files Browse the repository at this point in the history
Signed-off-by: Mengni Wang <[email protected]>
  • Loading branch information
mengniwang95 committed Jun 20, 2024
1 parent 9ea41ea commit 3b42dcd
Show file tree
Hide file tree
Showing 64 changed files with 1,468 additions and 947 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ pip install onnx-neural-compressor
pip install -r requirements.txt
```

> Note: Validated ONNX Runtime [Version](/docs/source/installation_guide.md#validated-software-environment).

## 2. Prepare Model

Expand Down
116 changes: 47 additions & 69 deletions examples/image_recognition/resnet50/quantization/ptq_static/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,28 @@
# under the License.
# pylint:disable=redefined-outer-name,logging-format-interpolation

import logging
import argparse
import collections
import logging
import os
import re
import time

import cv2
import numpy as np
import onnx
import re
import os
import collections
from PIL import Image
import onnxruntime as ort
from PIL import Image
from sklearn import metrics
from onnx_neural_compressor import data_reader
from onnx_neural_compressor import config
from onnx_neural_compressor import quantization

from onnx_neural_compressor import config, data_reader, quantization
from onnx_neural_compressor.quantization import tuning

logger = logging.getLogger(__name__)
logging.basicConfig(format = '%(asctime)s - %(levelname)s - %(name)s - %(message)s',
datefmt = '%m/%d/%Y %H:%M:%S',
level = logging.WARN)
logging.basicConfig(
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s", datefmt="%m/%d/%Y %H:%M:%S", level=logging.WARN
)


def _topk_shape_validate(preds, labels):
# preds shape can be Nxclass_num or class_num(N=1 by default)
Expand Down Expand Up @@ -79,13 +81,14 @@ def _topk_shape_validate(preds, labels):
class_num = preds.shape[1]

label_N = labels.shape[0]
assert label_N == N, 'labels batch size should same with preds'
assert label_N == N, "labels batch size should same with preds"
labels = labels.reshape([N, -1])
# one-hot labels will have 2 dimension not equal 1
if labels.shape[1] != 1:
labels = labels.argsort()[..., -1:]
return preds, labels


class TopK:
def __init__(self, k=1):
self.k = k
Expand All @@ -94,7 +97,7 @@ def __init__(self, k=1):

def update(self, preds, labels, sample_weight=None):
preds, labels = _topk_shape_validate(preds, labels)
preds = preds.argsort()[..., -self.k:]
preds = preds.argsort()[..., -self.k :]
if self.k == 1:
correct = metrics.accuracy_score(preds, labels, normalize=False)
self.num_correct += correct
Expand All @@ -103,7 +106,7 @@ def update(self, preds, labels, sample_weight=None):
for p, l in zip(preds, labels):
# get top-k labels with np.argpartition
# p = np.argpartition(p, -self.k)[-self.k:]
l = l.astype('int32')
l = l.astype("int32")
if l in p:
self.num_correct += 1

Expand All @@ -128,7 +131,7 @@ def __init__(self, model_path, dataset_location, image_list, batch_size=1, calib
src_lst = []
label_lst = []
num = 0
with open(image_list, 'r') as f:
with open(image_list, "r") as f:
for s in f:
image_name, label = re.split(r"\s+", s.strip())
src = os.path.join(dataset_location, image_name)
Expand All @@ -153,18 +156,18 @@ def __init__(self, model_path, dataset_location, image_list, batch_size=1, calib

def _preprpcess(self, src):
with Image.open(src) as image:
image = np.array(image.convert('RGB')).astype(np.float32)
image = image / 255.
image = np.array(image.convert("RGB")).astype(np.float32)
image = image / 255.0
image = cv2.resize(image, (256, 256), interpolation=cv2.INTER_LINEAR)

h, w = image.shape[0], image.shape[1]

y0 = (h - 224) // 2
x0 = (w - 224) // 2
image = image[y0:y0 + 224, x0:x0 + 224, :]
image = image[y0 : y0 + 224, x0 : x0 + 224, :]
image = (image - [0.485, 0.456, 0.406]) / [0.229, 0.224, 0.225]
image = image.transpose((2, 0, 1))
return image.astype('float32')
return image.astype("float32")

def get_next(self):
lst = next(self.iter_next, None)
Expand All @@ -186,54 +189,22 @@ def eval_func(model, dataloader, metric):
metric.update(output, labels[idx])
return metric.result()


if __name__ == "__main__":
logger.info("Evaluating ONNXRuntime full precision accuracy and performance:")
parser = argparse.ArgumentParser(
description="Resnet50 fine-tune examples for image classification tasks.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
'--model_path',
type=str,
help="Pre-trained model on onnx file"
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument("--model_path", type=str, help="Pre-trained model on onnx file")
parser.add_argument("--dataset_location", type=str, help="Imagenet data path")
parser.add_argument("--label_path", type=str, help="Imagenet label path")
parser.add_argument("--benchmark", action="store_true", default=False)
parser.add_argument("--tune", action="store_true", default=False, help="whether quantize the model")
parser.add_argument("--output_model", type=str, help="output model path")
parser.add_argument("--mode", type=str, help="benchmark mode of performance or accuracy")
parser.add_argument(
'--dataset_location',
type=str,
help="Imagenet data path"
)
parser.add_argument(
'--label_path',
type=str,
help="Imagenet label path"
)
parser.add_argument(
'--benchmark',
action='store_true', \
default=False
)
parser.add_argument(
'--tune',
action='store_true', \
default=False,
help="whether quantize the model"
)
parser.add_argument(
'--output_model',
type=str,
help="output model path"
)
parser.add_argument(
'--mode',
type=str,
help="benchmark mode of performance or accuracy"
)
parser.add_argument(
'--quant_format',
type=str,
default='QOperator',
choices=['QDQ', 'QOperator'],
help="quantization format"
"--quant_format", type=str, default="QOperator", choices=["QDQ", "QOperator"], help="quantization format"
)
parser.add_argument(
"--batch_size",
Expand All @@ -245,25 +216,26 @@ def eval_func(model, dataloader, metric):
model = onnx.load(args.model_path)
top1 = TopK()
dataloader = DataReader(args.model_path, args.dataset_location, args.label_path, args.batch_size)

def eval(onnx_model):
dataloader.rewind()
return eval_func(onnx_model, dataloader, top1)

if args.benchmark:
if args.mode == 'performance':
if args.mode == "performance":
total_time = 0.0
num_iter = 100
num_warmup = 10

sess_options = ort.SessionOptions()
sess_options.intra_op_num_threads = args.intra_op_num_threads
session = onnxruntime.InferenceSession(model.SerializeToString(),
sess_options,
providers=onnxruntime.get_available_providers())
session = ort.InferenceSession(
model.SerializeToString(), sess_options, providers=ort.get_available_providers()
)
ort_inputs = {}
len_inputs = len(session.get_inputs())
inputs_names = [session.get_inputs()[i].name for i in range(len_inputs)]

for idx, batch in enumerate(dataloader):
if idx + 1 > num_iter:
break
Expand All @@ -277,17 +249,23 @@ def eval(onnx_model):
print(args)
throughput = (num_iter - num_warmup) / total_time
print("Throughput: {} samples/s".format(throughput))
elif args.mode == 'accuracy':
elif args.mode == "accuracy":
acc_result = eval_func(model, dataloader, top1)
print("Batch size = %d" % dataloader.batch_size)
print("Accuracy: %.5f" % acc_result)

if args.tune:
calibration_data_reader = DataReader(args.model_path, args.dataset_location, args.label_path, args.batch_size, calibration_sampling_size=100)
calibration_data_reader = DataReader(
args.model_path, args.dataset_location, args.label_path, args.batch_size, calibration_sampling_size=100
)

custom_tune_config = tuning.TuningConfig(
config_set=config.StaticQuantConfig.get_config_set_for_tuning(
quant_format=quantization.QuantFormat.QOperator if args.quant_format == "QOperator" else quantization.QuantFormat.QDQ,
quant_format=(
quantization.QuantFormat.QOperator
if args.quant_format == "QOperator"
else quantization.QuantFormat.QDQ
),
)
)
best_model = tuning.autotune(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ function init_params {
do
case $var in
--input_model=*)
input_model=$(echo $var |cut -f2 -d=)
input_model=$(echo "$var" |cut -f2 -d=)
;;
--dataset_location=*)
dataset_location=$(echo $var |cut -f2 -d=)
dataset_location=$(echo "$var" |cut -f2 -d=)
;;
--label_path=*)
label_path=$(echo $var |cut -f2 -d=)
label_path=$(echo "$var" |cut -f2 -d=)
;;
--mode=*)
mode=$(echo $var |cut -f2 -d=)
mode=$(echo "$var" |cut -f2 -d=)
;;
esac
done
Expand All @@ -33,10 +33,10 @@ function init_params {
function run_benchmark {

python main.py \
--model_path ${input_model} \
--dataset_location ${dataset_location} \
--label_path ${label_path-${dataset_location}/../val.txt} \
--mode=${mode} \
--model_path "${input_model}" \
--dataset_location "${dataset_location}" \
--label_path "${label_path-${dataset_location}/../val.txt}" \
--mode="${mode}" \
--batch_size 1 \
--benchmark

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ function init_params {
do
case $var in
--input_model=*)
input_model=$(echo $var |cut -f2 -d=)
input_model=$(echo "$var" |cut -f2 -d=)
;;
--output_model=*)
output_model=$(echo $var |cut -f2 -d=)
output_model=$(echo "$var" |cut -f2 -d=)
;;
--dataset_location=*)
dataset_location=$(echo $var |cut -f2 -d=)
dataset_location=$(echo "$var" |cut -f2 -d=)
;;
--label_path=*)
label_path=$(echo $var |cut -f2 -d=)
label_path=$(echo "$var" |cut -f2 -d=)
;;
--quant_format=*)
quant_format=$(echo $var |cut -f2 -d=)
quant_format=$(echo "$var" |cut -f2 -d=)
;;
esac
done
Expand All @@ -36,11 +36,11 @@ function init_params {
# run_tuning
function run_tuning {
python main.py \
--model_path ${input_model} \
--dataset_location ${dataset_location} \
--label_path ${label_path-${dataset_location}/../val.txt} \
--output_model ${output_model} \
--quant_format ${quant_format-QOperator} \
--model_path "${input_model}" \
--dataset_location "${dataset_location}" \
--label_path "${label_path-${dataset_location}/../val.txt}" \
--output_model "${output_model}" \
--quant_format "${quant_format-QOperator}" \
--tune
}

Expand Down
1 change: 0 additions & 1 deletion examples/nlp/bert/quantization/ptq_dynamic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ pip install onnx-neural-compressor
pip install -r requirements.txt
```

> Note: Validated ONNX Runtime [Version](/docs/installation_guide.md#validated-software-environment).

## 2. Prepare Dataset

Expand Down
Loading

0 comments on commit 3b42dcd

Please sign in to comment.