Repository navigation
fix(ml): scores of every label and the model description in a prediction - #91
Conversation
…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.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
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 Report❌ Patch coverage is 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. 🚀 New features to boost your workflow:
|
…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.
|
@codex review |
|
Codex Review: Didn't find any major issues. Delightful! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
What
ClassifyImagereturned only the winning label.ImagePrediction.ScoresandImagePrediction.ModelInfowere leftnull(the code filling them was commented out inImageClassificationService.Predict), and a model saved without theLabels.txtentry 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.ImageClassification1.1.0 toTailoredApps.Shared.MediatR.ML2.1.0, without any error.Changes
ImageClassificationService.PredictfillsScores(label -> score) andModelInfoagain, and both belong to the model that made the prediction.Labels.txtis not used for a prediction.ModelFilePathis 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.Scoresstaysnullwhen the model does not name its scores or the names do not match them one to one.ModelInfostaysnullwhenModelFilePathis 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 withoutLabels.txtits labels come from the loaded model.PredictionEnginePoolAdapterpredicts and reports the labels and the model in one call, through an internal contract.GetLabelsreturns the engine to the pool and answers with an empty array for a model whose scores are not named, instead of throwing.ModelHelper.GetChecksumkeeps the checksum of a model file until its size or write time changes.ModelHelper.GetVersion/GetLabelsopen the file with read-only, shared access. The default access asked for write as well, so a read-only model file reportedUNKNOWN, and two requests reading at the same time could lock each other out.No public API changes: the build with
-p:ValidatePublicApi=truepasses.Behaviour that changes for an application on 2.1.0
ScoresandModelInfoare filled where they werenull.GetModelInfo().Labelsis filled for a model withoutLabels.txtwhere it was empty.GetVersion/GetLabelssucceed on a read-only model file where they returnedUNKNOWN/ 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 ownLabels.txttakes 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 onnet10.0andnet11.0;MediatR.ML.Tests107 passed, 1 skipped (the training test that was already skipped).PredictionEnginePool.mkdocs build --strictpasses for both sites.Labels.txt, three images, .NET 11, on Windows and inmcr.microsoft.com/dotnet/aspnet:11.0(model mounted read-only):TailoredApps.Shared.MediatR.ImageClassification1.1.0 returns for these images;Docs
docs/Libraries/MediatR/ML.md,docs-en/Libraries/MediatR/ML.mdand the packageREADME.mddescribe the two fields, when they arenull, where labels come from and the checksum.Not in this PR
masterhas the same commented-out code inTailoredApps.Shared.Mediator.ML; it needs the same change.