新转化的PaddleHubModule使用serving部署后图片参数如何传递?
Created by: Payne-Zhu
使用resnet_v2_50_imagenet迁移学习后新产生的模型按照下列方式转化为PaddleHubModule; 再使用hub install 安装到本机后新模型xxx_test使用hub serving start -m xxx_test部署可以成功; 调用这个服务参数要怎么写?使用示例中的方法会出错;返回需要使用json数据(arnning: "This usage is out of date, please use 'application/json' as content-type to post to ),但是使用json的话图片如何转换传递呢? 如何实现原版的 resnet_v2_50_imagenet 通过 http://localhost:8866/predict/image/resnet_v2_50_imagenet 传递Post参数image: data:image/jpeg;base64,... 就能直接返回的方式呢?
`import os import numpy as np import paddlehub as hub from paddlehub.module.module import moduleinfo, serving
@moduleinfo( name="xxx_test", version="1.0.0", summary="ERNIE tiny which was fine-tuned on the chnsenticorp dataset.", author="payne", author_email="payne@qq.com", type="cv/xxx_model", ) class RestNetFinetuned(hub.Module): def _initialize(self, ckpt_dir="xxx_model", num_class=4, max_seq_len=128, use_gpu=False, batch_size=4): self.ckpt_dir = os.path.join(self.directory, ckpt_dir)
self.module = hub.Module(name="resnet_v2_50_imagenet")
inputs, outputs, program = self.module.context(trainable=True)
self.vocab_path = self.module.get_vocab_path()
reader = hub.reader.ImageClassificationReader(
image_width=self.module.get_expected_image_width(),
image_height=self.module.get_expected_image_height(),
images_mean=self.module.get_pretrained_images_mean(),
images_std=self.module.get_pretrained_images_std(),
)
pooled_output = outputs["feature_map"]
feed_list = [inputs["image"].name]
config = hub.RunConfig(
use_data_parallel=False,
use_cuda=use_gpu,
batch_size=batch_size,
checkpoint_dir=self.ckpt_dir,
strategy=hub.finetune.strategy.DefaultFinetuneStrategy())
self.cls_task = hub.ImageClassifierTask(
data_reader=reader,
feature=pooled_output,
feed_list=feed_list,
num_classes=4,
config=config)
@serving
def predict(self, data, return_result=False, accelerate_mode=True):
run_states = self.cls_task.predict(data=data)
# return run_states
results = [run_state.run_results for run_state in run_states]
prediction = []
for batch_result in results:
# get predict index
batch_result = np.argmax(batch_result, axis=2)[0]
batch_result = batch_result.tolist()
prediction += batch_result
return prediction
if name == "main": dm_4_npc = RestNetFinetuned(ckpt_dir="dm_4_npc") data = ["data/1.png","data/4.png","data/2.png","data/3.png"] run_states = dm_4_npc.predict(data=data) print(run_states)`