Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined reference to tensorflow::FreezeSavedModel #249

Closed
busFred opened this issue Dec 22, 2020 · 4 comments · Fixed by #246
Closed

Undefined reference to tensorflow::FreezeSavedModel #249

busFred opened this issue Dec 22, 2020 · 4 comments · Fixed by #246

Comments

@busFred
Copy link

busFred commented Dec 22, 2020

The problem I am encountering when I try to compile a method with usign tensorflow::FreezeSavedModel(). It keeps complaining about not being able to find tensorflow::FreezeSavedModel(). However, I did have it included in my header file.
I tried to google search the issue, and someone encountered similar issue said it was the protobuf. But I explicitly tell CMake to link to a specific library file.

Error Message I am getting.

[main] Building folder: tensorflow_cpp_tutorial 
[build] Starting build
[proc] Executing command: /usr/bin/cmake --build /home/fred/Documents/research/tensorflow_cpp_tutorial/build --config Debug --target all -- -j 10
[build] [1/1   0% :: 0.000] Re-running CMake...
[build] -- Configuring done
[build] -- Generating done
[build] -- Build files have been written to: /home/fred/Documents/research/tensorflow_cpp_tutorial/build
[build] [1/1 100% :: 7.097] Linking CXX executable ../bin/tutorial_02/tutorial_02
[build] FAILED: ../bin/tutorial_02/tutorial_02 
[build] : && /bin/g++-10  -g   src/tutorial_02/CMakeFiles/tutorial_02.dir/tutorial_02.cpp.o  -o ../bin/tutorial_02/tutorial_02  -Wl,-rpath,/usr/local/lib  src/tutorial_02/CatDogCnn/libCatDogCnn.a  /usr/lib/x86_64-linux-gnu/libprotobuf.so  /usr/local/lib/libtensorflow_cc.so.2  -lpthread  /usr/local/cuda/lib64/libcudart_static.a  -lpthread  -ldl  /usr/lib/x86_64-linux-gnu/librt.so && :
[build] /usr/bin/ld: src/tutorial_02/CatDogCnn/libCatDogCnn.a(CatDogCnn.cpp.o): in function `tensorflow_tutorial::CatDogCnn::FreezeModel(std::filesystem::__cxx11::path const&)':
[build] /home/fred/Documents/research/tensorflow_cpp_tutorial/build/../src/tutorial_02/CatDogCnn/CatDogCnn.cpp:570: undefined reference to `tensorflow::FreezeSavedModel(tensorflow::SavedModelBundle const&, tensorflow::GraphDef*, std::unordered_set<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >*, std::unordered_set<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >*)'
[build] collect2: error: ld returned 1 exit status
[build] ninja: build stopped: subcommand failed.
[build] Build finished with exit code 1

Configure message I get from CMakeLists.txt

[main] Configuring folder: tensorflow_cpp_tutorial 
[driver] Removing /home/fred/Documents/research/tensorflow_cpp_tutorial/build/CMakeCache.txt
[driver] Removing /home/fred/Documents/research/tensorflow_cpp_tutorial/build/CMakeFiles
[proc] Executing command: /usr/bin/cmake --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_C_COMPILER:FILEPATH=/bin/gcc-10 -DCMAKE_CXX_COMPILER:FILEPATH=/bin/g++-10 -H/home/fred/Documents/research/tensorflow_cpp_tutorial -B/home/fred/Documents/research/tensorflow_cpp_tutorial/build -G Ninja
[cmake] Not searching for unused variables given on the command line.
[cmake] -- The C compiler identification is GNU 10.2.0
[cmake] -- The CXX compiler identification is GNU 10.2.0
[cmake] -- Check for working C compiler: /bin/gcc-10
[cmake] -- Check for working C compiler: /bin/gcc-10 -- works
[cmake] -- Detecting C compiler ABI info
[cmake] -- Detecting C compiler ABI info - done
[cmake] -- Detecting C compile features
[cmake] -- Detecting C compile features - done
[cmake] -- Check for working CXX compiler: /bin/g++-10
[cmake] -- Check for working CXX compiler: /bin/g++-10 -- works
[cmake] -- Detecting CXX compiler ABI info
[cmake] -- Detecting CXX compiler ABI info - done
[cmake] -- Detecting CXX compile features
[cmake] -- Detecting CXX compile features - done
[cmake] -- Looking for pthread.h
[cmake] -- Looking for pthread.h - found
[cmake] -- Performing Test CMAKE_HAVE_LIBC_PTHREAD
[cmake] -- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
[cmake] -- Looking for pthread_create in pthreads
[cmake] -- Looking for pthread_create in pthreads - not found
[cmake] -- Looking for pthread_create in pthread
[cmake] -- Looking for pthread_create in pthread - found
[cmake] -- Found Threads: TRUE  
[cmake] -- Found CUDA: /usr/local/cuda (found version "11.2") 
[cmake] -- Found Protobuf: /usr/local/lib/libprotobuf.so;-lpthread (found version "3.14.0") 
[cmake] -- Configuring done
[cmake] -- Generating done
[cmake] -- Build files have been written to: /home/fred/Documents/research/tensorflow_cpp_tutorial/build

