Skip to content

Commit 5ac01e0

Browse files
committed
Add missing files
1 parent 023c1a4 commit 5ac01e0

File tree

4 files changed

+127
-45
lines changed

4 files changed

+127
-45
lines changed

Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/DXResourceBinding.cpp

Lines changed: 122 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
#include "DXResourceBinding.g.cpp"
77
#include "stdafx.h"
88
#include "ORTHelpers.h"
9+
#include "winrt/Microsoft.AI.MachineLearning.h"
10+
#include "winrt/Microsoft.AI.MachineLearning.Experimental.h"
11+
12+
using namespace winrt::Microsoft::AI::MachineLearning;
913

1014
#undef min
1115

@@ -14,6 +18,51 @@ static std::optional<Ort::Session> preprocesingSession;
1418
static std::optional<Ort::Session> inferenceSession;
1519
D3D12Quad sample(800, 600, L"D3D12 Quad");
1620

21+
using TensorInt64Bit = winrt::Microsoft::AI::MachineLearning::TensorInt64Bit;
22+
using TensorKind = winrt::Microsoft::AI::MachineLearning::TensorKind;
23+
using LearningModelBuilder = winrt::Microsoft::AI::MachineLearning::Experimental::LearningModelBuilder;
24+
using LearningModelOperator = winrt::Microsoft::AI::MachineLearning::Experimental::LearningModelOperator;
25+
26+
std::array<int64_t, 4> preprocessInputShape;
27+
28+
std::array<long, 6> CalculateCenterFillDimensions(long oldH, long oldW, long h, long w)
29+
{
30+
long resizedW, resizedH, top, bottom, left, right;
31+
auto oldHFloat = (float)oldH;
32+
auto oldWFloat = (float)oldW;
33+
auto hFloat = (float)h;
34+
auto wFloat = (float)w;
35+
36+
auto oldAspectRatio = oldWFloat / oldHFloat;
37+
auto newAspectRatio = wFloat / hFloat;
38+
39+
auto scale = (newAspectRatio < oldAspectRatio) ? (hFloat / oldHFloat) : (wFloat / oldWFloat);
40+
resizedW = (newAspectRatio < oldAspectRatio) ? (long)std::floor(scale * oldWFloat) : w;
41+
resizedH = (newAspectRatio < oldAspectRatio) ? h : (long)std::floor(scale * oldHFloat);
42+
long totalPad = (newAspectRatio < oldAspectRatio) ? resizedW - w : resizedH - h;
43+
long biggerDim = (newAspectRatio < oldAspectRatio) ? w : h;
44+
long first = (totalPad % 2 == 0) ? totalPad / 2 : (long)std::floor(totalPad / 2.0f);
45+
long second = first + biggerDim;
46+
47+
if (newAspectRatio < oldAspectRatio)
48+
{
49+
top = 0;
50+
bottom = h;
51+
left = first;
52+
right = second;
53+
}
54+
else
55+
{
56+
top = first;
57+
bottom = second;
58+
left = 0;
59+
right = w;
60+
}
61+
62+
std::array<long, 6> new_dimensions = { resizedW, resizedH, top, bottom, left, right };
63+
return new_dimensions;
64+
}
65+
1766
namespace winrt::WinMLSamplesGalleryNative::implementation
1867
{
1968
// Create ORT Sessions and launch D3D window in a separate thread
@@ -39,42 +88,45 @@ namespace winrt::WinMLSamplesGalleryNative::implementation
3988
auto rowPitchInPixels = (width + 255) & ~255;
4089
auto rowPitchInBytes = rowPitchInPixels * 4;
4190
auto bufferInBytes = rowPitchInBytes * height;
42-
const std::array<int64_t, 4> preprocessInputShape = { 1, bufferInBytes};
91+
preprocessInputShape = { 1, bufferInBytes};
92+
//const std::array<int64_t, 4> preprocessInputShape = { 1, 512, 512, 4 };
93+
//const std::array<int64_t, 4> preprocessInputShape = { 1, -1, -1, -1 };
94+
4395
const std::array<int64_t, 4> preprocessOutputShape = { 1, 224, 224, 3 };
4496

45-
var kernel = new float[] {
97+
auto kernel = new float[] {
4698
0,0,1,
4799
0,1,0,
48100
1,0,0
49101
};
50102

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"))
103+
//.Inputs.Add(LearningModelBuilder.CreateTensorFeatureDescriptor("Input", TensorKind.UInt8, new long[] { 1, bufferInBytes }))
104+
// .Outputs.Add(LearningModelBuilder.CreateTensorFeatureDescriptor("Output", TensorKind.Float, new long[] { 1, newH, newW, c }))
105+
// .Operators.Add(new LearningModelOperator("Cast")
106+
// .SetInput("input", "Input")
107+
// .SetAttribute("to", TensorInt64Bit.CreateFromIterable(new long[] {}, new long[] { (long)OnnxDataType.FLOAT }))
108+
// .SetOutput("output", "CastOutput"))
109+
// .Operators.Add(new LearningModelOperator("Reshape")
110+
// .SetInput("data", "CastOutput")
111+
// .SetConstant("shape", TensorInt64Bit.CreateFromIterable(new long[] { 4 }, new long[] { 1, height, width, 4 }))
112+
// .SetOutput("reshaped", "ReshapeOutput"))
113+
// .Operators.Add(new LearningModelOperator("Slice")
114+
// .SetInput("data", "ReshapeOutput")
115+
// .SetConstant("starts", TensorInt64Bit.CreateFromIterable(new long[] { 4 }, new long[] { 0, 0, 0, 0 }))
116+
// .SetConstant("ends", TensorInt64Bit.CreateFromIterable(new long[] { 4 }, new long[] { long.MaxValue, long.MaxValue, width, c - 1 }))
117+
// .SetOutput("output", "SliceOutput"))
118+
// .Operators.Add(new LearningModelOperator("Resize")
119+
// .SetInput("X", "ReshapeOutput")
120+
// .SetConstant("roi", TensorFloat.CreateFromIterable(new long[] { 8 }, new float[] { 0, 0, 0, 0, 1, 1, 1, 1 }))
121+
// .SetConstant("scales", TensorFloat.CreateFromIterable(new long[] { 4 }, new float[] { 1, (float)(1 + resizedH) / (float)h, (float)(1 + resizedH) / (float)h, 1 }))
122+
// //.SetConstant("sizes", TensorInt64Bit.CreateFromIterable(new long[] { 4 }, new long[] { 1, 3, resizedH, resizedW }))
123+
// .SetAttribute("mode", TensorString.CreateFromArray(new long[] {}, new string[]{ interpolationMode }))
124+
// .SetOutput("Y", "ResizeOutput"))
125+
// .Operators.Add(new LearningModelOperator("Slice")
126+
// .SetInput("data", "ResizeOutput")
127+
// .SetConstant("starts", TensorInt64Bit.CreateFromIterable(new long[] { 4 }, new long[] { 0, 0, 0, 0 }))
128+
// .SetConstant("ends", TensorInt64Bit.CreateFromIterable(new long[] { 4 }, new long[] { long.MaxValue, 224, 224, 3 }))
129+
// .SetOutput("output", "SliceOutput"))
78130
// This is just getting bgr to rgb
79131
/* .Operators.Add(new LearningModelOperator("Conv")
80132
.SetInput("X", "Input")
@@ -86,35 +138,63 @@ namespace winrt::WinMLSamplesGalleryNative::implementation
86138
//.SetConstant("W", TensorFloat.CreateFromArray(new long[] { 3, 1, 1, 3 }, kernel))
87139
//.SetConstant("B", TensorFloat.CreateFromArray(new long[] { 1, 1, 1, 3 }, new float[] { 0, 0, 0 }))
88140

141+
//auto resize_op = LearningModelOperator(L"Resize")
142+
// .SetInput(L"X", L"Input")
143+
// .SetConstant(L"roi", TensorFloat::CreateFromIterable({ 8 }, { 0, 0, 0, 0, 1, 1, 1, 1 }))
144+
// .SetConstant(L"scales", TensorFloat::CreateFromIterable({ 4 }, { 1, (float)(1 + resizedH) / (float)h, (float)(1 + resizedH) / (float)h, 1 }))
145+
// .SetAttribute(L"mode", TensorString::CreateFromArray({}, { interpolationMode }))
146+
// .SetOutput(L"Y", L"ResizeOutput");
147+
148+
//auto slice_op = LearningModelOperator(L"Slice")
149+
// .SetInput(L"data", L"ResizeOutput")
150+
// .SetConstant(L"starts", TensorInt64Bit::CreateFromIterable({ 4 }, { 0, top, left, 0 }))
151+
// .SetConstant(L"ends", TensorInt64Bit::CreateFromIterable({ 4 }, { LLONG_MAX, bottom, right, 3 }))
152+
// .SetOutput(L"output", L"Output");
153+
154+
auto cast_op = LearningModelOperator(L"Cast")
155+
.SetInput(L"input", L"Input")
156+
.SetAttribute(L"to", TensorInt64Bit::CreateFromIterable({}, { (long)1 }))
157+
.SetOutput(L"output", L"CastOutput");
158+
159+
auto reshape_op = LearningModelOperator(L"Reshape")
160+
.SetInput(L"data", L"CastOutput")
161+
.SetConstant(L"shape", TensorInt64Bit::CreateFromIterable({ 4 }, { 1, 512, 512, 4 }))
162+
.SetOutput(L"reshaped", L"ReshapeOutput");
163+
164+
auto slice_1 = LearningModelOperator(L"Slice")
165+
.SetInput(L"data", L"ReshapeOutput")
166+
.SetConstant(L"starts", TensorInt64Bit::CreateFromIterable({ 4 }, { 0, 0, 0, 0 }))
167+
.SetConstant(L"ends", TensorInt64Bit::CreateFromIterable({ 4 }, { LONG_MAX, LONG_MAX, width, c - 1 }))
168+
.SetOutput(L"output", L"SliceOutput");
169+
89170
auto resize_op = LearningModelOperator(L"Resize")
90-
.SetInput(L"X", L"Input")
171+
.SetInput(L"X", L"SliceOutput")
91172
.SetConstant(L"roi", TensorFloat::CreateFromIterable({ 8 }, { 0, 0, 0, 0, 1, 1, 1, 1 }))
92173
.SetConstant(L"scales", TensorFloat::CreateFromIterable({ 4 }, { 1, (float)(1 + resizedH) / (float)h, (float)(1 + resizedH) / (float)h, 1 }))
93174
.SetAttribute(L"mode", TensorString::CreateFromArray({}, { interpolationMode }))
94175
.SetOutput(L"Y", L"ResizeOutput");
95176

96-
auto slice_op = LearningModelOperator(L"Slice")
177+
auto slice_2 = LearningModelOperator(L"Slice")
97178
.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 }))
179+
.SetConstant(L"starts", TensorInt64Bit::CreateFromIterable({ 4 }, { 0, 0, 0, 0 }))
180+
.SetConstant(L"ends", TensorInt64Bit::CreateFromIterable({ 4 }, { LONG_MAX, 224, 224, 3 }))
100181
.SetOutput(L"output", L"Output");
101182

102-
103183
auto preprocessingModelBuilder =
104184
LearningModelBuilder::Create(12)
105-
.Inputs().Add(LearningModelBuilder::CreateTensorFeatureDescriptor(L"Input", TensorKind::UInt8, preprocessInputShape))
185+
.Inputs().Add(LearningModelBuilder::CreateTensorFeatureDescriptor(L"Input", TensorKind::Float, preprocessInputShape))
106186
.Outputs().Add(LearningModelBuilder::CreateTensorFeatureDescriptor(L"Output", TensorKind::Float, preprocessOutputShape))
187+
.Operators().Add(cast_op)
188+
.Operators().Add(reshape_op)
189+
.Operators().Add(slice_1)
107190
.Operators().Add(resize_op)
108-
.Operators().Add(slice_op);
109-
//.Operators().Add(dimension_transpose);
191+
.Operators().Add(slice_2);
110192
auto preprocessingModel = preprocessingModelBuilder.CreateModel();
111193

112-
preprocessingModelBuilder.Save(L"C:/Users/numform/Windows-Machine-Learning/Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/dx_preprocessor_efficient_net.onnx");
113-
114-
194+
preprocessingModelBuilder.Save(L"C:/Users/numform/Windows-Machine-Learning/Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/dx_preprocessor_efficient_net_v2.onnx");
115195

116196
// Create ORT Sessions that will be used for preprocessing and classification
117-
const wchar_t* preprocessingModelFilePath = L"C:/Users/numform/Windows-Machine-Learning/Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/dx_preprocessor_efficient_net.onnx";
197+
const wchar_t* preprocessingModelFilePath = L"C:/Users/numform/Windows-Machine-Learning/Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/dx_preprocessor_efficient_net_v2.onnx";
118198
const wchar_t* inferencemodelFilePath = L"C:/Users/numform/Windows-Machine-Learning/Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/efficientnet-lite4-11.onnx";
119199
preprocesingSession = CreateSession(preprocessingModelFilePath);
120200
inferenceSession = CreateSession(inferencemodelFilePath);
@@ -137,7 +217,7 @@ namespace winrt::WinMLSamplesGalleryNative::implementation
137217

138218
// Preprocess the buffer (shrink from 512 x 512 x 4 to 224 x 224 x 3)
139219
Ort::Value preprocessedInput = Preprocess(*preprocesingSession,
140-
currentBuffer);
220+
currentBuffer, preprocessInputShape);
141221

142222
// Classify the image using EfficientNet and return the results
143223
winrt::com_array<float> results = Eval(*inferenceSession, preprocessedInput);

Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/ORTHelpers.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ Ort::Session CreateSession(const wchar_t* model_file_path)
4747
// Run the buffer through a preprocessing model that will shrink the
4848
// image from 512 x 512 x 4 to 224 x 224 x 3
4949
Ort::Value Preprocess(Ort::Session& session,
50-
ComPtr<ID3D12Resource> currentBuffer)
50+
ComPtr<ID3D12Resource> currentBuffer,
51+
const std::array<int64_t, 4> inputShape)
5152
{
5253
// Init OrtAPI
5354
OrtApi const& ortApi = Ort::GetApi(); // Uses ORT_API_VERSION
@@ -58,7 +59,7 @@ Ort::Value Preprocess(Ort::Session& session,
5859
const char* memoryInformationName = "DML";
5960
Ort::MemoryInfo memoryInformation(memoryInformationName, OrtAllocatorType::OrtDeviceAllocator, 0, OrtMemType::OrtMemTypeDefault);
6061
ComPtr<IUnknown> inputTensorEpWrapper;
61-
const std::array<int64_t, 4> inputShape = { 1, 512, 512, 4 };
62+
//const std::array<int64_t, 4> inputShape = { 1, 512, 512, 4 };
6263
Ort::Value inputTensor = CreateTensorValueFromD3DResource(
6364
*ortDmlApi,
6465
memoryInformation,

Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/ORTHelpers.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ using namespace Microsoft::WRL;
1313
Ort::Session CreateSession(const wchar_t* model_file_path);
1414

1515
Ort::Value Preprocess(Ort::Session& session,
16-
ComPtr<ID3D12Resource> currentBuffer);
16+
ComPtr<ID3D12Resource> currentBuffer,
17+
const std::array<int64_t, 4> inputShape);
1718

1819
winrt::com_array<float> Eval(Ort::Session& session, const Ort::Value& prev_input);
1920

Binary file not shown.

0 commit comments

Comments
 (0)