Skip to content
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

Alternate solution for ColumnConcatenatingTransformer #4875

Merged
merged 2 commits into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/Microsoft.ML.Data/Transforms/ColumnConcatenatingTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -896,15 +896,14 @@ public void SaveAsOnnx(OnnxContext ctx)
Host.CheckValue(ctx, nameof(ctx));
Contracts.Assert(CanSaveOnnx(ctx));

string opType = "Concat";
for (int iinfo = 0; iinfo < _columns.Length; ++iinfo)
{
var colInfo = _parent._columns[iinfo];
var boundCol = _columns[iinfo];

string outName = colInfo.Name;
var outColType = boundCol.OutputType;
if (!outColType.IsKnownSize)
if ((!outColType.IsKnownSize) || (!(outColType.GetItemType() is NumberDataViewType)))
{
ctx.RemoveColumn(outName, false);
continue;
Expand All @@ -925,10 +924,19 @@ public void SaveAsOnnx(OnnxContext ctx)
InputSchema[srcIndex].Type.GetValueCount()));
}

string opType = "FeatureVectorizer";
int outVectorSize = (int)inputList.Sum(x => x.Value);
var vectorizerOutputType = new VectorDataViewType(NumberDataViewType.Single, outVectorSize);
var vectorizerOutputName = ctx.AddIntermediateVariable(vectorizerOutputType, "VectorFeaturizerOutput");
var node = ctx.CreateNode(opType, inputList.Select(t => t.Key),
new[] { ctx.AddIntermediateVariable(outColType, outName) }, ctx.GetNodeName(opType), "");

node.AddAttribute("axis", 1);
new[] { vectorizerOutputName }, ctx.GetNodeName(opType));
node.AddAttribute("inputdimensions", inputList.Select(x => x.Value));

opType = "Cast";
var dstVectorType = new VectorDataViewType(outColType.GetItemType() as PrimitiveDataViewType, outVectorSize);
var dstVariableName = ctx.AddIntermediateVariable(dstVectorType, outName);
var castNode = ctx.CreateNode(opType, vectorizerOutputName, dstVariableName, ctx.GetNodeName(opType), "");
castNode.AddAttribute("to", outColType.ItemType.RawType);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,35 @@
"F1",
"F22"
],
"output": [
"VectorFeaturizerOutput"
],
"name": "FeatureVectorizer",
"opType": "FeatureVectorizer",
"attribute": [
{
"name": "inputdimensions",
"ints": [
"1",
"10"
],
"type": "INTS"
}
],
"domain": "ai.onnx.ml"
},
{
"input": [
"VectorFeaturizerOutput"
],
"output": [
"Features"
],
"name": "Concat",
"opType": "Concat",
"name": "Cast1",
"opType": "Cast",
"attribute": [
{
"name": "axis",
"name": "to",
"i": "1",
"type": "INT"
}
Expand Down Expand Up @@ -431,7 +452,7 @@
"output": [
"PredictedLabel"
],
"name": "Cast1",
"name": "Cast2",
"opType": "Cast",
"attribute": [
{
Expand Down Expand Up @@ -638,6 +659,24 @@
}
}
},
{
"name": "VectorFeaturizerOutput",
"type": {
"tensorType": {
"elemType": 1,
"shape": {
"dim": [
{
"dimValue": "-1"
},
{
"dimValue": "11"
}
]
}
}
}
},
{
"name": "Features",
"type": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,35 @@
"F1",
"F21"
],
"output": [
"VectorFeaturizerOutput"
],
"name": "FeatureVectorizer",
"opType": "FeatureVectorizer",
"attribute": [
{
"name": "inputdimensions",
"ints": [
"8",
"9"
],
"type": "INTS"
}
],
"domain": "ai.onnx.ml"
},
{
"input": [
"VectorFeaturizerOutput"
],
"output": [
"Features"
],
"name": "Concat",
"opType": "Concat",
"name": "Cast1",
"opType": "Cast",
"attribute": [
{
"name": "axis",
"name": "to",
"i": "1",
"type": "INT"
}
Expand Down Expand Up @@ -757,7 +778,7 @@
"output": [
"PredictedLabel"
],
"name": "Cast1",
"name": "Cast2",
"opType": "Cast",
"attribute": [
{
Expand Down Expand Up @@ -946,6 +967,24 @@
}
}
},
{
"name": "VectorFeaturizerOutput",
"type": {
"tensorType": {
"elemType": 1,
"shape": {
"dim": [
{
"dimValue": "-1"
},
{
"dimValue": "17"
}
]
}
}
}
},
{
"name": "Features",
"type": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,35 @@
"F1",
"F22"
],
"output": [
"VectorFeaturizerOutput"
],
"name": "FeatureVectorizer",
"opType": "FeatureVectorizer",
"attribute": [
{
"name": "inputdimensions",
"ints": [
"1",
"10"
],
"type": "INTS"
}
],
"domain": "ai.onnx.ml"
},
{
"input": [
"VectorFeaturizerOutput"
],
"output": [
"Features"
],
"name": "Concat",
"opType": "Concat",
"name": "Cast1",
"opType": "Cast",
"attribute": [
{
"name": "axis",
"name": "to",
"i": "1",
"type": "INT"
}
Expand Down Expand Up @@ -384,7 +405,7 @@
"output": [
"PredictedLabel"
],
"name": "Cast1",
"name": "Cast2",
"opType": "Cast",
"attribute": [
{
Expand Down Expand Up @@ -871,6 +892,24 @@
}
}
},
{
"name": "VectorFeaturizerOutput",
"type": {
"tensorType": {
"elemType": 1,
"shape": {
"dim": [
{
"dimValue": "-1"
},
{
"dimValue": "11"
}
]
}
}
}
},
{
"name": "Features",
"type": {
Expand Down
30 changes: 30 additions & 0 deletions test/Microsoft.ML.Tests/OnnxConversionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,36 @@ public void LoadingPredictorModelAndOnnxConversionTest()
Done();
}

[Fact]
Lynx1820 marked this conversation as resolved.
Show resolved Hide resolved
public void ConcatenateOnnxConversionTest()
{
var mlContext = new MLContext(seed: 1);
string dataPath = GetDataPath("breast-cancer.txt");

var data = ML.Data.LoadFromTextFile(dataPath, new[] {
new TextLoader.Column("VectorDouble2", DataKind.Double, 1),
new TextLoader.Column("VectorDouble1", DataKind.Double, 4, 8),
new TextLoader.Column("Label", DataKind.Boolean, 0)
});
var pipeline = mlContext.Transforms.Concatenate("Features", "VectorDouble1", "VectorDouble2");
var model = pipeline.Fit(data);
var transformedData = model.Transform(data);
var onnxModel = mlContext.Model.ConvertToOnnxProtobuf(model, data);

// Compare results produced by ML.NET and ONNX's runtime.
if (IsOnnxRuntimeSupported())
{
var onnxModelName = "Concatenate.onnx";
var onnxModelPath = GetOutputPath(onnxModelName);
SaveOnnxModel(onnxModel, onnxModelPath, null);
// Evaluate the saved ONNX model using the data used to train the ML.NET pipeline.
var onnxEstimator = mlContext.Transforms.ApplyOnnxModel(onnxModelPath);
var onnxTransformer = onnxEstimator.Fit(data);
var onnxResult = onnxTransformer.Transform(data);
CompareSelectedColumns<double>("Features", "Features", transformedData, onnxResult);
}
Done();
}

[Fact]
public void RemoveVariablesInPipelineTest()
Expand Down