forked from intel/neural-compressor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
299 lines (269 loc) · 10.2 KB
/
main.py
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# pylint:disable=redefined-outer-name,logging-format-interpolation
import logging
import argparse
import cv2
import numpy as np
import onnx
import re
import os
import collections
from PIL import Image
import onnxruntime as ort
from sklearn.metrics import accuracy_score
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)
def _topk_shape_validate(preds, labels):
# preds shape can be Nxclass_num or class_num(N=1 by default)
# it's more suitable for 'Accuracy' with preds shape Nx1(or 1) output from argmax
if isinstance(preds, int):
preds = [preds]
preds = np.array(preds)
elif isinstance(preds, np.ndarray):
preds = np.array(preds)
elif isinstance(preds, list):
preds = np.array(preds)
preds = preds.reshape((-1, preds.shape[-1]))
# consider labels just int value 1x1
if isinstance(labels, int):
labels = [labels]
labels = np.array(labels)
elif isinstance(labels, tuple):
labels = np.array([labels])
labels = labels.reshape((labels.shape[-1], -1))
elif isinstance(labels, list):
if isinstance(labels[0], int):
labels = np.array(labels)
labels = labels.reshape((labels.shape[0], 1))
elif isinstance(labels[0], tuple):
labels = np.array(labels)
labels = labels.reshape((labels.shape[-1], -1))
else:
labels = np.array(labels)
# labels most have 2 axis, 2 cases: N(or Nx1 sparse) or Nxclass_num(one-hot)
# only support 2 dimension one-shot labels
# or 1 dimension one-hot class_num will confuse with N
if len(preds.shape) == 1:
N = 1
class_num = preds.shape[0]
preds = preds.reshape([-1, class_num])
elif len(preds.shape) >= 2:
N = preds.shape[0]
preds = preds.reshape([N, -1])
class_num = preds.shape[1]
label_N = labels.shape[0]
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
self.num_correct = 0
self.num_sample = 0
def update(self, preds, labels, sample_weight=None):
preds, labels = _topk_shape_validate(preds, labels)
preds = preds.argsort()[..., -self.k:]
if self.k == 1:
correct = accuracy_score(preds, labels, normalize=False)
self.num_correct += correct
else:
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')
if l in p:
self.num_correct += 1
self.num_sample += len(labels)
def reset(self):
self.num_correct = 0
self.num_sample = 0
def result(self):
if self.num_sample == 0:
logger.warning("Sample num during evaluation is 0.")
return 0
return self.num_correct / self.num_sample
class Dataloader:
def __init__(self, dataset_location, image_list, batch_size):
self.batch_size = batch_size
self.image_list = []
self.label_list = []
self.random_crop = False
self.resize_side= 256
self.mean_value = [0.485, 0.456, 0.406]
self.std_value = [0.229, 0.224, 0.225]
self.height = 224
self.width = 224
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)
if not os.path.exists(src):
continue
self.image_list.append(src)
self.label_list.append(int(label))
def _preprpcess(self, src):
with Image.open(src) as image:
image = np.array(image.convert('RGB'))
height, width = image.shape[0], image.shape[1]
scale = self.resize_side / width if height > width else self.resize_side / height
new_height = int(height*scale)
new_width = int(width*scale)
image = cv2.resize(image, (new_height, new_width))
image = image / 255.
shape = image.shape
if self.random_crop:
y0 = np.random.randint(low=0, high=(shape[0] - self.height +1))
x0 = np.random.randint(low=0, high=(shape[1] - self.width +1))
else:
y0 = (shape[0] - self.height) // 2
x0 = (shape[1] - self.width) // 2
if len(image.shape) == 2:
image = np.array([image])
image = np.repeat(image, 3, axis=0)
image = image.transpose(1, 2, 0)
image = image[y0:y0+self.height, x0:x0+self.width, :]
image = ((image - self.mean_value)/self.std_value).astype(np.float32)
image = image.transpose(2, 0, 1)
return image
def __iter__(self):
return self._generate_dataloader()
def _generate_dataloader(self):
sampler = iter(range(0, len(self.image_list), 1))
def collate(batch):
"""Puts each data field into a pd frame with outer dimension batch size"""
elem = batch[0]
if isinstance(elem, collections.abc.Mapping):
return {key: collate([d[key] for d in batch]) for key in elem}
elif isinstance(elem, collections.abc.Sequence):
batch = zip(*batch)
return [collate(samples) for samples in batch]
elif isinstance(elem, np.ndarray):
try:
return np.stack(batch)
except:
return batch
else:
return batch
def batch_sampler():
batch = []
for idx in sampler:
batch.append(idx)
if len(batch) == self.batch_size:
yield batch
batch = []
if len(batch) > 0:
yield batch
def fetcher(ids):
data = [self._preprpcess(self.image_list[idx]) for idx in ids]
label = [self.label_list[idx] for idx in ids]
return collate(data), label
for batched_indices in batch_sampler():
try:
data = fetcher(batched_indices)
yield data
except StopIteration:
return
def eval_func(model, dataloader, metric):
metric.reset()
sess = ort.InferenceSession(model.SerializeToString(), providers=ort.get_available_providers())
input_names = [i.name for i in sess.get_inputs()]
for input_data, label in dataloader:
output = sess.run(None, dict(zip(input_names, [input_data])))
metric.update(output, label)
return metric.result()
if __name__ == "__main__":
logger.info("Evaluating ONNXRuntime full precision accuracy and performance:")
parser = argparse.ArgumentParser(
description="VGG16 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"
)
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='default',
choices=['default', 'QDQ', 'QOperator'],
help="quantization format"
)
parser.add_argument(
"--batch_size",
default=1,
type=int,
)
args = parser.parse_args()
model = onnx.load(args.model_path)
dataloader = Dataloader(args.dataset_location, args.label_path, args.batch_size)
top1 = TopK()
def eval(onnx_model):
return eval_func(onnx_model, dataloader, top1)
if args.benchmark:
if args.mode == 'performance':
from neural_compressor.benchmark import fit
from neural_compressor.config import BenchmarkConfig
conf = BenchmarkConfig(warmup=10, iteration=1000, cores_per_instance=4, num_of_instance=1)
fit(model, conf, b_dataloader=dataloader)
elif args.mode == 'accuracy':
acc_result = eval(model)
print("Batch size = %d" % dataloader.batch_size)
print("Accuracy: %.5f" % acc_result)
if args.tune:
from neural_compressor import quantization, PostTrainingQuantConfig
config = PostTrainingQuantConfig(quant_format=args.quant_format)
q_model = quantization.fit(model, config, calib_dataloader=dataloader,
eval_func=eval)
q_model.save(args.output_model)