OpenCV traincascade with multithreading - opencv

I want to train haar cascade by opencv_traincascade.
after
opencv_traincascade -data result/apple/ -vec samples/sample.vec -bg negatives.dat -minHitRate 0.999 -mode ALL -numPos 2000 -numNeg 1500 -precalcValBufSize 2000 -precalcIdxBufSize 2000
I saw that only one core is loaded. What can I do to quickly train the cascade? If I need to recompile OpenCV with some options please show me how to do it. Thank you!
UPD:
Found this thread. There is a new question: how to set up OpenCV with flag HAVE_TBB? And will it help me? Which version of IPP I can use with OpenCV 2.4.3?

Sorry for my English.
I found the answer to my question.
Tested on xubuntu 12-10 with IPP 7.1 for intel64 and Opencv 2.4.3
To use IPP&&TBB with OpenCV you should download and install them from the official site
ia32 for 32-bit architecture, intel64 for 64 bit!
After you should compile OpenCv by this command:
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_PYTHON_SUPPORT=ON -D WITH_IPP=ON -D IPP_H_PATH=/opt/intel/ipp/include/ -D WITH_TBB=ON ..
Check output after cmake!
make
sudo make install

Related

OpenCV: training a cascade classifier

I am trying to train a cascade classifier using OpenCV, a tutorial & UIUC Image Database for Car Detection. However, the training 'hangs' at stage 0 and never generates any files; in the tutorial, results are seen in a matter of minutes.
I am running OpenCV 2.4.8, which I have installed using conda, on a 2015 MBP running Yosemite (10.10.5)
Steps:
Downloaded the dataset & saved the positive images in a pos folder and the negative images in a neg folder
generated a txt file for the positive images
find pos -iname "*.pgm" > cars.txt
sed -i '' 's/.pgm/.pgm 1 0 0 100 40/g' cars.txt
generated a txt file for the negative images
find neg -iname "*.pgm" > bg.txt
generated a vec file from cars.txt
opencv_createsamples -info cars.txt -num 550 -w 48 -h 24 -vec cars.vec
create a data dir
mkdir data
train cascade
opencv_traincascade -data data -vec cars.vec -bg bg.txt -numPos 500 -numNeg 500 -numStages 2 -w 48 -h 24 -featureType LBP
Output:
PARAMETERS:
cascadeDirName: data
vecFileName: cars.vec
bgFileName: bg.txt
numPos: 500
numNeg: 500
numStages: 2
precalcValBufSize[Mb] : 256
precalcIdxBufSize[Mb] : 256
stageType: BOOST
featureType: LBP
sampleWidth: 48
sampleHeight: 24
boostType: GAB
minHitRate: 0.995
maxFalseAlarmRate: 0.5
weightTrimRate: 0.95
maxDepth: 1
maxWeakCount: 100
===== TRAINING 0-stage =====
<BEGIN
POS count : consumed 500 : 500
NEG count : acceptanceRatio 500 : 1
I have waited for several hours but the training never progresses nor does it generate any files in my data directory. What is causing the 'hanging'?
For what it's worth, I ran into the same symptom and it was driving me crazy. Uninstalling and reinstalling opencv fixed it. The exact command that was hanging suddenly worked fine. It could be you have an old version of opencv lingering somewhere like I had:
$ brew uninstall opencv
Uninstalling /usr/local/Cellar/opencv/2.4.12... (225 files, 36M)
opencv 2.4.11_1 is still installed.
Remove them all with `brew uninstall --force opencv`.
$ brew uninstall --force opencv
Uninstalling opencv... (222 files, 35M)
$ brew install opencv --with-tbb
At any rate, something to consider.
I run into the same problem. It turns out using haar cascade training works just fine. You could specific more stages for it to improve accuracy.
I just don't see how you expect to get anything good out of two training stages. You should train until your -acceptanceRatioBreakValue reaches 10e-5. You've ordered 10 burgers from OpenCV but left after only getting 2.
See here: http://docs.opencv.org/3.1.0/dc/d88/tutorial_traincascade.html#gsc.tab=0
Compiling OpenCV with --use-tbb (from homebrew) solved this for me, I believe it is a bug with GCD implementation on OS X as mentioned here: https://github.com/opencv/opencv/issues/4765

