Clean up residual changes to squeezenet

This commit is contained in:
janezdu 2021-06-16 16:21:42 -04:00
Родитель a7f3c3cfa6
Коммит 3f0a38fdb2
15 изменённых файлов: 198 добавлений и 1694 удалений

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -1,311 +0,0 @@
#include "pch.h"
#include "MelSpectrogram.h"
#include "FileHelper.h"
#include <winrt/Microsoft.AI.MachineLearning.Experimental.h>
using namespace winrt;
using namespace Microsoft::AI::MachineLearning::Experimental;
using namespace Microsoft::AI::MachineLearning;
static const wchar_t MS_EXPERIMENTAL_DOMAIN[] = L"com.microsoft.experimental";
using Operator = LearningModelOperator;
#define INT64(x) static_cast<int64_t>(x)
#define SIZET(x) static_cast<size_t>(x)
#define INT32(x) static_cast<int32_t>(x)
template <typename T>
static auto MakePureFrequency(float frequency_in_hertz, size_t signal_size, size_t sample_rate) {
float amplitude = 4;
float angular_velocity = frequency_in_hertz * 2 * 3.1415f;
std::vector<T> signal(signal_size);
for (size_t i = 0; i < signal_size; i++) {
T time = i / static_cast<T>(sample_rate);
signal[i] = amplitude * cos(angular_velocity * time);
}
return signal;
}
template <typename T>
static auto MakeMiddleC(size_t signal_size, size_t sample_rate) {
float middle_c_in_hertz = 261.626f;
return MakePureFrequency<T>(middle_c_in_hertz, signal_size, sample_rate);
}
template <typename T>
static auto MakeC2(size_t signal_size, size_t sample_rate) {
float middle_c_in_hertz = 261.626f * 2;
return MakePureFrequency<T>(middle_c_in_hertz, signal_size, sample_rate);
}
template <typename T>
static auto MakeC4(size_t signal_size, size_t sample_rate) {
float middle_c_in_hertz = 261.626f * 4;
return MakePureFrequency<T>(middle_c_in_hertz, signal_size, sample_rate);
}
template <typename T>
static auto MakeThreeTones(size_t signal_size, size_t sample_rate) {
auto middle_c = MakeMiddleC<T>(signal_size, sample_rate);
auto c2 = MakeC2<T>(signal_size, sample_rate);
auto c4 = MakeC4<T>(signal_size, sample_rate);
for (size_t i = 0; i < signal_size; i++) {
middle_c[i] = (i < signal_size / 3) ?
middle_c[i] :
(i < 2 * signal_size / 3) ?
(middle_c[i] + c2[i]) :
(middle_c[i] + c2[i] + c4[i]);
}
return middle_c;
}
static void WindowFunction(const wchar_t* window_operator_name, TensorKind kind) {
std::vector<int64_t> scalar_shape = {};
std::vector<int64_t> output_shape = { 32 };
auto double_data_type = TensorInt64Bit::CreateFromArray({}, { 11 });
auto window_operator =
Operator(window_operator_name, MS_EXPERIMENTAL_DOMAIN)
.SetInput(L"size", L"Input")
.SetOutput(L"output", L"Output");
if (kind == TensorKind::Double) {
window_operator.SetAttribute(L"output_datatype", double_data_type);
}
auto model =
LearningModelBuilder::Create(13)
.Inputs().Add(LearningModelBuilder::CreateTensorFeatureDescriptor(L"Input", TensorKind::Int64, scalar_shape))
.Outputs().Add(LearningModelBuilder::CreateTensorFeatureDescriptor(L"Output", kind, output_shape))
.Operators().Add(window_operator)
.CreateModel();
LearningModelSession session(model);
LearningModelBinding binding(session);
binding.Bind(L"Input", TensorInt64Bit::CreateFromArray(scalar_shape, { 32 }));
// Evaluate
auto result = session.Evaluate(binding, L"");
// Check results
printf("Output\n");
if (kind == TensorKind::Float) {
auto y_tensor = result.Outputs().Lookup(L"Output").as<TensorFloat>();
auto y_ivv = y_tensor.GetAsVectorView();
for (int i = 0; i < output_shape[0]; i++) {
printf("%f, ", y_ivv.GetAt(i));
}
}
if (kind == TensorKind::Double) {
auto y_tensor = result.Outputs().Lookup(L"Output").as<TensorDouble>();
auto y_ivv = y_tensor.GetAsVectorView();
for (int i = 0; i < output_shape[0]; i++) {
printf("%f, ", y_ivv.GetAt(i));
}
}
printf("\n");
}
static void ModelBuilding_HannWindow() {
WindowFunction(L"HannWindow", TensorKind::Float);
WindowFunction(L"HannWindow", TensorKind::Double);
}
static void STFT(size_t batch_size, size_t signal_size, size_t dft_size,
size_t hop_size, size_t sample_rate, bool is_onesided = false) {
auto n_dfts = static_cast<size_t>(1 + floor((signal_size - dft_size) / hop_size));
auto input_shape = std::vector<int64_t>{ 1, INT64(signal_size) };
auto output_shape =
std::vector<int64_t>{
INT64(batch_size),
INT64(n_dfts),
is_onesided ? ((INT64(dft_size) >> 1) + 1) : INT64(dft_size),
2
};
auto dft_length = TensorInt64Bit::CreateFromArray({}, { INT64(dft_size) });
auto model =
LearningModelBuilder::Create(13)
.Inputs().Add(LearningModelBuilder::CreateTensorFeatureDescriptor(L"Input.TimeSignal", TensorKind::Float, input_shape))
.Outputs().Add(LearningModelBuilder::CreateTensorFeatureDescriptor(L"Output.STFT", TensorKind::Float, output_shape))
.Outputs().Add(LearningModelBuilder::CreateTensorFeatureDescriptor(L"Output.HannWindow", TensorKind::Float, { INT64(dft_size) }))
.Operators().Add(Operator(L"HannWindow", MS_EXPERIMENTAL_DOMAIN)
.SetConstant(L"size", dft_length)
.SetOutput(L"output", L"Output.HannWindow"))
.Operators().Add(Operator(L"STFT", MS_EXPERIMENTAL_DOMAIN)
.SetAttribute(L"onesided", TensorInt64Bit::CreateFromArray({}, { INT64(is_onesided) }))
.SetInput(L"signal", L"Input.TimeSignal")
.SetInput(L"window", L"Output.HannWindow")
.SetConstant(L"frame_length", dft_length)
.SetConstant(L"frame_step", TensorInt64Bit::CreateFromArray({}, { INT64(hop_size) }))
.SetOutput(L"output", L"Output.STFT"))
.CreateModel();
LearningModelSession session(model);
LearningModelBinding binding(session);
// Create signal binding
auto signal = MakeMiddleC<float>(signal_size, sample_rate);
printf("\n");
printf("Input.TimeSignal:\n");
for (size_t i = 0; i < dft_size; i++) {
printf("%f, ", signal[i]);
}
// Bind
binding.Bind(L"Input.TimeSignal", TensorFloat::CreateFromArray(input_shape, signal));
// Evaluate
auto result = session.Evaluate(binding, L"");
printf("\n");
printf("Output.HannWindow\n");
auto window_tensor = result.Outputs().Lookup(L"Output.HannWindow").as<TensorFloat>();
auto window_ivv = window_tensor.GetAsVectorView();
for (uint32_t i = 0; i < window_ivv.Size(); i++) {
printf("%f, ", window_ivv.GetAt(i));
}
printf("\n");
printf("Output.STFT\n");
// Check results
auto y_tensor = result.Outputs().Lookup(L"Output.STFT").as<TensorFloat>();
auto y_ivv = y_tensor.GetAsVectorView();
auto size = y_ivv.Size();
//WINML_EXPECT_EQUAL(size, n_dfts * output_shape[2] * 2);
for (size_t dft_idx = 0; dft_idx < n_dfts; dft_idx++) {
for (size_t i = 0; INT64(i) < output_shape[2]; i++) {
auto real_idx = static_cast<uint32_t>((i * 2) + (2 * dft_idx * output_shape[2]));
printf("(%d, %f , %fi), ", static_cast<uint32_t>(i), y_ivv.GetAt(real_idx), y_ivv.GetAt(real_idx + 1));
}
}
printf("\n");
}
static void ModelBuilding_MelWeightMatrix() {
std::vector<int64_t> output_shape = { INT64(9), INT64(8) };
auto builder =
LearningModelBuilder::Create(13)
.Outputs().Add(LearningModelBuilder::CreateTensorFeatureDescriptor(L"Output.MelWeightMatrix", TensorKind::Float, output_shape))
.Operators().Add(Operator(L"MelWeightMatrix", MS_EXPERIMENTAL_DOMAIN)
.SetConstant(L"num_mel_bins", TensorInt64Bit::CreateFromArray({}, { INT64(8) }))
.SetConstant(L"dft_length", TensorInt64Bit::CreateFromArray({}, { INT64(16) }))
.SetConstant(L"sample_rate", TensorInt64Bit::CreateFromArray({}, { INT64(8192) }))
.SetConstant(L"lower_edge_hertz", TensorFloat::CreateFromArray({}, { 0 }))
.SetConstant(L"upper_edge_hertz", TensorFloat::CreateFromArray({}, { 8192 / 2.f }))
.SetOutput(L"output", L"Output.MelWeightMatrix"));
auto model = builder.CreateModel();
LearningModelSession session(model);
LearningModelBinding binding(session);
auto result = session.Evaluate(binding, L"");
printf("\n");
printf("Output.MelWeightMatrix\n");
{
auto y_tensor = result.Outputs().Lookup(L"Output.MelWeightMatrix").as<TensorFloat>();
auto y_ivv = y_tensor.GetAsVectorView();
for (unsigned i = 0; i < y_ivv.Size(); i++) {
printf("%f, ", y_ivv.GetAt(i));
}
}
printf("\n");
}
void MelSpectrogram::MelSpectrogramOnThreeToneSignal(
size_t batch_size, size_t signal_size, size_t window_size, size_t dft_size,
size_t hop_size, size_t n_mel_bins, size_t sampling_rate) {
auto n_dfts = static_cast<size_t>(1 + floor((signal_size - dft_size) / hop_size));
auto onesided_dft_size = (dft_size >> 1) + 1;
std::vector<int64_t> signal_shape = { INT64(batch_size), INT64(signal_size) };
std::vector<int64_t> mel_spectrogram_shape = { INT64(batch_size), 1, INT64(n_dfts), INT64(n_mel_bins) };
auto builder =
LearningModelBuilder::Create(13)
.Inputs().Add(LearningModelBuilder::CreateTensorFeatureDescriptor(L"Input.TimeSignal", L"One-dimensional audio waveform", TensorKind::Float, signal_shape))
.Outputs().Add(LearningModelBuilder::CreateTensorFeatureDescriptor(L"Output.MelSpectrogram", L"Output description", TensorKind::Float, mel_spectrogram_shape))
.Operators().Add(Operator(L"HannWindow")
.SetConstant(L"size", TensorInt64Bit::CreateFromArray({}, { INT64(window_size) }))
.SetOutput(L"output", L"hann_window"))
.Operators().Add(Operator(L"STFT")
.SetName(L"STFT_NAMED_NODE")
.SetInput(L"signal", L"Input.TimeSignal")
.SetInput(L"window", L"hann_window")
.SetConstant(L"frame_length", TensorInt64Bit::CreateFromArray({}, { INT64(dft_size) }))
.SetConstant(L"frame_step", TensorInt64Bit::CreateFromArray({}, { INT64(hop_size) }))
.SetOutput(L"output", L"stft_output"))
.Operators().Add(Operator(L"ReduceSumSquare")
.SetInput(L"data", L"stft_output")
.SetAttribute(L"axes", TensorInt64Bit::CreateFromArray({ 1 }, { 3 }))
.SetAttribute(L"keepdims", TensorInt64Bit::CreateFromArray({}, { 0 }))
.SetOutput(L"reduced", L"magnitude_squared"))
.Operators().Add(Operator(L"Div")
.SetInput(L"A", L"magnitude_squared")
.SetConstant(L"B", TensorFloat::CreateFromArray({}, { static_cast<float>(dft_size) }))
.SetOutput(L"C", L"power_frames"))
.Operators().Add(Operator(L"MelWeightMatrix")
.SetConstant(L"num_mel_bins", TensorInt64Bit::CreateFromArray({}, { INT64(n_mel_bins) }))
.SetConstant(L"dft_length", TensorInt64Bit::CreateFromArray({}, { INT64(dft_size) }))
.SetConstant(L"sample_rate", TensorInt64Bit::CreateFromArray({}, { INT64(sampling_rate) }))
.SetConstant(L"lower_edge_hertz", TensorFloat::CreateFromArray({}, { 0 }))
.SetConstant(L"upper_edge_hertz", TensorFloat::CreateFromArray({}, { sampling_rate / 2.f }))
.SetOutput(L"output", L"mel_weight_matrix"))
.Operators().Add(Operator(L"Reshape")
.SetInput(L"data", L"power_frames")
.SetConstant(L"shape", TensorInt64Bit::CreateFromArray({ 2 }, { INT64(batch_size * n_dfts), INT64(onesided_dft_size) }))
.SetOutput(L"reshaped", L"reshaped_output"))
.Operators().Add(Operator(L"MatMul")
.SetInput(L"A", L"reshaped_output")
.SetInput(L"B", L"mel_weight_matrix")
.SetOutput(L"Y", L"mel_spectrogram"))
.Operators().Add(Operator(L"Reshape")
.SetInput(L"data", L"mel_spectrogram")
.SetConstant(L"shape", TensorInt64Bit::CreateFromArray({ 4 }, mel_spectrogram_shape))
.SetOutput(L"reshaped", L"Output.MelSpectrogram"));
auto model = builder.CreateModel();
LearningModelSession session(model);
LearningModelBinding binding(session);
// Bind input
auto signal = MakeThreeTones<float>(signal_size, sampling_rate);
binding.Bind(L"Input.TimeSignal", TensorFloat::CreateFromArray(signal_shape, signal));
// Bind output
auto output_image =
winrt::Windows::Media::VideoFrame(
winrt::Windows::Graphics::Imaging::BitmapPixelFormat::Bgra8,
INT32(n_mel_bins),
INT32(n_dfts));
binding.Bind(L"Output.MelSpectrogram", output_image);
// Evaluate
auto start = std::chrono::high_resolution_clock::now();
auto result = session.Evaluate(binding, L"");
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::micro> evaluate_duration_in_microseconds = end - start;
printf("Evaluate Took: %f\n", evaluate_duration_in_microseconds.count());
// Check the output video frame object by saving output image to disk
std::wstring out_name = L"mel_spectrogram.jpg";
// Save the output
std::wstring modulePath = FileHelper::GetModulePath();
winrt::Windows::Storage::StorageFolder folder = winrt::Windows::Storage::StorageFolder::GetFolderFromPathAsync(modulePath).get();
winrt::Windows::Storage::StorageFile file = folder.CreateFileAsync(out_name, winrt::Windows::Storage::CreationCollisionOption::ReplaceExisting).get();
winrt::Windows::Storage::Streams::IRandomAccessStream write_stream = file.OpenAsync(winrt::Windows::Storage::FileAccessMode::ReadWrite).get();
winrt::Windows::Graphics::Imaging::BitmapEncoder encoder = winrt::Windows::Graphics::Imaging::BitmapEncoder::CreateAsync(winrt::Windows::Graphics::Imaging::BitmapEncoder::JpegEncoderId(), write_stream).get();
encoder.SetSoftwareBitmap(output_image.SoftwareBitmap());
encoder.FlushAsync().get();
// Save the model
builder.Save(L"spectrogram.onnx");
printf("\n");
}

