README.md 4.2 KB
Newer Older
1 2 3
# 服务端预测部署

`PaddleDetection`训练出来的模型可以使用[Serving](https://github.com/PaddlePaddle/Serving) 部署在服务端。  
C
cnn 已提交
4
本教程以在COCO数据集上用`configs/yolov3/yolov3_darknet53_270e_coco.yml`算法训练的模型进行部署。  
5
预训练模型权重文件为[yolov3_darknet53_270e_coco.pdparams](https://paddledet.bj.bcebos.com/models/yolov3_darknet53_270e_coco.pdparams)
6 7 8

## 1. 首先验证模型
```
9
python tools/infer.py -c configs/yolov3/yolov3_darknet53_270e_coco.yml --infer_img=demo/000000014439.jpg -o use_gpu=True weights=https://paddledet.bj.bcebos.com/models/yolov3_darknet53_270e_coco.pdparams --infer_img=demo/000000014439.jpg
10 11 12
```

## 2. 安装 paddle serving
S
shangliang Xu 已提交
13
请参考[PaddleServing](https://github.com/PaddlePaddle/Serving/tree/v0.7.0) 中安装教程安装(版本>=0.7.0)。
14 15

## 3. 导出模型
W
wangguanzhong 已提交
16
PaddleDetection在训练过程包括网络的前向和优化器相关参数,而在部署过程中,我们只需要前向参数,具体参考:[导出模型](https://github.com/PaddlePaddle/PaddleDetection/blob/develop/deploy/EXPORT_MODEL.md)
17 18

```
19
python tools/export_model.py -c configs/yolov3/yolov3_darknet53_270e_coco.yml -o weights=https://paddledet.bj.bcebos.com/models/yolov3_darknet53_270e_coco.pdparams --export_serving_model=True
20 21
```

C
cnn 已提交
22
以上命令会在`output_inference/`文件夹下生成一个`yolov3_darknet53_270e_coco`文件夹:
23
```
C
cnn 已提交
24 25
output_inference
│   ├── yolov3_darknet53_270e_coco
26
│   │   ├── infer_cfg.yml
C
cnn 已提交
27 28 29
│   │   ├── model.pdiparams
│   │   ├── model.pdiparams.info
│   │   ├── model.pdmodel
30 31 32 33
│   │   ├── serving_client
│   │   │   ├── serving_client_conf.prototxt
│   │   │   ├── serving_client_conf.stream.prototxt
│   │   ├── serving_server
C
cnn 已提交
34 35 36 37
│   │   │   ├── __model__
│   │   │   ├── __params__
│   │   │   ├── serving_server_conf.prototxt
│   │   │   ├── serving_server_conf.stream.prototxt
38 39 40 41 42 43
│   │   │   ├── ...
```

`serving_client`文件夹下`serving_client_conf.prototxt`详细说明了模型输入输出信息
`serving_client_conf.prototxt`文件内容为:
```
C
cnn 已提交
44 45 46 47 48 49 50
feed_var {
  name: "im_shape"
  alias_name: "im_shape"
  is_lod_tensor: false
  feed_type: 1
  shape: 2
}
51 52 53 54 55 56 57 58 59 60
feed_var {
  name: "image"
  alias_name: "image"
  is_lod_tensor: false
  feed_type: 1
  shape: 3
  shape: 608
  shape: 608
}
feed_var {
C
cnn 已提交
61 62
  name: "scale_factor"
  alias_name: "scale_factor"
63
  is_lod_tensor: false
C
cnn 已提交
64
  feed_type: 1
65 66 67
  shape: 2
}
fetch_var {
S
shangliang Xu 已提交
68 69
  name: "multiclass_nms3_0.tmp_0"
  alias_name: "multiclass_nms3_0.tmp_0"
70 71 72 73
  is_lod_tensor: true
  fetch_type: 1
  shape: -1
}
C
cnn 已提交
74
fetch_var {
S
shangliang Xu 已提交
75 76 77
  name: "multiclass_nms3_0.tmp_2"
  alias_name: "multiclass_nms3_0.tmp_2"
  is_lod_tensor: false
C
cnn 已提交
78
  fetch_type: 2
79 80 81 82 83
```

## 4. 启动PaddleServing服务

```
C
cnn 已提交
84
cd output_inference/yolov3_darknet53_270e_coco/
85 86

# GPU
C
cnn 已提交
87
python -m paddle_serving_server.serve --model serving_server --port 9393 --gpu_ids 0
88 89 90 91 92 93

# CPU
python -m paddle_serving_server.serve --model serving_server --port 9393
```

## 5. 测试部署的服务
S
shangliang Xu 已提交
94
准备`label_list.txt`文件,示例`label_list.txt`文件内容为
95
```
S
shangliang Xu 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
person
bicycle
car
motorcycle
airplane
bus
train
truck
boat
traffic light
fire hydrant
stop sign
parking meter
bench
bird
cat
dog
horse
sheep
cow
elephant
bear
zebra
giraffe
backpack
umbrella
handbag
tie
suitcase
frisbee
skis
snowboard
sports ball
kite
baseball bat
baseball glove
skateboard
surfboard
tennis racket
bottle
wine glass
cup
fork
knife
spoon
bowl
banana
apple
sandwich
orange
broccoli
carrot
hot dog
pizza
donut
cake
chair
couch
potted plant
bed
dining table
toilet
tv
laptop
mouse
remote
keyboard
cell phone
microwave
oven
toaster
sink
refrigerator
book
clock
vase
scissors
teddy bear
hair drier
toothbrush
176 177
```

S
shangliang Xu 已提交
178 179
设置`prototxt`文件路径为`serving_client/serving_client_conf.prototxt`
设置`fetch``fetch=["multiclass_nms3_0.tmp_0"])`
180 181 182 183

测试
```
# 进入目录
C
cnn 已提交
184
cd output_inference/yolov3_darknet53_270e_coco/
185

C
cnn 已提交
186
# 测试代码 test_client.py 会自动创建output文件夹,并在output下生成`bbox.json`和`000000014439.jpg`两个文件
S
shangliang Xu 已提交
187
python ../../deploy/serving/test_client.py ../../deploy/serving/label_list.txt ../../demo/000000014439.jpg
188
```