Skip to main content

Fixed: dvc status Says a Model Is "modified" After Retraining with the Same Data

· 11 min read
Jagdish Kumawat
Founder @ Dewiride

After I retrained a model with the same data and the same settings, dvc status reported modified: models/model.skops, although every metric came out exactly the same. The model was the same; the file wasn't, because skops writes a new file every time it saves. Comparing the two models' predictions proved it, and dvc checkout --force put the committed file back.

What I Was Doing​

A machine learning project that predicts hourly bike rentals saves its trained model with skops, a library that saves scikit-learn models safely, as models/model.skops. DVC (Data Version Control) tracks that file. I was working in a fresh clone of the project, with its data and model downloaded by dvc pull. DVC and the project's commands are installed in the project, so they run through uv run.

The model's .dvc file, committed in Git, records the MD5 hash of the committed model:

Terminal
cat models/model.skops.dvc
Output
outs:
- md5: 0d54c8f60b3f6c40bce5e52814297c28
size: 2177170
hash: md5
path: model.skops

The model file in the project folder had that same hash:

Terminal
md5 models/model.skops
Output
MD5 (models/model.skops) = 0d54c8f60b3f6c40bce5e52814297c28

Then I trained the model again, with the same data, the same settings and the same code, to check that training gives the same result:

Terminal
uv run train
Output
INFO bike_demand_forecast.train: Training on 7185 hours, testing on 1460 hours
INFO bike_demand_forecast.train: Saved models/model.skops and metrics.json
INFO bike_demand_forecast.train: Test MAE 32.56, RMSE 51.77, R2 0.8011

The test scores were the ones the committed model has.

What I Expected​

dvc status to report no changes, Data and pipelines are up to date., as it did before the retrain. Nothing that goes into the model had changed.

What I Got​

Terminal
uv run dvc status
Output
models/model.skops.dvc:
changed outs:
modified: models/model.skops

The terminal after uv run dvc status: under models/model.skops.dvc and changed outs, models/model.skops is listed as modified.

DVC saw a different model file.

What I Checked​

First, whether the scores really were the same. Training writes them to metrics.json, which Git tracks:

Terminal
git status --short

Git printed nothing: metrics.json came out byte for byte the same as the committed one, so the new model scored exactly the same, to four decimal places. Git doesn't see the model file itself, because DVC keeps it out of Git.

Then the model file's hash:

Terminal
md5 models/model.skops
Output
MD5 (models/model.skops) = d834c51550dd208bb55ebb1a732dca28

A different hash from the one in the .dvc file, 0d54c8f6…. dvc diff, which compares the project folder with the last commit, agreed:

Terminal
uv run dvc diff
Output
Modified:
models/model.skops

files summary: 1 modified

The terminal after the checks: git status --short prints nothing, md5 models/model.skops prints the new hash d834c51550dd208bb55ebb1a732dca28, and uv run dvc diff lists models/model.skops as modified, with files summary: 1 modified.

Why It Happens​

DVC decides whether a file changed by its hash. The dvc status reference defines modified as: "An output or dependency is found in the workspace, but the corresponding file hash in the dvc.lock or .dvc file is not up to date." The retrained file's MD5 hash, d834c515…, wasn't the 0d54c8f6… in models/model.skops.dvc, so DVC reported it, correctly.

The file changed because skops 0.16.0 never writes the same file twice. Every save stores Python object ids in the file's schema.json and the time of saving in each of its zip entries, and both differ from one save to the next. Why a skops File Changes Every Time You Save the Same Model opens two such files and shows both differences.

So the hash tells you the file is new, not that the model is. I reported this to the skops project as skops-dev/skops#574.

The Fix​

First decide which model you want:

  • You retrained on purpose, to make a new model: save it as a new version with dvc add models/model.skops, and commit the .dvc file. How to Save a New Version of a DVC-Tracked File shows each step.
  • The model should stay the committed one, as here: check that the retrained model really is the same, then put the committed file back. These steps do that.

1. Keep a Copy of the Retrained Model​

Terminal
cp models/model.skops retrained.skops

The next step deletes the retrained file in models, and DVC has no copy of it. This copy keeps it, in case the models turn out to differ.

2. Put the Committed Model Back​

Terminal
uv run dvc checkout --force models/model.skops
Output
Building workspace index |2.00 [00:00, 312entry/s]
Comparing indexes |3.00 [00:00, 3.04kentry/s]
Applying changes |1.00 [00:00, 769file/s]
M models/model.skops

The terminal after cp models/model.skops retrained.skops and uv run dvc checkout --force models/model.skops: three progress bars, then M models/model.skops.

dvc checkout copies the committed version from DVC's cache into the project folder, and M means it replaced the file. It needs --force here, because the file it replaces isn't in the cache: without it, the command stops with Fixed: "Can't remove the following unsaved files without confirmation" in dvc checkout.

3. Compare What the Two Models Predict​

