bgawrych commented on a change in pull request #19587: URL: https://github.com/apache/incubator-mxnet/pull/19587#discussion_r539946597
########## File path: example/quantization/imagenet_gen_qsym_onednn.py ########## @@ -0,0 +1,274 @@ +# 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. + +import argparse +import logging +import os +import re +import sys +from inspect import currentframe, getframeinfo + +import mxnet as mx +from mxnet import gluon +from mxnet.contrib.quantization import quantize_net +from mxnet.gluon.data import DataLoader +from mxnet.gluon.data.vision import transforms +from mxnet.gluon.model_zoo.vision import get_model + +sys.path.append('../..') +from tools.rec2idx import IndexCreator + + +def download_calib_dataset(dataset_url, calib_dataset, logger=None): + if logger is not None: + logger.info('Downloading calibration dataset from %s to %s' % (dataset_url, calib_dataset)) + mx.test_utils.download(dataset_url, calib_dataset) + +def get_from_gluon(model_name, classes=1000, logger=None): + dir_path = os.path.dirname(os.path.realpath(__file__)) + model_path = os.path.join(dir_path, 'model') + if logger is not None: + logger.info('Converting model from Gluon-CV ModelZoo %s... into path %s' % (model_name, model_path)) + net = get_model(name=model_name, classes=classes, pretrained=True) + prefix = os.path.join(model_path, model_name) + return net, prefix + +def regex_find_excluded_symbols(patterns_dict, model_name): + for key, value in patterns_dict.items(): + if re.search(key, model_name) is not None: + return value + return None + +def get_exclude_symbols(model_name, exclude_first_conv): + # Grouped supported models at the time of commit: + # alexnet + # densenet121, densenet161 + # densenet169, densenet201 + # inceptionv3 + # mobilenet0.25, mobilenet0.5, mobilenet0.75, mobilenet1.0, + # mobilenetv2_0.25, mobilenetv2_0.5, mobilenetv2_0.75, mobilenetv2_1.0 + # resnet101_v1, resnet152_v1, resnet18_v1, resnet34_v1, resnet50_v1 + # resnet101_v2, resnet152_v2, resnet18_v2, resnet34_v2, resnet50_v2 + # squeezenet1.0, squeezenet1.1 + # vgg11, vgg11_bn, vgg13, vgg13_bn, vgg16, vgg16_bn, vgg19, vgg19_bn + exclude_symbol_regex = { + 'mobilenet[^v]': ['mobilenet_hybridsequential0_flatten0_flatten0', 'mobilenet_hybridsequential0_globalavgpool2d0_fwd'], + 'mobilenetv2': ['mobilenetv2_hybridsequential1_flatten0_flatten0'], + # resnetv2_hybridsequential0_hybridsequential0_bottleneckv20_batchnorm0_fwd is excluded for the sake of accuracy + 'resnet.*v2': ['resnetv2_hybridsequential0_flatten0_flatten0', 'resnetv2_hybridsequential0_hybridsequential0_bottleneckv20_batchnorm0_fwd'], + 'squeezenet1': ['squeezenet_hybridsequential1_flatten0_flatten0'], + } + excluded_sym_names = regex_find_excluded_symbols(exclude_symbol_regex, model_name) + if excluded_sym_names is None: + excluded_sym_names = [] + if exclude_first_conv: + first_conv_regex = { + 'alexnet': ['alexnet_hybridsequential0_conv2d0_fwd'], + 'densenet': ['densenet_hybridsequential0_conv2d0_fwd'], + 'inceptionv3': ['inception3_hybridsequential0_hybridsequential0_conv2d0_fwd'], + 'mobilenet[^v]': ['mobilenet_hybridsequential0_conv2d0_fwd'], + 'mobilenetv2': ['mobilenetv2_hybridsequential0_conv2d0_fwd'], + 'resnet.*v1': ['resnetv1_hybridsequential0_conv2d0_fwd'], + 'resnet.*v2': ['resnetv2_hybridsequential0_conv2d0_fwd'], + 'squeezenet1': ['squeezenet_hybridsequential0_conv2d0_fwd'], + 'vgg': ['vgg_hybridsequential0_conv2d0_fwd'], + } + excluded_first_conv_sym_names = regex_find_excluded_symbols(first_conv_regex, model_name) + if excluded_first_conv_sym_names is None: + raise ValueError('Currently, model %s is not supported in this script' % model_name) + excluded_sym_names += excluded_first_conv_sym_names + return excluded_sym_names + + + Review comment: Done ########## File path: example/quantization/README.md ########## @@ -0,0 +1,194 @@ +<!--- 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. --> + +# Model Quantization with Calibration Examples + +This folder contains examples of quantizing a FP32 model with IntelĀ® oneAPI Deep Neural Network Library (oneDNN) to (U)INT8 model. + +<h2 id="0">Contents</h2> + +* [1. Model Quantization with IntelĀ® oneDNN](#1) Review comment: Right, done ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