Просмотреть файл

@ -1,11 +0,0 @@
#pragma once
#ifndef MELSPECTROGRAM_H
#define MELSPECTROGRAM_H
class MelSpectrogram {
public:
static void MelSpectrogramOnThreeToneSignal(
size_t batch_size, size_t signal_size, size_t window_size, size_t dft_size,
size_t hop_size, size_t n_mel_bins, size_t sampling_rate);
};
#endif

Просмотреть файл

@ -1,8 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\..\packages\Microsoft.Windows.CppWinRT.2.0.210505.3\build\native\Microsoft.Windows.CppWinRT.props" Condition="Exists('..\..\packages\Microsoft.Windows.CppWinRT.2.0.210505.3\build\native\Microsoft.Windows.CppWinRT.props')" />
<Import Project="..\..\packages\Microsoft.AI.MachineLearning.1.8.0\build\native\Microsoft.AI.MachineLearning.props" Condition="Exists('..\..\packages\Microsoft.AI.MachineLearning.1.8.0\build\native\Microsoft.AI.MachineLearning.props')" />
<Import Project="..\..\packages\Microsoft.AI.DirectML.1.5.1\build\Microsoft.AI.DirectML.props" Condition="Exists('..\..\packages\Microsoft.AI.DirectML.1.5.1\build\Microsoft.AI.DirectML.props')" />
<Import Project="..\..\packages\Microsoft.AI.MachineLearning.1.3.0\build\native\Microsoft.AI.MachineLearning.props" Condition="Exists('..\..\packages\Microsoft.AI.MachineLearning.1.3.0\build\native\Microsoft.AI.MachineLearning.props')" />
<Import Project="..\..\packages\Microsoft.Windows.CppWinRT.2.0.200316.3\build\native\Microsoft.Windows.CppWinRT.props" Condition="Exists('..\..\packages\Microsoft.Windows.CppWinRT.2.0.200316.3\build\native\Microsoft.Windows.CppWinRT.props')" />
<PropertyGroup Label="Globals">
<CppWinRTEnabled>true</CppWinRTEnabled>
<RequiredBundles>$(RequiredBundles);Microsoft.Windows.CppWinRT</RequiredBundles>
@ -11,9 +10,9 @@
<ProjectGuid>{2bf804d4-daa2-42be-9f21-0e94f021ef53}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>SqueezeNetObjectDetection</RootNamespace>
<WindowsTargetPlatformVersion Condition=" '$(WindowsTargetPlatformVersion)' == '' ">10.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion Condition=" '$(WindowsTargetPlatformVersion)' == '' ">10.0.18362.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformMinVersion>10.0.17763.0</WindowsTargetPlatformMinVersion>
<ProjectName>MelSpectrogram</ProjectName>
<ProjectName>SqueezeNetObjectDetectionCPP</ProjectName>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<ItemGroup Label="ProjectConfigurations">
@ -44,7 +43,7 @@
</ItemGroup>
<PropertyGroup Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Debug'" Label="Configuration">
@ -124,11 +123,9 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="MelSpectrogram.h" />
<ClInclude Include="pch.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="MelSpectrogram.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="pch.cpp">
<PrecompiledHeader>Create</PrecompiledHeader>
@ -161,6 +158,7 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">false</ExcludedFromBuild>
<FileType>Document</FileType>
</CopyFileToFolders>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<CopyFileToFolders Include="..\..\..\..\SharedContent\media\kitten_224.png">
@ -183,15 +181,11 @@
<Project>{12103a5b-677a-4286-83d2-54eab9010c16}</Project>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
<Import Project="$(SolutionDir)\packages\rapidjson.v110.1.1.0\build\native\rapidjson.targets" Condition="Exists('$(SolutionDir)\packages\rapidjson.v110.1.1.0\build\native\rapidjson.targets')" />
<Import Project="..\..\packages\Microsoft.AI.DirectML.1.5.1\build\Microsoft.AI.DirectML.targets" Condition="Exists('..\..\packages\Microsoft.AI.DirectML.1.5.1\build\Microsoft.AI.DirectML.targets')" />
<Import Project="..\..\packages\Microsoft.AI.MachineLearning.1.8.0\build\native\Microsoft.AI.MachineLearning.targets" Condition="Exists('..\..\packages\Microsoft.AI.MachineLearning.1.8.0\build\native\Microsoft.AI.MachineLearning.targets')" />
<Import Project="..\..\packages\Microsoft.Windows.CppWinRT.2.0.210505.3\build\native\Microsoft.Windows.CppWinRT.targets" Condition="Exists('..\..\packages\Microsoft.Windows.CppWinRT.2.0.210505.3\build\native\Microsoft.Windows.CppWinRT.targets')" />
<Import Project="..\..\packages\Microsoft.Windows.CppWinRT.2.0.200316.3\build\native\Microsoft.Windows.CppWinRT.targets" Condition="Exists('..\..\packages\Microsoft.Windows.CppWinRT.2.0.200316.3\build\native\Microsoft.Windows.CppWinRT.targets')" />
<Import Project="..\..\packages\Microsoft.AI.MachineLearning.1.3.0\build\native\Microsoft.AI.MachineLearning.targets" Condition="Exists('..\..\packages\Microsoft.AI.MachineLearning.1.3.0\build\native\Microsoft.AI.MachineLearning.targets')" />
</ImportGroup>
<ItemDefinitionGroup>
<ClCompile>
@ -219,11 +213,9 @@
<PropertyGroup>
<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>
</PropertyGroup>
<Error Condition="!Exists('..\..\packages\Microsoft.AI.DirectML.1.5.1\build\Microsoft.AI.DirectML.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Microsoft.AI.DirectML.1.5.1\build\Microsoft.AI.DirectML.props'))" />
<Error Condition="!Exists('..\..\packages\Microsoft.AI.DirectML.1.5.1\build\Microsoft.AI.DirectML.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Microsoft.AI.DirectML.1.5.1\build\Microsoft.AI.DirectML.targets'))" />
<Error Condition="!Exists('..\..\packages\Microsoft.AI.MachineLearning.1.8.0\build\native\Microsoft.AI.MachineLearning.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Microsoft.AI.MachineLearning.1.8.0\build\native\Microsoft.AI.MachineLearning.props'))" />
<Error Condition="!Exists('..\..\packages\Microsoft.AI.MachineLearning.1.8.0\build\native\Microsoft.AI.MachineLearning.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Microsoft.AI.MachineLearning.1.8.0\build\native\Microsoft.AI.MachineLearning.targets'))" />
<Error Condition="!Exists('..\..\packages\Microsoft.Windows.CppWinRT.2.0.210505.3\build\native\Microsoft.Windows.CppWinRT.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Microsoft.Windows.CppWinRT.2.0.210505.3\build\native\Microsoft.Windows.CppWinRT.props'))" />
<Error Condition="!Exists('..\..\packages\Microsoft.Windows.CppWinRT.2.0.210505.3\build\native\Microsoft.Windows.CppWinRT.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Microsoft.Windows.CppWinRT.2.0.210505.3\build\native\Microsoft.Windows.CppWinRT.targets'))" />
<Error Condition="!Exists('..\..\packages\Microsoft.Windows.CppWinRT.2.0.200316.3\build\native\Microsoft.Windows.CppWinRT.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Microsoft.Windows.CppWinRT.2.0.200316.3\build\native\Microsoft.Windows.CppWinRT.props'))" />
<Error Condition="!Exists('..\..\packages\Microsoft.Windows.CppWinRT.2.0.200316.3\build\native\Microsoft.Windows.CppWinRT.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Microsoft.Windows.CppWinRT.2.0.200316.3\build\native\Microsoft.Windows.CppWinRT.targets'))" />
<Error Condition="!Exists('..\..\packages\Microsoft.AI.MachineLearning.1.3.0\build\native\Microsoft.AI.MachineLearning.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Microsoft.AI.MachineLearning.1.3.0\build\native\Microsoft.AI.MachineLearning.props'))" />
<Error Condition="!Exists('..\..\packages\Microsoft.AI.MachineLearning.1.3.0\build\native\Microsoft.AI.MachineLearning.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Microsoft.AI.MachineLearning.1.3.0\build\native\Microsoft.AI.MachineLearning.targets'))" />
</Target>
</Project>