This script loads both models and compares their predictions for every hour of the test set. Save it in the project folder as compare_models.py:

compare_models.py
import skops.io as sio

from bike_demand_forecast.data import FEATURES, TEST_FILE, load_rentals

# Reviewed: the trees a HistGradientBoosting model is made of. This project made both files.
TRUSTED_TYPES = ["sklearn.ensemble._hist_gradient_boosting.predictor.TreePredictor"]

test = load_rentals(TEST_FILE)
committed = sio.load("models/model.skops", trusted=TRUSTED_TYPES)
retrained = sio.load("retrained.skops", trusted=TRUSTED_TYPES)

same = (committed.predict(test[FEATURES]) == retrained.predict(test[FEATURES])).all()
print(f"Same predictions for all {len(test)} test hours: {same}")

The compare_models.py script in a macOS-style code window: it loads the test set, loads models/model.skops and retrained.skops with skops and one reviewed trusted type, and prints whether their predictions for every test hour are the same.

  • load_rentals, TEST_FILE and FEATURES come from the project's own code: the test set and the columns the model learns from.
  • sio.load loads each file. skops won't load the TreePredictor type unless you list it in trusted, and the list is written out because a person reviewed it. How to Find the Untrusted Types in a scikit-learn Model with skops explains why.
  • == compares the two arrays of predictions value by value, and .all() is True only when every value matches.

Run it:

Terminal
uv run python compare_models.py
Output
Same predictions for all 1460 test hours: True

The two files still have different hashes:

Terminal
md5 models/model.skops retrained.skops
Output
MD5 (models/model.skops) = 0d54c8f60b3f6c40bce5e52814297c28
MD5 (retrained.skops) = d834c51550dd208bb55ebb1a732dca28

The terminal after uv run python compare_models.py prints Same predictions for all 1460 test hours: True, and md5 shows models/model.skops back at 0d54c8f60b3f6c40bce5e52814297c28 while retrained.skops has d834c51550dd208bb55ebb1a732dca28.

Two different files, one model: it predicts the same for every one of the 1,460 test hours. The file in models is the committed one again.

4. Delete the Copy and the Script​

Neither is needed any more:

Terminal
rm retrained.skops compare_models.py

Had the predictions differed, the retrained model would be a different model. To keep it, move retrained.skops back to models/model.skops and save it as a new version, as in the first choice above.

After the Fix​

Terminal
git status --short
Terminal
uv run dvc status
Output
Data and pipelines are up to date.

The terminal after the fix: git status --short prints nothing, and uv run dvc status says Data and pipelines are up to date.

Git has nothing to commit, and DVC reports no changes: the project matches its last commit again.

Until skops Writes the Same File for the Same Model​

With skops 0.16.0, every retrain writes a new file, so dvc status will report the model as modified after every retrain, even when nothing about the model changed. Until skops-dev/skops#574 is fixed:

  • Judge a retrained model by its scores and predictions, never by dvc status alone.
  • Save a new version only when you mean to keep it, with dvc add.
  • Put the committed model back when it should stay, with dvc checkout --force, after keeping a copy.

Common Mistakes​

MistakeWhat happensDo this instead
Running dvc add after every retrain, to clear modifiedEvery retrain becomes a new version, another 2.2 MB copy in the cache and, after dvc push, in the remote, of the same modelCompare first, and dvc add only a model you mean to keep
Running dvc checkout --force without a copyThe retrained file is deleted, with no copy anywhereCopy it first, as in step 1
Comparing the files' hashes to compare the modelsThey always differ, even for the same modelCompare predictions

Troubleshooting​

SymptomLikely causeFix
modified: models/model.skops after a retrain, and metrics.json unchangedskops stores object ids and the save time in every new fileCompare predictions, then dvc checkout --force or dvc add
Can't remove the following unsaved files without confirmationdvc checkout without --forceAdd --force once the copy is kept
UntrustedTypesFoundException from compare_models.pyYour model holds a type skops doesn't trust by defaultReview the types the error names and list them in TRUSTED_TYPES

FAQ​

Why didn't Git show the change?​

Git doesn't see models/model.skops at all: when DVC started tracking it, it added the file to a .gitignore in models. And metrics.json, which Git does track, came out byte for byte the same.

Is the retrained model really the same?​

Here, yes: it predicted exactly the same for all 1,460 test hours, and its scores matched the committed ones to four decimal places.

Will DVC stop flagging the model once skops is fixed?​

If a skops release writes the same bytes for the same model, a retrain like this one gives the same hash, and dvc status stays quiet. skops-dev/skops#574 tracks it.

Conclusion​

dvc status reported modified: models/model.skops after a retrain with the same data, because skops 0.16.0 writes a new file, with a new hash, on every save. The scores were identical, and the retrained model predicted exactly the same for all 1,460 test hours, so it was the same model. dvc checkout --force put the committed file back, and DVC reported no changes.

Additional Resources​