@@ -18,6 +18,101 @@ namespace winrt::WinMLSamplesGalleryNative::implementation
1818{
1919 // Create ORT Sessions and launch D3D window in a separate thread
2020 void DXResourceBinding::LaunchWindow () {
21+
22+
23+ long newH = 224 ;
24+ long newW = 224 ;
25+ long h = 512 ;
26+ long w = 512 ;
27+ std::array<long , 6 > center_fill_dimensions = CalculateCenterFillDimensions (h, w, newH, newW);
28+ long resizedW = center_fill_dimensions[0 ];
29+ long resizedH = center_fill_dimensions[1 ];
30+ long top = center_fill_dimensions[2 ];
31+ long bottom = center_fill_dimensions[3 ];
32+ long left = center_fill_dimensions[4 ];
33+ long right = center_fill_dimensions[5 ];
34+ winrt::hstring interpolationMode = L" nearest" ;
35+ long c = 3 ;
36+
37+ auto width = 800 ;
38+ auto height = 600 ;
39+ auto rowPitchInPixels = (width + 255 ) & ~255 ;
40+ auto rowPitchInBytes = rowPitchInPixels * 4 ;
41+ auto bufferInBytes = rowPitchInBytes * height;
42+ const std::array<int64_t , 4 > preprocessInputShape = { 1 , bufferInBytes};
43+ const std::array<int64_t , 4 > preprocessOutputShape = { 1 , 224 , 224 , 3 };
44+
45+ var kernel = new float [] {
46+ 0 ,0 ,1 ,
47+ 0 ,1 ,0 ,
48+ 1 ,0 ,0
49+ };
50+
51+ .Inputs .Add (LearningModelBuilder.CreateTensorFeatureDescriptor (" Input" , TensorKind.UInt8 , new long [] { 1 , bufferInBytes }))
52+ .Outputs .Add (LearningModelBuilder.CreateTensorFeatureDescriptor (" Output" , TensorKind.Float , new long [] { 1 , newH, newW, c }))
53+ .Operators .Add (new LearningModelOperator (" Cast" )
54+ .SetInput (" input" , " Input" )
55+ .SetAttribute (" to" , TensorInt64Bit.CreateFromIterable (new long [] {}, new long [] { (long )OnnxDataType.FLOAT }))
56+ .SetOutput (" output" , " CastOutput" ))
57+ .Operators .Add (new LearningModelOperator (" Reshape" )
58+ .SetInput (" data" , " CastOutput" )
59+ .SetConstant (" shape" , TensorInt64Bit.CreateFromIterable (new long [] { 4 }, new long [] { 1 , height, width, 4 }))
60+ .SetOutput (" reshaped" , " ReshapeOutput" ))
61+ .Operators .Add (new LearningModelOperator (" Slice" )
62+ .SetInput (" data" , " ReshapeOutput" )
63+ .SetConstant (" starts" , TensorInt64Bit.CreateFromIterable (new long [] { 4 }, new long [] { 0 , 0 , 0 , 0 }))
64+ .SetConstant (" ends" , TensorInt64Bit.CreateFromIterable (new long [] { 4 }, new long [] { long .MaxValue , long .MaxValue , width, c - 1 }))
65+ .SetOutput (" output" , " SliceOutput" ))
66+ .Operators .Add (new LearningModelOperator (" Resize" )
67+ .SetInput (" X" , " ReshapeOutput" )
68+ .SetConstant (" roi" , TensorFloat.CreateFromIterable (new long [] { 8 }, new float [] { 0 , 0 , 0 , 0 , 1 , 1 , 1 , 1 }))
69+ .SetConstant (" scales" , TensorFloat.CreateFromIterable (new long [] { 4 }, new float [] { 1 , (float )(1 + resizedH) / (float )h, (float )(1 + resizedH) / (float )h, 1 }))
70+ // .SetConstant("sizes", TensorInt64Bit.CreateFromIterable(new long[] { 4 }, new long[] { 1, 3, resizedH, resizedW }))
71+ .SetAttribute (" mode" , TensorString.CreateFromArray (new long [] {}, new string[]{ interpolationMode }))
72+ .SetOutput (" Y" , " ResizeOutput" ))
73+ .Operators .Add (new LearningModelOperator (" Slice" )
74+ .SetInput (" data" , " ResizeOutput" )
75+ .SetConstant (" starts" , TensorInt64Bit.CreateFromIterable (new long [] { 4 }, new long [] { 0 , 0 , 0 , 0 }))
76+ .SetConstant (" ends" , TensorInt64Bit.CreateFromIterable (new long [] { 4 }, new long [] { long .MaxValue , 224 , 224 , 3 }))
77+ .SetOutput (" output" , " SliceOutput" ))
78+ // This is just getting bgr to rgb
79+ /* .Operators.Add(new LearningModelOperator("Conv")
80+ .SetInput("X", "Input")
81+ .SetConstant("W", TensorFloat.CreateFromArray(new long[] { 3, 3, 1, 1 }, kernel))
82+ .SetConstant("B", TensorFloat.CreateFromArray(new long[] { 1, 3, 1, 1 }, new float[] { 0, 0, 0 }))
83+ .SetOutput("Y", "Output"));*/
84+
85+ // if above doesn't work try
86+ // .SetConstant("W", TensorFloat.CreateFromArray(new long[] { 3, 1, 1, 3 }, kernel))
87+ // .SetConstant("B", TensorFloat.CreateFromArray(new long[] { 1, 1, 1, 3 }, new float[] { 0, 0, 0 }))
88+
89+ auto resize_op = LearningModelOperator (L" Resize" )
90+ .SetInput (L" X" , L" Input" )
91+ .SetConstant (L" roi" , TensorFloat::CreateFromIterable ({ 8 }, { 0 , 0 , 0 , 0 , 1 , 1 , 1 , 1 }))
92+ .SetConstant (L" scales" , TensorFloat::CreateFromIterable ({ 4 }, { 1 , (float )(1 + resizedH) / (float )h, (float )(1 + resizedH) / (float )h, 1 }))
93+ .SetAttribute (L" mode" , TensorString::CreateFromArray ({}, { interpolationMode }))
94+ .SetOutput (L" Y" , L" ResizeOutput" );
95+
96+ auto slice_op = LearningModelOperator (L" Slice" )
97+ .SetInput (L" data" , L" ResizeOutput" )
98+ .SetConstant (L" starts" , TensorInt64Bit::CreateFromIterable ({ 4 }, { 0 , top, left, 0 }))
99+ .SetConstant (L" ends" , TensorInt64Bit::CreateFromIterable ({ 4 }, { LLONG_MAX, bottom, right, 3 }))
100+ .SetOutput (L" output" , L" Output" );
101+
102+
103+ auto preprocessingModelBuilder =
104+ LearningModelBuilder::Create (12 )
105+ .Inputs ().Add (LearningModelBuilder::CreateTensorFeatureDescriptor (L" Input" , TensorKind::UInt8, preprocessInputShape))
106+ .Outputs ().Add (LearningModelBuilder::CreateTensorFeatureDescriptor (L" Output" , TensorKind::Float, preprocessOutputShape))
107+ .Operators ().Add (resize_op)
108+ .Operators ().Add (slice_op);
109+ // .Operators().Add(dimension_transpose);
110+ auto preprocessingModel = preprocessingModelBuilder.CreateModel ();
111+
112+ preprocessingModelBuilder.Save (L" C:/Users/numform/Windows-Machine-Learning/Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/dx_preprocessor_efficient_net.onnx" );
113+
114+
115+
21116 // Create ORT Sessions that will be used for preprocessing and classification
22117 const wchar_t * preprocessingModelFilePath = L" C:/Users/numform/Windows-Machine-Learning/Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/dx_preprocessor_efficient_net.onnx" ;
23118 const wchar_t * inferencemodelFilePath = L" C:/Users/numform/Windows-Machine-Learning/Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/efficientnet-lite4-11.onnx" ;
0 commit comments