Просмотреть файл

@ -3,11 +3,12 @@
<ItemGroup>
<ClCompile Include="main.cpp" />
<ClCompile Include="pch.cpp" />
<ClCompile Include="MelSpectrogram.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="pch.h" />
<ClInclude Include="MelSpectrogram.h" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Filter Include="SharedContent">
@ -25,7 +26,4 @@
<Filter>SharedContent</Filter>
</CopyFileToFolders>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
</Project>

Просмотреть файл

@ -1,31 +0,0 @@
#include "pch.h"
#include "backup.h"
#if __has_include("backup.g.cpp")
#include "backup.g.cpp"
#endif
using namespace winrt;
using namespace Windows::UI::Xaml;
namespace winrt::SqueezeNetObjectDetection::implementation
{
backup::backup()
{
InitializeComponent();
}
int32_t backup::MyProperty()
{
throw hresult_not_implemented();
}
void backup::MyProperty(int32_t /* value */)
{
throw hresult_not_implemented();
}
void backup::ClickHandler(IInspectable const&, RoutedEventArgs const&)
{
Button().Content(box_value(L"Clicked"));
}
}

Просмотреть файл

@ -1,23 +0,0 @@
#pragma once
#include "backup.g.h"
namespace winrt::SqueezeNetObjectDetection::implementation
{
struct backup : backupT<backup>
{
backup();
int32_t MyProperty();
void MyProperty(int32_t value);
void ClickHandler(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::RoutedEventArgs const& args);
};
}
namespace winrt::SqueezeNetObjectDetection::factory_implementation
{
struct backup : backupT<backup, implementation::backup>
{
};
}

