-
Notifications
You must be signed in to change notification settings - Fork 1.9k
TextNormalizing export to Onnx #4781
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 all commits
73cb012
d7ef927
bf8397d
2e11361
63e394a
b6d8e1c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
using Microsoft.ML.CommandLine; | ||
using Microsoft.ML.Data; | ||
using Microsoft.ML.Internal.Utilities; | ||
using Microsoft.ML.Model.OnnxConverter; | ||
using Microsoft.ML.Runtime; | ||
using Microsoft.ML.Transforms.Text; | ||
|
||
|
@@ -194,7 +195,7 @@ private static IRowMapper Create(IHostEnvironment env, ModelLoadContext ctx, Dat | |
|
||
private protected override IRowMapper MakeRowMapper(DataViewSchema schema) => new Mapper(this, schema); | ||
|
||
private sealed class Mapper : OneToOneMapperBase | ||
private sealed class Mapper : OneToOneMapperBase, ISaveAsOnnx | ||
{ | ||
private readonly DataViewType[] _types; | ||
private readonly TextNormalizingTransformer _parent; | ||
|
@@ -212,6 +213,44 @@ public Mapper(TextNormalizingTransformer parent, DataViewSchema inputSchema) | |
} | ||
} | ||
|
||
public bool CanSaveOnnx(OnnxContext ctx) => (_parent._keepDiacritics && _parent._keepNumbers && _parent._keepPunctuations); | ||
|
||
public void SaveAsOnnx(OnnxContext ctx) | ||
{ | ||
Host.CheckValue(ctx, nameof(ctx)); | ||
for (int iinfo = 0; iinfo < _types.Length; ++iinfo) | ||
{ | ||
string inputColumnName = _parent.ColumnPairs[iinfo].inputColumnName; | ||
if (!ctx.ContainsColumn(inputColumnName)) | ||
continue; | ||
|
||
string outputColumnName = _parent.ColumnPairs[iinfo].outputColumnName; | ||
string srcVariableName = ctx.GetVariableName(inputColumnName); | ||
string dstVariableName = ctx.AddIntermediateVariable(_types[iinfo], outputColumnName, true); | ||
SaveAsOnnxCore(ctx, srcVariableName, dstVariableName); | ||
} | ||
} | ||
|
||
private void SaveAsOnnxCore(OnnxContext ctx, string srcVariableName, string dstVariableName) | ||
{ | ||
// StringNormalizer only takes input of shapes [C] or [1,C], | ||
// so the input is squeezed to support inferred shapes ( e.g. [-1,C] ). | ||
var opType = "Squeeze"; | ||
var squeezeOutput = ctx.AddIntermediateVariable(null, "SqueezeOutput", true); | ||
var node = ctx.CreateNode(opType, srcVariableName, squeezeOutput, ctx.GetNodeName(opType), ""); | ||
node.AddAttribute("axes", new long[] { 0 }); | ||
|
||
opType = "StringNormalizer"; | ||
var normalizerOutput = ctx.AddIntermediateVariable(null, "NormalizerOutput", true); | ||
node = ctx.CreateNode(opType, squeezeOutput, normalizerOutput, ctx.GetNodeName(opType), ""); | ||
var isCaseChange = (_parent._caseMode == TextNormalizingEstimator.CaseMode.Lower) ? "LOWER" : | ||
(_parent._caseMode == TextNormalizingEstimator.CaseMode.Upper) ? "UPPER" : "NONE"; | ||
node.AddAttribute("case_change_action", isCaseChange); | ||
|
||
opType = "Unsqueeze"; | ||
node = ctx.CreateNode(opType, normalizerOutput, dstVariableName, ctx.GetNodeName(opType), ""); | ||
node.AddAttribute("axes", new long[] { 0 }); | ||
} | ||
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. If its 'None' there is no need for the 'StringNormalizer' node at all 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. Well, in the case that we are able to add the other options in the future, it would make sense to keep the option to pass "None". What do you think? |
||
protected override DataViewSchema.DetachedColumn[] GetOutputColumnsCore() | ||
{ | ||
var result = new DataViewSchema.DetachedColumn[_parent.ColumnPairs.Length]; | ||
|
Uh oh!
There was an error while loading. Please reload this page.