OpenCV Error: Assertion failed (_img.cols == winSize.width)

So I've been trying to run the command
opencv_traincascade -data HandsData -vec hands.vec -bg HandsNeg.txt -numPos 3641 -numNeg 2578 -numStages 20 -w 27 -h 48 -mode ALL -minHitRate 0.999 -maxFalseAlarmRate 0.5 -precalcValBufSize 1024 -precalcIdxBufSize 1024
and I get the error
===== TRAINING 0-stage =====
<BEGIN
POS count : consumed 3641 : 3641
OpenCV Error: Assertion failed (_img.cols == winSize.width) in get, file /builddir/build/BUILD/OpenCV-2.0.0/apps/traincascade/imagestorage.cpp, line 86
terminate called after throwing an instance of 'cv::Exception'
Aborted
I've seen suggestions around to change the positive image number, where I use 1043 instead and still get the same error. Then I also see suggestions of editing the source code. The problem is that I built OpenCV with yum and would like to not have to rebuild from source.
sudo find / -name imagestorage.cpp
turns up nothing.
I'm at a complete loss of what to do.
Additional info: Steps I took to get to this point
I created everything from some videos using ffmpeg. These were from a phone and VLC lists the info as
Resolution: 1920x1090
Display resolution: 1920x1080
The ffmpeg command was (replacing input/output with respective videos and locations)
steven ~/computer_vision $ ffmpeg -i videos/Not\ hands\ stuff.mp4 -y -r 40 -s 27x48 -f image2 NotHandsorFists/Negs-%4d.png
Files in Hands.txt are of the form
steven ~/computer_vision $ cat Hands.txt | head -n 1
Hands/LeftHand-0001.png 1 0 0 27 48
I compiled the vec file with
steven ~/computer_vision $ opencv_createsamples -info Hands.txt -num 3641 -w 27 -h 48 -vec hands.vec
The negative file is in the form
steven ~/computer_vision $ cat HandsNeg.txt | head -n 1
Fists/LeftFist-0001.png
and the working directory is
steven ~/computer_vision $ ls
Fists fists.txt Hands HandsData HandsNeg.txt Hands.txt hands.vec NotHandsorFists NotHandsorFists.txt videos
Edit:
I've tried changing png to jpg and bmp to get rid of the channels. No help.

Build OpenCV with CUDA support

