0
0
Ml-pythonHow-ToBeginner ยท 3 min read

How to Log Artifacts in MLflow: Simple Guide with Examples

To log artifacts in MLflow, use mlflow.log_artifact() to save a single file or mlflow.log_artifacts() to save all files in a directory. These functions store files like models, images, or logs in the current MLflow run's artifact storage.
๐Ÿ“

Syntax

The main functions to log artifacts in MLflow are:

  • mlflow.log_artifact(local_path, artifact_path=None): Logs a single file from local_path. Optionally, you can specify artifact_path to organize artifacts in folders.
  • mlflow.log_artifacts(local_dir, artifact_path=None): Logs all files from the directory local_dir. You can also specify artifact_path to place them inside a folder.

Both functions must be called inside an active MLflow run context.

python
import mlflow

# Log a single file
mlflow.log_artifact(local_path='path/to/file.txt', artifact_path='my_folder')

# Log all files in a directory
mlflow.log_artifacts(local_dir='path/to/folder', artifact_path='my_folder')
๐Ÿ’ป

Example

This example shows how to create a text file and log it as an artifact in MLflow. It demonstrates starting a run, logging the artifact, and ending the run.

python
import mlflow

# Create a sample file to log
file_name = 'example.txt'
with open(file_name, 'w') as f:
    f.write('Hello, MLflow artifacts!')

# Start an MLflow run
with mlflow.start_run():
    # Log the file as an artifact
    mlflow.log_artifact(file_name, artifact_path='texts')
    print(f'Logged artifact: {file_name}')
Output
Logged artifact: example.txt
โš ๏ธ

Common Pitfalls

  • Not starting an MLflow run: You must call mlflow.start_run() before logging artifacts, or you will get an error.
  • Incorrect file paths: Ensure the file or directory exists at the given path before logging.
  • Confusing log_artifact and log_artifacts: Use log_artifact for single files and log_artifacts for directories.
python
import mlflow

# Wrong: logging without a run
try:
    mlflow.log_artifact('example.txt')
except Exception as e:
    print(f'Error: {e}')

# Right: logging inside a run
with mlflow.start_run():
    mlflow.log_artifact('example.txt')
    print('Artifact logged successfully')
Output
Error: No active run. mlflow.start_run() should be used to start a run. Artifact logged successfully
๐Ÿ“Š

Quick Reference

FunctionPurposeParameters
mlflow.log_artifactLog a single file as an artifactlocal_path: file path, artifact_path: optional folder
mlflow.log_artifactsLog all files in a directorylocal_dir: directory path, artifact_path: optional folder
mlflow.start_runStart an MLflow run contextNo parameters needed
mlflow.end_runEnd the current MLflow runNo parameters needed
โœ…

Key Takeaways

Always call mlflow.start_run() before logging artifacts to create an active run.
Use mlflow.log_artifact() for single files and mlflow.log_artifacts() for directories.
Specify artifact_path to organize artifacts inside folders in the run's storage.
Check that file or directory paths exist before logging to avoid errors.
Artifacts are stored with the run and can be viewed or downloaded later from the MLflow UI.