为Heartex或Label Studio部署机器学习
pyheartex的Python项目详细描述
皮哈特克斯
Python接口,用于运行ML后端服务器并在Heartex platform内使用它进行主动学习和预标记和预测
安装
首先确保你已经运行了Redis server(否则你只能使用预测,不能使用主动学习)。在
安装Heartex SDK:
git clone https://github.com/heartexlabs/pyheartex.git
cd pyheartex/
pip install -r requirements.txt
pip install -e .
你应该做的最后一件事是在后台启动RQ workers:
^{pr2}$使用Docker
下面是如何开始提供图像分类器的示例:
cd examples/docker
docker-compose up
您需要用自己的模型替换的只是从this file更改加载、推理和训练脚本。在
快速入门
快速入门指南提供了以下Heartex平台中流行的机器学习框架的用法:
Scikit学习
让我们为文本分类提供scikit-learn模型。在
你可以简单地启动
python examples/quickstart.py
这个脚本看起来像
fromhtx.adapters.sklearnimportservefromsklearn.feature_extraction.textimportTfidfVectorizerfromsklearn.linear_modelimportLogisticRegressionfromsklearn.pipelineimportmake_pipelineif__name__=="__main__":# Creating sklearn-compatible modelmy_model=make_pipeline(TfidfVectorizer(),LogisticRegression())# Start serving this modelserve(my_model)
它开始在http://localhost:16118监听Heartex事件。 若要连接模型,请转到Heartex->设置->机器学习页面,然后选择“添加自定义模型”。在
也可以使用Heartex API激活模型:
curl -X POST -H 'Content-Type: application/json'\ -H 'Authorization: Token <PUT-YOUR-TOKEN-HERE>'\ -d '[{"url": "$HOST:$PORT", "name": "my_model", "title": "My model", "description": "My new model deployed on Heartex"}]'\ http://go.heartex.net/api/projects/{project-id}/backends/
其中$HOST:$PORT
是您的服务器URL,应该可以从外部访问。在
法斯泰
您可以像scikit learn一样集成FastAI模型。 检查this example以了解如何插入可更新的图像分类器。在
高级用法
当您想超越使用sklearn兼容的API时,您可以通过手动进行输入/输出接口转换来构建自己的模型。 必须按如下方式将Heartex模型子类化:
fromhtx.base_modelimportBaseModel# This class exposes methods needed to handle model in the runtime (loading into memory, running predictions)classMyModel(BaseModel):defget_input(self,task):"""Extract input from serialized task"""passdefget_output(self,task):"""Extract output from serialized task"""passdefload(self,train_output):"""Loads model into memory. `train_output` dict is actually the output the `train` method (see below)"""passdefpredict(self,tasks):"""Get list of tasks, already processed by `get_input` method, and returns completions in Heartex format"""pass# This method handles model retrainingdeftrain(input_tasks,output_model_dir,**kwargs):""" :param input_tasks: list of tasks already processed by `get_input` :param output_model_dir: output directory where you can optionally store model resources :param kwargs: any additional kwargs taken from `train_kwargs` :return: `train_output` dict for consequent model loading """pass
- 项目
标签: