1
- // Licensed to the .NET Foundation under one or more agreements.
1
+ // Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
// See the LICENSE file in the project root for more information.
4
4
@@ -119,8 +119,10 @@ public OnnxTransformTests(ITestOutputHelper output) : base(output)
119
119
{
120
120
}
121
121
122
- [ OnnxFact ]
123
- public void TestSimpleCase ( )
122
+ [ OnnxTheory ]
123
+ [ InlineData ( false ) ]
124
+ [ InlineData ( true ) ]
125
+ public void TestSimpleCase ( bool useOptionsCtor )
124
126
{
125
127
var modelFile = "squeezenet/00000001/model.onnx" ;
126
128
var samplevector = GetSampleArrayData ( ) ;
@@ -139,7 +141,19 @@ public void TestSimpleCase()
139
141
var xyData = new List < TestDataXY > { new TestDataXY ( ) { A = new float [ InputSize ] } } ;
140
142
var stringData = new List < TestDataDifferntType > { new TestDataDifferntType ( ) { data_0 = new string [ InputSize ] } } ;
141
143
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 ) ;
143
157
144
158
var invalidDataWrongNames = ML . Data . LoadFromEnumerable ( xyData ) ;
145
159
var invalidDataWrongTypes = ML . Data . LoadFromEnumerable ( stringData ) ;
@@ -713,14 +727,14 @@ public void TestOnnxModelNotDisposal()
713
727
714
728
private class OnnxMapInput
715
729
{
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 ; }
718
732
}
719
733
720
734
private class OnnxMapOutput
721
735
{
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 ; }
724
738
}
725
739
726
740
/// <summary>
@@ -753,10 +767,10 @@ public void SmokeInMemoryOnnxMapTypeTest()
753
767
var transformedDataView = model . Transform ( dataView ) ;
754
768
var transformedDataPoints = ML . Data . CreateEnumerable < OnnxMapOutput > ( transformedDataView , false ) . ToList ( ) ;
755
769
756
- for ( int i = 0 ; i < dataPoints . Count ( ) ; ++ i )
770
+ for ( int i = 0 ; i < dataPoints . Count ( ) ; ++ i )
757
771
{
758
772
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 )
760
774
Assert . Equal ( pair . Value , transformedDataPoints [ i ] . Output [ pair . Key + 1 ] ) ;
761
775
}
762
776
}
@@ -815,7 +829,7 @@ public void TestOnnxTransformWithCustomShapes()
815
829
transformedDataViews [ 2 ] = onnxTransformer [ 2 ] . Transform ( dataView ) ;
816
830
817
831
// Conduct the same check for all the 3 called public APIs.
818
- foreach ( var transformedDataView in transformedDataViews )
832
+ foreach ( var transformedDataView in transformedDataViews )
819
833
{
820
834
var transformedDataPoints = ML . Data . CreateEnumerable < PredictionWithCustomShape > ( transformedDataView , false ) . ToList ( ) ;
821
835
@@ -901,32 +915,32 @@ public void SpecifyOnnxShapes()
901
915
Assert . False ( somethingWrong ) ;
902
916
903
917
// 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 [ ] > ( ) {
905
919
{ "outb" , new int [ ] { 5 , 6 } } ,
906
920
} ;
907
- somethingWrong = false ;
921
+ somethingWrong = false ;
908
922
try
909
923
{
910
924
TryModelWithCustomShapesHelper ( shapeDictionary ) ;
911
925
}
912
926
catch
913
927
{
914
- somethingWrong = true ;
928
+ somethingWrong = true ;
915
929
}
916
930
Assert . True ( somethingWrong ) ;
917
931
918
932
// 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 [ ] > ( ) {
920
934
{ "outb" , new int [ ] { - 1 , - 1 , - 1 , - 1 } } ,
921
935
} ;
922
- somethingWrong = false ;
936
+ somethingWrong = false ;
923
937
try
924
938
{
925
939
TryModelWithCustomShapesHelper ( shapeDictionary ) ;
926
940
}
927
941
catch
928
942
{
929
- somethingWrong = true ;
943
+ somethingWrong = true ;
930
944
}
931
945
Assert . False ( somethingWrong ) ;
932
946
}
@@ -1024,7 +1038,7 @@ public void TestOnnxTransformSaveAndLoadWithRecursionLimit()
1024
1038
var pipe = ML . Transforms . LoadImages ( "data_0" , imageFolder , "imagePath" )
1025
1039
. Append ( ML . Transforms . ResizeImages ( "data_0" , imageHeight , imageWidth ) )
1026
1040
. 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 ,
1028
1042
gpuDeviceId : _gpuDeviceId , fallbackToCpu : _fallbackToCpu , shapeDictionary : null , recursionLimit : 50 ) ) ;
1029
1043
1030
1044
TestEstimatorCore ( pipe , data ) ;
0 commit comments