Skip to content

fix(ml): scores of every label and the model description in a prediction - #91

Merged
nieprzecietny merged 2 commits into
release/2.1from
fix/ml-scores-and-model-info
Dec 2, 2026
Merged

nieprzecietny merged 2 commits into
release/2.1from
fix/ml-scores-and-model-info

Conversation

@nieprzecietny

@nieprzecietny nieprzecietny commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

What

ClassifyImage returned only the winning label. ImagePrediction.Scores and ImagePrediction.ModelInfo were left null (the code filling them was commented out in ImageClassificationService.Predict), and a model saved without the Labels.txt entry had no labels at all, because 2.x reads labels only from that entry.

An application that stores every score together with the model's name, version and checksum therefore got nothing to store after moving from TailoredApps.Shared.MediatR.ImageClassification 1.1.0 to TailoredApps.Shared.MediatR.ML 2.1.0, without any error.

Changes

  • ImageClassificationService.Predict fills Scores (label -> score) and ModelInfo again, and both belong to the model that made the prediction.
    • The labels are the ones the loaded model names its scores with, read from the engine that made the prediction. Labels.txt is not used for a prediction.
    • The file at ModelFilePath is described once for every loaded model, when that model is first used. When the file is replaced and the pool does not reload it (watchForChanges: false), predictions keep the labels and the description of the model that still makes them.
    • Scores stays null when the model does not name its scores or the names do not match them one to one.
    • ModelInfo stays null when ModelFilePath is not configured or the file cannot be read, when the pool replaced the model while the prediction ran, and with an adapter other than the library's (the model behind it cannot be told apart from the file). A prediction that works today keeps working.
  • GetModelInfo() still describes the file as it is now; for a file without Labels.txt its labels come from the loaded model.
  • PredictionEnginePoolAdapter predicts and reports the labels and the model in one call, through an internal contract. GetLabels returns the engine to the pool and answers with an empty array for a model whose scores are not named, instead of throwing.
  • ModelHelper.GetChecksum keeps the checksum of a model file until its size or write time changes.
  • ModelHelper.GetVersion / GetLabels open the file with read-only, shared access. The default access asked for write as well, so a read-only model file reported UNKNOWN, and two requests reading at the same time could lock each other out.

No public API changes: the build with -p:ValidatePublicApi=true passes.

Behaviour that changes for an application on 2.1.0

  • Scores and ModelInfo are filled where they were null.
  • GetModelInfo().Labels is filled for a model without Labels.txt where it was empty.
  • GetVersion / GetLabels succeed on a read-only model file where they returned UNKNOWN / no labels.

Review

The first commit read the labels and the description from the model file at the time of every prediction. As the review pointed out, a file replaced without a reload would then attribute scores to the labels of another model. The second commit binds both to the loaded model; the scenario from the review is a test (When_Model_File_Is_Replaced_Without_A_Reload_...): a model of colours is loaded, a model of shapes with its own Labels.txt takes the file's place, and predictions keep the colours and the first checksum.

Checks

  • dotnet build TailoredApps.Shared.slnx -c Release -p:ValidatePublicApi=true: 0 errors, 0 warnings.
  • dotnet test TailoredApps.Shared.slnx -c Release: every project passes on net10.0 and net11.0; MediatR.ML.Tests 107 passed, 1 skipped (the training test that was already skipped).
  • The adapter tests train small ML.NET models in the test, save them and load them through PredictionEnginePool.
  • mkdocs build --strict passes for both sites.
  • A real model: a 95 MB image model from 2021 without Labels.txt, three images, .NET 11, on Windows and in mcr.microsoft.com/dotnet/aspnet:11.0 (model mounted read-only):
    • the same label, the same five scores and the same model name, version and checksum as TailoredApps.Shared.MediatR.ImageClassification 1.1.0 returns for these images;
    • about 70 ms per image on Windows and about 130 ms in the container, with the model description included.

Docs

docs/Libraries/MediatR/ML.md, docs-en/Libraries/MediatR/ML.md and the package README.md describe the two fields, when they are null, where labels come from and the checksum.

Not in this PR

  • master has the same commented-out code in TailoredApps.Shared.Mediator.ML; it needs the same change.
  • No tag: the release is a separate step.

…description again

