Skip to content

Instantly share code, notes, and snippets.

@iwatake2222
Last active January 17, 2022 10:34
Show Gist options
  • Save iwatake2222/3017d9ac3112b27cc9f5011ece993009 to your computer and use it in GitHub Desktop.
Save iwatake2222/3017d9ac3112b27cc9f5011ece993009 to your computer and use it in GitHub Desktop.
How to generate tensorflowlite.dll for Visual Studio (Windows)
# On Git Bash
cd path-to-tensorflow
git checkout 00cb358ab2e67d0b06a21901ded13c57fd47e673
./tensorflow/lite/tools/make/download_dependencies.sh
nano tensorflow/lite/build_def.bzl
###
# --- a/tensorflow/lite/build_def.bzl
# +++ b/tensorflow/lite/build_def.bzl
# @@ -159,6 +159,7 @@ def tflite_cc_shared_object(
# tf_cc_shared_object(
# name = name,
# copts = copts,
# + features = ["windows_export_all_symbols"],
# linkstatic = linkstatic,
# linkopts = linkopts + tflite_jni_linkopts(),
# framework_so = [],
###
# On Anaconda Powershell prompt (Miniconda3)
conda create -n build_tflite
conda activate build_tflite
conda install python
conda install numpy
cd path-to-tensorflow
python configure.py
bazel build -c opt //tensorflow/lite:tensorflowlite
ls ./bazel-bin/tensorflow/lite/tensorflowlite.dll
- Windows 10
- Visual Studio 2017
- Bazel
- bazel-1.2.1-windows-x86_64.zip
- Add path to bazel.exe
- Python 3.8.1 (on Miniconda)
@iwatake2222
Copy link
Author

iwatake2222 commented Feb 16, 2020

Reference:
tensorflow/tensorflow#33634
(Using windows_export_all_symbols seems workaround at the moment..)

Note:
00cb358ab2e67d0b06a21901ded13c57fd47e673 is just the version I confirmed.
You may not need this or you can specify another version.

@iwatake2222
Copy link
Author

When you use this,
you need to input tensorflowlite.dll.if.lib file to linker, then copy tensorflowlite.dll file to the same folder as executable file.

In cmake, I do like this:

target_link_libraries(${ProjectName} ${CMAKE_SOURCE_DIR}/../third_party/tensorflow_prebuilt/windows_x64/lib/tensorflowlite.dll.if.lib)
file(COPY ${CMAKE_SOURCE_DIR}/../third_party/tensorflow_prebuilt/windows_x64/lib/tensorflowlite.dll DESTINATION ${CMAKE_CURRENT_BINARY_DIR})

@PharrellWANG
Copy link

PharrellWANG commented Apr 7, 2020

When you use this,
you need to input tensorflowlite.dll.if.lib file to linker, then copy tensorflowlite.dll file to the same folder as executable file.

In cmake, I do like this:

target_link_libraries(${ProjectName} ${CMAKE_SOURCE_DIR}/../third_party/tensorflow_prebuilt/windows_x64/lib/tensorflowlite.dll.if.lib)
file(COPY ${CMAKE_SOURCE_DIR}/../third_party/tensorflow_prebuilt/windows_x64/lib/tensorflowlite.dll DESTINATION ${CMAKE_CURRENT_BINARY_DIR})

@iwatake2222 Hi, is it possible for you to share a minimal demo of using tflite cpp inference with visual studio and the generated tensoflowlite.dll? (CMAKE build is deprecated for TensorFlow, hence it is not that obvious regarding to generate MSVC project. )

@iwatake2222
Copy link
Author

You can refer to this repo. https://github.com/iwatake2222/EdgeTPU_CPP
It's not a minimal one, still I think it's simple enough.

Please uncheck ( USE_EDGETPU ) option in cmake-gui when generating MSVC project. Without this option, the project is for pure tensorflow lite, although repo name is "EdgeTPU".

@PharrellWANG
Copy link

You can refer to this repo. https://github.com/iwatake2222/EdgeTPU_CPP
It's not a minimal one, still I think it's simple enough.

Please uncheck ( USE_EDGETPU ) option in cmake-gui when generating MSVC project. Without this option, the project is for pure tensorflow lite, although repo name is "EdgeTPU".

I followed instructions in readme.md of https://github.com/iwatake2222/EdgeTPU_CPP, i can successfully build tensorflowlite.dll, but there is no tensorflowlite.dll.if.lib. Do you know why tensorflowlite.dll.if.lib is missing. Attached is the screen capture showing all files in bazel-bin/tensorflow/lite/ dir.
tmp

@iwatake2222
Copy link
Author

It's weird, the lib file should be generated automatically.
Did you use the same commit id as my sample? If not, the build script might changed.

By the way, you can actually create the lib file from DLL using dumpbin and lib commands in Developer Command Prompt for VS 20xx.

@PharrellWANG
Copy link

It's weird, the lib file should be generated automatically.
Did you use the same commit id as my sample? If not, the build script might changed.

By the way, you can actually create the lib file from DLL using dumpbin and lib commands in Developer Command Prompt for VS 20xx.

  1. The same commit id was used in my trial.
  2. The tip of creating lib from dll is so helpful.

Thanks a lot, iwatake!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment