@@ -21,6 +21,14 @@ def parse_input_args():
21
21
help = 'Perform fp16 quantizaton in addition to int8' ,
22
22
)
23
23
24
+ parser .add_argument (
25
+ "--fp32" ,
26
+ action = "store_true" ,
27
+ required = False ,
28
+ default = False ,
29
+ help = 'Perform no quantization' ,
30
+ )
31
+
24
32
parser .add_argument (
25
33
"--image_dir" ,
26
34
required = False ,
@@ -56,10 +64,10 @@ def __init__(self,
56
64
'''
57
65
:param image_folder: image dataset folder
58
66
:param width: image width
59
- :param height: image height
67
+ :param height: image height
60
68
:param start_index: start index of images
61
69
:param end_index: end index of images
62
- :param stride: image size of each data get
70
+ :param stride: image size of each data get
63
71
:param batch_size: batch size of inference
64
72
:param model_path: model name and path
65
73
:param input_name: model input name
@@ -153,12 +161,14 @@ def preprocess_imagenet(self, images_folder, height, width, start_index=0, size_
153
161
parameter images_folder: path to folder storing images
154
162
parameter height: image height in pixels
155
163
parameter width: image width in pixels
156
- parameter start_index: image index to start with
164
+ parameter start_index: image index to start with
157
165
parameter size_limit: number of images to load. Default is 0 which means all images are picked.
158
166
return: list of matrices characterizing multiple images
159
167
'''
160
168
def preprocess_images (input , channels = 3 , height = 224 , width = 224 ):
161
169
image = input .resize ((width , height ), Image .Resampling .LANCZOS )
170
+ if image .mode in ["CMYK" , "RGBA" ]:
171
+ image = image .convert ("RGB" )
162
172
input_data = np .asarray (image ).astype (np .float32 )
163
173
if len (input_data .shape ) != 2 :
164
174
input_data = input_data .transpose ([2 , 0 , 1 ])
@@ -249,7 +259,7 @@ def __init__(self,
249
259
providers = ["MIGraphXExecutionProvider" ]):
250
260
'''
251
261
:param model_path: ONNX model to validate
252
- :param synset_id: ILSVRC2012 synset id
262
+ :param synset_id: ILSVRC2012 synset id
253
263
:param data_reader: user implemented object to read in and preprocess calibration dataset
254
264
based on CalibrationDataReader Interface
255
265
:param providers: ORT execution provider type
@@ -281,9 +291,8 @@ def predict(self):
281
291
self .prediction_result_list = inference_outputs_list
282
292
283
293
def top_k_accuracy (self , truth , prediction , k = 1 ):
284
- '''From https://github.com/chainer/chainer/issues/606
294
+ '''From https://github.com/chainer/chainer/issues/606
285
295
'''
286
-
287
296
y = np .argsort (prediction )[:, - k :]
288
297
return np .any (y .T == truth .argmax (axis = 1 ), axis = 0 ).mean ()
289
298
@@ -293,7 +302,7 @@ def evaluate(self, prediction_results):
293
302
y_prediction = np .empty ((total_val_images , 1000 ), dtype = np .float32 )
294
303
i = 0
295
304
for res in prediction_results :
296
- y_prediction [i :i + batch_size , :] = res [0 ]
305
+ y_prediction [i :i + res [ 0 ]. shape [ 0 ] , :] = res [0 ]
297
306
i = i + batch_size
298
307
print ("top 1: " , self .top_k_accuracy (self .synset_id , y_prediction , k = 1 ))
299
308
print ("top 5: " , self .top_k_accuracy (self .synset_id , y_prediction , k = 5 ))
@@ -344,8 +353,9 @@ def get_dataset_size(dataset_path, calibration_dataset_size):
344
353
2. Download ILSVRC2012 validation dataset and development kit from http://www.image-net.org/challenges/LSVRC/2012/downloads.
345
354
3. Extract validation dataset JPEG files to 'ILSVRC2012/val'.
346
355
4. Extract development kit to 'ILSVRC2012/devkit'. Two files in the development kit are used, 'ILSVRC2012_validation_ground_truth.txt' and 'meta.mat'.
356
+ These are also available to download at https://github.com/miraclewkf/MobileNetV2-PyTorch/tree/master/ImageNet/ILSVRC2012_devkit_t12/data
347
357
5. Download 'synset_words.txt' from https://github.com/HoldenCaulfieldRye/caffe/blob/master/data/ilsvrc12/synset_words.txt into 'ILSVRC2012/'.
348
-
358
+
349
359
Please download Resnet50 model from ONNX model zoo https://github.com/onnx/models/blob/master/vision/classification/resnet/model/resnet50-v2-7.tar.gz
350
360
Untar the model into the workspace
351
361
'''
@@ -356,15 +366,18 @@ def get_dataset_size(dataset_path, calibration_dataset_size):
356
366
ilsvrc2012_dataset_path = flags .image_dir
357
367
augmented_model_path = "./augmented_model.onnx"
358
368
batch_size = flags .batch
359
- calibration_dataset_size = flags .cal_size # Size of dataset for calibration
369
+ calibration_dataset_size = 0 if flags .fp32 else flags .cal_size # Size of dataset for calibration
370
+
371
+ calibration_table_generation_enable = False
372
+ if not flags .fp32 :
373
+ # INT8 calibration setting
374
+ calibration_table_generation_enable = True # Enable/Disable INT8 calibration
360
375
361
- # INT8 calibration setting
362
- calibration_table_generation_enable = True # Enable/Disable INT8 calibration
376
+ # MIGraphX EP INT8 settings
377
+ os .environ ["ORT_MIGRAPHX_INT8_ENABLE" ] = "1" # Enable INT8 precision
378
+ os .environ ["ORT_MIGRAPHX_INT8_CALIBRATION_TABLE_NAME" ] = "calibration.flatbuffers" # Calibration table name
379
+ os .environ ["ORT_MIGRAPHX_INT8_NATIVE_CALIBRATION_TABLE" ] = "0" # Calibration table name
363
380
364
- # MIGraphX EP INT8 settings
365
- os .environ ["ORT_MIGRAPHX_INT8_ENABLE" ] = "1" # Enable INT8 precision
366
- os .environ ["ORT_MIGRAPHX_INT8_CALIBRATION_TABLE_NAME" ] = "calibration.flatbuffers" # Calibration table name
367
- os .environ ["ORT_MIGRAPHX_INT8_NATIVE_CALIBRATION_TABLE" ] = "0" # Calibration table name
368
381
execution_provider = ["MIGraphXExecutionProvider" ]
369
382
370
383
# Convert static batch to dynamic batch
@@ -378,7 +391,7 @@ def get_dataset_size(dataset_path, calibration_dataset_size):
378
391
if calibration_table_generation_enable :
379
392
print ("Generating Calibration Table" )
380
393
calibrator = create_calibrator (new_model_path , [], augmented_model_path = augmented_model_path )
381
- calibrator .set_execution_providers (["ROCMExecutionProvider" ])
394
+ calibrator .set_execution_providers (["ROCMExecutionProvider" ])
382
395
data_reader = ImageNetDataReader (ilsvrc2012_dataset_path ,
383
396
start_index = 0 ,
384
397
end_index = calibration_dataset_size ,
@@ -391,7 +404,7 @@ def get_dataset_size(dataset_path, calibration_dataset_size):
391
404
392
405
serial_cal_tensors = {}
393
406
for keys , values in cal_tensors .data .items ():
394
- serial_cal_tensors [keys ] = values .range_value
407
+ serial_cal_tensors [keys ] = [ float ( x [ 0 ]) for x in values .range_value ]
395
408
396
409
print ("Writing calibration table" )
397
410
write_calibration_table (serial_cal_tensors )
0 commit comments