[MachineBox系列]ClassificationBox—

2019-01-29  本文已影响0人  被秒

简介

ClassificationBox是MachineBox的一部分,提供了一个通用的分类器模型。 关于MachineBox的简介以及环境相关的设置可以参照我的介绍文章,这里就不赘述了。我们直接启动

docker run -p 8080:8080 -e "MB_KEY=<your-mb-key>" machinebox/classificationbox

接下来我们来实现一个猫狗识别的例子

例子

打开http://localhost:8080, 官方已经提供了详尽的API文档。

创建模型

首先第一步, 我们需要创建一个模型,来看API

POST http://localhost:8080/classificationbox/models
{
    "id": "sentiment1",
    "name": "sentimentModel",
    "options": {
        "ngrams": 1,
        "skipgrams": 1
    },
    "classes": [
        "class1",
        "class2",
        "class3"
    ]
}

在实际开发中,MachineBox也提供了官方的SDK, 不过这里我就直接发送请求了

curl -X POST \
  http://localhost:8080/classificationbox/models \
  -H 'accept: application/json; charset=utf-8' \
  -H 'content-type: application/json; charset=utf-8' \
  -d '{
    "id": "catVsDog",
    "name": "catVsDog",
    "classes": [
        "cat",
        "dog"
    ]
}'

好了,到此我们已经创建好了一个ID为catVsDog的模型

训练模型

接下来我们需要训练模型,先看单个样本的API

POST http://localhost:8080/classificationbox/models/{model_id}/teach
{
    "class": "class1",
    "inputs": [
        {"key": "user_age", "type": "number", "value": "25"},
        {"key": "user_interests", "type": "list", "value": "music,cooking,ml"},
        {"key": "user_location", "type": "keyword", "value": "London"}      
    ]
}
POST http://localhost:8080/classificationbox/models/{model_id}/teach-multi
{
    "examples": [
        {
            "class": "class1",
            "inputs": [{"key": "user_age", "type": "number", "value": "25"}]
        },
        {
            "class": "class2", 
            "inputs": [{"key": "user_age", "type": "number", "value": "55"}]
        },
        {...}
    ]
}

接下来我们用几个简单的猫狗识别的图片来训练模型

类别 图片 url
dog https://img.haomeiwen.com/i13069854/ec88087820d9877f.png
cat https://img.haomeiwen.com/i13069854/7668001c3341e1fc.png

还是直接调用API

curl -X POST \
  http://localhost:8080/classificationbox/models/catVsDog/teach-multi \
  -H 'accept: application/json; charset=utf-8' \
  -H 'content-type: application/json; charset=utf-8' \
  -d '{
    "examples": [
        {
            "class": "cat",
            "inputs": [{
                "key": "image",
                "type": "image_url",
                "value": "https://img.haomeiwen.com/i13069854/7668001c3341e1fc.png"
            }]
        },
        {
            "class": "dog", 
            "inputs": [{
                "key": "image",
                "type": "image_url",
                "value": "https://img.haomeiwen.com/i13069854/ec88087820d9877f.png"
            }]
        }
    ]
}'

模型预测

接下来我们使用模型来预测, 还是先来看API

POST http://localhost:8080/classificationbox/models/{model_id}/predict
{
    "limit": 10,
    "inputs": [
        {"key": "user_age", "type": "number", "value": "25"},
        {"key": "user_interests", "type": "list", "value": "music,cooking,ml"},
        {"key": "user_location", "type": "keyword", "value": "London"}
    ]
}
curl -X POST \
  http://localhost:8080/classificationbox/models/catVsDog/predict \
  -H 'accept: application/json; charset=utf-8' \
  -H 'content-type: application/json; charset=utf-8' \
  -d '{
    "inputs":[{
        "key": "image",
        "type": "image_url",
        "value": "https://img.haomeiwen.com/i13069854/494c4e162690d316.png"
    }]
}'

我们得到结果

{
    "success": true,
    "classes": [
        {
            "id": "cat",
            "score": 0.587596
        },
        {
            "id": "dog",
            "score": 0.412404
        }
    ]
}

结果并不理想,这主要是由于我们训练的样本太少造成的。 大家可以自行使用用Kaggle catVsDog数据集去实验

模型导出

导出模型是机器学习中极其重要的一步,我们可以通过API

GET http://localhost:8080/classificationbox/state/{model_id}

模型导入

同样也有导入的API

POST http://localhost:8080/classificationbox/state

该API支持file,url, base643种不同的输入

除了上述例子中涉及到的API, ClassificationBox还提供了模型查看, 删除等的API, 可以在http://localhost:8080中看到, 这里就不再一一罗列。 另外所有的MachineBox的镜像都提供了诸如/healthz, /liveness, /readyz等实用的API, 具体可以查阅官方文档

上一篇下一篇

猜你喜欢

热点阅读