Просмотреть файл

@ -1,9 +0,0 @@
namespace SqueezeNetObjectDetection
{
[default_interface]
runtimeclass backup : Windows.UI.Xaml.Controls.Page
{
backup();
Int32 MyProperty;
}
}

Просмотреть файл

@ -1,13 +0,0 @@
<Page
x:Class="SqueezeNetObjectDetection.backup"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:SqueezeNetObjectDetection"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<Button x:Name="Button" Click="ClickHandler">Click Me</Button>
</StackPanel>
</Page>

Просмотреть файл

@ -3,22 +3,20 @@
#include "pch.h"
#include "FileHelper.h"
#include <winrt/Microsoft.AI.MachineLearning.Experimental.h>
#include "MelSpectrogram.h"
using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
#ifdef USE_WINML_NUGET
using namespace Microsoft::AI::MachineLearning;
#else
using namespace Windows::AI::MachineLearning;
#endif
using namespace Windows::Media;
using namespace Windows::Graphics::Imaging;
using namespace Windows::Storage;
using namespace std;
// globals
vector<string> labels;
string labelsFileName("labels.txt");
@ -43,149 +41,178 @@ wstring GetModelPath()
// usage: SqueezeNet [imagefile] [cpu|directx]
int main(int argc, char* argv[])
{
printf("Melspec on 3 tone signal \n");
init_apartment();
MelSpectrogram ms;
// did they pass in the args
if (ParseArgs(argc, argv) == false)
{
printf("Usage: %s [imagefile] [cpu|directx]", argv[0]);
return -1;
}
size_t batch_size = 1;
size_t signal_size = 40;
size_t window_size = 5;
size_t dft_size = 3;
size_t hop_size = 1;
size_t n_mel_bins = 32;
size_t sampling_rate = 4000;
// Get model path
auto modelPath = GetModelPath();
// load the model
printf("Loading modelfile '%ws' on the '%s' device\n", modelPath.c_str(), deviceName.c_str());
DWORD ticks = GetTickCount();
auto model = LearningModel::LoadFromFilePath(modelPath);
ticks = GetTickCount() - ticks;
printf("model file loaded in %d ticks\n", ticks);
ms.MelSpectrogramOnThreeToneSignal(batch_size, signal_size, window_size, dft_size,
hop_size, n_mel_bins, sampling_rate);
printf("done");
// get model color management mode
printf("Getting model color management mode...\n");
ColorManagementMode colorManagementMode = GetColorManagementMode(model);
// load the image
printf("Loading the image...\n");
auto imageFrame = LoadImageFile(imagePath, colorManagementMode);
// now create a session and binding
LearningModelSession session(model, LearningModelDevice(deviceKind));
LearningModelBinding binding(session);
// bind the intput image
printf("Binding...\n");
binding.Bind(L"data_0", ImageFeatureValue::CreateFromVideoFrame(imageFrame));
// temp: bind the output (we don't support unbound outputs yet)
vector<int64_t> shape({ 1, 1000, 1, 1 });
binding.Bind(L"softmaxout_1", TensorFloat::Create(shape));
// now run the model
printf("Running the model...\n");
ticks = GetTickCount();
auto results = session.Evaluate(binding, L"RunId");
ticks = GetTickCount() - ticks;
printf("model run took %d ticks\n", ticks);
// get the output
auto resultTensor = results.Outputs().Lookup(L"softmaxout_1").as<TensorFloat>();
auto resultVector = resultTensor.GetAsVectorView();
PrintResults(resultVector);
}
bool ParseArgs(int argc, char* argv[])
{
if (argc < 2)
{
return false;
}
// get the image file
imagePath = hstring(wstring_to_utf8().from_bytes(argv[1]));
// did they pass a third arg?
if (argc >= 3)
{
deviceName = argv[2];
if (deviceName == "cpu")
{
deviceKind = LearningModelDeviceKind::Cpu;
}
else if (deviceName == "directx")
{
deviceKind = LearningModelDeviceKind::DirectX;
}
else
{
deviceName = "default";
deviceKind = LearningModelDeviceKind::Default;
}
}
return true;
}
ColorManagementMode GetColorManagementMode(const LearningModel& model)
{
// Get model color space gamma
hstring gammaSpace = L"";
try
{
gammaSpace = model.Metadata().Lookup(L"Image.ColorSpaceGamma");
}
catch (...)
{
printf(" Model does not have color space gamma information. Will color manage to sRGB by default...\n");
}
if (gammaSpace == L"" || _wcsicmp(gammaSpace.c_str(), L"SRGB") == 0)
{
return ColorManagementMode::ColorManageToSRgb;
}
// Due diligence should be done to make sure that the input image is within the model's colorspace. There are multiple non-sRGB color spaces.
printf(" Model metadata indicates that color gamma space is : %ws. Will not manage color space to sRGB...\n", gammaSpace.c_str());
return ColorManagementMode::DoNotColorManage;
}
//bool ParseArgs(int argc, char* argv[])
//{
// if (argc < 2)
// {
// return false;
// }
// // get the image file
// imagePath = hstring(wstring_to_utf8().from_bytes(argv[1]));
// // did they pass a third arg?
// if (argc >= 3)
// {
// deviceName = argv[2];
// if (deviceName == "cpu")
// {
// deviceKind = LearningModelDeviceKind::Cpu;
// }
// else if (deviceName == "directx")
// {
// deviceKind = LearningModelDeviceKind::DirectX;
// }
// else
// {
// deviceName = "default";
// deviceKind = LearningModelDeviceKind::Default;
// }
// }
// return true;
//}
//
//ColorManagementMode GetColorManagementMode(const LearningModel& model)
//{
// // Get model color space gamma
// hstring gammaSpace = L"";
// try
// {
// gammaSpace = model.Metadata().Lookup(L"Image.ColorSpaceGamma");
// }
// catch (...)
// {
// printf(" Model does not have color space gamma information. Will color manage to sRGB by default...\n");
// }
// if (gammaSpace == L"" || _wcsicmp(gammaSpace.c_str(), L"SRGB") == 0)
// {
// return ColorManagementMode::ColorManageToSRgb;
// }
// // Due diligence should be done to make sure that the input image is within the model's colorspace. There are multiple non-sRGB color spaces.
// printf(" Model metadata indicates that color gamma space is : %ws. Will not manage color space to sRGB...\n", gammaSpace.c_str());
// return ColorManagementMode::DoNotColorManage;
//}
//
//VideoFrame LoadImageFile(hstring filePath, ColorManagementMode colorManagementMode)
//{
// BitmapDecoder decoder = NULL;
// try
// {
// // open the file
// StorageFile file = StorageFile::GetFileFromPathAsync(filePath).get();
// // get a stream on it
// auto stream = file.OpenAsync(FileAccessMode::Read).get();
// // Create the decoder from the stream
// decoder = BitmapDecoder::CreateAsync(stream).get();
// }
// catch (...)
// {
// printf(" Failed to load the image file, make sure you are using fully qualified paths\r\n");
// exit(EXIT_FAILURE);
// }
// SoftwareBitmap softwareBitmap = NULL;
// try
// {
// softwareBitmap = decoder.GetSoftwareBitmapAsync(
// decoder.BitmapPixelFormat(),
// decoder.BitmapAlphaMode(),
// BitmapTransform(),
// ExifOrientationMode::RespectExifOrientation,
// colorManagementMode
// ).get();
// }
// catch (hresult_error hr)
// {
// printf(" Failed to create SoftwareBitmap! Please make sure that input image is within the model's colorspace.\n");
// printf(" %ws\n", hr.message().c_str());
// exit(hr.code());
// }
// VideoFrame inputImage = NULL;
// try
// {
// // load a videoframe from it
// inputImage = VideoFrame::CreateWithSoftwareBitmap(softwareBitmap);
// }
// catch (hresult_error hr)
// {
// printf("Failed to create videoframe from software bitmap.");
// printf(" %ws\n", hr.message().c_str());
// exit(hr.code());
// }
// // all done
// return inputImage;
//}
//
//void PrintResults(IVectorView<float> results)
//{
// // load the labels
// auto modulePath = FileHelper::GetModulePath();
// std::string labelsFilePath =
// std::string(modulePath.begin(), modulePath.end()) + labelsFileName;
// labels = FileHelper::LoadLabels(labelsFilePath);
//
// vector<pair<float, uint32_t>> sortedResults;
// for (uint32_t i = 0; i < results.Size(); i++) {
// pair<float, uint32_t> curr;
// curr.first = results.GetAt(i);
// curr.second = i;
// sortedResults.push_back(curr);
// }
// std::sort(sortedResults.begin(), sortedResults.end(),
// [](pair<float, uint32_t> const &a, pair<float, uint32_t> const &b) { return a.first > b.first; });
//
// // Display the result
// for (int i = 0; i < 3; i++)
// {
// pair<float, uint32_t> curr = sortedResults.at(i);
// printf("%s with confidence of %f\n", labels[curr.second].c_str(), curr.first);
// }
//}
VideoFrame LoadImageFile(hstring filePath, ColorManagementMode colorManagementMode)
{
BitmapDecoder decoder = NULL;
try
{
// open the file
StorageFile file = StorageFile::GetFileFromPathAsync(filePath).get();
// get a stream on it
auto stream = file.OpenAsync(FileAccessMode::Read).get();
// Create the decoder from the stream
decoder = BitmapDecoder::CreateAsync(stream).get();
}
catch (...)
{
printf(" Failed to load the image file, make sure you are using fully qualified paths\r\n");
exit(EXIT_FAILURE);
}
SoftwareBitmap softwareBitmap = NULL;
try
{
softwareBitmap = decoder.GetSoftwareBitmapAsync(
decoder.BitmapPixelFormat(),
decoder.BitmapAlphaMode(),
BitmapTransform(),
ExifOrientationMode::RespectExifOrientation,
colorManagementMode
).get();
}
catch (hresult_error hr)
{
printf(" Failed to create SoftwareBitmap! Please make sure that input image is within the model's colorspace.\n");
printf(" %ws\n", hr.message().c_str());
exit(hr.code());
}
VideoFrame inputImage = NULL;
try
{
// load a videoframe from it
inputImage = VideoFrame::CreateWithSoftwareBitmap(softwareBitmap);
}
catch (hresult_error hr)
{
printf("Failed to create videoframe from software bitmap.");
printf(" %ws\n", hr.message().c_str());
exit(hr.code());
}
// all done
return inputImage;
}
void PrintResults(IVectorView<float> results)
{
// load the labels
auto modulePath = FileHelper::GetModulePath();
std::string labelsFilePath =
std::string(modulePath.begin(), modulePath.end()) + labelsFileName;
labels = FileHelper::LoadLabels(labelsFilePath);
vector<pair<float, uint32_t>> sortedResults;
for (uint32_t i = 0; i < results.Size(); i++) {
pair<float, uint32_t> curr;
curr.first = results.GetAt(i);
curr.second = i;
sortedResults.push_back(curr);
}
std::sort(sortedResults.begin(), sortedResults.end(),
[](pair<float, uint32_t> const &a, pair<float, uint32_t> const &b) { return a.first > b.first; });
// Display the result
for (int i = 0; i < 3; i++)
{
pair<float, uint32_t> curr = sortedResults.at(i);
printf("%s with confidence of %f\n", labels[curr.second].c_str(), curr.first);
}
}

