NEW_WEB_SERVICE_CN.md 2.3 KB
Newer Older
B
barrierye 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
# 如何开发一个新的Web Service?

(简体中文|[English](NEW_WEB_SERVICE.md))

本文档将以Imagenet图像分类服务为例,来介绍如何开发一个新的Web Service。您可以在[这里](https://github.com/PaddlePaddle/Serving/blob/develop/python/examples/imagenet/image_classification_service.py)查阅完整的代码。

## WebService基类

Paddle Serving实现了[WebService](https://github.com/PaddlePaddle/Serving/blob/develop/python/paddle_serving_server/web_service.py#L23)基类,您需要重写它的`preprocess`方法和`postprocess`方法,默认实现如下:

```python
class WebService(object):
  
		def preprocess(self, feed={}, fetch=[]):
        return feed, fetch
    def postprocess(self, feed={}, fetch=[], fetch_map=None):
        return fetch_map
```

###preprocess方法

preprocess方法有两个输入参数,`feed``fetch`。对于一个HTTP请求`request`

- `feed`的值为请求数据`request.json`
- `fetch`的值为请求数据中的fetch部分`request.json["fetch"]`

返回值分别是预测过程中用到的feed和fetch值。

###postprocess方法

postprocess方法有三个输入参数,`feed``fetch``fetch_map`

- `feed`的值为请求数据`request.json`
- `fetch`的值为请求数据中的fetch部分`request.json["fetch"]`
- `fetch_map`的值为fetch到的模型输出值

返回值将会被处理成`{"reslut": fetch_map}`作为HTTP请求的返回。

## 开发ImageService类

```python
class ImageService(WebService):
    def preprocess(self, feed={}, fetch=[]):
        reader = ImageReader()
        if "image" not in feed:
            raise ("feed data error!")
        if isinstance(feed["image"], list):
            feed_batch = []
            for image in feed["image"]:
                sample = base64.b64decode(image)
                img = reader.process_image(sample)
                res_feed = {}
                res_feed["image"] = img.reshape(-1)
                feed_batch.append(res_feed)
            return feed_batch, fetch
        else:
            sample = base64.b64decode(feed["image"])
            img = reader.process_image(sample)
            res_feed = {}
            res_feed["image"] = img.reshape(-1)
            return res_feed, fetch
```

对于上述的`ImageService`,只重写了前处理方法,将base64格式的图片数据处理成模型预测需要的数据格式。