TailoredApps.Shared.Mediator.ML 3.0.0-preview.3

This is a prerelease version of TailoredApps.Shared.Mediator.ML.
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
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TailoredApps.Shared.Mediator.ML" Version="3.0.0-preview.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TailoredApps.Shared.Mediator.ML" Version="3.0.0-preview.3" />
                    
Directory.Packages.props
<PackageReference Include="TailoredApps.Shared.Mediator.ML" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TailoredApps.Shared.Mediator.ML --version 3.0.0-preview.3
                    
#r "nuget: TailoredApps.Shared.Mediator.ML, 3.0.0-preview.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package TailoredApps.Shared.Mediator.ML@3.0.0-preview.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TailoredApps.Shared.Mediator.ML&version=3.0.0-preview.3&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=TailoredApps.Shared.Mediator.ML&version=3.0.0-preview.3&prerelease
                    
Install as a Cake Tool

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.Mediator namespace and the mediator is registered with AddMediator(...). In 2.x this package was named TailoredApps.Shared.MediatR.ML.
  • The section is required: without it the options throw InvalidOperationException when first used.
  • ClassifyImage throws ArgumentException for empty content, for content over MaxImageBytes and for content that does not start with a JPEG or PNG signature.
  • The returned ImagePrediction has FileName, PredictedLabel and PredictedScore, the score of every label in Scores and the description of the model in ModelInfo. 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 by FromModelFile when it loads the model, from the same bytes. Replacing the file later does not change what the predictions of an already loaded model report; with watchForChanges the 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.
  • Scores is null when the model does not name its scores or the names do not match them one to one. ModelInfo is null when the model was loaded in another way (the FromFile of ML.NET, a URI), when the pool replaced the model while the prediction ran, and with your own IPredictionEnginePoolAdapter: 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 the Labels.txt entry; 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 TrainingRoot and ModelsRoot. A path outside its root throws ArgumentException; a root that is not configured throws InvalidOperationException.
  • Training reads files with the extension .jpg or .png from Source and 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 which Version.txt (a yyyyMMdd.HHmmss timestamp) and Labels.txt are added.
  • Prediction uses the default model of the pool, so register the model without a model name.
  • PredictionEngineServiceConfiguration is the one public type in the namespace TailoredApps.Shared.Mediator.ML.Infrastructure.

Documentation

https://shared.tailoredapps.pl/en/Libraries/Mediator/ML/

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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