Skip to content

Commit 65ff453

Browse files
committed
Add 'GetAssetPath'
1 parent 61f25ee commit 65ff453

File tree

5 files changed

+30
-105
lines changed

5 files changed

+30
-105
lines changed

Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/D3D12Quad.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ void D3D12Quad::OnInit()
3434
{
3535
auto title = GetTitle();
3636
fileNames = {
37-
L"C:/Users/numform/Windows-Machine-Learning/Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/dog.jpg",
38-
L"C:/Users/numform/Windows-Machine-Learning/Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/cat.jpg",
39-
L"C:/Users/numform/Windows-Machine-Learning/Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/hummingbird.jpg"
37+
Win32Application::GetAssetPath(L"dog.jpg").c_str(),
38+
Win32Application::GetAssetPath(L"cat.jpg").c_str(),
39+
Win32Application::GetAssetPath(L"hummingbird.jpg").c_str()
4040
};
4141
fileIndex = 0;
4242
updateCounter = 0;
@@ -218,10 +218,8 @@ void D3D12Quad::CreatePipelineState()
218218
UINT compileFlags = 0;
219219
#endif
220220

221-
//ThrowIfFailed(D3DCompileFromFile(GetAssetFullPath(L"shaders.hlsl").c_str(), nullptr, nullptr, "VSMain", "vs_5_0", compileFlags, 0, &vertexShader, nullptr));
222-
ThrowIfFailed(D3DCompileFromFile(L"C:/Users/numform/Windows-Machine-Learning/Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/shaders.hlsl", nullptr, nullptr, "VSMain", "vs_5_0", compileFlags, 0, &vertexShader, nullptr));
223-
//ThrowIfFailed(D3DCompileFromFile(GetAssetFullPath(L"shaders.hlsl").c_str(), nullptr, nullptr, "PSMain", "ps_5_0", compileFlags, 0, &pixelShader, nullptr));
224-
ThrowIfFailed(D3DCompileFromFile(L"C:/Users/numform/Windows-Machine-Learning/Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/shaders.hlsl", nullptr, nullptr, "PSMain", "ps_5_0", compileFlags, 0, &pixelShader, nullptr));
221+
ThrowIfFailed(D3DCompileFromFile(Win32Application::GetAssetPath(L"shaders.hlsl").c_str(), nullptr, nullptr, "VSMain", "vs_5_0", compileFlags, 0, &vertexShader, nullptr));
222+
ThrowIfFailed(D3DCompileFromFile(Win32Application::GetAssetPath(L"shaders.hlsl").c_str(), nullptr, nullptr, "PSMain", "ps_5_0", compileFlags, 0, &pixelShader, nullptr));
225223

226224
// Define the vertex input layout.
227225
D3D12_INPUT_ELEMENT_DESC inputElementDescs[] =

Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/DXResourceBinding.cpp

Lines changed: 2 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -100,104 +100,9 @@ namespace winrt::WinMLSamplesGalleryNative::implementation
100100
1,0,0
101101
};
102102

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"))
130-
// This is just getting bgr to rgb
131-
/* .Operators.Add(new LearningModelOperator("Conv")
132-
.SetInput("X", "Input")
133-
.SetConstant("W", TensorFloat.CreateFromArray(new long[] { 3, 3, 1, 1 }, kernel))
134-
.SetConstant("B", TensorFloat.CreateFromArray(new long[] { 1, 3, 1, 1 }, new float[] { 0, 0, 0 }))
135-
.SetOutput("Y", "Output"));*/
136-
137-
//if above doesn't work try
138-
//.SetConstant("W", TensorFloat.CreateFromArray(new long[] { 3, 1, 1, 3 }, kernel))
139-
//.SetConstant("B", TensorFloat.CreateFromArray(new long[] { 1, 1, 1, 3 }, new float[] { 0, 0, 0 }))
140-
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, height, 832, 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-
170-
auto resize_op = LearningModelOperator(L"Resize")
171-
.SetInput(L"X", L"SliceOutput")
172-
.SetConstant(L"roi", TensorFloat::CreateFromIterable({ 8 }, { 0, 0, 0, 0, 1, 1, 1, 1 }))
173-
.SetConstant(L"scales", TensorFloat::CreateFromIterable({ 4 }, { 1, (float)(1 + resizedH) / (float)h, (float)(1 + resizedH) / (float)h, 1 }))
174-
.SetAttribute(L"mode", TensorString::CreateFromArray({}, { interpolationMode }))
175-
.SetOutput(L"Y", L"ResizeOutput");
176-
177-
auto slice_2 = LearningModelOperator(L"Slice")
178-
.SetInput(L"data", L"ResizeOutput")
179-
.SetConstant(L"starts", TensorInt64Bit::CreateFromIterable({ 4 }, { 0, 0, 0, 0 }))
180-
.SetConstant(L"ends", TensorInt64Bit::CreateFromIterable({ 4 }, { LONG_MAX, 224, 224, 3 }))
181-
.SetOutput(L"output", L"Output");
182-
183-
auto preprocessingModelBuilder =
184-
LearningModelBuilder::Create(12)
185-
.Inputs().Add(LearningModelBuilder::CreateTensorFeatureDescriptor(L"Input", TensorKind::UInt8, preprocessInputShape))
186-
.Outputs().Add(LearningModelBuilder::CreateTensorFeatureDescriptor(L"Output", TensorKind::Float, preprocessOutputShape))
187-
.Operators().Add(cast_op)
188-
.Operators().Add(reshape_op)
189-
.Operators().Add(slice_1)
190-
.Operators().Add(resize_op)
191-
.Operators().Add(slice_2);
192-
auto preprocessingModel = preprocessingModelBuilder.CreateModel();
193-
194-
preprocessingModelBuilder.Save(L"C:/Users/numform/Windows-Machine-Learning/Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/dx_preprocessor_efficient_net_v2.onnx");
195-
196103
// Create ORT Sessions that will be used for preprocessing and classification
197-
const wchar_t* preprocessingModelFilePath = L"C:/Users/numform/Windows-Machine-Learning/Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/dx_preprocessor_efficient_net_v2.onnx";
198-
const wchar_t* inferencemodelFilePath = L"C:/Users/numform/Windows-Machine-Learning/Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/efficientnet-lite4-11.onnx";
199-
preprocesingSession = CreateSession(preprocessingModelFilePath);
200-
inferenceSession = CreateSession(inferencemodelFilePath);
104+
preprocesingSession = CreateSession(Win32Application::GetAssetPath(L"dx_preprocessor_efficient_net_v2.onnx").c_str());
105+
inferenceSession = CreateSession(Win32Application::GetAssetPath(L"efficientnet-lite4-11.onnx").c_str());
201106

