File size: 5,584 Bytes
e5b568e 6eef315 e5b568e 6eef315 596a24b e5b568e 596a24b e5b568e 596a24b e5b568e 6eef315 596a24b e5b568e 596a24b e5b568e 596a24b e5b568e 596a24b e5b568e 6eef315 83c563e 596a24b 83c563e 6eef315 596a24b e5b568e 596a24b e5b568e 6eef315 596a24b e5b568e 83c563e 6eef315 83c563e 6eef315 83c563e 596a24b 83c563e 596a24b 83c563e 596a24b e5b568e 596a24b e5b568e 83c563e e5b568e 83c563e 57699b7 596a24b e5b568e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
import os
import sys
import numpy as np
import cv2 as cv
import onnx
from neural_compressor.experimental import Quantization, common
from neural_compressor.experimental.metric import BaseMetric
class Accuracy(BaseMetric):
def __init__(self, *args):
self.pred_list = []
self.label_list = []
self.samples = 0
def update(self, predict, label):
predict = np.array(predict)
label = np.array(label)
self.pred_list.append(np.argmax(predict[0]))
self.label_list.append(label[0][0])
self.samples += 1
def reset(self):
self.pred_list = []
self.label_list = []
self.samples = 0
def result(self):
correct_num = np.sum(np.array(self.pred_list) == np.array(self.label_list))
return correct_num / self.samples
class Quantize:
def __init__(self, model_path, config_path, custom_dataset=None, eval_dataset=None, metric=None):
self.model_path = model_path
self.config_path = config_path
self.custom_dataset = custom_dataset
self.eval_dataset = eval_dataset
self.metric = metric
def run(self):
print('Quantizing (int8) with Intel\'s Neural Compressor:')
print('\tModel: {}'.format(self.model_path))
print('\tConfig: {}'.format(self.config_path))
output_name = '{}-int8-quantized.onnx'.format(self.model_path[:-5])
model = onnx.load(self.model_path)
quantizer = Quantization(self.config_path)
quantizer.model = common.Model(model)
if self.custom_dataset is not None:
quantizer.calib_dataloader = common.DataLoader(self.custom_dataset)
if self.eval_dataset is not None:
quantizer.eval_dataloader = common.DataLoader(self.eval_dataset)
if self.metric is not None:
quantizer.metric = common.Metric(metric_cls=self.metric, name='metric')
q_model = quantizer()
q_model.save(output_name)
class Dataset:
def __init__(self, root, size=None, dim='chw', scale=1.0, mean=0.0, std=1.0, swapRB=False, toFP32=False):
self.root = root
self.size = size
self.dim = dim
self.scale = scale
self.mean = mean
self.std = std
self.swapRB = swapRB
self.toFP32 = toFP32
self.image_list, self.label_list = self.load_image_list(self.root)
def load_image_list(self, path):
image_list = []
label_list = []
for f in os.listdir(path):
if not f.endswith('.jpg'):
continue
image_list.append(os.path.join(path, f))
label_list.append(1)
return image_list, label_list
def __getitem__(self, idx):
img = cv.imread(self.image_list[idx])
if self.swapRB:
img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
if self.size:
img = cv.resize(img, dsize=self.size)
if self.toFP32:
img = img.astype(np.float32)
img = img * self.scale
img = img - self.mean
img = img / self.std
if self.dim == 'chw':
img = img.transpose(2, 0, 1) # hwc -> chw
return img, self.label_list[idx]
def __len__(self):
return len(self.image_list)
class FerDataset(Dataset):
def __init__(self, root, size=None, dim='chw', scale=1.0, mean=0.0, std=1.0, swapRB=False, toFP32=False):
super(FerDataset, self).__init__(root, size, dim, scale, mean, std, swapRB, toFP32)
def load_image_list(self, path):
image_list = []
label_list = []
for f in os.listdir(path):
if not f.endswith('.jpg'):
continue
image_list.append(os.path.join(path, f))
label_list.append(int(f.split("_")[2]))
return image_list, label_list
models = dict(
mobilenetv1=Quantize(model_path='../../models/image_classification_mobilenet/image_classification_mobilenetv1_2022apr.onnx',
config_path='./inc_configs/mobilenet.yaml'),
mobilenetv2=Quantize(model_path='../../models/image_classification_mobilenet/image_classification_mobilenetv2_2022apr.onnx',
config_path='./inc_configs/mobilenet.yaml'),
mp_handpose=Quantize(model_path='../../models/handpose_estimation_mediapipe/handpose_estimation_mediapipe_2022may.onnx',
config_path='./inc_configs/mp_handpose.yaml',
custom_dataset=Dataset(root='../../benchmark/data/palm_detection', dim='hwc', swapRB=True, mean=127.5, std=127.5, toFP32=True)),
fer=Quantize(model_path='../../models/facial_expression_recognition/facial_expression_recognition_mobilefacenet_2022july.onnx',
config_path='./inc_configs/fer.yaml',
custom_dataset=FerDataset(root='../../benchmark/data/facial_expression_recognition/fer_calibration', size=(112, 112), toFP32=True, swapRB=True, scale=1./255, mean=0.5, std=0.5),
eval_dataset=FerDataset(root='../../benchmark/data/facial_expression_recognition/fer_evaluation', size=(112, 112), toFP32=True, swapRB=True, scale=1./255, mean=0.5, std=0.5),
metric=Accuracy),
)
if __name__ == '__main__':
selected_models = []
for i in range(1, len(sys.argv)):
selected_models.append(sys.argv[i])
if not selected_models:
selected_models = list(models.keys())
print('Models to be quantized: {}'.format(str(selected_models)))
for selected_model_name in selected_models:
q = models[selected_model_name]
q.run()
|