写在前面
我用pytorch
搭建了一个cnn
模型,训练mnist
数据集,然后将训练后的模型部署到了线上,并提供一个可供调用的api
接口。
http://pytorch-cnn-mnist.herokuapp.com/
http://pytorch-cnn-mnist.herokuapp.com/predict/
请求方法post
必要参数 字段类型描述data字符串图片的base64编码(包含头部信息) 成功响应 字段类型描述prediction整型手写数字的预测结果confidence字符串预测结果正确的概率 请求示例 测试图片import requests, base64, json
with open('test.jpg', 'rb') as f:
data = base64.b64encode(f.read()).decode()
data = 'data:image/jpeg;base64,'+data
url = 'http://pytorch-cnn-mnist.herokuapp.com/predict/'
res = requests.post(url, data=data).json()
print(json.dumps(res, indent=4))
响应结果
{
"prediction": 2,
"confidence": "98.30%"
}
温馨提示
接口仅供调用测试,有次数限制,非法滥用会封IP
,请勿用于实际生产环境。