202107
// Spawn the window in a separate thread
203108
std::thread d3d_th(Win32Application::Run, &sample, 10);

Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/Win32Application.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,22 @@ void Win32Application::CloseWindow()
145145
{
146146
close_window = true;
147147
}
148+
149+
std::wstring Win32Application::GetAssetPath(LPCWSTR assetName)
150+
{
151+
WCHAR modulePath[512];
152+
UINT modulePathSize = _countof(modulePath);
153+
GetModuleFileName(nullptr, modulePath, modulePathSize);
154+
155+
const wchar_t* gallery = L"WinMLSamplesGallery";
156+
UINT gallerySize = 19; // length of the gallery string
157+
158+
// Erase everything after the first occurence of the gallery string
159+
std::wstring path(modulePath);
160+
size_t found = path.find(gallery);
161+
path.erase(path.begin() + found + gallerySize, path.end());
162+
163+
std::wstring native = L"\\WinMLSamplesGalleryNative\\";
164+
path = path + native + assetName;
165+
return path;
166+
}

Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/Win32Application.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class Win32Application
77
static int Run(D3D12Quad* pSample, int nCmdShow);
88
static HWND GetHwnd() { return m_hwnd; }
99
static void CloseWindow();
10+
static std::wstring GetAssetPath(LPCWSTR assetName);
1011

1112
protected:
1213
static LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/stdafx.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,6 @@
3030
#include <string>
3131
#include <wrl.h>
3232
#include <shellapi.h>
33-
#include "Win32Application.h"
33+
#include "Win32Application.h"
34+
#include <string.h>
35+
#include <stdio.h>

0 commit comments

Comments
 (0)