Skip to content

Commit

Permalink
Merge pull request PaddlePaddle#49 from jiweibo/2.0_python_api
Browse files Browse the repository at this point in the history
update demo with 2.0 api.
  • Loading branch information
jiweibo authored Dec 3, 2020
2 parents 4dada08 + 3cbb588 commit c2d7812
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 165 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ Paddle Inference为飞桨核心框架推理引擎。Paddle Inference功能特性
1) 在python目录中,我们通过真实输入的方式罗列了一系列的测试样例,其中包括图像的分类,分割,检测,以及NLP的Ernie/Bert等Python使用样例,同时也包含Paddle-TRT, 多线程的使用样例。

2) 在c++目录中,我们通过单测方式展现了一系列的测试样例,其中包括图像的分类,分割,检测,以及NLP的Ernie/Bert等C++使用样例,同时也包含Paddle-TRT, 多线程的使用样例。

注意:如果您使用2.0以前的Paddle,请参考[release/1.8分支](https://github.com/PaddlePaddle/Paddle-Inference-Demo/tree/release/1.8)
2 changes: 1 addition & 1 deletion python/ELMo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### 一:准备环境

请您在环境中安装1.7或以上版本的Paddle,具体的安装方式请参照[飞桨官方页面](https://www.paddlepaddle.org.cn/)的指示方式。
请您在环境中安装2.0或以上版本的Paddle,具体的安装方式请参照[飞桨官方页面](https://www.paddlepaddle.org.cn/)的指示方式。

### 二:下载模型以及测试数据

Expand Down
83 changes: 38 additions & 45 deletions python/ELMo/infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,39 @@
import reader
import sys

from paddle.fluid.core import AnalysisConfig
from paddle.fluid.core import create_paddle_predictor
from paddle.inference import Config
from paddle.inference import create_predictor


def parse_args():
"""
Parsing the input parameters.
"""
parser = argparse.ArgumentParser("Inference for lexical analyzer.")
parser.add_argument(
"--model_dir",
type=str,
default="elmo",
help="The folder where the test data is located.")
parser.add_argument(
"--testdata_dir",
type=str,
default="elmo_data/dev",
help="The folder where the test data is located.")
parser.add_argument(
"--use_gpu",
type=int,
default=False,
help="Whether or not to use GPU. 0-->CPU 1-->GPU")
parser.add_argument(
"--word_dict_path",
type=str,
default="elmo_data/vocabulary_min5k.txt",
help="The path of the word dictionary.")
parser.add_argument(
"--label_dict_path",
type=str,
default="elmo_data/tag.dic",
help="The path of the label dictionary.")
parser.add_argument(
"--word_rep_dict_path",
type=str,
default="elmo_data/q2b.dic",
help="The path of the word replacement Dictionary.")
parser.add_argument("--model_dir",
type=str,
default="elmo",
help="The folder where the test data is located.")
parser.add_argument("--testdata_dir",
type=str,
default="elmo_data/dev",
help="The folder where the test data is located.")
parser.add_argument("--use_gpu",
type=int,
default=False,
help="Whether or not to use GPU. 0-->CPU 1-->GPU")
parser.add_argument("--word_dict_path",
type=str,
default="elmo_data/vocabulary_min5k.txt",
help="The path of the word dictionary.")
parser.add_argument("--label_dict_path",
type=str,
default="elmo_data/tag.dic",
help="The path of the label dictionary.")
parser.add_argument("--word_rep_dict_path",
type=str,
default="elmo_data/q2b.dic",
help="The path of the word replacement Dictionary.")

args = parser.parse_args()
return args
Expand All @@ -65,13 +59,12 @@ def to_lodtensor(data):
return flattened_data, [lod]


def create_predictor(args):
def init_predictor(args):
if args.model_dir is not "":
config = AnalysisConfig(args.model_dir)
config = Config(args.model_dir)
else:
config = AnalysisConfig(args.model_file, args.params_file)
config = Config(args.model_file, args.params_file)

config.switch_use_feed_fetch_ops(False)
config.enable_memory_optim()
if args.use_gpu:
config.enable_use_gpu(1000, 0)
Expand All @@ -80,26 +73,26 @@ def create_predictor(args):
# The thread num should not be greater than the number of cores in the CPU.
config.set_cpu_math_library_num_threads(4)

predictor = create_paddle_predictor(config)
predictor = create_predictor(config)
return predictor


def run(predictor, datas, lods):
input_names = predictor.get_input_names()
for i, name in enumerate(input_names):
input_tensor = predictor.get_input_tensor(name)
input_tensor = predictor.get_input_handle(name)
input_tensor.reshape(datas[i].shape)
input_tensor.copy_from_cpu(datas[i].copy())
input_tensor.set_lod(lods[i])

# do the inference
predictor.zero_copy_run()
predictor.run()

results = []
# get out data from output tensor
output_names = predictor.get_output_names()
for i, name in enumerate(output_names):
output_tensor = predictor.get_output_tensor(name)
output_tensor = predictor.get_output_handle(name)
output_data = output_tensor.copy_to_cpu()
results.append(output_data)
return results
Expand All @@ -114,12 +107,12 @@ def run(predictor, datas, lods):
word_dict_len = max(map(int, word2id_dict.values())) + 1
label_dict_len = max(map(int, label2id_dict.values())) + 1

pred = create_predictor(args)
pred = init_predictor(args)

test_data = paddle.batch(
reader.file_reader(args.testdata_dir, word2id_dict, label2id_dict,
word_rep_dict),
batch_size=1)
test_data = paddle.batch(reader.file_reader(args.testdata_dir,
word2id_dict, label2id_dict,
word_rep_dict),
batch_size=1)
batch_id = 0
id2word = {v: k for k, v in word2id_dict.items()}
id2label = {v: k for k, v in label2id_dict.items()}
Expand Down
2 changes: 1 addition & 1 deletion python/resnet50/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

### 一:准备环境

请您在环境中安装1.7或以上版本的Paddle,具体的安装方式请参照[飞桨官方页面](https://www.paddlepaddle.org.cn/)的指示方式。
请您在环境中安装2.0或以上版本的Paddle,具体的安装方式请参照[飞桨官方页面](https://www.paddlepaddle.org.cn/)的指示方式。


### 二:下载模型以及测试数据
Expand Down
27 changes: 15 additions & 12 deletions python/resnet50/img_preprocess.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import cv2
import numpy as np


def resize_short(img, target_size):
""" resize_short """
percent = float(target_size) / min(img.shape[0], img.shape[1])
Expand All @@ -9,6 +10,7 @@ def resize_short(img, target_size):
resized = cv2.resize(img, (resized_width, resized_height))
return resized


def crop_image(img, target_size, center):
""" crop_image """
height, width = img.shape[:2]
Expand All @@ -21,18 +23,19 @@ def crop_image(img, target_size, center):
h_start = np.random.randint(0, height - size + 1)
w_end = w_start + size
h_end = h_start + size
img = img[int(h_start):int(h_end), int(w_start):int(w_end), :]w_start:w_end, :]
img = img[int(h_start):int(h_end), int(w_start):int(w_end), :]
return img


def preprocess(img):
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
img = resize_short(img, 224)
img = crop_image(img, 224, True)
# bgr-> rgb && hwc->chw
img = img[:, :, ::-1].astype('float32').transpose((2, 0, 1)) / 255
img_mean = np.array(mean).reshape((3, 1, 1))
img_std = np.array(std).reshape((3, 1, 1))
img -= img_mean
img /= img_std
return img[np.newaxis, :]
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
img = resize_short(img, 224)
img = crop_image(img, 224, True)
# bgr-> rgb && hwc->chw
img = img[:, :, ::-1].astype('float32').transpose((2, 0, 1)) / 255
img_mean = np.array(mean).reshape((3, 1, 1))
img_std = np.array(std).reshape((3, 1, 1))
img -= img_mean
img /= img_std
return img[np.newaxis, :]
117 changes: 70 additions & 47 deletions python/resnet50/infer_resnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,64 +2,87 @@
import argparse
import cv2

from paddle.fluid.core import AnalysisConfig
from paddle.fluid.core import create_paddle_predictor
from paddle.inference import Config
from paddle.inference import create_predictor

from img_preprocess import preprocess

def create_predictor(args):
if args.model_dir is not "":
config = AnalysisConfig(args.model_dir)
else:
config = AnalysisConfig(args.model_file, args.params_file)

config.switch_use_feed_fetch_ops(False)
config.enable_memory_optim()
if args.use_gpu:
config.enable_use_gpu(1000, 0)
else:
# If not specific mkldnn, you can set the blas thread.
# The thread num should not be greater than the number of cores in the CPU.
config.set_cpu_math_library_num_threads(4)
#config.enable_mkldnn()
def init_predictor(args):
if args.model_dir is not "":
config = Config(args.model_dir)
else:
config = Config(args.model_file, args.params_file)

config.enable_memory_optim()
if args.use_gpu:
config.enable_use_gpu(1000, 0)
else:
# If not specific mkldnn, you can set the blas thread.
# The thread num should not be greater than the number of cores in the CPU.
config.set_cpu_math_library_num_threads(4)
config.enable_mkldnn()

predictor = create_predictor(config)
return predictor


predictor = create_paddle_predictor(config)
return predictor

def run(predictor, img):
# copy img data to input tensor
input_names = predictor.get_input_names()
for i, name in enumerate(input_names):
input_tensor = predictor.get_input_tensor(name)
input_tensor.reshape(img[i].shape)
input_tensor.copy_from_cpu(img[i].copy())
# copy img data to input tensor
input_names = predictor.get_input_names()
for i, name in enumerate(input_names):
input_tensor = predictor.get_input_handle(name)
input_tensor.reshape(img[i].shape)
input_tensor.copy_from_cpu(img[i].copy())

# do the inference
predictor.zero_copy_run()
# do the inference
predictor.run()

results = []
# get out data from output tensor
output_names = predictor.get_output_names()
for i, name in enumerate(output_names):
output_tensor = predictor.get_output_handle(name)
output_data = output_tensor.copy_to_cpu()
results.append(output_data)

return results

results = []
# get out data from output tensor
output_names = predictor.get_output_names()
for i, name in enumerate(output_names):
output_tensor = predictor.get_output_tensor(name)
output_data = output_tensor.copy_to_cpu()
results.append(output_data)

return results

def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--model_file", type=str, default="", help="Model filename, Specify this when your model is a combined model.")
parser.add_argument("--params_file", type=str, default="", help="Parameter filename, Specify this when your model is a combined model.")
parser.add_argument("--model_dir", type=str, default="", help="Model dir, If you load a non-combined model, specify the directory of the model.")
parser.add_argument("--use_gpu", type=int, default=0, help="Whether use gpu.")
parser.add_argument(
"--model_file",
type=str,
default="",
help="Model filename, Specify this when your model is a combined model."
)
parser.add_argument(
"--params_file",
type=str,
default="",
help=
"Parameter filename, Specify this when your model is a combined model."
)
parser.add_argument(
"--model_dir",
type=str,
default="",
help=
"Model dir, If you load a non-combined model, specify the directory of the model."
)
parser.add_argument("--use_gpu",
type=int,
default=0,
help="Whether use gpu.")
return parser.parse_args()


if __name__ == '__main__':
args = parse_args()
pred = create_predictor(args)
img = cv2.imread('./ILSVRC2012_val_00000247.jpeg')
img = preprocess(img)
#img = np.ones((1, 3, 224, 224)).astype(np.float32)
result = run(pred, [img])
print ("class index: ", np.argmax(result[0][0]))
args = parse_args()
pred = init_predictor(args)
img = cv2.imread('./ILSVRC2012_val_00000247.jpeg')
img = preprocess(img)
#img = np.ones((1, 3, 224, 224)).astype(np.float32)
result = run(pred, [img])
print("class index: ", np.argmax(result[0][0]))
2 changes: 1 addition & 1 deletion python/yolov3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

### 一:准备环境

请您在环境中安装1.7或以上版本的Paddle,具体的安装方式请参照[飞桨官方页面](https://www.paddlepaddle.org.cn/)的指示方式。
请您在环境中安装2.0或以上版本的Paddle,具体的安装方式请参照[飞桨官方页面](https://www.paddlepaddle.org.cn/)的指示方式。


### 二:下载模型以及测试数据
Expand Down
Loading

0 comments on commit c2d7812

Please sign in to comment.