摸索了两天,终于判分成功了。这个问题也解决了。先上代码
```python
from model_service.pytorch_model_service import PTServingBaseService
import json
import pandas as pd
from pandas import to_datetime
import numpy as np
import torch
import torch.nn.functional as F
import torch.nn as nn
import logging
logger = logging.getLogger(__name__)
class Net(torch.nn.Module):
def __init__(self):
super(Net, self).__init__()
self.layer1 = nn.Linear(in_features=2, out_features=10, bias=True)
self.layer2 = nn.Linear(in_features=10, out_features=50, bias=True)
self.layer3 = nn.Linear(in_features=50, out_features=100, bias=True)
self.layer4 = nn.Linear(in_features=100, out_features=20, bias=True)
self.layer5 = nn.Linear(in_features=20, out_features=1, bias=True)
def forward(self, inputs):
outputs = F.logsigmoid(self.layer1(inputs))
outputs = F.logsigmoid(self.layer2(outputs))
outputs = F.logsigmoid(self.layer3(outputs))
outputs = F.logsigmoid(self.layer4(outputs))
outputs = F.logsigmoid(self.layer5(outputs))
return outputs
class user_Service(PTServingBaseService):
def __init__(self, model_name, model_path):
#print('begin to init')
logger.info("begin to init")
super(user_Service, self).__init__(model_name, model_path)
self.model_name = model_name
self.model_path = model_path
model = Net()
model.load_state_dict(torch.load(self.model_path))
self.model = model
print('model_name:', model_name)
print('model_path:', model_path)
#print('end to init')
logger.info("end to init")
def _preprocess(self, data):
logger.info("begin to pre process")
#print('begin to pre process')
print('data:',data)
days = data["req_data"][0].split(",")
df = pd.DataFrame(columns = ["weekday", "timeindex"])
for day in days:
timestamp = to_datetime(day, format="%Y-%m-%d")
df1 = pd.DataFrame({"weekday":(timestamp.dayofweek/6.0), "timeindex":(np.arange(300, 1256, 5))/(24 * 60.0)})
df = df.append(df1, ignore_index=True)
input_data = np.array(df, dtype=np.float32)
input_data = torch.tensor(input_data)
#print('end to pre process')
logger.info("end to pre process")
return input_data
def _inference(self, data):
#print('start to infer')
logger.info("start to infer")
predict = self.model(data)
#print('end to infer')
logger.info("end to infer")
return predict
def _postprocess(self, result_data):
predict = result_data.detach().numpy()
mean = 23.019272
std = 13.458502
predict = (predict * std) + mean
predict = np.reshape(predict, (predict.shape[0]))
predict = predict.tolist()
tmp = {"wuhe_zhangheng":''}
tmp["wuhe_zhangheng"] = predict
predict = json.dumps(tmp)
output = {"data":{"resp_data":''}}
output['data']['resp_data'] = predict
#output变量类型是一个dict对象,不是字符串
return output
```
```
{
"model_algorithm": "traffic-prediction",
"model_type": "PyTorch",
"runtime": "python3.6",
"metrics": {},
"apis": [
{
"procotol": "http",
"url": "/",
"method": "post",
"request": {
"Content-type": "applicaton/json",
"data": {
"type": "object",
"properties": {
"req_data": {
"type": "array",
"items": [
{
"type": "string"
}
]
}
}
}
},
"response": {
"Content-type": "applicaton/json",
"data": {
"type": "object",
"properties": {
"resp_data": {
"type": "array",
"items": [
{
"type": "number"
}
]
}
}
}
}
}
],
"dependencies": [
{
"installer": "pip",
"packages": [
{
"restraint": "",
"package_version": "",
"package_name": "numpy"
},
{
"restraint": "",
"package_version": "",
"package_name": "pandas"
}
]
}
]
}
```
这个问题主要的原因是_postprocess方法返回的类型和格式不对。
# 1.类型
_postprocess方法返回的output变量的类型是dict对象。我没有测试过将output转为str的情况,因为今天已经超过的提交评分的次数了,没法测试了,但是output为dict对象时,确实能评分成功。
# 2.格式
官方的赛事介绍里说明_postprocess返回的格式是{"data":{"resp_data":{"wuhe_zhangheng":[1,4,5,6,4...]}}},是一个json格式的字符串,我按照这个格式返回,测试过多次,仍然评分失败。但是实际上的格式应该为{"data":{"resp_data":'{"wuhe_zhangheng":[1,4,5,6,4...]}'}},注意主要的区别就是下划线部分,下划线部分因该是字符串,不是dict对象,将下划线部分用引号包起来就可以了。
# 官方文档没说清楚,真是害死人,在论坛上问,又说代码问题,自行解决。这些东西花了两天时间一点一点猜,一点一点试,最后终于试出来了,如果推理代码能够方便的调试,根本能很快的解决。
不同的框架,配置文件和推理代码写法不一样的,先研究下模型包规范:
https://support.huaweicloud.com/engineers-modelarts/modelarts_23_0091.html
另外,这几个问题,可以好好看下:https://developer.huaweicloud.com/hero/forum.php?mod=viewthread&tid=51715
这个题之前有同学用pytorch做过,再仔细看下配置文件和推理代码编写规范,也可以多参考示例:
https://support.huaweicloud.com/engineers-modelarts/modelarts_23_0175.html
如果都按照规范写了还是有问题,那应该就是数据后处理代码有问题,你可以根据日志来排查代码。
我也是同样的问题阿,,,有没有解决阿,,,,推理代码不停的出错,评分失败,推理代码改了40次,创建模型40个,部署40次,测试40次,每次要花好几分钟,,,,,一天的时间就浪费这在上面了,,,真是鸡肋,啊啊啊啊啊啊啊啊啊啊啊
云上AI
2020-4-24 11:18
我也是同样的问题,我按照这个范例编写了推理代码,但是仍然没有解决问题,评分仍然失败。按照赛事介绍中所说,_postprocess方法应该返回这个格式{"data":{"resp_data":{"wuhe_zhangheng":[1,4,5,6,4...]}}} ,类型为字符串。我也是按照这个格式返回的,模型的创建、部署和测试都没有问题,都能成功,就是评分失败,评分失败的原因是“判分作业运行失败。scoring job failed”。没有给出判分作业运行的具体的log,我们也不知道错在哪里,如何去改。。
Armeria
2020-4-23 15:35
Hiroid
2020-4-24 08:07