Skip to content

Commit f684cc6

Browse files
committed
Add test.
1 parent f81a9ac commit f684cc6

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

test/Microsoft.ML.OnnxTransformerTest/OnnxTransformTests.cs

+32-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

@@ -119,8 +119,10 @@ public OnnxTransformTests(ITestOutputHelper output) : base(output)
119119
{
120120
}
121121

122-
[OnnxFact]
123-
public void TestSimpleCase()
122+
[OnnxTheory]
123+
[InlineData(false)]
124+
[InlineData(true)]
125+
public void TestSimpleCase(bool useOptionsCtor)
124126
{
125127
var modelFile = "squeezenet/00000001/model.onnx";
126128
var samplevector = GetSampleArrayData();
@@ -139,7 +141,19 @@ public void TestSimpleCase()
139141
var xyData = new List<TestDataXY> { new TestDataXY() { A = new float[InputSize] } };
140142
var stringData = new List<TestDataDifferntType> { new TestDataDifferntType() { data_0 = new string[InputSize] } };
141143
var sizeData = new List<TestDataSize> { new TestDataSize() { data_0 = new float[2] } };
142-
var pipe = ML.Transforms.ApplyOnnxModel(new[] { "softmaxout_1" }, new[] { "data_0" }, modelFile, gpuDeviceId: _gpuDeviceId, fallbackToCpu: _fallbackToCpu);
144+
var options = new OnnxOptions()
145+
{
146+
OutputColumns = new[] { "softmaxout_1" },
147+
InputColumns = new[] {"data_0" },
148+
ModelFile = modelFile,
149+
GpuDeviceId = _gpuDeviceId,
150+
FallbackToCpu = _fallbackToCpu,
151+
InterOpNumThreads = 1,
152+
IntraOpNumThreads = 1
153+
};
154+
var pipe = useOptionsCtor ?
155+
ML.Transforms.ApplyOnnxModel(options) :
156+
ML.Transforms.ApplyOnnxModel(options.OutputColumns, options.InputColumns, modelFile, gpuDeviceId: _gpuDeviceId, fallbackToCpu: _fallbackToCpu);
143157

144158
var invalidDataWrongNames = ML.Data.LoadFromEnumerable(xyData);
145159
var invalidDataWrongTypes = ML.Data.LoadFromEnumerable(stringData);
@@ -713,14 +727,14 @@ public void TestOnnxModelNotDisposal()
713727

714728
private class OnnxMapInput
715729
{
716-
[OnnxMapType(typeof(int),typeof(float))]
717-
public IDictionary<int,float> Input { get; set; }
730+
[OnnxMapType(typeof(int), typeof(float))]
731+
public IDictionary<int, float> Input { get; set; }
718732
}
719733

720734
private class OnnxMapOutput
721735
{
722-
[OnnxMapType(typeof(int),typeof(float))]
723-
public IDictionary<int,float> Output { get; set; }
736+
[OnnxMapType(typeof(int), typeof(float))]
737+
public IDictionary<int, float> Output { get; set; }
724738
}
725739

726740
/// <summary>
@@ -753,10 +767,10 @@ public void SmokeInMemoryOnnxMapTypeTest()
753767
var transformedDataView = model.Transform(dataView);
754768
var transformedDataPoints = ML.Data.CreateEnumerable<OnnxMapOutput>(transformedDataView, false).ToList();
755769

756-
for(int i = 0; i < dataPoints.Count(); ++i)
770+
for (int i = 0; i < dataPoints.Count(); ++i)
757771
{
758772
Assert.Equal(dataPoints[i].Input.Count(), transformedDataPoints[i].Output.Count());
759-
foreach(var pair in dataPoints[i].Input)
773+
foreach (var pair in dataPoints[i].Input)
760774
Assert.Equal(pair.Value, transformedDataPoints[i].Output[pair.Key + 1]);
761775
}
762776
}
@@ -815,7 +829,7 @@ public void TestOnnxTransformWithCustomShapes()
815829
transformedDataViews[2] = onnxTransformer[2].Transform(dataView);
816830

817831
// Conduct the same check for all the 3 called public APIs.
818-
foreach(var transformedDataView in transformedDataViews)
832+
foreach (var transformedDataView in transformedDataViews)
819833
{
820834
var transformedDataPoints = ML.Data.CreateEnumerable<PredictionWithCustomShape>(transformedDataView, false).ToList();
821835

@@ -901,32 +915,32 @@ public void SpecifyOnnxShapes()
901915
Assert.False(somethingWrong);
902916

903917
// Case 3: this shape conflicts with output shape [1, 1, 1, 5] loaded from the model.
904-
shapeDictionary= new Dictionary<string, int[]>() {
918+
shapeDictionary = new Dictionary<string, int[]>() {
905919
{ "outb", new int[] { 5, 6 } },
906920
};
907-
somethingWrong= false;
921+
somethingWrong = false;
908922
try
909923
{
910924
TryModelWithCustomShapesHelper(shapeDictionary);
911925
}
912926
catch
913927
{
914-
somethingWrong= true;
928+
somethingWrong = true;
915929
}
916930
Assert.True(somethingWrong);
917931

918932
// Case 4: this shape works with output shape [1, 1, 1, 5] loaded from the model.
919-
shapeDictionary= new Dictionary<string, int[]>() {
933+
shapeDictionary = new Dictionary<string, int[]>() {
920934
{ "outb", new int[] { -1, -1, -1, -1 } },
921935
};
922-
somethingWrong= false;
936+
somethingWrong = false;
923937
try
924938
{
925939
TryModelWithCustomShapesHelper(shapeDictionary);
926940
}
927941
catch
928942
{
929-
somethingWrong= true;
943+
somethingWrong = true;
930944
}
931945
Assert.False(somethingWrong);
932946
}
@@ -1024,7 +1038,7 @@ public void TestOnnxTransformSaveAndLoadWithRecursionLimit()
10241038
var pipe = ML.Transforms.LoadImages("data_0", imageFolder, "imagePath")
10251039
.Append(ML.Transforms.ResizeImages("data_0", imageHeight, imageWidth))
10261040
.Append(ML.Transforms.ExtractPixels("data_0", interleavePixelColors: true))
1027-
.Append(ML.Transforms.ApplyOnnxModel(new []{ "softmaxout_1" }, new []{ "data_0" }, modelFile,
1041+
.Append(ML.Transforms.ApplyOnnxModel(new[] { "softmaxout_1" }, new[] { "data_0" }, modelFile,
10281042
gpuDeviceId: _gpuDeviceId, fallbackToCpu: _fallbackToCpu, shapeDictionary: null, recursionLimit: 50));
10291043

10301044
TestEstimatorCore(pipe, data);

0 commit comments

Comments
 (0)