From dd374963f859ea353b244988f215ec7a5290962c Mon Sep 17 00:00:00 2001 From: Dmitri Smirnov Date: Thu, 24 Oct 2019 10:06:47 -0700 Subject: [PATCH] Update build instructions. Make samples build and run. (#2244) Making samples build. Update build instructions. --- BUILD.md | 1 + samples/c_cxx/CMakeLists.txt | 22 +++++---- samples/c_cxx/MNIST/MNIST.cpp | 7 +-- samples/c_cxx/README.md | 46 +++++++++++++++---- .../fns_candy_style_transfer.c | 12 +++-- samples/c_cxx/imagenet/image_loader_wic.cc | 2 +- 6 files changed, 63 insertions(+), 27 deletions(-) diff --git a/BUILD.md b/BUILD.md index 99a3308430..86be1005fc 100644 --- a/BUILD.md +++ b/BUILD.md @@ -16,6 +16,7 @@ ### Build Instructions #### Windows +Open Developer Command Prompt for Visual Studio version you are going to use. This will properly setup the environment including paths to your compiler, linker, utilities and header files. ``` .\build.bat --config RelWithDebInfo --build_shared_lib --parallel ``` diff --git a/samples/c_cxx/CMakeLists.txt b/samples/c_cxx/CMakeLists.txt index a8d427e37a..1914b2444a 100644 --- a/samples/c_cxx/CMakeLists.txt +++ b/samples/c_cxx/CMakeLists.txt @@ -22,6 +22,20 @@ option(onnxruntime_USE_NUPHAR "Build with Nuphar" OFF) option(onnxruntime_USE_BRAINSLICE "Build with BrainSlice" OFF) option(onnxruntime_USE_TENSORRT "Build with TensorRT support" OFF) option(LIBPNG_ROOTDIR "libpng root dir") +option(ONNXRUNTIME_ROOTDIR "onnxruntime root dir") + +if(NOT ONNXRUNTIME_ROOTDIR) + if(WIN32) + set(ONNXRUNTIME_ROOTDIR "C:/Program Files (x86)/onnxruntime") + else() + include_directories("/usr/local/include/onnxruntime") + endif() +endif() + +if(WIN32) + include_directories("${ONNXRUNTIME_ROOTDIR}/include" "${ONNXRUNTIME_ROOTDIR}/include/onnxruntime/core/session") + link_directories("${ONNXRUNTIME_ROOTDIR}/lib") +endif() #if JPEG lib is available, we'll use it for image decoding, otherwise we'll use WIC find_package(JPEG) @@ -70,14 +84,6 @@ if(onnxruntime_USE_DML) add_definitions(-DUSE_DML) endif() -#TODO: remove this part(need to talk to Ryan and find a solution for this) -if(UNIX) - include_directories("/usr/local/include/onnxruntime") -else() - include_directories("C:\\Program Files (x86)\\onnxruntime\\include" "C:\\Program Files (x86)\\onnxruntime\\include\\onnxruntime" "C:\\Program Files (x86)\\onnxruntime\\include\\onnxruntime\\core\\session") - link_directories("C:\\Program Files (x86)\\onnxruntime\\lib") -endif() - add_subdirectory(imagenet) if(PNG_FOUND) diff --git a/samples/c_cxx/MNIST/MNIST.cpp b/samples/c_cxx/MNIST/MNIST.cpp index 5db289ad89..49f6440f4e 100644 --- a/samples/c_cxx/MNIST/MNIST.cpp +++ b/samples/c_cxx/MNIST/MNIST.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #pragma comment(lib, "user32.lib") #pragma comment(lib, "gdi32.lib") @@ -161,10 +162,10 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) auto greatest = mnist_.results_[mnist_.result_]; auto range = greatest - least; - auto graphs_zero = graphs_left - least * graph_width / range; + int graphs_zero = static_cast(graphs_left - least * graph_width / range); // Hilight the winner - RECT rc{graphs_left, mnist_.result_ * 16, graphs_left + graph_width + 128, (mnist_.result_ + 1) * 16}; + RECT rc{graphs_left, static_cast(mnist_.result_) * 16, graphs_left + graph_width + 128, static_cast(mnist_.result_ + 1) * 16}; FillRect(hdc, &rc, brush_winner_); // For every entry, draw the odds and the graph for it @@ -177,7 +178,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) auto length = wsprintf(value, L"%2d: %d.%02d", i, int(result), abs(int(result * 100) % 100)); TextOut(hdc, graphs_left + graph_width + 5, y, value, length); - Rectangle(hdc, graphs_zero, y + 1, graphs_zero + result * graph_width / range, y + 14); + Rectangle(hdc, graphs_zero, y + 1, static_cast(graphs_zero + result * graph_width / range), y + 14); } // Draw the zero line diff --git a/samples/c_cxx/README.md b/samples/c_cxx/README.md index 3e15190ec3..d1a68e0b28 100644 --- a/samples/c_cxx/README.md +++ b/samples/c_cxx/README.md @@ -1,9 +1,11 @@ -This directory contains a few C/C++ sample applications for demoing onnxruntime usage: +This directory contains a few (Windows only) C/C++ sample applications for demoing onnxruntime usage: 1. fns_candy_style_transfer: A C application that uses the FNS-Candy style transfer model to re-style images. 2. MNIST: A windows GUI application for doing handwriting recognition 3. imagenet: An end-to-end sample for the [ImageNet Large Scale Visual Recognition Challenge 2012](http://www.image-net.org/challenges/LSVRC/2012/) +Imagenet sample requires ATL libraries installed as a part of VS Studio installation. + # How to build ## Prerequisites @@ -16,23 +18,47 @@ You may get a precompiled libpng library from [https://onnxruntimetestdata.blob. ## Install ONNX Runtime You may either get a prebuit onnxruntime from nuget.org, or build it from source by following the [build instructions](../../BUILD.md). If you build it by yourself, you must append the "--build_shared_lib" flag to your build command. +Open Developer Command Prompt for Visual Studio version you are going to use. This will setup necessary environment for the compiler and other things to be found. ``` -build.bat --config RelWithDebInfo --build_shared_lib --parallel +build.bat --config RelWithDebInfo --build_shared_lib --parallel ``` -When the build is done, run Visual Studio as administrator and open the onnxruntime.sln file in your build directory. -![vs.png](vs.png) -When the solution is loaded, change the build configuration to "RelWithDebInfo"(which must match your previous build command), then select the "INSTALL" project, and build it. It will install your onnxruntime to "C:\Program Files (x86)\onnxruntime" +By default this will build a project with "C:\Program Files (x86)\onnxruntime" install destination. This is a protected folder on Windows. If you do not want to run installation with elevated priviliges you will need to override the default installation location by passing extra CMake arguments. For example: + +``` +build.bat --config RelWithDebInfo --build_shared_lib --parallel --cmake_extra_defines CMAKE_INSTALL_PREFIX=c:\dev\ort_install +``` + +By default products of the build on Windows go to .\build\Windows\ folder. In the case above it would be .\build\Windows\RelWithDebInfo. +If you did not specify alternative installation location above you would need to open an elevated command prompt to install onnxruntime. +Run the following commands. + +``` +cd .\Windows\RelWithDebInfo +msbuild INSTALL.vcxproj /p:Configuration=RelWithDebInfo +``` ## Build the samples -Open cmd.exe, change your current directory to samples\c_cxx, then run + +Open Developer Command Prompt for Visual Studio version you are going to use, change your current directory to samples\c_cxx, then run ```bat -mkdir build -cmake .. -A x64 -T host=x64 -DLIBPNG_ROOTDIR=C:\path\to\your\libpng\binary +mkdir build && cd build +cmake .. -A x64 -T host=x64 -DLIBPNG_ROOTDIR=C:\path\to\your\libpng\binary -DONNXRUNTIME_ROOTDIR=c:\dev\ort_install ``` -You may omit the "-DLIBPNG_ROOTDIR=..." argument if you don't have the libpng library. +You may omit the "-DLIBPNG_ROOTDIR=..." argument if you don't have the libpng library. +You may omit "-DONNXRUNTIME_ROOTDIR=..." if you installed to a default location. + You may append "-Donnxruntime_USE_CUDA=ON" or "-Donnxruntime_USE_DML=ON" to the last command args if your onnxruntime binary was built with CUDA or DirectML support respectively. -Then you can open the onnxruntime_samples.sln file in the "build" directory and build the solution. +You can then either open the solution in a Visual Studio and build it from there +```bat +devenv onnxruntime_samples.sln +``` +Or build it using msbuild +```bat +msbuild onnxruntime_samples.sln /p:Configuration=Debug|Release +``` + +To run the samples make sure that your Install Folder Bin is in the path so your sample executable can find onnxruntime dll and libpng if you used it. diff --git a/samples/c_cxx/fns_candy_style_transfer/fns_candy_style_transfer.c b/samples/c_cxx/fns_candy_style_transfer/fns_candy_style_transfer.c index d8476e4b6c..acee710bab 100644 --- a/samples/c_cxx/fns_candy_style_transfer/fns_candy_style_transfer.c +++ b/samples/c_cxx/fns_candy_style_transfer/fns_candy_style_transfer.c @@ -1,6 +1,6 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -#include "onnxruntime/core/session/onnxruntime_c_api.h" +#include "onnxruntime_c_api.h" #include "providers.h" #include #include @@ -15,7 +15,7 @@ #define tcscmp strcmp #endif -const OrtApi* g_ort = OrtGetApiBase()->GetApi(ORT_API_VERSION); +const OrtApi* g_ort = NULL; #define ORT_ABORT_ON_ERROR(expr) \ do { \ @@ -125,8 +125,8 @@ static int write_tensor_to_png_file(OrtValue* tensor, const char* output_file) { memset(&image, 0, (sizeof image)); image.version = PNG_IMAGE_VERSION; image.format = PNG_FORMAT_BGR; - image.height = dims[2]; - image.width = dims[3]; + image.height = (png_uint_32)dims[2]; + image.width = (png_uint_32)dims[3]; chw_to_hwc(f, image.height, image.width, &model_output_bytes); int ret = 0; if (png_image_write_to_file(&image, output_file, 0 /*convert_to_8bit*/, model_output_bytes, 0 /*row_stride*/, @@ -240,6 +240,8 @@ int main(int argc, char* argv[]) { usage(); return -1; } + + g_ort = OrtGetApiBase()->GetApi(ORT_API_VERSION); #ifdef _WIN32 //CoInitializeEx is only needed if Windows Image Component will be used in this program for image loading/saving. HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); @@ -248,7 +250,7 @@ int main(int argc, char* argv[]) { ORTCHAR_T* model_path = argv[1]; ORTCHAR_T* input_file = argv[2]; ORTCHAR_T* output_file = argv[3]; - ORTCHAR_T* execution_provider = argc >= 5 ? argv[4] : nullptr; + ORTCHAR_T* execution_provider = (argc >= 5) ? argv[4] : NULL; OrtEnv* env; ORT_ABORT_ON_ERROR(g_ort->CreateEnv(ORT_LOGGING_LEVEL_WARNING, "test", &env)); OrtSessionOptions* session_options; diff --git a/samples/c_cxx/imagenet/image_loader_wic.cc b/samples/c_cxx/imagenet/image_loader_wic.cc index 27afb3c83d..143b244627 100644 --- a/samples/c_cxx/imagenet/image_loader_wic.cc +++ b/samples/c_cxx/imagenet/image_loader_wic.cc @@ -92,7 +92,7 @@ OrtStatus* LoadImageFromFileAndCrop(void* loader, const ORTCHAR_T* filename, dou *out_width = bbox_w_size; *out_height = bbox_h_size; return nullptr; - } catch (std::exception& ex) { + } catch (const std::exception& ex) { std::ostringstream oss; oss << "Load " << filename << " failed:" << ex.what(); return Ort::GetApi().CreateStatus(ORT_FAIL, oss.str().c_str());