Intel® Distribution of OpenVINO™ Toolkit
Community assistance about the Intel® Distribution of OpenVINO™ toolkit, OpenCV, and all aspects of computer vision-related on Intel® platforms.

OpenCV-OpenVINO vs OpenCV

Rosten__Erik
Beginner
2,582 Views

Hi Everyone,

 

I'm using the cv2.dnn module to read in a pytorch model and perform inference, but also am using some pretrained openVINO optimized models as well. I've found that when using the pytorch model and performing inference on the same input, the output differs before and after running the openVINO setupvars.sh. The reason seems to be that after setting up the variables, importing cv2 points to the /opt/intel/openvino/python/python3.5 library instead of the one installed using pip. I'm not sure what the difference is, but is there a way to import the original cv2 and openvino cv2 separately in python code? Furthermore, what is the reason for the differences in the outputs?

 

Thanks,

Erik

0 Kudos
8 Replies
Rosten__Erik
Beginner
2,582 Views

Bump

0 Kudos
HemanthKum_G_Intel
2,581 Views

Hi Erik,

When you source the setupvars.sh, w.r.t. python it will -

  • check if the python is installed or not by using the python --version command
  • If it is installed, it will check for the python version and captures the major and minor numbers.
  • At last, it will point the "PYTHONPATH" to %INTEL_OPENVINO_DIR%\python\python%Major%.%Minor%;%PYTHONPATH%

In your case, it is pointing to /opt/intel/openvino/python/python3.5 which seems to be correct.

If you have multiple versions of python or want to maintain sanity by having openvino specific python and system python, python virtual environment can be installed. This is provided as an option while installing model optimizer > install_prerequisites >  install_prerequisites*.sh followed by venv. So when you use venv and source the setupvars.sh it will point to openvino version of python and when you deactivate the venv and run your code directly without sourcing the setupvars.sh in a different terminal, it can point to system installed opencv. However carefully observe the setupvars.sh script and can be modified according to your specific requirements if any.

(I'm posting an excerpt for the Windows setupvars.bat but for Linux it should be similar):

:: OpenCV
if exist "%INTEL_OPENVINO_DIR%\opencv\setupvars.bat" (
call "%INTEL_OPENVINO_DIR%\opencv\setupvars.bat"
) else (
set "OpenCV_DIR=%INTEL_OPENVINO_DIR%\opencv\x64\vc14\lib"
set "PATH=%INTEL_OPENVINO_DIR%\opencv\x64\vc14\bin;%PATH%"
)

What you must do is set OpenCV_DIR to point to your OpenCV installation lib directory.

Ref. https://software.intel.com/en-us/forums/computer-vision/topic/802287

0 Kudos
Rosten__Erik
Beginner
2,581 Views

Thank you for the feedback! While it's been very helpful, I need to be running both versions of opencv in the same "terminal" so to speak. As a last question, what might be the source of the differences between opencv and opencv-openvino when performing inference with the torch model. Is the source code for the cv2.dnn module inherently different?

 

Edit: Since the entire python version is being overwritten by openvino's setup script, is it possible that the source of the differences are not within opencv-openvino vs opencv, but another library altogether?

 

Best,

Erik

0 Kudos
HemanthKum_G_Intel
2,581 Views

Hi Erik, 

There is no source difference between stock opencv and openvino's opencv. OpenVINO does some hardware and software optimizations at the library level. 

0 Kudos
Maksim_S_Intel
Employee
2,581 Views

Probably OpenCV provided with OpenVINO use InferenceEngine backend by default, you should explicitly set backend to DNN_BACKEND_OPENCV (Net::setPreferableBackend) to get same results as OpenCV from PIP. Also results can differ due to version difference.

 

You can't load two different cv2 modules in single Python process.

0 Kudos
fei__wang
Beginner
2,581 Views

Hemanth Kumar G. (Intel) wrote:

Hi Erik, 

opencv版本和opencv-openvino版本之间只是优化了openvino嘛?在编译时cmake参数有什么变化嘛

There is no source difference between stock opencv and openvino's opencv. OpenVINO does some hardware and software optimizations at the library level. 

0 Kudos
Hoško__Filip
Beginner
2,582 Views

Hemanth Kumar G. (Intel) wrote:

Hi Erik, 

There is no source difference between stock opencv and openvino's opencv. OpenVINO does some hardware and software optimizations at the library level. 

That's fine but the problem is when we would like to load modules that are listed as "extra" and are part of the opencv-contrib-python. For example, I would like to use the Tracker API but as far as I know, I can't do it without the contrib package. Is there a way I can use the OpenCV Tracker API with OpenVINO?

0 Kudos
_Mai_
Novice
2,246 Views

There is a way to install the contrib modules by giving it to the make function 

git clone https://github.com/opencv/opencv.git
cd opencv && git checkout 4.1.2openvino
cd .. && git clone https://github.com/opencv/opencv_contrib.git
cd opencv_contrib && git checkout 4.1.2
 
 
cd ../opencv && mkdir build && cd build
cmake -D CMAKE_INSTALL_PREFIX=/opt/intel/openvino/opencv \
-D OPENCV_PYTHON3_INSTALL_PATH=/opt/intel/openvino/python/python3.6 \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules\
-D PYTHON3_EXECUTABLE=/home/demo/anaconda3/envs/openvino/bin/python ..
make all -j32 && make install -j32 
# 32 represents your number of cpu cores (so for 8 cores you'd use: make all -j8 && make install -j8)

 

0 Kudos
Reply