Просмотреть файл

@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.AI.DirectML" version="1.5.1" targetFramework="native" />
<package id="Microsoft.AI.MachineLearning" version="1.8.0" targetFramework="native" />
<package id="Microsoft.Windows.CppWinRT" version="2.0.210505.3" targetFramework="native" />
<package id="Microsoft.AI.MachineLearning" version="1.3.0" targetFramework="native" />
<package id="Microsoft.Windows.CppWinRT" version="2.0.200316.3" targetFramework="native" />
</packages>

Просмотреть файл

@ -32,9 +32,8 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AI.MachineLearning" Version="1.8.0" />
<PackageReference Include="Microsoft.Windows.CppWinRT" Version="2.0.210505.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Microsoft.AI.MachineLearning" Version="1.7.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
</ItemGroup>
<ItemGroup>

Просмотреть файл

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
@ -32,8 +32,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Windows.CppWinRT" Version="2.0.210505.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
</ItemGroup>
<ItemGroup>

Просмотреть файл

@ -51,6 +51,7 @@ Global
{2CF5F1AA-BF69-498C-8B74-04137B2F67F3}.Release|x86.Build.0 = Release|x86
{2CF5F1AA-BF69-498C-8B74-04137B2F67F3}.Release|x86.Deploy.0 = Release|x86
{D4357184-82D4-484F-A683-C45F10CC0CD3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D4357184-82D4-484F-A683-C45F10CC0CD3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D4357184-82D4-484F-A683-C45F10CC0CD3}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{D4357184-82D4-484F-A683-C45F10CC0CD3}.Debug|ARM.ActiveCfg = Debug|ARM
{D4357184-82D4-484F-A683-C45F10CC0CD3}.Debug|ARM.Build.0 = Debug|ARM
@ -88,6 +89,7 @@ Global
{2BF804D4-DAA2-42BE-9F21-0E94F021EF53}.Release|x86.ActiveCfg = Release|Win32
{2BF804D4-DAA2-42BE-9F21-0E94F021EF53}.Release|x86.Build.0 = Release|Win32
{D20A1934-366E-4055-AFFB-3F592FC5744A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D20A1934-366E-4055-AFFB-3F592FC5744A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D20A1934-366E-4055-AFFB-3F592FC5744A}.Debug|ARM.ActiveCfg = Debug|Any CPU
{D20A1934-366E-4055-AFFB-3F592FC5744A}.Debug|x64.ActiveCfg = Debug|x64
{D20A1934-366E-4055-AFFB-3F592FC5744A}.Debug|x64.Build.0 = Debug|x64
@ -101,7 +103,6 @@ Global
{D20A1934-366E-4055-AFFB-3F592FC5744A}.Release|x86.ActiveCfg = Release|x86
{D20A1934-366E-4055-AFFB-3F592FC5744A}.Release|x86.Build.0 = Release|x86
{12103A5B-677A-4286-83D2-54EAB9010C16}.Debug|Any CPU.ActiveCfg = Debug|Win32
{12103A5B-677A-4286-83D2-54EAB9010C16}.Debug|Any CPU.Build.0 = Debug|Win32
{12103A5B-677A-4286-83D2-54EAB9010C16}.Debug|ARM.ActiveCfg = Debug|Win32
{12103A5B-677A-4286-83D2-54EAB9010C16}.Debug|x64.ActiveCfg = Debug|x64
{12103A5B-677A-4286-83D2-54EAB9010C16}.Debug|x64.Build.0 = Debug|x64
@ -114,6 +115,7 @@ Global
{12103A5B-677A-4286-83D2-54EAB9010C16}.Release|x86.ActiveCfg = Release|Win32
{12103A5B-677A-4286-83D2-54EAB9010C16}.Release|x86.Build.0 = Release|Win32
{C475A99E-5976-4C5C-941F-1CE3E71F035C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C475A99E-5976-4C5C-941F-1CE3E71F035C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C475A99E-5976-4C5C-941F-1CE3E71F035C}.Debug|ARM.ActiveCfg = Debug|Any CPU
{C475A99E-5976-4C5C-941F-1CE3E71F035C}.Debug|ARM.Build.0 = Debug|Any CPU
{C475A99E-5976-4C5C-941F-1CE3E71F035C}.Debug|x64.ActiveCfg = Debug|Any CPU

Просмотреть файл

@ -11,7 +11,7 @@
<AssemblyName>SqueezeNetObjectDetectionCS</AssemblyName>
<DefaultLanguage>en-US</DefaultLanguage>
<TargetPlatformIdentifier>UAP</TargetPlatformIdentifier>
<TargetPlatformVersion Condition=" '$(TargetPlatformVersion)' == '' ">10.0.18362.0</TargetPlatformVersion>
<TargetPlatformVersion Condition=" '$(TargetPlatformVersion)' == '' ">10.0.17763.0</TargetPlatformVersion>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<MinimumVisualStudioVersion>14</MinimumVisualStudioVersion>
<FileAlignment>512</FileAlignment>
@ -137,13 +137,10 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
<Version>6.2.12</Version>
</PackageReference>
<PackageReference Include="Microsoft.Windows.CppWinRT">
<Version>2.0.210505.3</Version>
<Version>6.0.1</Version>
</PackageReference>
<PackageReference Include="Newtonsoft.Json">
<Version>13.0.1</Version>
<Version>11.0.1</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>