ClassifyImage returned only the winning label: ImagePrediction.Scores and ModelInfo were left null, and a
model saved without the Labels.txt entry (one not trained with this library) had no labels at all. An
application that stores every score with the model's name, version and checksum got nothing to store.

- ImageClassificationService.Predict fills Scores (label -> score) and ModelInfo. Labels come from
  Labels.txt and, when the model file has none, from the slot names of the loaded model; GetModelInfo uses
  the same fallback.
- Scores stays null when the labels are not known or do not match the scores one to one. ModelInfo stays
  null when ModelFilePath is not configured or the file cannot be read: the pool decides where the model
  is loaded from, so a prediction never fails because of that file.
- ModelHelper keeps the checksum of a model file until its size or write time changes. A prediction with
  the description of a 95 MB model takes about 70 ms instead of about 270 ms.
- ModelHelper reads the version and the labels with read-only, shared access. The default access asked
  for write as well, so a read-only model file reported UNKNOWN and two concurrent reads could lock each
  other out.
- PredictionEnginePoolAdapter.GetLabels returns the engine to the pool and answers with an empty array
  for a model whose scores are not named, instead of throwing.

No public API changes. Tests cover the service, the model helper and the adapter against small models
trained in the test; the docs and the package README describe the fields.
@nieprzecietny
nieprzecietny requested a review from a team as a code owner December 2, 2026 00:09
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-02T00:23:01.162318Z d69b72b Manual request
🔒 Security Review ✅ Completed 2026-12-02T00:11:43.015255Z 7210c08 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

var labels = modelInfoService.Labels;
if (labels != null && labels.Length > 0)
{
return labels;

P2 Badge Bind score labels to the model that produced the prediction

When the configured model file is replaced while the pool still holds the previous model (for example, with watchForChanges: false, as used in the adapter tests), this reads labels from the replacement file rather than the model that produced prediction.Score. If both models have the same number of classes but different labels or ordering, MapScores accepts them and silently attributes scores to the wrong classes; DescribeModel also identifies the replacement as the model that produced the prediction. Obtain labels and metadata from the same loaded-model snapshot used for inference, or withhold these fields when correspondence cannot be established.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@codecov

codecov Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.85714% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.20%. Comparing base (bac292c) to head (d69b72b).
⚠️ Report is 1 commits behind head on release/2.1.

Files with missing lines Patch % Lines
...MediatR.ML/Infrastructure/ClassificationService.cs 91.30% 0 Missing and 4 partials ⚠️
...R.ML/Infrastructure/PredictionEnginePoolAdapter.cs 88.88% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@               Coverage Diff               @@
##           release/2.1      #91      +/-   ##
===============================================
+ Coverage        89.67%   90.20%   +0.53%     
===============================================
  Files              135      136       +1     
  Lines             4638     3899     -739     
  Branches           511      526      +15     
===============================================
- Hits              4159     3517     -642     
+ Misses             364      264     -100     
- Partials           115      118       +3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

…odel that made it

Review of the previous commit: the labels were read from the Labels.txt entry of the model file and the
description from that file at the time of every prediction. When the file is replaced while the pool keeps
the model it loaded (watchForChanges: false), a model with as many classes but other labels would have its
scores attributed to the wrong labels, and the description would name the replacement.

- The adapter the library registers predicts and reports, in one call, the labels of the engine that made
  the prediction and the model the pool held meanwhile (an internal contract; no public API change).
- Scores are keyed by those labels only. Labels.txt is no longer used for a prediction; GetModelInfo(),
  which describes the file, still reads it and falls back to the loaded model.
- The file is described once for every loaded model, when that model is first used, and the description is
  kept for as long as the model lives. A replaced file changes the description only after the pool reloads it.
- ModelInfo stays null when the pool replaced the model while the prediction ran, and with an adapter other
  than the library's, because the model behind it cannot be told apart from the file.

Tests: the service against an adapter that reports its model (replaced file, reloaded model, model swapped
during a prediction), and the whole path against a real pool: a model of colours is loaded, a model of
shapes with its own Labels.txt takes the file's place, and predictions keep the colours and the first
checksum.
@nieprzecietny

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: d69b72b8e9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@nieprzecietny
nieprzecietny merged commit 6d2f554 into release/2.1 Dec 2, 2026
3 checks passed
@nieprzecietny
nieprzecietny deleted the fix/ml-scores-and-model-info branch December 2, 2026 00:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant