Skip to content

Commit

Permalink
Merge pull request PaddlePaddle#86 from wangye707/wy_vgg
Browse files Browse the repository at this point in the history
添加vgg11模型
  • Loading branch information
wangye707 authored Aug 9, 2021
2 parents a81c3c7 + 2dfae1d commit 020b8c3
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 20 deletions.
30 changes: 15 additions & 15 deletions inference/python_api_test/test_case/infer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def load_config(self, **kwargs):
else:
raise Exception(f"model file path is not exist, [{model_path}] or [{model_file}] invalid!")

def get_truth_val(self, input_data_dict: dict, device: str) -> dict:
def get_truth_val(self, input_data_dict: dict, device: str, gpu_mem=1000) -> dict:
"""
get truth value calculated by target device kernel
Args:
Expand All @@ -72,7 +72,7 @@ def get_truth_val(self, input_data_dict: dict, device: str) -> dict:
if device == "cpu":
self.pd_config.disable_gpu()
elif device == "gpu":
self.pd_config.enable_use_gpu(1000, 0)
self.pd_config.enable_use_gpu(gpu_mem, 0)
else:
raise Exception(f"{device} not support in current test codes")
self.pd_config.switch_ir_optim(False)
Expand Down Expand Up @@ -194,7 +194,7 @@ def mkldnn_test(self, input_data_dict: dict, output_data_dict: dict, mkldnn_cach
abs(out_data - output_data_truth_val[j]) <= delta
), f"{out_data} - {output_data_truth_val[j]} > {delta}"

def trt_fp32_bz1_test(self, input_data_dict: dict, output_data_dict: dict, repeat=5, delta=1e-5):
def trt_fp32_bz1_test(self, input_data_dict: dict, output_data_dict: dict, repeat=5, delta=1e-5, gpu_mem=1000):
"""
test enable_tensorrt_engine()
batch_size = 1
Expand All @@ -208,7 +208,7 @@ def trt_fp32_bz1_test(self, input_data_dict: dict, output_data_dict: dict, repea
Returns:
None
"""
self.pd_config.enable_use_gpu(1000, 0)
self.pd_config.enable_use_gpu(gpu_mem, 0)
self.pd_config.enable_tensorrt_engine(
workspace_size=1 << 30,
max_batch_size=1,
Expand Down Expand Up @@ -238,7 +238,7 @@ def trt_fp32_bz1_test(self, input_data_dict: dict, output_data_dict: dict, repea
abs(out_data - output_data_truth_val[j]) <= delta
), f"{out_data} - {output_data_truth_val[j]} > {delta}"

def trt_fp32_more_bz_test(self, input_data_dict: dict, output_data_dict: dict, repeat=5, delta=1e-5):
def trt_fp32_more_bz_test(self, input_data_dict: dict, output_data_dict: dict, repeat=1, delta=1e-5, gpu_mem=1000):
"""
test enable_tensorrt_engine()
batch_size = 10
Expand All @@ -252,7 +252,7 @@ def trt_fp32_more_bz_test(self, input_data_dict: dict, output_data_dict: dict, r
Returns:
None
"""
self.pd_config.enable_use_gpu(1000, 0)
self.pd_config.enable_use_gpu(gpu_mem, 0)
self.pd_config.enable_tensorrt_engine(
workspace_size=1 << 30,
max_batch_size=10,
Expand Down Expand Up @@ -283,7 +283,7 @@ def trt_fp32_more_bz_test(self, input_data_dict: dict, output_data_dict: dict, r
), f"{out_data} - {output_data_truth_val[j]} > {delta}"

def trt_fp32_bz1_multi_thread_test(
self, input_data_dict: dict, output_data_dict: dict, repeat=2, thread_num=5, delta=1e-5
self, input_data_dict: dict, output_data_dict: dict, repeat=1, thread_num=2, delta=1e-5, gpu_mem=1000
):
"""
test enable_tensorrt_engine()
Expand All @@ -301,7 +301,7 @@ def trt_fp32_bz1_multi_thread_test(
Returns:
None
"""
self.pd_config.enable_use_gpu(1000, 0)
self.pd_config.enable_use_gpu(gpu_mem, 0)
self.pd_config.enable_tensorrt_engine(
workspace_size=1 << 30,
max_batch_size=1,
Expand All @@ -320,7 +320,7 @@ def trt_fp32_bz1_multi_thread_test(
record_thread.start()
record_thread.join()

def trt_fp16_bz1_test(self, input_data_dict: dict, output_data_dict: dict, repeat=5, delta=1e-5):
def trt_fp16_bz1_test(self, input_data_dict: dict, output_data_dict: dict, repeat=5, delta=1e-5, gpu_mem=1000):
"""
test enable_tensorrt_engine()
batch_size = 1
Expand All @@ -334,7 +334,7 @@ def trt_fp16_bz1_test(self, input_data_dict: dict, output_data_dict: dict, repea
Returns:
None
"""
self.pd_config.enable_use_gpu(1000, 0)
self.pd_config.enable_use_gpu(gpu_mem, 0)
self.pd_config.enable_tensorrt_engine(
workspace_size=1 << 30,
max_batch_size=1,
Expand Down Expand Up @@ -364,7 +364,7 @@ def trt_fp16_bz1_test(self, input_data_dict: dict, output_data_dict: dict, repea
abs(out_data - output_data_truth_val[j]) <= delta
), f"{out_data} - {output_data_truth_val[j]} > {delta}"

def trt_fp16_more_bz_test(self, input_data_dict: dict, output_data_dict: dict, repeat=5, delta=1e-5):
def trt_fp16_more_bz_test(self, input_data_dict: dict, output_data_dict: dict, repeat=5, delta=1e-5, gpu_mem=1000):
"""
test enable_tensorrt_engine()
batch_size = 10
Expand All @@ -378,7 +378,7 @@ def trt_fp16_more_bz_test(self, input_data_dict: dict, output_data_dict: dict, r
Returns:
None
"""
self.pd_config.enable_use_gpu(1000, 0)
self.pd_config.enable_use_gpu(gpu_mem, 0)
self.pd_config.enable_tensorrt_engine(
workspace_size=1 << 30,
max_batch_size=10,
Expand Down Expand Up @@ -409,7 +409,7 @@ def trt_fp16_more_bz_test(self, input_data_dict: dict, output_data_dict: dict, r
), f"{out_data} - {output_data_truth_val[j]} > {delta}"

def trt_fp16_bz1_multi_thread_test(
self, input_data_dict: dict, output_data_dict: dict, repeat=2, thread_num=5, delta=1e-5
self, input_data_dict: dict, output_data_dict: dict, repeat=1, thread_num=2, delta=1e-5, gpu_mem=1000
):
"""
test enable_tensorrt_engine()
Expand All @@ -427,7 +427,7 @@ def trt_fp16_bz1_multi_thread_test(
Returns:
None
"""
self.pd_config.enable_use_gpu(1000, 0)
self.pd_config.enable_use_gpu(gpu_mem, 0)
self.pd_config.enable_tensorrt_engine(
workspace_size=1 << 30,
max_batch_size=1,
Expand All @@ -447,7 +447,7 @@ def trt_fp16_bz1_multi_thread_test(
record_thread.join()

def run_multi_thread_test_predictor(
self, predictor, input_data_dict: dict, output_data_dict: dict, repeat=5, delta=1e-5
self, predictor, input_data_dict: dict, output_data_dict: dict, repeat=1, delta=1e-5
):
"""
test paddle predictor in multithreaded task
Expand Down
2 changes: 1 addition & 1 deletion inference/python_api_test/test_class_model/run.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export FLAGS_call_stack_level=2
cases=`find . -name "test*.py" | sort`
ignore=""
ignore="test_vgg11.py"
bug=0

echo "============ failed cases =============" >> result.txt
Expand Down
8 changes: 4 additions & 4 deletions inference/python_api_test/test_class_model/test_resnet50.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ def test_trtfp32_more_bz():

file_path = "./resnet50"
images_size = 224
batch_size_pool = 10
for batch_size in range(1, batch_size_pool + 1):
batch_size_pool = [1, 5, 10]
for batch_size in batch_size_pool:
test_suite = InferenceTest()
test_suite.load_config(model_file="./resnet50/inference.pdmodel", params_file="./resnet50/inference.pdiparams")
images_list, npy_list = test_suite.get_images_npy(file_path, images_size)
Expand Down Expand Up @@ -178,8 +178,8 @@ def test_trtfp16_more_bz():

file_path = "./resnet50"
images_size = 224
batch_size_pool = 10
for batch_size in range(1, batch_size_pool + 1):
batch_size_pool = [1, 5, 10]
for batch_size in batch_size_pool:
test_suite = InferenceTest()
test_suite.load_config(model_file="./resnet50/inference.pdmodel", params_file="./resnet50/inference.pdiparams")
images_list, npy_list = test_suite.get_images_npy(file_path, images_size)
Expand Down
197 changes: 197 additions & 0 deletions inference/python_api_test/test_class_model/test_vgg11.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
# -*- coding: utf-8 -*-
# encoding=utf-8 vi:ts=4:sw=4:expandtab:ft=python
"""
test vgg11 model
"""

import os
import sys
import logging
import tarfile
import six
import wget
import pytest
import numpy as np

# pylint: disable=wrong-import-position
sys.path.append("..")
from test_case import InferenceTest

# pylint: enable=wrong-import-position


def check_model_exist():
"""
check model exist
"""
vgg11_url = "https://paddle-qa.bj.bcebos.com/inference_model/2.1/class/vgg11.tgz"
if not os.path.exists("./vgg11/inference.pdiparams"):
wget.download(vgg11_url, out="./")
tar = tarfile.open("vgg11.tgz")
tar.extractall()
tar.close()


@pytest.mark.p0
@pytest.mark.config_init_combined_model
def test_config():
"""
test combined model config
"""
check_model_exist()
test_suite = InferenceTest()
test_suite.load_config(model_file="./vgg11/inference.pdmodel", params_file="./vgg11/inference.pdiparams")
test_suite.config_test()


@pytest.mark.p0
@pytest.mark.config_disablegpu_memory
def test_disable_gpu():
"""
test no gpu resources occupied after disable gpu
"""
check_model_exist()
test_suite = InferenceTest()
test_suite.load_config(model_file="./vgg11/inference.pdmodel", params_file="./vgg11/inference.pdiparams")
batch_size = 1
fake_input = np.random.randn(batch_size, 3, 224, 224).astype("float32")
print(fake_input.shape)
input_data_dict = {"x": fake_input}
test_suite.disable_gpu_test(input_data_dict)


@pytest.mark.p1
@pytest.mark.trt_fp32_more_bz_precision
def test_trtfp32_more_bz():
"""
compared trt fp32 batch_size=1-10 vgg11 outputs with true val
"""
check_model_exist()

file_path = "./vgg11"
images_size = 224
batch_size_pool = [1, 10]
for batch_size in batch_size_pool:
test_suite = InferenceTest()
test_suite.load_config(model_file="./vgg11/inference.pdmodel", params_file="./vgg11/inference.pdiparams")
images_list, npy_list = test_suite.get_images_npy(file_path, images_size)
fake_input = np.array(images_list[0:batch_size]).astype("float32")
input_data_dict = {"x": fake_input}
output_data_dict = test_suite.get_truth_val(input_data_dict, device="gpu")

del test_suite # destroy class to save memory

test_suite2 = InferenceTest()
test_suite2.load_config(model_file="./vgg11/inference.pdmodel", params_file="./vgg11/inference.pdiparams")
test_suite2.trt_fp32_more_bz_test(input_data_dict, output_data_dict)

del test_suite2 # destroy class to save memory


@pytest.mark.p1
@pytest.mark.trt_fp32_multi_thread_bz1_precision
def test_trtfp32_bz1_multi_thread():
"""
compared trt fp32 batch_size=1 vgg11 multi_thread outputs with true val
"""
check_model_exist()

file_path = "./vgg11"
images_size = 224
batch_size = 1
test_suite = InferenceTest()
test_suite.load_config(model_file="./vgg11/inference.pdmodel", params_file="./vgg11/inference.pdiparams")
images_list, npy_list = test_suite.get_images_npy(file_path, images_size)
fake_input = np.array(images_list[0:batch_size]).astype("float32")
input_data_dict = {"x": fake_input}
output_data_dict = test_suite.get_truth_val(input_data_dict, device="gpu")

del test_suite # destroy class to save memory

test_suite2 = InferenceTest()
test_suite2.load_config(model_file="./vgg11/inference.pdmodel", params_file="./vgg11/inference.pdiparams")
test_suite2.trt_fp32_bz1_multi_thread_test(input_data_dict, output_data_dict)

del test_suite2 # destroy class to save memory


@pytest.mark.p1
@pytest.mark.trt_fp16_more_bz_precision
def test_trtfp16_more_bz():
"""
compared trt fp16 batch_size=1-10 vgg11 outputs with true val
"""
check_model_exist()

file_path = "./vgg11"
images_size = 224
batch_size_pool = [1, 10]
for batch_size in batch_size_pool:
test_suite = InferenceTest()
test_suite.load_config(model_file="./vgg11/inference.pdmodel", params_file="./vgg11/inference.pdiparams")
images_list, npy_list = test_suite.get_images_npy(file_path, images_size)
fake_input = np.array(images_list[0:batch_size]).astype("float32")
input_data_dict = {"x": fake_input}
output_data_dict = test_suite.get_truth_val(input_data_dict, device="gpu")

del test_suite # destroy class to save memory

test_suite2 = InferenceTest()
test_suite2.load_config(model_file="./vgg11/inference.pdmodel", params_file="./vgg11/inference.pdiparams")
test_suite2.trt_fp16_more_bz_test(input_data_dict, output_data_dict, repeat=1, delta=1e-3, gpu_mem=3000)

del test_suite2 # destroy class to save memory


@pytest.mark.p1
@pytest.mark.trt_fp16_multi_thread_bz1_precision
def test_trtfp16_bz1_multi_thread():
"""
compared trt fp16 batch_size=1 vgg11 multi_thread outputs with true val
"""
check_model_exist()

file_path = "./vgg11"
images_size = 224
batch_size = 1
test_suite = InferenceTest()
test_suite.load_config(model_file="./vgg11/inference.pdmodel", params_file="./vgg11/inference.pdiparams")
images_list, npy_list = test_suite.get_images_npy(file_path, images_size)
fake_input = np.array(images_list[0:batch_size]).astype("float32")
input_data_dict = {"x": fake_input}
output_data_dict = test_suite.get_truth_val(input_data_dict, device="gpu")

del test_suite # destroy class to save memory

test_suite2 = InferenceTest()
test_suite2.load_config(model_file="./vgg11/inference.pdmodel", params_file="./vgg11/inference.pdiparams")
test_suite2.trt_fp16_bz1_multi_thread_test(input_data_dict, output_data_dict, gpu_mem=3000)

del test_suite2 # destroy class to save memory


@pytest.mark.p1
@pytest.mark.mkldnn_bz1_precision
def test_mkldnn():
"""
compared mkldnn vgg11 outputs with true val
"""
check_model_exist()

file_path = "./vgg11"
images_size = 224
batch_size = 1
test_suite = InferenceTest()
test_suite.load_config(model_file="./vgg11/inference.pdmodel", params_file="./vgg11/inference.pdiparams")
images_list, npy_list = test_suite.get_images_npy(file_path, images_size)
fake_input = np.array(images_list[0:batch_size]).astype("float32")
input_data_dict = {"x": fake_input}
output_data_dict = test_suite.get_truth_val(input_data_dict, device="gpu")

del test_suite # destroy class to save memory

test_suite2 = InferenceTest()
test_suite2.load_config(model_file="./vgg11/inference.pdmodel", params_file="./vgg11/inference.pdiparams")
test_suite2.mkldnn_test(input_data_dict, output_data_dict)

del test_suite2 # destroy class to save memory

0 comments on commit 020b8c3

Please sign in to comment.