ciyongch commented on a change in pull request #19587: URL: https://github.com/apache/incubator-mxnet/pull/19587#discussion_r536528388
########## File path: example/quantization/imagenet_inference.py ########## @@ -0,0 +1,180 @@ +# 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 time + +import mxnet as mx +import numpy as np +from mxnet import gluon +from mxnet.gluon.data import DataLoader +from mxnet.gluon.data.vision import transforms + + +def download_dataset(dataset_url, dataset_dir, logger=None): + if logger is not None: + logger.info('Downloading dataset for inference from %s to %s' % (dataset_url, dataset_dir)) + mx.test_utils.download(dataset_url, dataset_dir) + + +def score(symblock, data, ctx, max_num_examples, skip_num_batches, logger=None): + metrics = [gluon.metric.create('acc')]#, + # gluon.metric.create('top_k_accuracy', top_k=5)] + + # make sure that fp32 inference works on the same images as calibrated quantized model + logger.info('Skipping the first %d batches' % skip_num_batches) + + tic = time.time() + num = 0 + for i, input_data in enumerate(data): + if i < skip_num_batches: + continue + x = input_data[0].as_in_context(ctx) + label = input_data[1].as_in_context(ctx) + outputs = symblock.forward(x) + for m in metrics: + m.update(label, outputs) + num += batch_size + if max_num_examples is not None and num >= max_num_examples: + break + + speed = num / (time.time() - tic) + + if logger is not None: + logger.info('Finished inference with %d images' % num) + logger.info('Finished with %f images per second', speed) + for m in metrics: + logger.info(m.get()) + +def initialize_block_params(block, initializer): + for _, param in block.collect_params('.*gamma|.*moving_var|.*running_var').items(): + param.initialize(mx.init.Constant(1)) + for _, param in block.collect_params('.*beta|.*moving_mean|.*running_mean|.*bias').items(): + param.initialize(mx.init.Constant(0)) + for _, param in block.collect_params('.*weight').items(): + param.initialize(initializer) + +def benchmark_score(symblock, ctx, batch_size, warmup_batches, num_batches, data_layer_type): + if data_layer_type == "int8": + dshape = mx.io.DataDesc(name='data', shape=( + batch_size,) + data_shape, dtype=np.int8) + elif data_layer_type == 'uint8': + dshape = mx.io.DataDesc(name='data', shape=( + batch_size,) + data_shape, dtype=np.uint8) + else: # float32 + dshape = mx.io.DataDesc(name='data', shape=( + batch_size,) + data_shape, dtype=np.float32) + + # get data + if data_layer_type == "float32": + data = [mx.random.uniform(-1.0, 1.0, shape=shape, ctx=ctx, dtype=data_layer_type) + for _, shape in [dshape]] + else: + data = [mx.nd.full(shape=shape, val=127, ctx=ctx, dtype=data_layer_type) + for _, shape in [dshape]] + + # run + for i in range(warmup_batches+num_batches): + if i == warmup_batches: + tic = time.time() + outputs = symblock.forward(*data) + for output in outputs: + output.wait_to_read() + + # return num images per second + return num_batches * batch_size / (time.time() - tic) + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description='Score a model on a dataset') + parser.add_argument('--benchmark', type=bool, default=False, help='dummy data benchmark') + parser.add_argument('--symbol-file', type=str, required=True, help='symbol file path') + parser.add_argument('--param-file', type=str, required=False, help='param file path') + parser.add_argument('--batch-size', type=int, default=32) + parser.add_argument('--dataset', type=str, required=False, help='dataset path') + parser.add_argument('--rgb-mean', type=str, default='0,0,0') + parser.add_argument('--rgb-std', type=str, default='1,1,1') + parser.add_argument('--image-shape', type=str, default='3,224,224') + parser.add_argument('--data-nthreads', type=int, default=60, help='number of threads for data decoding') + parser.add_argument('--num-skipped-batches', type=int, default=0, help='skip the number of batches for inference') + parser.add_argument('--num-inference-batches', type=int, required=True, help='number of images used for inference') + parser.add_argument('--num-warmup-batches', type=int, default=5, help='number of warmup batches used for benchmark') + parser.add_argument('--shuffle-dataset', action='store_true', default=True, + help='shuffle the score dataset') + parser.add_argument('--data-layer-type', type=str, default='float32', + choices=['float32', 'int8', 'uint8'], + help='data type for data layer (only with --benchmark)') + + args = parser.parse_args() + + ctx = mx.cpu(0) Review comment: Can we restore the option for choosing different `ctx` here and leave an error msg for those not supported ones (like gpu, etc..)? BTW, previously this script also supports low precision like `bf16` and `fp16` data types, although they're not related to INT8/quantization, just wondering if there's any limitation to support amp in current master code base? ---------------------------------------------------------------- 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]