The header I have included in CatDogCnn.hpp

#include <tensorflow/cc/client/client_session.h>
#include <tensorflow/cc/framework/gradients.h>
#include <tensorflow/cc/ops/image_ops.h>
#include <tensorflow/cc/ops/standard_ops.h>
#include <tensorflow/cc/ops/state_ops.h>
#include <tensorflow/cc/tools/freeze_saved_model.h>
#include <tensorflow/core/framework/tensor.h>
#include <tensorflow/core/lib/io/path.h>
#include <tensorflow/core/platform/types.h>
#include <tensorflow/core/public/session.h>
#include <tensorflow/core/summary/summary_file_writer.h>

the method that is causing the problem in the CatDogCnn.cpp file

tensorflow::Status CatDogCnn::FreezeModel(const std::filesystem::path& path) {
  std::vector<tensorflow::Tensor> output_tensors{};
  TF_CHECK_OK(session_ptr_->Run(weights_biases_, &output_tensors));
  std::unordered_map<std::string, tensorflow::Tensor> name_variable_map;
  for (size_t i = 0; i < weights_biases_.size(); ++i) {
    name_variable_map[weights_biases_[i].node()->name()] = output_tensors[i];
  }
  tensorflow::GraphDef graph_def{};
  TF_CHECK_OK(network_root_scope_.ToGraphDef(&graph_def));
  tensorflow::SavedModelBundle saved_model_bundle{};
  tensorflow::SignatureDef signature_def{};
  (*(signature_def.mutable_inputs()))[input_batch_tensor_node_.name()].set_name(
      input_batch_tensor_node_.name());
  (*(signature_def.mutable_inputs()))[output_classification_.name()].set_name(
      output_classification_.name());
  tensorflow::MetaGraphDef* meta_graph_def =
      &(saved_model_bundle.meta_graph_def);
  (*(meta_graph_def->mutable_signature_def()))["signature_def"] = signature_def;
  *(meta_graph_def->mutable_graph_def()) = graph_def;
  tensorflow::SessionOptions options{};
  saved_model_bundle.session.reset(tensorflow::NewSession(options));
  tensorflow::GraphDef frozen_graph_def{};
  std::unordered_set<std::string> inputs;
  std::unordered_set<std::string> outputs;
  tensorflow::FreezeSavedModel(saved_model_bundle, &frozen_graph_def, &inputs,
                               &outputs);
}

the root CMakeLists.txt

# CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

project(tensorflow_cc_example)

find_package(TensorflowCC REQUIRED)
find_package(CUDA)

include_directories(${CMAKE_SOURCE_DIR}/include)

add_subdirectory(src)

CMakeLists.txt for CatDogCnn. I linked to a specify the location of libprotobuf.so becasue ${PROTOBUF_LIBRARIES} keeps pointing to version 2.5 which doesn't have google/protobuf/arena.h

add_library(CatDogCnn STATIC CatDogCnn.cpp)

find_package(absl REQUIRED)
find_package(Protobuf REQUIRED)
target_include_directories(
  CatDogCnn INTERFACE
  "${PROTOBUF_INCLUDE_DIR}"
)

target_link_libraries(CatDogCnn 
  /usr/lib/x86_64-linux-gnu/libprotobuf.so
  TensorflowCC::TensorflowCC
)

if(CUDA_FOUND)
  target_link_libraries(CatDogCnn ${CUDA_LIBRARIES})
endif(CUDA_FOUND)
@FloopCZ
Copy link
Owner

FloopCZ commented Mar 1, 2021

This may be because your protobuf header files have a different version than you protobuf library. I have no idea if your /usr/lib/x86_64-linux-gnu/libprotobuf.so and "${PROTOBUF_INCLUDE_DIR}" are compatible. Can you use the same version of protobuf 3.* everywhere?

@FloopCZ
Copy link
Owner

FloopCZ commented Jun 13, 2021

Closing for inactivity, feel free to reopen if the issue persists.

@FloopCZ FloopCZ closed this as completed Jun 13, 2021
@GosuPaper
Copy link

Hi,

Sorry to reopen that but anyone has a fix for this please ?

@FloopCZ FloopCZ linked a pull request May 21, 2022 that will close this issue
@FloopCZ
Copy link
Owner

FloopCZ commented May 25, 2022

Hi, you can try the -DINSTALL_PROTOBUF=ON option from #246 (or use one of the dockerfiles).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants