pythonpython-3.xmlflowmlops

How to download artifacts from mlflow in python


I am creating an mlflow experiment which logs a logistic regression model together with a metric and an artifact.

import mlflow
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import precision_recall_fscore_support

with mlflow.start_run(run_name=run_name, experiment_id=experiment_id):

        logreg = LogisticRegression()
        logreg.fit(x_train, y_train)
        print('training over', flush=True)
        y_pred = logreg.predict(x_test)
        mlflow.sklearn.log_model(logreg, "model")
   
        mlflow.log_metric("f1", precision_recall_fscore_support(y_test, y_pred, average='weighted')[2])
        mlflow.log_artifact(x_train.to_csv('train.csv')

for some data (x_train, y_train, x_test, y_test)

Is there any way to access the artifacts for that specific experiment_id for this run_name and read the train.csv and also read the model ?


Solution

  • There is a download_artifacts function that allows you to get access to the logged artifact:

    from mlflow.client import MlflowClient
    client = MlflowClient()
    local_path = client.download_artifacts(run_id, "train.csv", local_dir)
    

    The model artifact could either downloaded using the same function (there should be the object called model/model.pkl (for scikit-learn, or something else), or you can load model by run:

    loaded_model = mlflow.pyfunc.load_model(f"runs:/{run_id}/model")