failed compiling caffe because fatal error opencv2/core/core.hpp: no such file or directory

1,668 views
Skip to first unread message

Stevanus Calvin

unread,
Nov 2, 2020, 6:49:45 AM11/2/20
to Caffe Users

Hi guys
I was trying to compile caffe based on https://github.com/adeelz92/Install-Caffe-on-Ubuntu-16.04-Python-3 . but i got this meseage:

PROTOC src/caffe/proto/caffe.proto
CXX .build_release/src/caffe/proto/caffe.pb.cc
CXX src/caffe/layer.cpp
CXX src/caffe/blob.cpp
CXX src/caffe/syncedmem.cpp
CXX src/caffe/solver.cpp
CXX src/caffe/data_transformer.cpp
src/caffe/data_transformer.cpp:1:10: fatal error: opencv2/core/core.hpp: No such file or directory
    1 | #include <opencv2/core/core.hpp>
      |          ^~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make: *** [Makefile:525: .build_release/src/caffe/data_transformer.o] Error 1

could anyone help me how to solve this problem? it says fatal error opencv2 while i use opencv 4.4.0 and i already uncomment it on makefile.config. I also have installed all the dependencies but i keep having this error
pleas help me

Tamas Nemes

unread,
Nov 2, 2020, 11:18:04 AM11/2/20
to Caffe Users
Hi Calvin,
As I see, you are rather new to either OpenCV or handling of C++ itself which is completely fine. If you have no or little experience, Caffe installation is one of the most difficult things to start with.
Since OpenCV 2, all include files (.h or .hpp) are in a folder named "opencv2" even if the version is 3 or 4. It became sort of a standard. As such, nothing is wrong, but the solution to your problem depends of the way you installed OpenCV. Here, it's always good to supply further info of your system, e.g. OS distro/version, Python version and whether you compiled OpenCV from source or installed it as a pre-compiled package.
First, you have to include all paths with your needed include-files to the INCLUDE_DIRS variable in Makefile.config. You should append /usr/local/include/opencv4 (check which path it is on your computer) to the variable in all cases. Also, uncomment OPENCV_VERSION := 4 and, if you compiled from source, USE_PKG_CONFIG := 1. You can search your computer for a file named "opencv4.pc" and, if it exists, create a symlink ( sudo ln -s opencv4.pc opencv.pc ) and append the following to your .bashrc-file:

export PKG_CONFIG_PATH="/path/to/your/file" # (usually "/home/[YOUR_USERNAME]/opencv/build/unix-install")

Then do source .bashrc and you should be good to go. If you need further detailed guides to follow, you can check out these:
https://github.com/Totemi1324/caffe-installation-guide (My guide tested on Ubuntu 20.04 and OpenCV 4.4.0)

Best of luck!

Stevanus Calvin

unread,
Nov 7, 2020, 12:48:02 AM11/7/20
to Caffe Users
Thank you for your reminder and suggestion. i succeeded installing opencv. now i met new problem ( idk why but i met a never ending problem :'( ) . This is what i got after running make all --j8
src/caffe/layers/cudnn_conv_layer.cpp: In member function ‘virtual void caffe::CuDNNConvolutionLayer<Dtype>::Reshape(const std::vector<caffe::Blob<Dtype>*>&, const std::vector<caffe::Blob<Dtype>*>&)’:
src/caffe/layers/cudnn_conv_layer.cpp:300:1: error: a template declaration cannot appear at block scope
 template <typename Dtype>
 ^~~~~~~~
In file included from ./include/caffe/blob.hpp:8,
                 from ./include/caffe/layers/cudnn_conv_layer.hpp:6,
                 from src/caffe/layers/cudnn_conv_layer.cpp:5:
src/caffe/layers/cudnn_conv_layer.cpp:331:1: error: expected primary-expression before ‘template’
 INSTANTIATE_CLASS(CuDNNConvolutionLayer);
 ^~~~~~~~~~~~~~~~~
src/caffe/layers/cudnn_conv_layer.cpp:331:1: error: expected primary-expression before ‘template’
 INSTANTIATE_CLASS(CuDNNConvolutionLayer);
 ^~~~~~~~~~~~~~~~~
src/caffe/layers/cudnn_conv_layer.cpp: At global scope:
src/caffe/layers/cudnn_conv_layer.cpp:333:1: error: expected ‘}’ at end of input
 }   // namespace caffe
 ^
src/caffe/layers/cudnn_conv_layer.cpp:7:17: note: to match this ‘{’
 namespace caffe {
                 ^
make: *** [Makefile:586: .build_release/src/caffe/layers/cudnn_conv_layer.o] Error 1
make: *** Waiting for unfinished jobs....
does anyone have an idea about this?

Tamas Nemes

unread,
Nov 7, 2020, 6:12:52 AM11/7/20
to Caffe Users
Running into a lot of error messages is normal. This error is not your fault, it's an error provided with the package itself because the developers who transformed Caffe for OpenCV 4 and cuDNN 8 made a mistake. You have to understand how C++ code works to solve this.
Just open the file /src/caffe/layers/cudnn_conv_layer.cpp with a text editor, go to line 194 and make a new line where you put an additional curly bracket '}'. This is what it should look like:

194 |    if(!found_conv_algorithm) LOG(ERROR) << "cuDNN did not return a suitable algorithm for convolution.";
195 |  }
196 |#else
197 |    // choose forward and backward algorithms + workspace(s)
    ...

Then do make clean and try again. You should be good to go.

Reply all
Reply to author
Forward
0 new messages