Skip to content

Commit c6cc8c6

Browse files
authored
User/brian/steamfromresource (#239)
* sample loading a model from an embedded resource * add to build pipeline * put it all in a try/catch and free the resource earlier * remove unnecessary seek * remove commented out code * add an accurate readme
1 parent 9c72a21 commit c6cc8c6

File tree

13 files changed

+375
-0
lines changed

13 files changed

+375
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.28307.645
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "StreamFromResource", "StreamFromResource\StreamFromResource.vcxproj", "{8C080D24-D4A8-4FFD-86D7-D99A06A0DBDE}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|x64 = Debug|x64
11+
Debug|x86 = Debug|x86
12+
Release|x64 = Release|x64
13+
Release|x86 = Release|x86
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{8C080D24-D4A8-4FFD-86D7-D99A06A0DBDE}.Debug|x64.ActiveCfg = Debug|x64
17+
{8C080D24-D4A8-4FFD-86D7-D99A06A0DBDE}.Debug|x64.Build.0 = Debug|x64
18+
{8C080D24-D4A8-4FFD-86D7-D99A06A0DBDE}.Debug|x86.ActiveCfg = Debug|Win32
19+
{8C080D24-D4A8-4FFD-86D7-D99A06A0DBDE}.Debug|x86.Build.0 = Debug|Win32
20+
{8C080D24-D4A8-4FFD-86D7-D99A06A0DBDE}.Release|x64.ActiveCfg = Release|x64
21+
{8C080D24-D4A8-4FFD-86D7-D99A06A0DBDE}.Release|x64.Build.0 = Release|x64
22+
{8C080D24-D4A8-4FFD-86D7-D99A06A0DBDE}.Release|x86.ActiveCfg = Release|Win32
23+
{8C080D24-D4A8-4FFD-86D7-D99A06A0DBDE}.Release|x86.Build.0 = Release|Win32
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {D9226ABF-70C9-49FC-BD4B-7CDE31124738}
30+
EndGlobalSection
31+
EndGlobal
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ImportGroup Label="PropertySheets" />
4+
<PropertyGroup Label="UserMacros">
5+
<!--To troubleshoot CppWinRT build issues, set CppWinRTVerbosity to 'high'
6+
For complete documentation on CppWinRT project customization, please see:
7+
https://github.com/Microsoft/xlang/tree/master/src/package/cppwinrt/nuget -->
8+
<CppWinRTVerbosity>normal</CppWinRTVerbosity>
9+
</PropertyGroup>
10+
<PropertyGroup />
11+
<ItemDefinitionGroup />
12+
<ItemGroup>
13+
<BuildMacro Include="CppWinRTVerbosity">
14+
<Value>$(CppWinRTVerbosity)</Value>
15+
</BuildMacro>
16+
</ItemGroup>
17+
</Project>
2.95 KB
Binary file not shown.
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="..\packages\Microsoft.Windows.CppWinRT.2.0.190506.1\build\native\Microsoft.Windows.CppWinRT.props" Condition="Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.190506.1\build\native\Microsoft.Windows.CppWinRT.props')" />
4+
<PropertyGroup Label="Globals">
5+
<CppWinRTOptimized>true</CppWinRTOptimized>
6+
<MinimalCoreWin>true</MinimalCoreWin>
7+
<VCProjectVersion>15.0</VCProjectVersion>
8+
<ProjectGuid>{8c080d24-d4a8-4ffd-86d7-d99a06a0dbde}</ProjectGuid>
9+
<Keyword>Win32Proj</Keyword>
10+
<RootNamespace>StreamFromResource</RootNamespace>
11+
<WindowsTargetPlatformVersion Condition=" '$(WindowsTargetPlatformVersion)' == '' ">10.0.17763.0</WindowsTargetPlatformVersion>
12+
<WindowsTargetPlatformMinVersion>10.0.17134.0</WindowsTargetPlatformMinVersion>
13+
</PropertyGroup>
14+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
15+
<ItemGroup Label="ProjectConfigurations">
16+
<ProjectConfiguration Include="Debug|Win32">
17+
<Configuration>Debug</Configuration>
18+
<Platform>Win32</Platform>
19+
</ProjectConfiguration>
20+
<ProjectConfiguration Include="Release|Win32">
21+
<Configuration>Release</Configuration>
22+
<Platform>Win32</Platform>
23+
</ProjectConfiguration>
24+
<ProjectConfiguration Include="Debug|x64">
25+
<Configuration>Debug</Configuration>
26+
<Platform>x64</Platform>
27+
</ProjectConfiguration>
28+
<ProjectConfiguration Include="Release|x64">
29+
<Configuration>Release</Configuration>
30+
<Platform>x64</Platform>
31+
</ProjectConfiguration>
32+
</ItemGroup>
33+
<PropertyGroup Label="Configuration">
34+
<ConfigurationType>Application</ConfigurationType>
35+
<PlatformToolset>v140</PlatformToolset>
36+
<PlatformToolset Condition="'$(VisualStudioVersion)' == '15.0'">v141</PlatformToolset>
37+
<PlatformToolset Condition="'$(VisualStudioVersion)' == '16.0'">v142</PlatformToolset>
38+
<CharacterSet>Unicode</CharacterSet>
39+
</PropertyGroup>
40+
<PropertyGroup Condition="'$(Configuration)'=='Debug'" Label="Configuration">
41+
<UseDebugLibraries>true</UseDebugLibraries>
42+
<LinkIncremental>true</LinkIncremental>
43+
</PropertyGroup>
44+
<PropertyGroup Condition="'$(Configuration)'=='Release'" Label="Configuration">
45+
<UseDebugLibraries>false</UseDebugLibraries>
46+
<WholeProgramOptimization>true</WholeProgramOptimization>
47+
<LinkIncremental>false</LinkIncremental>
48+
</PropertyGroup>
49+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
50+
<ImportGroup Label="ExtensionSettings">
51+
</ImportGroup>
52+
<ImportGroup Label="Shared">
53+
</ImportGroup>
54+
<ImportGroup Label="PropertySheets">
55+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
56+
</ImportGroup>
57+
<ImportGroup Label="PropertySheets">
58+
<Import Project="PropertySheet.props" />
59+
</ImportGroup>
60+
<PropertyGroup Label="UserMacros" />
61+
<ItemDefinitionGroup>
62+
<ClCompile>
63+
<PrecompiledHeader>Use</PrecompiledHeader>
64+
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
65+
<PrecompiledHeaderOutputFile>$(IntDir)pch.pch</PrecompiledHeaderOutputFile>
66+
<PreprocessorDefinitions>_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
67+
<WarningLevel>Level4</WarningLevel>
68+
<AdditionalOptions>%(AdditionalOptions) /permissive- /bigobj</AdditionalOptions>
69+
</ClCompile>
70+
</ItemDefinitionGroup>
71+
<ItemDefinitionGroup Condition="'$(Configuration)'=='Debug'">
72+
<ClCompile>
73+
<Optimization>Disabled</Optimization>
74+
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
75+
</ClCompile>
76+
<Link>
77+
<SubSystem>Console</SubSystem>
78+
<GenerateWindowsMetadata>false</GenerateWindowsMetadata>
79+
</Link>
80+
</ItemDefinitionGroup>
81+
<ItemDefinitionGroup Condition="'$(Platform)'=='Win32'">
82+
<ClCompile>
83+
<PreprocessorDefinitions>WIN32;%(PreprocessorDefinitions)</PreprocessorDefinitions>
84+
</ClCompile>
85+
</ItemDefinitionGroup>
86+
<ItemDefinitionGroup Condition="'$(Configuration)'=='Release'">
87+
<ClCompile>
88+
<Optimization>MaxSpeed</Optimization>
89+
<FunctionLevelLinking>true</FunctionLevelLinking>
90+
<IntrinsicFunctions>true</IntrinsicFunctions>
91+
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
92+
</ClCompile>
93+
<Link>
94+
<SubSystem>Console</SubSystem>
95+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
96+
<OptimizeReferences>true</OptimizeReferences>
97+
<GenerateWindowsMetadata>false</GenerateWindowsMetadata>
98+
</Link>
99+
</ItemDefinitionGroup>
100+
<ItemGroup>
101+
<ClInclude Include="pch.h" />
102+
<ClInclude Include="resource.h" />
103+
</ItemGroup>
104+
<ItemGroup>
105+
<ClCompile Include="main.cpp" />
106+
<ClCompile Include="pch.cpp">
107+
<PrecompiledHeader>Create</PrecompiledHeader>
108+
</ClCompile>
109+
</ItemGroup>
110+
<ItemGroup>
111+
<None Include="model.onnx" />
112+
<None Include="packages.config" />
113+
<None Include="PropertySheet.props" />
114+
<Text Include="readme.txt">
115+
<DeploymentContent>false</DeploymentContent>
116+
</Text>
117+
</ItemGroup>
118+
<ItemGroup>
119+
<ResourceCompile Include="Resource.rc" />
120+
</ItemGroup>
121+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
122+
<ImportGroup Label="ExtensionTargets">
123+
<Import Project="..\packages\Microsoft.Windows.CppWinRT.2.0.190506.1\build\native\Microsoft.Windows.CppWinRT.targets" Condition="Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.190506.1\build\native\Microsoft.Windows.CppWinRT.targets')" />
124+
</ImportGroup>
125+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
126+
<PropertyGroup>
127+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
128+
</PropertyGroup>
129+
<Error Condition="!Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.190506.1\build\native\Microsoft.Windows.CppWinRT.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Windows.CppWinRT.2.0.190506.1\build\native\Microsoft.Windows.CppWinRT.props'))" />
130+
<Error Condition="!Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.190506.1\build\native\Microsoft.Windows.CppWinRT.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Windows.CppWinRT.2.0.190506.1\build\native\Microsoft.Windows.CppWinRT.targets'))" />
131+
</Target>
132+
</Project>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<Filter Include="Source Files">
5+
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
6+
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
7+
</Filter>
8+
<Filter Include="Header Files">
9+
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
10+
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
11+
</Filter>
12+
<Filter Include="Resource Files">
13+
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
14+
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
15+
</Filter>
16+
</ItemGroup>
17+
<ItemGroup>
18+
<ClInclude Include="pch.h">
19+
<Filter>Header Files</Filter>
20+
</ClInclude>
21+
<ClInclude Include="resource.h">
22+
<Filter>Header Files</Filter>
23+
</ClInclude>
24+
</ItemGroup>
25+
<ItemGroup>
26+
<ClCompile Include="pch.cpp">
27+
<Filter>Source Files</Filter>
28+
</ClCompile>
29+
<ClCompile Include="main.cpp">
30+
<Filter>Source Files</Filter>
31+
</ClCompile>
32+
</ItemGroup>
33+
<ItemGroup>
34+
<None Include="PropertySheet.props" />
35+
<None Include="packages.config" />
36+
<None Include="model.onnx">
37+
<Filter>Resource Files</Filter>
38+
</None>
39+
</ItemGroup>
40+
<ItemGroup>
41+
<Text Include="readme.txt" />
42+
</ItemGroup>
43+
<ItemGroup>
44+
<ResourceCompile Include="Resource.rc">
45+
<Filter>Resource Files</Filter>
46+
</ResourceCompile>
47+
</ItemGroup>
48+
</Project>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include "pch.h"
2+
#include <Windows.h>
3+
#include "resource.h"
4+
#include <winrt/windows.storage.streams.h>
5+
#include <winrt/Windows.AI.MachineLearning.h>
6+
7+
using namespace winrt;
8+
using namespace winrt::Windows::Storage::Streams;
9+
using namespace winrt::Windows::AI::MachineLearning;
10+
11+
int main()
12+
{
13+
init_apartment();
14+
15+
// Find the resource
16+
const auto modelResource = FindResource(NULL, MAKEINTRESOURCE(IDR_SQUEEZENET_MODEL), DataFileTypeString);
17+
if (modelResource == nullptr)
18+
{
19+
printf("failed to find resource.");
20+
return 1;
21+
}
22+
23+
// Load the resource
24+
const auto modelMem = LoadResource(NULL, modelResource);
25+
if (modelMem == nullptr)
26+
{
27+
printf("failed to load resource");
28+
return 2;
29+
}
30+
31+
try
32+
{
33+
// get a byte point to the resource
34+
const unsigned char* pModelData = static_cast<const unsigned char*>(LockResource(modelMem));
35+
const auto size = SizeofResource(NULL, modelResource);
36+
37+
// write the bytes into a stream
38+
InMemoryRandomAccessStream modelStream;
39+
DataWriter writer(modelStream);
40+
writer.WriteBytes(array_view<const unsigned char>(pModelData, pModelData + size));
41+
writer.StoreAsync().get();
42+
43+
// clean up.
44+
FreeResource(modelMem);
45+
46+
// wrap the stream in a stream reference
47+
auto modelStreamReference = RandomAccessStreamReference::CreateFromStream(modelStream);
48+
49+
// load the model from stream reference
50+
auto learningModel = LearningModel::LoadFromStream(modelStreamReference);
51+
52+
// create the session binding
53+
auto learningModelSession = LearningModelSession(learningModel);
54+
auto learningModelBinding = LearningModelBinding(learningModelSession);
55+
56+
// bind uninitialized data
57+
std::vector<int64_t> shape = { 1,3,224,224 };
58+
std::vector<float> inputData;
59+
inputData.resize(3 * 224 * 224);
60+
auto inputTensor = TensorFloat::CreateFromArray(shape, inputData);
61+
learningModelBinding.Bind(L"data_0", inputTensor);
62+
63+
// evaluate
64+
auto results = learningModelSession.Evaluate(learningModelBinding, L"");
65+
}
66+
catch (...)
67+
{
68+
printf("caught an exception");
69+
return 3;
70+
}
71+
72+
printf("success!");
73+
return 0;
74+
}
4.72 MB
Binary file not shown.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="Microsoft.Windows.CppWinRT" version="2.0.190506.1" targetFramework="native" />
4+
</packages>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "pch.h"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#pragma once
2+
#include <winrt/Windows.Foundation.h>
3+
#include <winrt/Windows.Foundation.Collections.h>
4+

0 commit comments

Comments
 (0)