MLflow概要
MLflowは以下の4つで構成されている
- MLflow Tracking: Tracking experiments to record and compare parameters and results.
- MLflow Projects: Packaging ML code in a reusable, reproducible form in order to share with other data scientists or transfer to production.
- MLflow Models: Managing and deploying models from a variety of ML libraries to a variety of model serving and inference platforms.
- MLflow Model Registry: Providing a central model store to collaboratively manage the full lifecycle of an MLflow Model, including model versioning, stage transitions, and annotations.
QuickStart
QuickStartでは、Tracking, Projects, Modelsの簡単な例を試せる
Install
pip install mlflow
1. MLflow Tracking
UIを起動
mlflow ui
http://localhost:5000 をブラウザで開く
Tutorialの前のステップで、GitHubをCloneして、 `mlflow` ディレクトリの中にいるとうまく立ち上がらないので注意
localhost:5000を開くと以下のような表示になったら、 mlflow ui
を mlflow
ディレクトリの外からRunすることで解決
Unable to display MLflow UI - landing page (index.html) not found.
You are very likely running the MLflow server using a source installation of the Python MLflow
package.
If you are a developer making MLflow source code changes and intentionally running a source
installation of MLflow, you can view the UI by running the Javascript dev server:
https://github.com/mlflow/mlflow/blob/master/CONTRIBUTING.rst#running-the-javascript-dev-server
Otherwise, uninstall MLflow via 'pip uninstall mlflow', reinstall an official MLflow release
from PyPI via 'pip install mlflow', and rerun the MLflow server.
Git examples
git clone https://github.com/mlflow/mlflow
cd mlflow/examples
2. MLflow Projects
mlflow run https://github.com/mlflow/mlflow-example.git -P alpha=5.0
condaがないと1つ目の`mlflow run sklearn_elasticnet_wine -P alpha=0.5`は動かないので注意
mlflow run sklearn_elasticnet_wine -P alpha=0.5
/Users/masato-naka/.pyenv/versions/3.7.9/lib/python3.7/site-packages/pandas/compat/__init__.py:124: UserWarning: Could not import the lzma module. Your installed Python is incomplete. Attempting to use lzma compression will result in a RuntimeError.
warnings.warn(msg)
2021/10/20 06:44:27 ERROR mlflow.cli: === Could not find Conda executable at conda. Ensure Conda is installed as per the instructions at https://conda.io/projects/conda/en/latest/user-guide/install/index.html. You can also configure MLflow to look for a specific Conda executable by setting the MLFLOW_CONDA_HOME environment variable to the path of the Conda executable ===
condaを使わない場合--no-conda
とする
mlflow run sklearn_elasticnet_wine -P alpha=0.5 --no-conda
/Users/masato-naka/.pyenv/versions/3.7.9/lib/python3.7/site-packages/pandas/compat/__init__.py:124: UserWarning: Could not import the lzma module. Your installed Python is incomplete. Attempting to use lzma compression will result in a RuntimeError.
warnings.warn(msg)
2021/10/19 10:26:05 INFO mlflow.projects.utils: === Created directory /var/folders/5g/vmdg2t1j2011ggd9p983ns6h0000gn/T/tmpwjrtcnnt for downloading remote URIs passed to arguments of type 'path' ===
2021/10/19 10:26:05 INFO mlflow.projects.backend.local: === Running command 'python train.py 0.5 0.1' in run with ID '5d1c5ff1305c44e58e4ec7d235561331' ===
/Users/masato-naka/.pyenv/versions/3.7.9/lib/python3.7/site-packages/pandas/compat/__init__.py:124: UserWarning: Could not import the lzma module. Your installed Python is incomplete. Attempting to use lzma compression will result in a RuntimeError.
warnings.warn(msg)
Elasticnet model (alpha=0.500000, l1_ratio=0.100000):
RMSE: 0.7460550348172179
MAE: 0.576381895873763
R2: 0.21136606570632266
2021/10/19 10:26:27 INFO mlflow.projects: === Run (ID '5d1c5ff1305c44e58e4ec7d235561331') succeeded ===
3. MLflow Models
Save
python sklearn_logistic_regression/train.py
/Users/masato-naka/.pyenv/versions/3.7.9/lib/python3.7/site-packages/pandas/compat/__init__.py:124: UserWarning: Could not import the lzma module. Your installed Python is incomplete. Attempting to use lzma compression will result in a RuntimeError.
warnings.warn(msg)
Score: 0.6666666666666666
Model saved in run 55c3893c1ead4d538bb91ab1165612d1
Serve
mlflow models serve -m runs:/<RUN_ID>/model
Check
curl -d '{"columns":["x"], "data":[[1], [-1]]}' -H 'Content-Type: application/json; format=pandas-split' -X POST localhost:5000/invocations
結果: [1, 0]
condaがなくてServing失敗するケース
mlflow models serve -m runs:/55c3893c1ead4d538bb91ab1165612d1/model
/Users/masato-naka/.pyenv/versions/3.7.9/lib/python3.7/site-packages/pandas/compat/__init__.py:124: UserWarning: Could not import the lzma module. Your installed Python is incomplete. Attempting to use lzma compression will result in a RuntimeError.
warnings.warn(msg)
2021/10/20 06:51:21 INFO mlflow.models.cli: Selected backend for flavor 'python_function'
Traceback (most recent call last):
File "/Users/masato-naka/.pyenv/versions/3.7.9/lib/python3.7/site-packages/mlflow/utils/conda.py", line 76, in get_or_create_conda_env
process.exec_cmd([conda_path, "--help"], throw_on_error=False)
File "/Users/masato-naka/.pyenv/versions/3.7.9/lib/python3.7/site-packages/mlflow/utils/process.py", line 51, in exec_cmd
**kwargs
File "/Users/masato-naka/.pyenv/versions/3.7.9/lib/python3.7/subprocess.py", line 800, in __init__
restore_signals, start_new_session)
File "/Users/masato-naka/.pyenv/versions/3.7.9/lib/python3.7/subprocess.py", line 1551, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'conda': 'conda'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/masato-naka/.pyenv/versions/3.7.9/bin/mlflow", line 8, in <module>
sys.exit(cli())
File "/Users/masato-naka/.pyenv/versions/3.7.9/lib/python3.7/site-packages/click/core.py", line 1137, in __call__
return self.main(*args, **kwargs)
File "/Users/masato-naka/.pyenv/versions/3.7.9/lib/python3.7/site-packages/click/core.py", line 1062, in main
rv = self.invoke(ctx)
File "/Users/masato-naka/.pyenv/versions/3.7.9/lib/python3.7/site-packages/click/core.py", line 1668, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/Users/masato-naka/.pyenv/versions/3.7.9/lib/python3.7/site-packages/click/core.py", line 1668, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/Users/masato-naka/.pyenv/versions/3.7.9/lib/python3.7/site-packages/click/core.py", line 1404, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/Users/masato-naka/.pyenv/versions/3.7.9/lib/python3.7/site-packages/click/core.py", line 763, in invoke
return __callback(*args, **kwargs)
File "/Users/masato-naka/.pyenv/versions/3.7.9/lib/python3.7/site-packages/mlflow/models/cli.py", line 56, in serve
).serve(model_uri=model_uri, port=port, host=host)
File "/Users/masato-naka/.pyenv/versions/3.7.9/lib/python3.7/site-packages/mlflow/pyfunc/backend.py", line 92, in serve
conda_env_path, command, self._install_mlflow, command_env=command_env
File "/Users/masato-naka/.pyenv/versions/3.7.9/lib/python3.7/site-packages/mlflow/pyfunc/backend.py", line 149, in _execute_in_conda_env
conda_env_name = get_or_create_conda_env(conda_env_path, env_id=env_id)
File "/Users/masato-naka/.pyenv/versions/3.7.9/lib/python3.7/site-packages/mlflow/utils/conda.py", line 85, in get_or_create_conda_env
"to the path of the Conda executable".format(conda_path, MLFLOW_CONDA_HOME)
mlflow.exceptions.ExecutionException: Could not find Conda executable at conda. Ensure Conda is installed as per the instructions at https://conda.io/projects/conda/en/latest/user-guide/install/index.html. You can also configure MLflow to look for a specific Conda executable by setting the MLFLOW_CONDA_HOME environment variable to the path of the Conda executable
解決策: --no-conda
mlflow models serve -m runs:/55c3893c1ead4d538bb91ab1165612d1/model --no-conda
/Users/masato-naka/.pyenv/versions/3.7.9/lib/python3.7/site-packages/pandas/compat/__init__.py:124: UserWarning: Could not import the lzma module. Your installed Python is incomplete. Attempting to use lzma compression will result in a RuntimeError.
warnings.warn(msg)
2021/10/20 06:53:42 INFO mlflow.models.cli: Selected backend for flavor 'python_function'
2021/10/20 06:53:42 INFO mlflow.pyfunc.backend: === Running command 'gunicorn --timeout=60 -b 127.0.0.1:5000 -w 1 ${GUNICORN_CMD_ARGS} -- mlflow.pyfunc.scoring_server.wsgi:app'
[2021-10-20 06:53:42 +0900] [39510] [INFO] Starting gunicorn 20.1.0
[2021-10-20 06:53:42 +0900] [39510] [INFO] Listening at: http://127.0.0.1:5000 (39510)
[2021-10-20 06:53:42 +0900] [39510] [INFO] Using worker: sync
[2021-10-20 06:53:42 +0900] [39513] [INFO] Booting worker with pid: 39513
/Users/masato-naka/.pyenv/versions/3.7.9/lib/python3.7/site-packages/pandas/compat/__init__.py:124: UserWarning: Could not import the lzma module. Your installed Python is incomplete. Attempting to use lzma compression will result in a RuntimeError.
warnings.warn(msg)
mlflow uiとポートがかぶってRun出来ないケース
mlflow models serve -m runs:/55c3893c1ead4d538bb91ab1165612d1/model --no-conda
/Users/masato-naka/.pyenv/versions/3.7.9/lib/python3.7/site-packages/pandas/compat/__init__.py:124: UserWarning: Could not import the lzma module. Your installed Python is incomplete. Attempting to use lzma compression will result in a RuntimeError.
warnings.warn(msg)
2021/10/20 06:53:23 INFO mlflow.models.cli: Selected backend for flavor 'python_function'
2021/10/20 06:53:23 INFO mlflow.pyfunc.backend: === Running command 'gunicorn --timeout=60 -b 127.0.0.1:5000 -w 1 ${GUNICORN_CMD_ARGS} -- mlflow.pyfunc.scoring_server.wsgi:app'
[2021-10-20 06:53:23 +0900] [39288] [INFO] Starting gunicorn 20.1.0
[2021-10-20 06:53:23 +0900] [39288] [ERROR] Connection in use: ('127.0.0.1', 5000)
[2021-10-20 06:53:23 +0900] [39288] [ERROR] Retrying in 1 second.
[2021-10-20 06:53:24 +0900] [39288] [ERROR] Connection in use: ('127.0.0.1', 5000)
[2021-10-20 06:53:24 +0900] [39288] [ERROR] Retrying in 1 second.
[2021-10-20 06:53:25 +0900] [39288] [ERROR] Connection in use: ('127.0.0.1', 5000)
[2021-10-20 06:53:25 +0900] [39288] [ERROR] Retrying in 1 second.
[2021-10-20 06:53:26 +0900] [39288] [ERROR] Connection in use: ('127.0.0.1', 5000)
[2021-10-20 06:53:26 +0900] [39288] [ERROR] Retrying in 1 second.
[2021-10-20 06:53:27 +0900] [39288] [ERROR] Connection in use: ('127.0.0.1', 5000)
[2021-10-20 06:53:27 +0900] [39288] [ERROR] Retrying in 1 second.
[2021-10-20 06:53:28 +0900] [39288] [ERROR] Can't connect to ('127.0.0.1', 5000)
解決策: --port <port番号>
で指定するか mlflow ui
をストップしてから models serveをrunする