未验证 提交 8386c609 编写于 作者: G Guanghua Yu 提交者: GitHub

fix unittest in post training quantization (#49256)

上级 4537ba23
...@@ -209,7 +209,12 @@ class TestPostTrainingQuantization(unittest.TestCase): ...@@ -209,7 +209,12 @@ class TestPostTrainingQuantization(unittest.TestCase):
infer_program, infer_program,
feed_dict, feed_dict,
fetch_targets, 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) val_reader = paddle.batch(val(), batch_size)
iterations = infer_iterations iterations = infer_iterations
...@@ -224,16 +229,21 @@ class TestPostTrainingQuantization(unittest.TestCase): ...@@ -224,16 +229,21 @@ class TestPostTrainingQuantization(unittest.TestCase):
label = label.reshape([-1, 1]) label = label.reshape([-1, 1])
t1 = time.time() t1 = time.time()
_, acc1, _ = exe.run( pred = exe.run(
infer_program, infer_program,
feed={feed_dict[0]: image, feed_dict[1]: label}, feed={feed_dict[0]: image},
fetch_list=fetch_targets, fetch_list=fetch_targets,
) )
t2 = time.time() t2 = time.time()
period = t2 - t1 period = t2 - t1
periods.append(period) 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) cnt += len(data)
if (batch_id + 1) % 100 == 0: if (batch_id + 1) % 100 == 0:
...@@ -277,6 +287,8 @@ class TestPostTrainingQuantization(unittest.TestCase): ...@@ -277,6 +287,8 @@ class TestPostTrainingQuantization(unittest.TestCase):
executor=exe, executor=exe,
sample_generator=val_reader, sample_generator=val_reader,
model_dir=model_path, model_dir=model_path,
model_filename="inference.pdmodel",
params_filename="inference.pdiparams",
batch_size=batch_size, batch_size=batch_size,
batch_nums=batch_nums, batch_nums=batch_nums,
algo=algo, algo=algo,
...@@ -288,7 +300,11 @@ class TestPostTrainingQuantization(unittest.TestCase): ...@@ -288,7 +300,11 @@ class TestPostTrainingQuantization(unittest.TestCase):
is_use_cache_file=is_use_cache_file, is_use_cache_file=is_use_cache_file,
) )
ptq.quantize() 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( def run_test(
self, self,
...@@ -316,7 +332,7 @@ class TestPostTrainingQuantization(unittest.TestCase): ...@@ -316,7 +332,7 @@ class TestPostTrainingQuantization(unittest.TestCase):
) )
) )
(fp32_throughput, fp32_latency, fp32_acc1) = self.run_program( (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, batch_size,
infer_iterations, infer_iterations,
) )
...@@ -327,7 +343,7 @@ class TestPostTrainingQuantization(unittest.TestCase): ...@@ -327,7 +343,7 @@ class TestPostTrainingQuantization(unittest.TestCase):
) )
) )
self.generate_quantized_model( self.generate_quantized_model(
os.path.join(model_cache_folder, "model"), os.path.join(model_cache_folder, "MobileNetV1_infer"),
quantizable_op_type, quantizable_op_type,
batch_size, batch_size,
algo, algo,
...@@ -371,9 +387,9 @@ class TestPostTrainingKLForMobilenetv1(TestPostTrainingQuantization): ...@@ -371,9 +387,9 @@ class TestPostTrainingKLForMobilenetv1(TestPostTrainingQuantization):
algo = "KL" algo = "KL"
round_type = "round" round_type = "round"
data_urls = [ 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 = [ quantizable_op_type = [
"conv2d", "conv2d",
"depthwise_conv2d", "depthwise_conv2d",
...@@ -405,9 +421,9 @@ class TestPostTrainingavgForMobilenetv1(TestPostTrainingQuantization): ...@@ -405,9 +421,9 @@ class TestPostTrainingavgForMobilenetv1(TestPostTrainingQuantization):
algo = "avg" algo = "avg"
round_type = "round" round_type = "round"
data_urls = [ 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 = [ quantizable_op_type = [
"conv2d", "conv2d",
"depthwise_conv2d", "depthwise_conv2d",
...@@ -437,9 +453,9 @@ class TestPostTraininghistForMobilenetv1(TestPostTrainingQuantization): ...@@ -437,9 +453,9 @@ class TestPostTraininghistForMobilenetv1(TestPostTrainingQuantization):
algo = "hist" algo = "hist"
round_type = "round" round_type = "round"
data_urls = [ 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 = [ quantizable_op_type = [
"conv2d", "conv2d",
"depthwise_conv2d", "depthwise_conv2d",
...@@ -471,9 +487,9 @@ class TestPostTrainingAbsMaxForMobilenetv1(TestPostTrainingQuantization): ...@@ -471,9 +487,9 @@ class TestPostTrainingAbsMaxForMobilenetv1(TestPostTrainingQuantization):
algo = "abs_max" algo = "abs_max"
round_type = "round" round_type = "round"
data_urls = [ 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 = [ quantizable_op_type = [
"conv2d", "conv2d",
"mul", "mul",
...@@ -503,9 +519,9 @@ class TestPostTrainingAvgONNXFormatForMobilenetv1(TestPostTrainingQuantization): ...@@ -503,9 +519,9 @@ class TestPostTrainingAvgONNXFormatForMobilenetv1(TestPostTrainingQuantization):
algo = "emd" algo = "emd"
round_type = "round" round_type = "round"
data_urls = [ 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 = [ quantizable_op_type = [
"conv2d", "conv2d",
"depthwise_conv2d", "depthwise_conv2d",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册