-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Added the assembly name of the custom transform to the model file #4989
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
2a2b655
87eb370
c77a09e
47966e7
db3dcb7
2fd1d40
019d7cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,9 @@ | |
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Diagnostics.Contracts; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just curious, why do we need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
using System.IO; | ||
using System.Reflection; | ||
using System.Text; | ||
using Microsoft.ML; | ||
using Microsoft.ML.Data; | ||
|
@@ -40,14 +42,17 @@ private static VersionInfo GetVersionInfo() | |
{ | ||
return new VersionInfo( | ||
modelSignature: "CUSTOMXF", | ||
verWrittenCur: 0x00010001, | ||
verReadableCur: 0x00010001, | ||
//verWrittenCur: 0x00010001, // Initial | ||
verWrittenCur: 0x00010002, // Added name of assembly in which the contractName is present | ||
verReadableCur: 0x00010002, | ||
verWeCanReadBack: 0x00010001, | ||
loaderSignature: LoaderSignature, | ||
loaderAssemblyName: typeof(LambdaTransform).Assembly.FullName); | ||
} | ||
|
||
internal static void SaveCustomTransformer(IExceptionContext ectx, ModelSaveContext ctx, string contractName) | ||
private const uint VerAssemblyNameSaved = 0x00010002; | ||
|
||
internal static void SaveCustomTransformer(IExceptionContext ectx, ModelSaveContext ctx, string contractName, string contractAssembly) | ||
{ | ||
ectx.CheckValue(ctx, nameof(ctx)); | ||
ectx.CheckValue(contractName, nameof(contractName)); | ||
|
@@ -56,16 +61,24 @@ internal static void SaveCustomTransformer(IExceptionContext ectx, ModelSaveCont | |
ctx.SetVersionInfo(GetVersionInfo()); | ||
|
||
ctx.SaveString(contractName); | ||
ctx.SaveString(contractAssembly); | ||
} | ||
|
||
// Factory for SignatureLoadModel. | ||
private static ITransformer Create(IHostEnvironment env, ModelLoadContext ctx) | ||
{ | ||
Contracts.CheckValue(env, nameof(env)); | ||
env.CheckValue(ctx, nameof(ctx)); | ||
ctx.CheckAtModel(GetVersionInfo()); | ||
var versionInfo = GetVersionInfo(); | ||
ctx.CheckAtModel(versionInfo); | ||
|
||
var contractName = ctx.LoadString(); | ||
if (ctx.Header.ModelVerWritten >= VerAssemblyNameSaved) | ||
{ | ||
var contractAssembly = ctx.LoadString(); | ||
Assembly assembly = Assembly.Load(contractAssembly); | ||
env.ComponentCatalog.RegisterAssembly(assembly); | ||
} | ||
|
||
object factoryObject = env.ComponentCatalog.GetExtensionValue(env, typeof(CustomMappingFactoryAttributeAttribute), contractName); | ||
if (!(factoryObject is ICustomMappingFactory mappingFactory)) | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -184,6 +184,10 @@ public void RegisterTypeWithAttribute() | |||||||||||||||||||||||||||||||||||||||||||||
var tribeTransformed = model.Transform(tribeDataView); | ||||||||||||||||||||||||||||||||||||||||||||||
var tribeEnumerable = ML.Data.CreateEnumerable<SuperAlienHero>(tribeTransformed, false).ToList(); | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
// save and reload the model | ||||||||||||||||||||||||||||||||||||||||||||||
ML.Model.Save(model, tribeDataView.Schema, "customTransform.zip"); | ||||||||||||||||||||||||||||||||||||||||||||||
var modelSaved = ML.Model.Load("customTransform.zip", out var tribeDataViewSaved); | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
// Make sure the pipeline output is correct. | ||||||||||||||||||||||||||||||||||||||||||||||
Assert.Equal(tribeEnumerable[0].Name, "Super " + tribe[0].Name); | ||||||||||||||||||||||||||||||||||||||||||||||
Assert.Equal(tribeEnumerable[0].Merged.Age, tribe[0].One.Age + tribe[0].Two.Age); | ||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -192,7 +196,7 @@ public void RegisterTypeWithAttribute() | |||||||||||||||||||||||||||||||||||||||||||||
Assert.Equal(tribeEnumerable[0].Merged.HandCount, tribe[0].One.HandCount + tribe[0].Two.HandCount); | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
// Build prediction engine from the trained pipeline. | ||||||||||||||||||||||||||||||||||||||||||||||
var engine = ML.Model.CreatePredictionEngine<AlienHero, SuperAlienHero>(model); | ||||||||||||||||||||||||||||||||||||||||||||||
var engine = ML.Model.CreatePredictionEngine<AlienHero, SuperAlienHero>(modelSaved); | ||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be good to have this test both for the model before saving it, and for the model that is loaded from disk. Ideally both models should behave the same, but if a bug is introduced that only appears in one of both cases, it might be good to have both of the tests. The machinelearning/test/Microsoft.ML.Tests/PermutationFeatureImportanceTests.cs Lines 30 to 51 in cacc72f
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||||||||||||||||||||||||||||||||
var alien = new AlienHero("TEN.LM", 1, 2, 3, 4, 5, 6, 7, 8); | ||||||||||||||||||||||||||||||||||||||||||||||
var superAlien = engine.Predict(alien); | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any case where the loaded model would actually require having a different name registered from the "FullName" retrieved from here? #Resolved
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or any case where trying to access that member of
_mapAction
would throw? #ResolvedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The call to Method can throw a MemberAccessException. But that would be up to the caller to fix in their code and the exception would help with that.
In reply to: 401790783 [](ancestors = 401790783)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should enforce the idea that the same transform that is used in training should be used in prediction as well. If they were to be different, then they are not the same pipelines and not the same models.
In reply to: 401431899 [](ancestors = 401431899)