TailoredApps.Shared.Mediator.ML
3.0.0-preview.3
dotnet add package TailoredApps.Shared.Mediator.ML --version 3.0.0-preview.3
NuGet\Install-Package TailoredApps.Shared.Mediator.ML -Version 3.0.0-preview.3
<PackageReference Include="TailoredApps.Shared.Mediator.ML" Version="3.0.0-preview.3" />
<PackageVersion Include="TailoredApps.Shared.Mediator.ML" Version="3.0.0-preview.3" />
<PackageReference Include="TailoredApps.Shared.Mediator.ML" />
paket add TailoredApps.Shared.Mediator.ML --version 3.0.0-preview.3
#r "nuget: TailoredApps.Shared.Mediator.ML, 3.0.0-preview.3"
#:package TailoredApps.Shared.Mediator.ML@3.0.0-preview.3
#addin nuget:?package=TailoredApps.Shared.Mediator.ML&version=3.0.0-preview.3&prerelease
#tool nuget:?package=TailoredApps.Shared.Mediator.ML&version=3.0.0-preview.3&prerelease
TailoredApps.Shared.Mediator.ML
Image classification with ML.NET as mediator requests. ClassifyImage runs a trained model on an uploaded image
and TrainImageClassificationModel trains a new model from folders of labelled images. The code lives in the
namespace TailoredApps.Shared.Mediator.ImageClassification, not under the package name.
Installation
dotnet add package TailoredApps.Shared.Mediator.ML
Registration
using Microsoft.Extensions.ML;
using TailoredApps.Shared.Mediator.ImageClassification.Infrastructure;
using TailoredApps.Shared.Mediator.ImageClassification.Interfaces.Domain.Models;
// The handlers of both commands are in this package's assembly
builder.Services.AddMediator(cfg =>
{
cfg.RegisterServicesFromAssembly(typeof(Program).Assembly);
cfg.RegisterServicesFromAssembly(typeof(AddPredictionEngineExtension).Assembly);
});
builder.Services.AddPredictionEngine(engine =>
engine.RegisterMachineLearningModel<InMemoryImageData, ImagePredictionScore>(pool =>
pool.FromModelFile(builder.Configuration[ImageClassificationOptions.ModelFilePathConfig]!)));
AddPredictionEngine registers IImageClassificationService, IModelInfoService and IModelHelper (scoped)
and binds ImageClassificationOptions. RegisterMachineLearningModel adds the ML.NET PredictionEnginePool;
where the model is loaded from is decided by the pool builder you configure (FromModelFile above), not by the
options. FromModelFile is the loader of this library: it describes the model from the same bytes it loads it from.
Usage
using TailoredApps.Shared.Mediator.ImageClassification.Domain.DataModel.Message.Commands;
var classified = await mediator.Send(new ClassifyImage { FileByteArray = imageBytes, FileName = "photo.jpg" });
var label = classified.ImagePrediction?.PredictedLabel;
var score = classified.ImagePrediction?.PredictedScore;
var scores = classified.ImagePrediction?.Scores; // label -> score, for every label of the model
var model = classified.ImagePrediction?.ModelInfo; // Name, Checksum, Version, Labels
var trained = await mediator.Send(new TrainImageClassificationModel
{
Source = "pets", // folder inside TrainingRoot, one sub-folder per label
ModelDestFolderPath = "pets.zip", // model file inside ModelsRoot
});
// trained.ModelPath, trained.ModelVersion, trained.Labels, trained.ModelInfo (evaluation metrics as text)
Configuration
The ImageClassification section:
{
"ImageClassification": {
"ModelFilePath": "/var/app/models/current.zip",
"TrainingRoot": "/var/app/training-sets",
"ModelsRoot": "/var/app/models",
"MaxImageBytes": 10485760
}
}
| Key | Meaning | Default |
|---|---|---|
ModelFilePath |
Model file described by IImageClassificationService.GetModelInfo() (name, checksum, version, labels); the registration above also loads the model from it |
none |
TrainingRoot |
Directory that training sets must be inside; required for training | none |
ModelsRoot |
Directory that trained models are written inside; required for training | none |
MaxImageBytes |
Largest image accepted by ClassifyImage; 0 turns the limit off |
10485760 (10 MB) |
Good to know
- Built on ForkForward.Mediator, a fork of MediatR 12.5.0 under the Apache-2.0 licence, not on the MediatR package: requests and handlers use the
ForkForward.Mediatornamespace and the mediator is registered withAddMediator(...). In 2.x this package was namedTailoredApps.Shared.MediatR.ML. - The section is required: without it the options throw
InvalidOperationExceptionwhen first used. ClassifyImagethrowsArgumentExceptionfor empty content, for content overMaxImageBytesand for content that does not start with a JPEG or PNG signature.- The returned
ImagePredictionhasFileName,PredictedLabelandPredictedScore, the score of every label inScoresand the description of the model inModelInfo. Both belong to the model that made the prediction: the labels are the ones the loaded model names its scores with, and the description (file name, checksum, version) is recorded byFromModelFilewhen it loads the model, from the same bytes. Replacing the file later does not change what the predictions of an already loaded model report; withwatchForChangesthe pool loads the new file and its predictions carry the new description. A prediction never combines the scores of one model with the description of the other, also while the file is being replaced. A file that cannot be read yet (still being written, or held by its writer) is tried again for about a minute after the last change; until it loads, the loaded model stays in use. Watching starts before the first load, so a file replaced while the application starts is picked up as well, and disposing the service provider leaves the file closed. Scoresisnullwhen the model does not name its scores or the names do not match them one to one.ModelInfoisnullwhen the model was loaded in another way (theFromFileof ML.NET, a URI), when the pool replaced the model while the prediction ran, and with your ownIPredictionEnginePoolAdapter: in each case no file can be proven to be the model that predicted.IImageClassificationService.GetModelInfo()describes the file as it is now. Its labels come from theLabels.txtentry; a file saved without it (not trained with this library) gets the labels of the loaded model.- The checksum of a model file is computed once and again only when the file's size or write time changes.
- Training paths are resolved against
TrainingRootandModelsRoot. A path outside its root throwsArgumentException; a root that is not configured throwsInvalidOperationException. - Training reads files with the extension
.jpgor.pngfromSourceand its sub-folders and uses the name of the folder a file is in as its label. It runs inside the request and can take long; a fifth of the images is held back for evaluation. The saved model is a zip archive to whichVersion.txt(ayyyyMMdd.HHmmsstimestamp) andLabels.txtare added. - Prediction uses the default model of the pool, so register the model without a model name.
PredictionEngineServiceConfigurationis the one public type in the namespaceTailoredApps.Shared.Mediator.ML.Infrastructure.
Documentation
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. net11.0 is compatible. |
-
net10.0
- FluentValidation (>= 12.2.0)
- Microsoft.Extensions.Caching.Abstractions (>= 10.0.15)
- Microsoft.Extensions.DependencyInjection (>= 10.0.15)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.15)
- Microsoft.Extensions.ML (>= 6.0.0)
- Microsoft.Extensions.Options (>= 10.0.15)
- Microsoft.ML (>= 6.0.0)
- Microsoft.ML.AutoML (>= 0.24.0)
- Microsoft.ML.ImageAnalytics (>= 6.0.0)
- Microsoft.ML.Vision (>= 6.0.0)
- TailoredApps.Shared.Mediator (>= 3.0.0-preview.3)
-
net11.0
- FluentValidation (>= 12.2.0)
- Microsoft.Extensions.DependencyInjection (>= 11.0.0)
- Microsoft.Extensions.ML (>= 6.0.0)
- Microsoft.ML (>= 6.0.0)
- Microsoft.ML.AutoML (>= 0.24.0)
- Microsoft.ML.ImageAnalytics (>= 6.0.0)
- Microsoft.ML.Vision (>= 6.0.0)
- TailoredApps.Shared.Mediator (>= 3.0.0-preview.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 3.0.0-preview.3 | 47 | 12/2/2026 |