opencv use cuda memory anyway - c++

I want to know how much my code is faster with CUDA, so I compile OpenCV with below switches (including cuda)
cmake CMAKE_BUILD_TYPE=RELEASE \
CMAKE_INSTALL_PREFIX=/usr/local \
WITH_CUDA=ON \
ENABLE_FAST_MATH=1 \
CUDA_FAST_MATH=1 \
WITH_CUBLAS=1 \
INSTALL_PYTHON_EXAMPLES=OFF \
OPENCV_EXTRA_MODULES_PATH=/home/saeed/opencv_contrib/modules \
BUILD_EXAMPLES=OFF ..
and I'm working on Linux based machine and with dedicated GPU on my system so how I can measure gpu usage for opencv code? I did that with using
$ nvidia-smi
command,which give me general information about gpu usage and etc (in bash) .
for trace this output I use
$ watch -n 0.1 nvidia-smi
command,which updates informations every 0.1 second(0.1 second is minimum)
So I have a code like below in opencv(You can see I don't use any cuda function in it)
#include "opencv2/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
int main()
{
cv::Mat src(1500,1500,CV_8UC3),res;
for (int var = 0; var < 70; ++var)
{
Canny(src,res,50,150,5);
}
}
So when i run above code I take a screen shot in my nvidia-smi page(to know how much gpu ram is in use)
As you can see there is 23 MiB is in use !!! so why this happen !!if it is normal how we could stop that !? How to stop GPU for this problem

You can make the CUDA runtime indicate that there are no available GPUs with the following environment variable:
CUDA_VISIBLE_DEVICES="" ./my_opencv_code_that_wont_use_gpu
If you want OpenCV to actually not do anything with the GPU, my best guess would be to compile it without CUDA support:
cmake CMAKE_BUILD_TYPE=RELEASE \
CMAKE_INSTALL_PREFIX=/usr/local \
WITH_CUDA=ON \ ********MODIFY
ENABLE_FAST_MATH=1 \
CUDA_FAST_MATH=1 \ ********MODIFY
WITH_CUBLAS=1 \ ********MODIFY
INSTALL_PYTHON_EXAMPLES=OFF \
OPENCV_EXTRA_MODULES_PATH=/home/saeed/opencv_contrib/modules \
BUILD_EXAMPLES=OFF ..

Related

Cannot build cc-tools with Boost 1.76 on macOS got 'cannot find the flags to link with Boost regex' error

I'm trying to build cc-tool debugger as described here https://www.zigbee2mqtt.io/information/flashing_the_cc2531.html
But at configure step got this error
checking for the Boost regex library... no
configure: error: cannot find the flags to link with Boost regex
Same issue described here https://github.com/dashesy/cc-tool/issues/25 and suggested solution is to downgrade boost to 1.60. But it does not work anymore because 1.60 deleted from brew.
I've tried to use clang compiler instead of gcc but it does not work too.
MacOS: Big sur 11.5.2 (20G95)
Here the solution:
CC=/usr/bin/clang \
CXX=/usr/bin/clang++ \
CPPFLAGS=-I/usr/local/include \
LDFLAGS=-I/usr/local/include \
./bootstrap
CC=/usr/bin/clang \
CXX=/usr/bin/clang++ \
CPPFLAGS=-I/usr/local/include \
CXXFLAGS="-std=c++0x" \
LDFLAGS="-I/usr/local/include -lboost_system" \
LIBUSB_CFLAGS=-I/usr/local/include/libusb-1.0 \
LIBUSB_LIBS="-L/usr/local/lib -lusb-1.0" \
./configure
It allows to configure with boost 1.76. Found here https://gist.github.com/kidpixo/ef1a26ae953e3939a4eebe1b6fd2f07c
For mac with M1 CPU, macOS Monterey (boost version 1.78.0):
CC=/usr/bin/clang \
CXX=/usr/bin/clang++ \
CPPFLAGS=-I/opt/homebrew/include \
LDFLAGS=-I/opt/homebrew/include \
./bootstrap
CC=/usr/bin/clang \
CXX=/usr/bin/clang++ \
CPPFLAGS=-I/opt/homebrew/include \
CXXFLAGS="-std=c++0x" \
LDFLAGS="-L/opt/homebrew/Cellar/boost/1.78.0_1/lib/" \
LIBUSB_CFLAGS=-I/opt/homebrew/include/libusb-1.0 \
LIBUSB_LIBS="-L/opt/homebrew/lib -lusb-1.0" \
./configure
I've hit the very same issue, right today (same OS: MacOS 11.5.2).
I think one solution would be to create your own tap, with the Formula copied from the removed boost#1.60 (Homebrew/homebrew-core#84434).

Compact build for OpenCV applications on Windows

I was reading this article on how to create compact builds for applications using OpenCV.
But, this article includes only GCC related optimizations. I am looking for equivalent options in Visual Studio, but am able to find only the option to minimize size (/Os) and the option /LTCG.
I am building a very simple OpenCV console application (code is below) using OpenCV static libraries built from source. The issue is the size of executable is too large (37 MB in Debug build and 19 MB in Release build). I want to reduce that size to a few MBs and I am looking for ways to do that - I found that article while researching about this.
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
int main()
{
cv::Mat m = cv::imread("sample.jpg");
cv::imwrite("output.jpg", m);
std::cout << "Hello World!\n" << m.rows << " " << m.cols;
return 0;
}
Just in case anyone wants to know, I am including following libraries (the below are for Release mode):
opencv_core430.lib
opencv_imgproc430.lib
opencv_imgcodecs430.lib
libjpeg-turbo.lib
libpng.lib
libtiff.lib
libwebp.lib
ippiw.lib
zlib.lib
IlmImf.lib
libjasper.lib
ittnotify.lib
ippicvmt.lib
If anyone has any ideas what equivalent options are in Visual Studio, it would be of great help.
Also, is there anyone who was successful to reduce the executable size using static OpenCV libraries on Visual Studio?
I encountered the same issue and I found two things which make a big difference: performance libraries/frameworks and unnecessary file types.
Since you are building from source, you should be able to use cmake-gui to adjust the configuration to disable these:
WITH_IPP
WITH_ITT
WITH_OPENCL
Then, disable these additional image types if you need only JPG:
WITH_JASPER
WITH_PNG
WITH_OPENJPEG
WITH_WEBP
WITH_TIFF
WITH_OPENEXR
WITH_IMGCODEC_HDR
WITH_IMGCODEC_SUNRASTER
WITH_IMGCODEC_PXM
WITH_IMGCODEC_PFM
Since imread and imwrite determine the file type based on the given string, the linker has no way of knowing that the code for the other unnecessary image formats can be removed.
When I apply these changes and compile your program using Visual Studio 2022 it comes out to about 1.7 MB in release mode. (Starting with the default configuration and disabling only BUILD_SHARED_LIBS, I had seen the same 19 MB executable that you had reported.)
You can build as per your modules requirements, details in the blog.
The simplest way to build is using OpenCV cmake example:
cmake
-D WITH_CUDA=OFF \
-D WITH_MATLAB=OFF \
-D BUILD_ANDROID_EXAMPLES=OFF \
-D BUILD_DOCS=OFF \
-D BUILD_PERF_TESTS=OFF \
-D BUILD_TESTS=OFF \
-D BUILD_SHARED_LIBS=OFF \
-D BUILD_opencv_objdetect=OFF \
-D BUILD_opencv_video=OFF \
-D BUILD_opencv_videoio=OFF \
-D BUILD_opencv_features2d=OFF \
-D BUILD_opencv_flann=OFF \
-D BUILD_opencv_ml=OFF \
-D BUILD_opencv_photo=OFF \
-D BUILD_opencv_python=OFF \
-D BUILD_opencv_shape=OFF \
-D BUILD_opencv_stitching=OFF \
-D BUILD_opencv_superres=OFF \
-D BUILD_opencv_ts=OFF \
-D BUILD_opencv_videostab=OFF \
-D BUILD_opencv_highgui=ON \
-D BUILD_opencv_imgproc=ON \
..
For static linking, you can pick and choice the module(s) by un-archiving .a to .o and bundle them in single .a.
CAUTION: be very sure that you do not break the dependencies.
Example:
to unarchive a .a using ar -x libopencv_imgproc.a and to archive again
ar -rc libopencv_custom.a *.o

Travis-CI Complex Makefile fails

I am new to using Travis-CI, and the C++ project that I am starting with uses a GNU Makefile, The makefile contains many functions, one of which calls other functions. I noticed that in the Travis-CI log, this function does not successfully call the other functions, instead, it leaves a blank space, which causes the build to fail. Despite it working on any computer.
The function is:
define Compile
mkdir -p $(#D)
if [[ $(2) == Linking ]]; then \
$(call Print,$(2) $(#F),$(WIDTH)); \
else \
$(call PrintCpp,$(2) $(#F),$(WIDTH)); \
fi
$(1) 2> $#.log; \
RESULT=$$?; \
if [ $$RESULT -ne 0 ]; then \
$(cross); \
else \
$(check); \
fi; \
cat $#.log; \
rm -f $#.log
endef
The functions called are:
define Line =
$(shell printf '%0.1s' "$(2)"{1..$(1)})
endef
define Print
var="$(1)"; \
width="$(2)";\
printf '%s%*.*s' "$$var" 0 $$(($$width - $${#var} - 1)) "$(call
Line,$(2),.)"
endef
define PrintCpp
var="$(1)"; \
var=$${var%.*}.cpp; \
width="$(2)";\
printf '%s%*.*s' "$$var" 0 $$(($$width - $${#var} - 1)) "$(call
Line,$(2),.)"
endef
define check =
printf "%b\n" "$(OK_COLOR)\xE2\x9C\x94 $(NO_COLOR)"
endef
define cross =
printf "%b\n" "$(ERR_COLOR)\xE2\x9D\x8C $(NO_COLOR)"
endef
The GitHub for the project can be found here:
https://github.com/LuxAtrumStudio/Pessum
And the Travis-CI log here:
https://travis-ci.org/LuxAtrumStudio/Pessum/builds/256427744
I had a very similar problem, which turned out to be because Travis by default still uses the deprecated Ubuntu Trusty release. Trusty only has GNU Make version 3, while we require GNU Make > 4.
My problem was fixed when I switched Travis to Ubuntu Xenial by adding the following to my .travis.yml:
dist: xenial
Before the switch from Trusty to Xenial, make -v was at GNU Make 3.81. After the switch, make -v is now at GNU Make 4.1
Switching to Xenial might require you to make some additional changes. I had to bump my Python version from 3.4 to 3.7, for example.

OpenCV was built without CUDA Video decoding support

After unzip opencv3.1.0, I was cmake with:
sudo cmake -D CMAKE_BUILD_TYPE=RELEASE -D INSTALL_C_EXAMPLES=ON –D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_CUDA=ON -D CUDA_FAST_MATH=1 -D WITH_CUBLAS=1 -D WITH_OPENGL=ON -D WITH_V4L=ON –D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_TBB=ON ..
But when I run opencv-3.1.0/sample/gpu/video_reader.cpp with compile :
sudo g++ $(pkg-config --libs --cflags opencv) -o stream video_reader.cpp
I have received notice:
OpenCV was built without CUDA Video decoding support
I'm using Raspberry 3. Thanks all !
Raspberry Pi 3 has no NVIDIA GPU, therefore CUDA is not supported.
From NVIDIA page:
CUDA® is a parallel computing platform and programming model invented by NVIDIA. It enables dramatic increases in computing performance by harnessing the power of the graphics processing unit (GPU). - See more at: http://www.nvidia.com/object/cuda_home_new.html#sthash.5wWaGnZI.dpuf
From answer related to similar question:
Q: Is it possible to use the GPU for calculations? (e.g. CUDA/OpenCL)
A: Not at present - there is only a framebuffer interface for display purposes. There is no OpenCL and no plans for it nor is there documentation available to create OpenCL. CUDA is Nvida only so isn't applicable. Once an OpenGL driver becomes available you may be able to engineer some calculations via the GPU but how useful that will be remains to be seen.
Instead you can use OpenGL ES or OpenVG to write code using GPU. Raspberry PI video api

Mingw-w64 - printf does not work

First I wanted to modificate ffplay according to my requirments. Then I noticed that original ffplay from my build can't play some video files, but it didn't write any message to console. Then I noticed that ffmpeg also don't write any usage message when I run it without params. But it works. If I run it from terminal it's like running asynchronously! The terminal just shows next row. I mean it asks for a next command. But the ffmpeg process is visible in task manager and it writes the output video file what I had requested!
I created following souce file. I have modified the Makefile. So it have built the exe-file works just the same way. I have no idea how it can be.
#include <SDL.h>
#include "cmdutils.h"
const char program_name[] = "hello";
const int program_birth_year = 2013;
void show_help_default(const char *opt, const char *arg)
{
printf("zxcvbnm\n");
}
int main(int argc, char **argv)
{
printf("1234567890\n");
return 0;
}
And after that I created real hello world app with MinGW-w64 and qmake without eny extra libs. And its printf does not work.
I want to prevent this behavior.
I want to make printf working in traditional manner.
How I build FFmpeg:
PKG_CONFIG_PATH=/home/developer/workspace/MinGW32fs/lib/pkgconfig/ \
SDL_CONFIG=/home/developer/workspace/MinGW32fs/bin/sdl-config \
./configure \
--prefix=/home/developer/workspace/MinGW32fs \
--extra-ldflags="-L/home/developer/workspace/MinGW32fs/lib" \
--extra-cflags="-I/home/developer/workspace/MinGW32fs/include" \
--arch=x86 --target-os=mingw32 --cross-prefix=i686-w64-mingw32- \
--pkg-config=pkg-config --enable-libtheora --enable-libvorbis --enable-libvpx \
--enable-outdev=sdl --enable-shared --disable-static \
--disable-doc --disable-manpages --disable-podpages
make
Solved!
By some reason you must visibly specify you make console app.
In qmake helloworld:
CONFIG += console
In FFmpeg's configure script it's an extra-ldflag console:
PKG_CONFIG_PATH=/home/developer/workspace/MinGW32fs/lib/pkgconfig/ \
SDL_CONFIG=/home/developer/workspace/MinGW32fs/bin/sdl-config \
./configure \
--prefix=/home/developer/workspace/MinGW32fs \
--extra-ldflags="-L/home/developer/workspace/MinGW32fs/lib -Wl,-subsystem,console" \
--extra-cflags="-I/home/developer/workspace/MinGW32fs/include" \
--arch=x86 --target-os=mingw32 --cross-prefix=i686-w64-mingw32- \
--pkg-config=pkg-config --enable-libtheora --enable-libvorbis --enable-libvpx \
--enable-outdev=sdl --enable-shared --disable-static \
--disable-doc --disable-manpages --disable-podpages