Surya Sharma

Machine Learning Applications | Computer Vision | This website may be my recipe book.

USING libtensorflow dll’s IN a Visual Studio project

This guide is a part of a series of articles that show you how to save a Keras model and use it for predictions in a Visual Studio C program.

The guide will walk you through using Tensorflow’s C API (libtensorflow) in Visual Studio. You will get the C binaries (dlls), then create a .lib file. We’ll setup a new Visual Studio Project to use these dlls, and then call the TF_Version() function to make sure everything is running fine.

The caveat of the method here is that you’re limited to the CUDA version the binaries were compiled with. The version of the libtensorflow files might change over time, so I will attempt to specify what version we’re using here.

I’m working with VS 2017 and libtensorflow 1.13.1

Lets get libtensorflow

Head to the libtensorflow page and get the zip file for Windows. Here are the two options as of 6/6/2019:

Windows
Windows CPU onlyhttps://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-windows-x86_64-1.13.1.zip
Windows GPU onlyhttps://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-gpu-windows-x86_64-1.13.1.zip

I prefer the GPU version, and that’s the one I will get. I’m working with version 1.13.1-GPU.

Extract and setup environment variables

Extract the libtensorflow files to a folder you like. I prefer short and simple folder paths, so I put my files in D:\libtensorflow\

I prefer setting up environment variables so that any project can find these files. This is optional, but if you’re following this guide, I recommend you stick to these steps.

Find the Environment Variables settings on Windows (type environment in the start menu):

Click the Environment Variables button

Add a new entry called TF_DIR to the user variables. We will use TF_DIR later in the Visual Studio Project.

Hit okay and save these new settings.

Generate a .lib file

While the tensorflow files come with a .dll, they’re missing a .lib, which must be generated. You can download my .lib here. We can do this using visual studio tools.

There are a few guides on how to do this.

Adrian Henke’s Blog, VLC guide, Stack Overflow

I prefer a variant of the VLC guide which should let us do this in a few quick commands.

First find VS tools under: Start->Programs->Microsoft Visual Studio->Tools

Alternatively, start Visual Studio 2017, and launch the Visual Studio Command Prompt under Tools->Visual Studio Command Prompt

Here run the following commands. The filenames are important. Not using tensorflow.def as the filename can cause issues. Change the path of your libtensorflow folder if needed.

Create your visual studio project

Now that you have the environment and .lib files ready, we can create our visual studio project (or use a project that you want to add libtensorflow to).

We’ll start with a C++ project, and use C code instead. There are other ways to create a C project, but I’ll move ahead with this for simplicity, the steps remain the same.

Set your project to 64 bit.

This is needed for libtensorflow. It runs only on 64 bit projects.

Find the project properties under Project->Properties->Configuration Manager. Set the project to x64 if you already have that option, otherwise click new and create a new solution platform, then set that to x64.

Add the dll to the project.

Some of this is sourced from the openCV guide on building a visual studio project.

Setting the environment variables (above) makes this a little easier.

Under Project->Properties, first go to the C++ groups General entry and under the “Additional Include Directories” add the path to your libtensorflow directory. We created an environment variable, so we can use $(TF_DIR). If you don’t have “C/C++” group, you should add any .c/.cpp file to the project.

You can either click the edit button, or just add $(TF_DIR) directly. To separate entries if you already have them, use the semicolon ;

Add the lib to the project

Next go to the Linker ‣ General and under the “Additional Library Directories” add the libs directory: $(TF_DIR)

Then you need to specify the libraries in which the linker should look into. To do this go to the Linker ‣ Input and under the “Additional Dependencies” entry add the name of all modules which you want to use: tensorflow.lib

Run the program

Add the following simple code to your program:

At this stage, hit F5 to build and run the program. If it builds and runs, you’re good to move on to the next step: Running predictions using tensorflow in C.

If it doesn’t build

You need to check that Visual Studio can find the .lib and the .h files (above), or that your code is correct.

IF IT BUILDS BUT CAN’T FIND DLLs

You’ll have to figure out which DLLs are missing:

tensorflow.dll

Make sure you set up the environment variable correctly, and that tensorflow.dll is in that folder.

CUDART / CUBLAS / CUDNN

This implies that you downloaded the GPU version of tensorflow, and the version of CUDNN or CUDA doesn’t match the version the tensorflow dll was compiled for.

For cudart / cublas you will need to install the correct version of CUDA based on the name of the missing file. For example cudart64_80.dll implies that version 8.0 of CUDA is missing. You can find this in the NVIDIA download archive.

For cudnn, follow similar steps to get the correct version from the CUDNN archive.

Note, both the above steps require sign up (free) at NVIDIA’s website.

Next Post

Previous Post

4 Comments

  1. Nikita June 21, 2019

    Might be a few errors in the manual.
    Your second code fragment in the “Run the program” section is missing.
    Also, for me the “lib” generation process gives a bunch of warnings, like “warning LNK4017: TF_Function statement not supported for the target platform; ignored” and the “tensorflow.lib” file is just 2KB.
    And when trying to build the project, VS2017 still tells me “cannot open input file ‘tensorflow.lib'”

    • Surya June 21, 2019 — Post Author

      Thanks! I fixed the gist.
      If the lib isn’t being generated,
      try following the examples linked to do it manually: https://wiki.videolan.org/GenerateLibFromDll/

      I’ve added a .lib download if you’d like to use the one I generated: iamsurya.com/tensorflow.lib

    • Nikita June 21, 2019

      Nevermind, the “lib” creation works. Just need to make sure that the “EXPORTS” line is at the top of the “tensorflow.def” file.

      • Surya June 21, 2019 — Post Author

        Ah I have a bug on line 1. Its writing EXPORTS to libvlc.def and not libtensorflow.def.

        Thanks!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2024 Surya Sharma

Theme by Anders Norén