I am working on wrapping tensorflow model for deployment on device and thus use tensorflow c++ api due to its better encryption. When trying to make a dynamic shared library (.so file) by Boost Python for latter usage in other python script, I met this error during importing the library in python:
2018-05-30 16:19:48.421138: F tensorflow/core/common_runtime/device_factory.cc:77] Duplicate registration of device factory for type GPU with the same priority 210.
I found this error would be repeated even just declaring a pointer in the wrapped class. BTW, making shared library without boost python works fine! Any idea about this error or tensorflow device usage mechanism will be greatly appreciated!
test.h file
#ifndef OPENCLASS
#define OPENCLASS
#include <iostream>
#include <opencv2/core/mat.hpp>
#include <boost/python.hpp>
#include "tensorflow/core/public/session.h"
using namespace cv;
using namespace std;
using namespace boost::python;
class OpencvClass {
cv::Mat frame;
std::unique_ptr<tensorflow::Session> session;
public:
OpencvClass();
int readImage(string filename);
void displayImage();
};
BOOST_PYTHON_MODULE(test)
{
class_<OpencvClass, boost::noncopyable>("OpencvClass")
.def("readImage", &OpencvClass::readImage)
.def("displayImage", &OpencvClass::displayImage);
}
#endif
test.cpp file
#include <iostream>
#include <utility>
#include <fstream>
#include <opencv2/core/mat.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <cv.hpp>
#include "test.h"
OpencvClass::OpencvClass(){
}
int OpencvClass::readImage(string filename) {
this->frame = imread(filename);
if (! this->frame.data){
cout << "Cannot read or open image" << endl;
return -1;
} else{
return 0;
}
}
void OpencvClass::displayImage() {
namedWindow("test", WINDOW_AUTOSIZE);
imshow("test", this->frame);
waitKey(0);
}
CmakeList.txt for compile
cmake_minimum_required(VERSION 3.7)
# project(libdetect)
set(CMAKE_CXX_STANDARD 11)
ADD_COMPILE_OPTIONS(-fPIC)
set(SOURCE_FILES test.cpp)
add_library(test SHARED ${SOURCE_FILES})
SET_TARGET_PROPERTIES(test_cv PROPERTIES PREFIX "")
# OpenCV libs
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(test_cv ${OpenCV_LIBS})
# boost lib
include_directories("/usr/include")
target_link_libraries(test_cv "/usr/lib/x86_64-linux-gnu/libboost_python-py35.so")
# python lib
include_directories("/usr/include/python3.5")
target_link_libraries(test_cv "/usr/lib/python3.5/config-3.5m-x86_64-linux-gnu/libpython3.5.so")
# Eigen lib headers
include_directories("/usr/local/include/eigen3")
include_directories("/usr/local/include/google")
# TensorFlow headers
include_directories("/usr/local/include/tf/")
include_directories("/usr/local/include/tf/bazel-genfiles/")
include_directories("/usr/local/include/tf/tensorflow/")
include_directories("/usr/local/include/tf/third-party/")
# Link TensorFlow libs
target_link_libraries(test_cv "/usr/local/lib/libtensorflow_cc.so")
target_link_libraries(test_cv "/usr/local/lib/libtensorflow_framework.so")
python test file
import test
image = "test.jpeg"
myclass = test.OpencvClass()
myclass.readImage(image)
myclass.displayImage()
ERROR LOG:
2018-05-30 16:19:48.421138: F tensorflow/core/common_runtime/device_factory.cc:77] Duplicate registration of device factory for type GPU with the same priority 210
Thank you for your post. We noticed you have not filled out the following field in the issue template. Could you update them if they are relevant in your case, or leave them as N/A? Thanks.
What is the top-level directory of the model you are using
Have I written custom code
Exact command to reproduce
Met the same problem and solved. IN MY CASE I put
/usr/local/lib/libtensorflow_cc.so
/usr/local/lib//libtensorflow_framework.so
at the end of target_link_libraries, which has led to collisions. After switching them to the front of libraries linked to my project everything turns out ok.
Make sure cc.so is linked before framework.so or you may come up with "unary variantdecodefn for type_name: tensorflow::tensor already registered"
Closing as per the solution provided above. Please try and add comments if any, we will reopen the issue if it is required.
having same problem .getting error F tensorflow/core/common_runtime/device_factory.cc:77] Duplicate registration of device factory for type CPU with the same priority 60. Cmakefile is given below---
cmake_minimum_required(VERSION 3.7)
project(tf_detector_example)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp utils.cpp utils.h)
add_executable(tf_detector_example ${SOURCE_FILES})
target_link_libraries(tf_detector_example "/usr/local/lib/libtensorflow_cc.so")
target_link_libraries(tf_detector_example "/usr/local/lib/libtensorflow_framework.so")
include_directories("/usr/local/include/eigen3")
include_directories("/usr/local/include/tf/")
include_directories("/usr/local/include/tf/bazel-genfiles/")
include_directories("/usr/local/include/tf/tensorflow/")
include_directories("/usr/local/include/tf/third-party/")
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(tf_detector_example ${OpenCV_LIBS})
Please help ..
I think you can remove the --config=monolithic compile option just if you have one, the problem will disappear.
But I didn't know why surely?
Met the same problem. But my whole project is compiled with Bazel instead of Cmake. Built TF with --config==monolithic. Currently trying a new build with monolithic config removed
My problem results from the version of cuda and NVIDIA driver. I
reinstalled cuda and the whole package and it worked...HOPE it helps
Jingyu_Qian notifications@github.com 於 2019年8月13日 星期二寫道:
Met the same problem. But my whole project is compiled with Bazel instead
of Cmake. Built TF with --config==monolithic. Currently trying a new
build with monolithic config removed—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/tensorflow/models/issues/4405?email_source=notifications&email_token=AEMGIFHATEKAMPDFC4T7CW3QEH343A5CNFSM4FCLNGPKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD4EFMCI#issuecomment-520640009,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AEMGIFGKHORF34PN3UWT7UTQEH343ANCNFSM4FCLNGPA
.
My problem results from the version of cuda and NVIDIA driver. I reinstalled cuda and the whole package and it worked...HOPE it helps Jingyu_Qian notifications@github.com 於 2019年8月13日 星期二寫道:
…
After disabling --config=monolithic I think my build is stable now.
Just want to add to this thread that I have encountered this problem and how it was solved.
First, when linked using
-ltensorflow -ltensorflow_cc -ltensorflow_framework, I saw error message
F tensorflow/core/common_runtime/device_factory.cc:78] Duplicate registration of device factory for type XLA_CPU with the same priority 50.
Then, I fixed it by removing the -ltensorflow, and switched to -ltensorflow_cc -ltensorflow_framework, problem solved.
Most helpful comment
Met the same problem and solved. IN MY CASE I put
/usr/local/lib/libtensorflow_cc.so
/usr/local/lib//libtensorflow_framework.so
at the end of target_link_libraries, which has led to collisions. After switching them to the front of libraries linked to my project everything turns out ok.
Make sure cc.so is linked before framework.so or you may come up with "unary variantdecodefn for type_name: tensorflow::tensor already registered"