segmentation fault with parallel_for_

asked 2017-11-13 03:10:09 -0600

aniskraini@yahoo.fr gravatar image

updated 2017-11-13 05:41:54 -0600

berak gravatar image

Please i have many problem with "parallel_for_" ( random segmentation fault). in the majority of cases, the code works but sometimes it displays segmentation error (random). I use parallel_for_ with opencv 3.1.0. My code is attached

    class Paralleltextzone : public ParallelLoopBody
{
public:
    Paralleltextzone  (vector <String> &_liste_files, const Mat &_mask_base,  int _mask_debug, const char* _debug_path, vector<float> &_param_image)
        :liste_files(_liste_files),  mask_base(_mask_base), mask_debug (_mask_debug), debug_path (_debug_path),  param_image (_param_image)
    {
    }
virtual void operator ()(const Range& range) const
{
    for (int r = range.start; r < range.end; r++)

    {    int resl=0;
        //get text zone
        cout<<"Processing the frame: "<<r<<endl;            
        Mat output_mat, mask, Kernel,  boxFilter;   
        string image_name=liste_files.at(r);            
        Mat image=imread(image_name);         
        threshold(mask_base,mask, 10, 255,  cv::THRESH_BINARY);
        resize(mask_base,mask_base,image.size());            
        image.copyTo(output_mat, mask_base);                     
         resl=text_recognition(output_mat, output_mat.rows, output_mat.cols, r, 0, "/home/krayni/Bureau/VTT/TextDetect/txt", 0,  (double)param_image.at(0),  (double)param_image.at(1), (int) param_image.at(2),  (int)param_image.at(3),(int)param_image.at(4));           

    }
}

Paralleltextzone& operator=(const Paralleltextzone  &) {
    return *this;
};



   /*Text Regions Detection*/
extern "C"  int text_recognition(Mat &image_orig, int rows, int cols, int stime, int Debug, const char *tempPath, int MULTI_CHANNEL, double alpha, double beta, int hue, int sat, int level)
{

Mat grey;
Mat image_processed = Mat::zeros(image_orig.size()*3, image_orig.type());
prepare_image_for_ocr1(image_orig, image_processed, alpha, beta); // enhance image contrast and brighteness

vector<Mat> channels(2); 
vector<vector<ERStat> > regions(2);   
cvtColor(image_processed, grey,COLOR_RGB2GRAY);  
channels.push_back(grey);
channels.push_back(255-grey);
regions[0].clear();
regions[1].clear();
vector< Ptr<ERFilter> > er_filters1;
vector< Ptr<ERFilter> > er_filters2;    
Ptr<ERFilter> er_filter1 = createERFilterNM1(loadClassifierNM1("trained_classifierNM1.xml"),8,0.00015f,0.13f,0.2f,true,0.1f);
Ptr<ERFilter> er_filter2 = createERFilterNM2(loadClassifierNM2("trained_classifierNM2.xml"),0.5);
// Create ERFilter objects with the 1st and 2nd stage default classifiers

 for (int i=0; i<2; i++)
{
//Ptr<ERFilter> er_filter1 = createERFilterNM1(loadClassifierNM1("/opt/exe/ASAP_TraitementPTT_python/trained_classifierNM1.xml"),8,0.00015f,0.13f,0.2f,true,0.1f);
//Ptr<ERFilter> er_filter2 = createERFilterNM2(loadClassifierNM2("/opt/exe/ASAP_TraitementPTT_python/trained_classifierNM2.xml"),0.5);
er_filters1.push_back(er_filter1);
er_filters2.push_back(er_filter2);
}

// Apply the default cascade classifier to each independent channel (could be done in parallel)


Extractfeatures extractfeatures(channels,regions,er_filters1,er_filters2);

parallel_for_(cv::Range(0,2), extractfeatures);    

//ananothe process 


return 1;

}

//valgrind erro
==25778== Invalid read of size 1
==25778==    at 0xD1D7445: cv::text::ERFilterNM::er_tree_extract(cv::_InputArray const&) (in /usr/local/lib/libopencv_text.so.3.1.0)
==25778==    by 0xD1D8D6A: cv::text::ERFilterNM::run(cv::_InputArray const&, std::vector<cv::text::ERStat, std::allocator<cv::text::ERStat> >&) (in /usr/local/lib/libopencv_text.so.3.1.0)
==25778==    by 0x14DFAA: text_recognition (in /home/krayni/Bureau/VTT/TextDetect/test_memory_leak/TEST_LOGO_DETECT)
==25778==    by 0x11CCFA: Paralleltextzone::operator()(cv::Range const&) const (in /home/krayni/Bureau/VTT/TextDetect/test_memory_leak/TEST_LOGO_DETECT)
==25778==    by 0x5D47961: tbb::interface7::internal::start_for<tbb::blocked_range<int>, (anonymous namespace)::ProxyLoopBody, tbb::auto_partitioner const>::execute() (in /usr/local/lib/libopencv_core.so.3.1.0)
==25778==    by 0x4E6067C: ??? (in /usr/lib/x86_64-linux-gnu/libtbb.so.2)
==25778==    by 0x4E5D83F: ??? (in /usr/lib/x86_64-linux-gnu/libtbb.so.2)
==25778==    by 0x5D47B74: cv::parallel_for_(cv::Range const&, cv::ParallelLoopBody const&, double) (in /usr ...
(more)
edit retag flag offensive close merge delete

Comments

valgrind message

==24403== Process terminating with default action of signal 11 (SIGSEGV)

==24403== Access not within mapped region at address 0x0 ==24403== at 0xD1D7445: cv::text::ERFilterNM::er_tree_extract(cv::_InputArray const&) (in /usr/local/lib/libopencv_text.so.3.1.0) ==24403== by 0xD1D8D6A: cv::text::ERFilterNM::run(cv::_InputArray const&, std::vector<cv::text::erstat, std::allocator<cv::text::erstat=""> >&) (in /usr/local/lib/libopencv_text.so.3.1.0) ==24403== by 0x1580D4: Extractfeatures::operator()(cv::Range const&) const (in /home/krayni/Bureau/VTT/TextDetect/test_memory_leak/TEST_LOGO_DETECT) ==24403== by 0x5D475FD: tbb::interface7::internal::start_for<tbb::blocked_range<int>, (anonymous namespace)::ProxyLoopBody, tbb::auto_partitioner const>::execute() (in /u

aniskraini@yahoo.fr gravatar image[email protected] ( 2017-11-13 03:22:19 -0600 )edit

that's a wall of unformatted code.

please format it (edit -> 10101 button) and reduce it to the minimum reproducable example, please.

then, why did you assume, this would be thread-safe, at all ?

berak gravatar imageberak ( 2017-11-13 04:53:21 -0600 )edit

In fact, it work but when i try to run two excution (two console ) i have this problem

aniskraini@yahoo.fr gravatar image[email protected] ( 2017-11-13 05:17:40 -0600 )edit

i'm quite shure, that sharing a Ptr<ERFilter> between threads is not safe.

berak gravatar imageberak ( 2017-11-13 08:10:57 -0600 )edit

In fact, we need to creat a Ptr<erfilter> for each thread (like here https://github.com/opencv/opencv/issu...).</erfilter>

aniskraini@yahoo.fr gravatar image[email protected] ( 2017-11-13 08:44:50 -0600 )edit

again, imho, you should optimize the hell out of the opencv libs, like building with tbb, eigen, using opencl, wherever you can and so on, not build your own multithreading on top of it

berak gravatar imageberak ( 2017-11-13 09:01:03 -0600 )edit