Skip to content

Commit 308271d

Browse files
committed
create new examples directory
1 parent 4e4134d commit 308271d

File tree

421 files changed

+30246
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

421 files changed

+30246
-0
lines changed
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
## Image Classification
2+
3+
([简体中文](./README_CN.md)|English)
4+
5+
The example uses the ResNet50_vd model to perform the imagenet 1000 classification task.
6+
7+
### Get model config and sample dataset
8+
```
9+
sh get_model.sh
10+
```
11+
12+
### Install preprocess module
13+
14+
```
15+
pip3 install paddle_serving_app
16+
```
17+
18+
19+
### Inference Service(Support BRPC-Client/GRPC-Client/Http-Client)
20+
21+
launch server side
22+
```
23+
python3 -m paddle_serving_server.serve --model ResNet50_vd_model --port 9696 #cpu inference service
24+
```
25+
26+
```
27+
python3 -m paddle_serving_server.serve --model ResNet50_vd_model --port 9696 --gpu_ids 0 #gpu inference service
28+
```
29+
30+
### BRPC-Client
31+
client send inference request
32+
```
33+
python3 resnet50_rpc_client.py ResNet50_vd_client_config/serving_client_conf.prototxt
34+
```
35+
*the port of server side in this example is 9696
36+
37+
### GRPC-Client/Http-Client
38+
client send inference request
39+
```
40+
python3 resnet50_http_client.py ResNet50_vd_client_config/serving_client_conf.prototxt
41+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
## 图像分类示例
2+
3+
(简体中文|[English](./README.md))
4+
5+
示例中采用ResNet50_vd模型执行imagenet 1000分类任务。
6+
7+
### 获取模型配置文件和样例数据
8+
```
9+
sh get_model.sh
10+
```
11+
12+
### 安装数据预处理模块
13+
14+
```
15+
pip3 install paddle_serving_app
16+
```
17+
18+
### 启动服务端(支持BRPC-Client、GRPC-Client、Http-Client)
19+
20+
启动server端
21+
```
22+
python3 -m paddle_serving_server.serve --model ResNet50_vd_model --port 9696 #cpu预测服务
23+
```
24+
25+
```
26+
python3 -m paddle_serving_server.serve --model ResNet50_vd_model --port 9696 --gpu_ids 0 #gpu预测服务
27+
```
28+
29+
### BRPC-Client预测
30+
client端进行预测
31+
```
32+
python3 resnet50_rpc_client.py ResNet50_vd_client_config/serving_client_conf.prototxt
33+
```
34+
*server端示例中服务端口为9696端口
35+
36+
37+
### GRPC-Client/Http-Client预测
38+
client端进行预测
39+
```
40+
python3 resnet50_http_client.py ResNet50_vd_client_config/serving_client_conf.prototxt
41+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
# pylint: disable=doc-string-missing
17+
18+
from __future__ import unicode_literals, absolute_import
19+
import os
20+
import sys
21+
import time
22+
import requests
23+
import json
24+
import base64
25+
from paddle_serving_client import Client
26+
from paddle_serving_client.utils import MultiThreadRunner
27+
from paddle_serving_client.utils import benchmark_args, show_latency
28+
from paddle_serving_app.reader import Sequential, File2Image, Resize
29+
from paddle_serving_app.reader import CenterCrop, RGB2BGR, Transpose, Div, Normalize
30+
31+
args = benchmark_args()
32+
33+
seq_preprocess = Sequential([
34+
File2Image(), Resize(256), CenterCrop(224), RGB2BGR(), Transpose((2, 0, 1)),
35+
Div(255), Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225], True)
36+
])
37+
38+
39+
def single_func(idx, resource):
40+
file_list = []
41+
turns = resource["turns"]
42+
latency_flags = False
43+
if os.getenv("FLAGS_serving_latency"):
44+
latency_flags = True
45+
latency_list = []
46+
for file_name in os.listdir("./image_data/n01440764"):
47+
file_list.append(file_name)
48+
img_list = []
49+
for i in range(1000):
50+
img_list.append("./image_data/n01440764/" + file_list[i])
51+
profile_flags = False
52+
if "FLAGS_profile_client" in os.environ and os.environ[
53+
"FLAGS_profile_client"]:
54+
profile_flags = True
55+
if args.request == "rpc":
56+
fetch = ["score"]
57+
client = Client()
58+
client.load_client_config(args.model)
59+
client.connect([resource["endpoint"][idx % len(resource["endpoint"])]])
60+
start = time.time()
61+
for i in range(turns):
62+
if args.batch_size >= 1:
63+
l_start = time.time()
64+
feed_batch = []
65+
i_start = time.time()
66+
for bi in range(args.batch_size):
67+
img = seq_preprocess(img_list[i])
68+
feed_batch.append({"image": img})
69+
i_end = time.time()
70+
if profile_flags:
71+
print("PROFILE\tpid:{}\timage_pre_0:{} image_pre_1:{}".
72+
format(os.getpid(),
73+
int(round(i_start * 1000000)),
74+
int(round(i_end * 1000000))))
75+
76+
result = client.predict(feed=feed_batch, fetch=fetch)
77+
l_end = time.time()
78+
if latency_flags:
79+
latency_list.append(l_end * 1000 - l_start * 1000)
80+
else:
81+
print("unsupport batch size {}".format(args.batch_size))
82+
83+
elif args.request == "http":
84+
py_version = sys.version_info[0]
85+
server = "http://" + resource["endpoint"][idx % len(resource[
86+
"endpoint"])] + "/image/prediction"
87+
start = time.time()
88+
for i in range(turns):
89+
if py_version == 2:
90+
image = base64.b64encode(
91+
open("./image_data/n01440764/" + file_list[i]).read())
92+
else:
93+
image_path = "./image_data/n01440764/" + file_list[i]
94+
image = base64.b64encode(open(image_path, "rb").read()).decode(
95+
"utf-8")
96+
req = json.dumps({"feed": [{"image": image}], "fetch": ["score"]})
97+
r = requests.post(
98+
server, data=req, headers={"Content-Type": "application/json"})
99+
end = time.time()
100+
if latency_flags:
101+
return [[end - start], latency_list]
102+
return [[end - start]]
103+
104+
105+
if __name__ == '__main__':
106+
multi_thread_runner = MultiThreadRunner()
107+
endpoint_list = [
108+
"127.0.0.1:9292", "127.0.0.1:9293", "127.0.0.1:9294", "127.0.0.1:9295"
109+
]
110+
turns = 100
111+
start = time.time()
112+
result = multi_thread_runner.run(
113+
single_func, args.thread, {"endpoint": endpoint_list,
114+
"turns": turns})
115+
#result = single_func(0, {"endpoint": endpoint_list})
116+
end = time.time()
117+
total_cost = end - start
118+
avg_cost = 0
119+
for i in range(args.thread):
120+
avg_cost += result[0][i]
121+
avg_cost = avg_cost / args.thread
122+
print("total cost: {}s".format(end - start))
123+
print("each thread cost: {}s.".format(avg_cost))
124+
print("qps: {}samples/s".format(args.batch_size * args.thread * turns /
125+
total_cost))
126+
if os.getenv("FLAGS_serving_latency"):
127+
show_latency(result[1])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
rm profile_log*
2+
export CUDA_VISIBLE_DEVICES=0,1,2,3
3+
export FLAGS_profile_server=1
4+
export FLAGS_profile_client=1
5+
python -m paddle_serving_server.serve --model $1 --port 9292 --thread 4 --gpu_ids 0,1,2,3 --mem_optim --ir_optim 2> elog > stdlog &
6+
7+
sleep 5
8+
gpu_id=0
9+
#save cpu and gpu utilization log
10+
if [ -d utilization ];then
11+
rm -rf utilization
12+
else
13+
mkdir utilization
14+
fi
15+
16+
#warm up
17+
$PYTHONROOT/bin/python3 benchmark.py --thread 4 --batch_size 1 --model $2/serving_client_conf.prototxt --request rpc > profile 2>&1
18+
echo -e "import psutil\ncpu_utilization=psutil.cpu_percent(1,False)\nprint('CPU_UTILIZATION:', cpu_utilization)\n" > cpu_utilization.py
19+
20+
for thread_num in 1 4 8 16
21+
do
22+
for batch_size in 1 4 16 64
23+
do
24+
job_bt=`date '+%Y%m%d%H%M%S'`
25+
nvidia-smi --id=0 --query-compute-apps=used_memory --format=csv -lms 100 > gpu_use.log 2>&1 &
26+
nvidia-smi --id=0 --query-gpu=utilization.gpu --format=csv -lms 100 > gpu_utilization.log 2>&1 &
27+
gpu_memory_pid=$!
28+
$PYTHONROOT/bin/python benchmark.py --thread $thread_num --batch_size $batch_size --model $2/serving_client_conf.prototxt --request rpc > profile 2>&1
29+
kill ${gpu_memory_pid}
30+
kill `ps -ef|grep used_memory|awk '{print $2}'`
31+
echo "model name :" $1
32+
echo "thread num :" $thread_num
33+
echo "batch size :" $batch_size
34+
echo "=================Done===================="
35+
echo "model name :$1" >> profile_log
36+
echo "batch size :$batch_size" >> profile_log
37+
job_et=`date '+%Y%m%d%H%M%S'`
38+
awk 'BEGIN {max = 0} {if(NR>1){if ($1 > max) max=$1}} END {print "MAX_GPU_MEMORY:", max}' gpu_use.log >> profile_log_$1
39+
awk 'BEGIN {max = 0} {if(NR>1){if ($1 > max) max=$1}} END {print "GPU_UTILIZATION:", max}' gpu_utilization.log >> profile_log_$1
40+
rm -rf gpu_use.log gpu_utilization.log
41+
$PYTHONROOT/bin/python ../util/show_profile.py profile $thread_num >> profile_log
42+
tail -n 8 profile >> profile_log
43+
echo "" >> profile_log_$1
44+
done
45+
done
46+
47+
#Divided log
48+
awk 'BEGIN{RS="\n\n"}{i++}{print > "ResNet_log_"i}' profile_log_$1
49+
mkdir $1_log && mv ResNet_log_* $1_log
50+
ps -ef|grep 'serving'|grep -v grep|cut -c 9-15 | xargs kill -9
38.8 KB
Loading
Loading
55.5 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
wget --no-check-certificate https://paddle-serving.bj.bcebos.com/imagenet-example/ResNet50_vd.tar.gz
2+
tar -xzvf ResNet50_vd.tar.gz
3+
wget --no-check-certificate https://paddle-serving.bj.bcebos.com/imagenet-example/ResNet101_vd.tar.gz
4+
tar -xzvf ResNet101_vd.tar.gz
5+
6+
wget --no-check-certificate https://paddle-serving.bj.bcebos.com/imagenet-example/image_data.tar.gz
7+
tar -xzvf image_data.tar.gz

0 commit comments

Comments
 (0)