Im use CMake to generate visual studio 2013 solution. Next im try to build it, but get follow error:
Building NVCC (Device) object modules/core/CMakeFiles/cuda_compile.dir/src/cuda/Debug/cuda_compile_generated_gpu_mat.cu.obj
nvcc fatal : Unsupported gpu architecture 'compute_11'
Im try version 2.10 and 3.0 with cuda 6.5 and 7.0.
CUDA_ARCH_BIN set to : 1.1 1.2 1.3 2.0 2.1(2.0) 3.0 3.5
Another option.
Ubuntu 14.04, GTX Titan X, opencv-2.4.10
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_TIFF=ON -D BUILD_EXAMPLES=ON -D CUDA_GENERATION=Auto -D BUILD_NEW_PYTHON_SUPPORT=ON ..
I also applied the patch,
but I'm not sure whether it ended up being needed. I had tried with and withoutCUDA_GENERATION=Maxwell but Maxwell isn't detected. I did not try CUDA_GENERATION=Auto prior to the patch, that's why I don't know for sure.
When using cmake to do configurations, set the option CUDA_GENERATION to specific your GPU architecture. I ran across the same error and tried this to work out the problem.
Following up on Yun's answer (could not leave comment), this worked for me and shows a possible value for CUDA_GENERATION:
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D CUDA_GENERATION=Kepler ..
(Ubuntu 12.04 and 14.04, GTX Titan, and OpenCV 2.4.11 and 3.0.0.)
You can use CUDA_GENERATION to specify the corresponding generation code name for your GPU architecture.
Here's the relevant opencv cmake code that parses the CUDA_GENERATION value:
set(__cuda_arch_ptx "")
if(CUDA_GENERATION STREQUAL "Fermi")
set(__cuda_arch_bin "2.0")
elseif(CUDA_GENERATION STREQUAL "Kepler")
set(__cuda_arch_bin "3.0 3.5 3.7")
elseif(CUDA_GENERATION STREQUAL "Maxwell")
set(__cuda_arch_bin "5.0 5.2")
elseif(CUDA_GENERATION STREQUAL "Pascal")
set(__cuda_arch_bin "6.0 6.1")
elseif(CUDA_GENERATION STREQUAL "Volta")
set(__cuda_arch_bin "7.0")
elseif(CUDA_GENERATION STREQUAL "Auto")
execute_process( COMMAND "${CUDA_NVCC_EXECUTABLE}" ${CUDA_NVCC_FLAGS} "${OpenCV_SOURCE_DIR}/cmake/checks/OpenCVDetectCudaArch.cu" "--run"
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/"
RESULT_VARIABLE _nvcc_res OUTPUT_VARIABLE _nvcc_out
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
if(NOT _nvcc_res EQUAL 0)
message(STATUS "Automatic detection of CUDA generation failed. Going to build for all known architectures.")
else()
set(__cuda_arch_bin "${_nvcc_out}")
string(REPLACE "2.1" "2.1(2.0)" __cuda_arch_bin "${__cuda_arch_bin}")
endif()
endif()
And the wikipedia CUDA page has a nice table for mapping your video card to the right microarchitecture code name (sorry, it's too large to reproduce here):
https://en.wikipedia.org/wiki/CUDA#GPUs_supported
For example, my middling-2012 Macbook Pro has an antique GeForce GT 650M, which the wikipedia table indicates uses the Kepler microarchitecture. Therefore, I use this in my cmake command line:
cmake -D CUDA_GENERATION="Kepler" ...
and the opencv script converts that to "3.0 3.5 3.7" when it displays the config summary, and passes on the corresponding flags to nvcc.
In my case, before setting this properly, I was getting errors about compute_70 not supported. Apparently, there is still an open issue in the opencv tracker as of today (2017-10-07) about auto-detection not working properly.
Thank you,
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D CUDA_GENERATION=Kepler ..
This let me install opencv-2.4.9.
If you want to know more detaills check this link.
This is because of your gpu type is mismatching.
You have to define CUDA_GENERATION explicitly.
In my side, I could find 3 types of CUDA_GENERATION; Auto, Kepler, Fermi.
When I set CUDA_GENERATION as Kepler, compute_11 changed to compute_30 and build successful.
You should set with cmake these entries CUDA_ARCH_BIN = 3.2 and CUDA_ARCH_PTX = 3.2
Hope it helps.
Regards
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_TIFF=ON -D BUILD_EXAMPLES=ON -D CUDA_GENERATION=Kepler -D BUILD_NEW_PYTHON_SUPPORT=ON ..
worked for me for OpenCV 2.4.11

What is the truly correct usage of -S parameter on weka classifier A1DE?

So I'm using weka 3.7.11 in a Windows machine (and runnings bash scripts with cygwin), and I found an inconsistency regarding the AODE classifier (which in this version of weka, comes from an add-on package).
Using Averaged N-Dependencies Estimators from the GUI, I get the following configuration (from an example that worked alright in the Weka Explorer):
weka.classifiers.meta.FilteredClassifier -F "weka.filters.unsupervised.attribute.Discretize -F -B 10 -M -1.0 -R first-last" -W weka.classifiers.bayes.AveragedNDependenceEstimators.A1DE -- -F 1 -M 1.0 -S
So I modified this to get the following command in my bash script:
java -Xmx60G -cp "C:\work\weka-3.7.jar;C:\Users\Oracle\wekafiles\packages\AnDE\AnDE.jar" weka.classifiers.meta.FilteredClassifier \
-t train_2.arff -T train_1.arff \
-classifications "weka.classifiers.evaluation.output.prediction.CSV -distribution -p 1 -file predictions_final_multi.csv -suppress" \
-threshold-file umbral_multi.csv \
-F "weka.filters.unsupervised.attribute.Discretize -F -B 10 -M -1.0 -R first-last" \
-W weka.classifiers.bayes.AveragedNDependenceEstimators.A1DE -- -F 1 -M 1.0 -S
But this gives me the error:
Weka exception: No value given for -S option.
Which is weird, since this was not a problem with the GUI. In the GUI, the Information box says that -S it's just a flag ("Subsumption Resolution can be achieved by using -S option"), so it shouldn't expect any number at all, which is consistent with what I got using the Explorer.
So then, what's the deal with the -S option when using the command line? Looking at the error text given by weka, I found this:
Options specific to classifier weka.classifiers.bayes.AveragedNDependenceEstimators.A1DE:
-D
Output debugging information
-F <int>
Impose a frequency limit for superParents (default is 1)
-M <double>
Specify a weight to use with m-estimate (default is 1)
-S <int>
Specify a critical value for specialization-generalilzation SR (default is 100)
-W
Specify if to use weighted AODE
So it seems that this class works in two different ways, depending on which method I use (GUI vs. Command Line).
The solution I found, at least for the meantime, was to write -S 100 on my script. Is this really the same as just putting -S in the GUI?
Thanks in advance.
JM
I've had a play with this Classifier, and can confirm that what you are experiencing on your end is consistent with what I have here. From the GUI, the -S Option (subsumption Resolution) requires no parameters while the Command Prompt does (specialization-generalization SR).
They don't sound like the same parameter, so you may need to raise this issue with the developer of the third party package if you would like to know more information on these parameters. You can find this information from the Tools -> Package Manager -> AnDE, which will point you to the contacts for the library.

opencv createsamples has no error, but no samples found

I'm using this tutorial, and I'm on the stage of creating lots of samples from my positive images. I'm using Windows.
This is the command:
perl bin/createsamples.pl positives.txt negatives.txt samples 1500\ "C:\opencv_built\bin\Release\opencv_createsamples.exe -bgcolor 0 -bgthresh 0 -maxxangle 1.1\ -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 80 -h 40"
And this is what I get as output, for each of the positive images:
C:\opencv_built\bin\Release\opencv_createsamples.exe -bgcolor 0 -bgthresh 0 -max
xangle 1.1\ -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 80 -h 40 -img 60inclin
ation_315azimuth.jpg -bg tmp -vec samples0inclination_315azimuth.jpg.vec -num 62
Info file name: (NULL)
Img file name: 60inclination_315azimuth.jpg
Vec file name: samples0inclination_315azimuth.jpg.vec
BG file name: tmp
Num: 62
BG color: 0
BG threshold: 0
Invert: FALSE
Max intensity deviation: 40
Max x angle: 1.1
Max y angle: 1.1
Max z angle: 0.5
Show samples: FALSE
Width: 80
Height: 40
Create training samples from single image applying distortions...
Done
The messages seemed successful so I went into the samples folder expecting to see 1500 samples, but there's nothing there! I noticed the info file is <NULL>, does that have anything to do with this? There doesn't seem to be any error, what went wrong?
Edit
I've changed the command to include absolute paths for everything, eg
perl bin/createsamples.pl C:\my_work\code\opencv-haar-classifier-training-master\positive_images\ C:\my_work\code\opencv-haar-classifier-training-master\positive_images\ samples 1500\ "C:\opencv_built\bin\Release\opencv_createsamples.exe -bgcolor 0 -bgthresh 0 -maxxangle 1.1\ -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 80 -h 40"
But still no luck!
I had a similar problem I'll try to explain what I did, I hope it will solve you problem.
First of all the NULL after -info is normal. As said in the official documentation of OpenCV for opencv_createcamples you need to either input an image (-img) or a collection of images (-info) I'll let you read the documentation for further understanding, but for this example you don't need it.
Secondly you don't need to put absolute paths in this command; here is my command:
perl bin/createsamples.pl positives.txt negatives.txt samples 1500 "opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 -maxzangle 0.5 -maxidev 40 -w 42 -h 40"
to do this be sure to be at the root of the gitHub directory you have download (the parent one of bin) be sure that your positives.txt and negatives. txt are in this directory. Also copy paste the opencv_createsamples.exe from OpenCV directory in this one.
This done I'll now explain the main problems: The project was developped for Ubuntu at the beginning so it works for mac easily but I think you must be under Windows. If you hadn't already please download Cygwin because it uses mainly Linux commands.
As I said I was blocked with a similar problem, so I tried to use opencv_createsample directly instead of the perl script to see if the problem was coming from there and I noticed that the problem was coming from the fact that my positives.txt and negatives.txt were under Windows format and not Unix so the Perl script wasn't able to read them properly.
The difference between windows and linux are big and Cygwin doesn't bridge the gap so there might be other encoding problems. So what I did is surely not the fastest way to resolve the problem, but it is an easy one.
I just installed an Ubuntu vm on my PC
Installed Opencv with TBB ( a lot of tutorials internet, the best is the one from the OpenCV site).
I Downloaded the gitHub Classifier training and then I followed the commands given and it worked well.
Another simpler explanation. Let's say you are new to OpenCV (as I am), and you're just following one of the tutorial videos, to create your first object detector. You might have created a sample image by cut-pasting from the web. Sample images must be monochrome, but more than that, they must be single-channel, 8-bit monochrome. If you photoshopped your sample to appear monochrome, but didn't change its channel configuration, opencv_createsamples will ignore it if it actually has more than one channel.
Source: ~/apps/createsamples/utilities.cpp, function icvStartSampleDistortion().
I was presented with a similar problem, also in Windows, but my solution was to check positives.txt and negatives.txt were in a format the the createsamples.pl file could understand. I'm not sure this was the same as your problem, but I ended up here so others might too.
I ran the following python script from the same directory as createsamples.pl to form the text files, which seemed to make the perl file work for me:
'''
Run this from the folder containing the positive and negative samples' folders (Note: outputs the text files to this directory)
'''
import glob
import sys
def write_locations(folder, textFile):
print('')
file = open(textFile, 'w')
for contents in glob.glob('{}/*'.format(folder)):
try:
fileName = contents.split("\\")[-1]
file.write('\n./{0}/{1}'.format(folder, fileName))
print('./{0}/{1}'.format(folder, fileName))
except:
pass
write_locations('positive', 'positives.txt')
write_locations('negative', 'negatives.txt')
print('\n -Done.')
The organisation of my folders might be slightly different to the tutorial (I only went there for the perl file), but all I'm doing is checking that the text files read something like:
./positive/1_1.png
./positive/1_2.png
./positive/1_3.png
./positive/1_4.png
and so on.
I had a similar problem I'll try to explain what I did, I hope it will solve your problem.
First, name the files positives.dat and negatives.dat instead of positives.txt and negatives.txt.
Second, copy and paste positives.dat and negatives.dat files into the current directory, then run:
perl bin/createsamples.pl positives.dat negatives.dat samples 1000 "opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 80 -h 40"
After that, check your samples folder, and you should see the positive & negative folders containing many vector files.
I had the same problem. The problem was that I had Captital letters in the filename, but not in the terminal code.
This should work:
perl bin/createsamples.pl positives.txt negatives.txt samples 3000 "opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 0 -maxyangle 0 maxzangle 0 -maxidev 40 -w 32 -h 32"

Resources