From 8386c609f410da4e95774ee0675e87bbf0c7823a Mon Sep 17 00:00:00 2001 From: Guanghua Yu <742925032@qq.com> Date: Thu, 22 Dec 2022 17:54:40 +0800 Subject: [PATCH] fix unittest in post training quantization (#49256) --- ..._post_training_quantization_mobilenetv1.py | 50 ++++++++++++------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/python/paddle/fluid/contrib/slim/tests/test_post_training_quantization_mobilenetv1.py b/python/paddle/fluid/contrib/slim/tests/test_post_training_quantization_mobilenetv1.py index 37daeab118..471798dec2 100644 --- a/python/paddle/fluid/contrib/slim/tests/test_post_training_quantization_mobilenetv1.py +++ b/python/paddle/fluid/contrib/slim/tests/test_post_training_quantization_mobilenetv1.py @@ -209,7 +209,12 @@ class TestPostTrainingQuantization(unittest.TestCase): infer_program, feed_dict, fetch_targets, - ] = fluid.io.load_inference_model(model_path, exe) + ] = fluid.io.load_inference_model( + model_path, + exe, + model_filename="inference.pdmodel", + params_filename="inference.pdiparams", + ) val_reader = paddle.batch(val(), batch_size) iterations = infer_iterations @@ -224,16 +229,21 @@ class TestPostTrainingQuantization(unittest.TestCase): label = label.reshape([-1, 1]) t1 = time.time() - _, acc1, _ = exe.run( + pred = exe.run( infer_program, - feed={feed_dict[0]: image, feed_dict[1]: label}, + feed={feed_dict[0]: image}, fetch_list=fetch_targets, ) t2 = time.time() period = t2 - t1 periods.append(period) - test_info.append(np.mean(acc1) * len(data)) + pred = np.array(pred[0]) + sort_array = pred.argsort(axis=1) + top_1_pred = sort_array[:, -1:][:, ::-1] + top_1 = np.mean(label == top_1_pred) + + test_info.append(np.mean(top_1) * len(data)) cnt += len(data) if (batch_id + 1) % 100 == 0: @@ -277,6 +287,8 @@ class TestPostTrainingQuantization(unittest.TestCase): executor=exe, sample_generator=val_reader, model_dir=model_path, + model_filename="inference.pdmodel", + params_filename="inference.pdiparams", batch_size=batch_size, batch_nums=batch_nums, algo=algo, @@ -288,7 +300,11 @@ class TestPostTrainingQuantization(unittest.TestCase): is_use_cache_file=is_use_cache_file, ) ptq.quantize() - ptq.save_quantized_model(self.int8_model) + ptq.save_quantized_model( + self.int8_model, + model_filename="inference.pdmodel", + params_filename="inference.pdiparams", + ) def run_test( self, @@ -316,7 +332,7 @@ class TestPostTrainingQuantization(unittest.TestCase): ) ) (fp32_throughput, fp32_latency, fp32_acc1) = self.run_program( - os.path.join(model_cache_folder, "model"), + os.path.join(model_cache_folder, "MobileNetV1_infer"), batch_size, infer_iterations, ) @@ -327,7 +343,7 @@ class TestPostTrainingQuantization(unittest.TestCase): ) ) self.generate_quantized_model( - os.path.join(model_cache_folder, "model"), + os.path.join(model_cache_folder, "MobileNetV1_infer"), quantizable_op_type, batch_size, algo, @@ -371,9 +387,9 @@ class TestPostTrainingKLForMobilenetv1(TestPostTrainingQuantization): algo = "KL" round_type = "round" data_urls = [ - 'http://paddle-inference-dist.bj.bcebos.com/int8/mobilenetv1_int8_model.tar.gz' + 'https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/inference/MobileNetV1_infer.tar' ] - data_md5s = ['13892b0716d26443a8cdea15b3c6438b'] + data_md5s = ['5ee2b1775b11dc233079236cdc216c2e'] quantizable_op_type = [ "conv2d", "depthwise_conv2d", @@ -405,9 +421,9 @@ class TestPostTrainingavgForMobilenetv1(TestPostTrainingQuantization): algo = "avg" round_type = "round" data_urls = [ - 'http://paddle-inference-dist.bj.bcebos.com/int8/mobilenetv1_int8_model.tar.gz' + 'https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/inference/MobileNetV1_infer.tar' ] - data_md5s = ['13892b0716d26443a8cdea15b3c6438b'] + data_md5s = ['5ee2b1775b11dc233079236cdc216c2e'] quantizable_op_type = [ "conv2d", "depthwise_conv2d", @@ -437,9 +453,9 @@ class TestPostTraininghistForMobilenetv1(TestPostTrainingQuantization): algo = "hist" round_type = "round" data_urls = [ - 'http://paddle-inference-dist.bj.bcebos.com/int8/mobilenetv1_int8_model.tar.gz' + 'https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/inference/MobileNetV1_infer.tar' ] - data_md5s = ['13892b0716d26443a8cdea15b3c6438b'] + data_md5s = ['5ee2b1775b11dc233079236cdc216c2e'] quantizable_op_type = [ "conv2d", "depthwise_conv2d", @@ -471,9 +487,9 @@ class TestPostTrainingAbsMaxForMobilenetv1(TestPostTrainingQuantization): algo = "abs_max" round_type = "round" data_urls = [ - 'http://paddle-inference-dist.bj.bcebos.com/int8/mobilenetv1_int8_model.tar.gz' + 'https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/inference/MobileNetV1_infer.tar' ] - data_md5s = ['13892b0716d26443a8cdea15b3c6438b'] + data_md5s = ['5ee2b1775b11dc233079236cdc216c2e'] quantizable_op_type = [ "conv2d", "mul", @@ -503,9 +519,9 @@ class TestPostTrainingAvgONNXFormatForMobilenetv1(TestPostTrainingQuantization): algo = "emd" round_type = "round" data_urls = [ - 'http://paddle-inference-dist.bj.bcebos.com/int8/mobilenetv1_int8_model.tar.gz' + 'https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/inference/MobileNetV1_infer.tar' ] - data_md5s = ['13892b0716d26443a8cdea15b3c6438b'] + data_md5s = ['5ee2b1775b11dc233079236cdc216c2e'] quantizable_op_type = [ "conv2d", "depthwise_conv2d", -- GitLab