DVC Pipelines : How to Build a Reproducible Machine Learning Pipeline

DVC Pipelines : How to Build a Reproducible Machine Learning Pipeline
2 min de lecture

PyImageSearch publishes a tutorial dedicated to building a reproducible machine learning pipeline with DVC, a data and model versioning tool that works with Git. Authored by Vikram Singh, this guide shows how to chain several steps, from data preprocessing to evaluation, to rerun them with a single command.

The example uses the Iris dataset, a classic in the field. The raw iris.csv file is first split into a training set and a test set, using an 80/20 ratio that preserves class proportions. Next, a logistic regression model is trained, followed by an evaluation that calculates accuracy and produces a classification report. The tutorial specifies that the entire project uses only three libraries: DVC 3.48.4, pandas 2.1.4, and scikit-learn 1.3.2.

Three steps linked by a configuration file

Each step is described in a dvc.yaml file, which specifies the command to execute, the files the step depends on, and the results it produces. The outputs of one step serve as inputs to the next. Thus, a change to the dataset reruns the entire pipeline, whereas a change only to the training script reruns only the training and evaluation.

These dependencies are recorded in a dvc.lock file, which captures the exact state of the pipeline when it runs. According to the author, this file should be versioned with Git, allowing another team member, or a continuous integration server, to reproduce the results with the commands dvc pull then dvc repro. When nothing has changed, DVC indicates that the steps are up to date and performs no computation.

Inspecting and comparing runs

The tutorial presents several inspection commands. dvc dag displays the step graph as a diagram, dvc status indicates which dependencies have changed, and dvc diff compares outputs with the last recorded version. For performance tracking, dvc metrics show displays the metrics from the latest run, and dvc metrics diff compares them from one commit to another.

In the presented example, the evaluation on 30 test samples yields an accuracy of 0.9333. The author notes that the same logic allows tracking the effect of a change in parameters or data, spotting both improvements and regressions.

The tutorial finally mentions optional extensions. Hyperparameters can be moved to a params.yaml file, plots can be tracked as outputs, and new steps, such as feature engineering or hyperparameter search, can be added to the graph without modifying the other steps. DVC then handles recalculating only the affected parts.