未验证 提交 a1bb4209 编写于 作者: D Dong Daxiang 提交者: GitHub

Merge pull request #511 from barrierye/fix-issue508

Fix doc: develop a new Web Service
...@@ -21,7 +21,7 @@ class WebService(object): ...@@ -21,7 +21,7 @@ class WebService(object):
The preprocess method has two input parameters, `feed` and `fetch`. For an HTTP request `request`: The preprocess method has two input parameters, `feed` and `fetch`. For an HTTP request `request`:
- The value of `feed` is request data `request.json` - The value of `feed` is the feed part `request.json["feed"]` in the request data
- The value of `fetch` is the fetch part `request.json["fetch"]` in the request data - The value of `fetch` is the fetch part `request.json["fetch"]` in the request data
The return values are the feed and fetch values used in the prediction. The return values are the feed and fetch values used in the prediction.
...@@ -30,7 +30,7 @@ The return values are the feed and fetch values used in the prediction. ...@@ -30,7 +30,7 @@ The return values are the feed and fetch values used in the prediction.
The postprocess method has three input parameters, `feed`, `fetch` and `fetch_map`: The postprocess method has three input parameters, `feed`, `fetch` and `fetch_map`:
- The value of `feed` is request data `request.json` - The value of `feed` is the feed part `request.json["feed"]` in the request data
- The value of `fetch` is the fetch part `request.json["fetch"]` in the request data - The value of `fetch` is the fetch part `request.json["fetch"]` in the request data
- The value of `fetch_map` is the model output value. - The value of `fetch_map` is the model output value.
...@@ -40,25 +40,17 @@ The return value will be processed as `{"reslut": fetch_map}` as the return of t ...@@ -40,25 +40,17 @@ The return value will be processed as `{"reslut": fetch_map}` as the return of t
```python ```python
class ImageService(WebService): class ImageService(WebService):
def preprocess(self, feed={}, fetch=[]): def preprocess(self, feed={}, fetch=[]):
reader = ImageReader() reader = ImageReader()
if "image" not in feed: feed_batch = []
raise ("feed data error!") for ins in feed:
if isinstance(feed["image"], list): if "image" not in ins:
feed_batch = [] raise ("feed data error!")
for image in feed["image"]: sample = base64.b64decode(ins["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) img = reader.process_image(sample)
res_feed = {} feed_batch.append({"image": img})
res_feed["image"] = img.reshape(-1) return feed_batch, fetch
return res_feed, fetch
``` ```
For the above `ImageService`, only the `preprocess` method is rewritten to process the image data in Base64 format into the data format required by prediction. For the above `ImageService`, only the `preprocess` method is rewritten to process the image data in Base64 format into the data format required by prediction.
...@@ -21,7 +21,7 @@ class WebService(object): ...@@ -21,7 +21,7 @@ class WebService(object):
preprocess方法有两个输入参数,`feed``fetch`。对于一个HTTP请求`request` preprocess方法有两个输入参数,`feed``fetch`。对于一个HTTP请求`request`
- `feed`的值为请求数据`request.json` - `feed`的值为请求数据中的feed部分`request.json["feed"]`
- `fetch`的值为请求数据中的fetch部分`request.json["fetch"]` - `fetch`的值为请求数据中的fetch部分`request.json["fetch"]`
返回值分别是预测过程中用到的feed和fetch值。 返回值分别是预测过程中用到的feed和fetch值。
...@@ -30,7 +30,7 @@ preprocess方法有两个输入参数,`feed`和`fetch`。对于一个HTTP请 ...@@ -30,7 +30,7 @@ preprocess方法有两个输入参数,`feed`和`fetch`。对于一个HTTP请
postprocess方法有三个输入参数,`feed``fetch``fetch_map` postprocess方法有三个输入参数,`feed``fetch``fetch_map`
- `feed`的值为请求数据`request.json` - `feed`的值为请求数据中的feed部分`request.json["feed"]`
- `fetch`的值为请求数据中的fetch部分`request.json["fetch"]` - `fetch`的值为请求数据中的fetch部分`request.json["fetch"]`
- `fetch_map`的值为fetch到的模型输出值 - `fetch_map`的值为fetch到的模型输出值
...@@ -40,25 +40,17 @@ postprocess方法有三个输入参数,`feed`、`fetch`和`fetch_map`: ...@@ -40,25 +40,17 @@ postprocess方法有三个输入参数,`feed`、`fetch`和`fetch_map`:
```python ```python
class ImageService(WebService): class ImageService(WebService):
def preprocess(self, feed={}, fetch=[]): def preprocess(self, feed={}, fetch=[]):
reader = ImageReader() reader = ImageReader()
if "image" not in feed: feed_batch = []
raise ("feed data error!") for ins in feed:
if isinstance(feed["image"], list): if "image" not in ins:
feed_batch = [] raise ("feed data error!")
for image in feed["image"]: sample = base64.b64decode(ins["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) img = reader.process_image(sample)
res_feed = {} feed_batch.append({"image": img})
res_feed["image"] = img.reshape(-1) return feed_batch, fetch
return res_feed, fetch
``` ```
对于上述的`ImageService`,只重写了前处理方法,将base64格式的图片数据处理成模型预测需要的数据格式。 对于上述的`ImageService`,只重写了前处理方法,将base64格式的图片数据处理成模型预测需要的数据格式。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册