/* * mp_threadedGabor * * Author:Andrew Salamon * Date: Fri Jun 2 2006 * * Copyright (c) 2006 Machine Perception Laboratory * University of California San Diego. * * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ #include "mp_threadedGabor.h" #include #include #include const unsigned int AUcnt = 8; template< class T > class functorWrap { public: functorWrap( T *_obj ) : obj(_obj) { } void operator()() { (*obj)(); } private: T *obj; }; /** mp_threadedGabor constructor. * The arrays used by FFTW need to be set up during construction. * This is so we can generate and store the plans. This also means that we can't * reallocate either of these array, which is a bit tricky with the RImage pixels because there are * a number of methods on that class that will change the array out from under us. * It might be better in the long run to copy the input RImage into an internal array. */ mp_threadedGabor::mp_threadedGabor( const RImage &_pixels, int width, int height, bool sub_proc ) : MP_Gabor( _pixels, width, height, sub_proc ) { // unsigned int splits[] = { 1, 5, 6, 9 }; // For two Gabors unsigned int splits[] = { 1, 3, 4, 6, 7, 9 }; // For three Gabors // unsigned int splits[] = { 1, 3, 4, 5, 6, 7, 8, 9 }; // For four Gabors // unsigned int splits[] = { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9 }; // For nine Gabors unsigned int cnt = 3 * 2; for( unsigned int index=0; index < cnt; index += 2 ) { MP_Gabor *gabor = new MP_Gabor( _pixels, width, height, true ); gabor->setNi( splits[index], splits[index+1] ); gabors.push_back( gabor ); } // create 3 gabors and save them // MP_Gabor *gabor = new MP_Gabor( _pixels, width, height, true ); // gabor->setNi( 1, 3 ); // gabors.push_back( gabor ); // // gabor = new MP_Gabor( _pixels, width, height, true ); // gabor->setNi( 4, 6 ); // gabors.push_back( gabor ); // // gabor = new MP_Gabor( _pixels, width, height, true ); // gabor->setNi( 7, 9 ); // gabors.push_back( gabor ); } mp_threadedGabor::~mp_threadedGabor() { for( unsigned int i=0; i < gabors.size(); ++i ) { MP_Gabor *gabor = gabors[i]; delete gabor; } } /** Copies new pixels and resets several variables. * Not all intermediate arrays are cleared out but that shouldn't * affect the results if each step is done in order. * /param[in] newImage Currently this needs to be a 96x96 aligned face patch */ void mp_threadedGabor::setPixels( const RImage &newImage ) { MP_Gabor::setPixels( newImage ); for( unsigned int i=0; i < gabors.size(); ++i ) gabors[i]->setPixels( newImage ); } /** Main AU calculation. * This method duplicates the main AU processing in the CERT Matlab scripts. * Currently only works on 96x96 aligned face patches. * Fourier transform the face, convolve with 72 Gabor kernels, then multiply by * SVM weights. SVM weights are read in from external files. * Returns values for eight AU's: 1, 2, 4, 5, 10, 12, 14, 20 * Part of this method would probably be a good candidate for threading. */ std::vector mp_threadedGabor::CERT_Gabor() { std::vector< boost::thread *> threads; loadError = false; clearYhat(); loadAllSVMWeights(); normalize(); fft(); // create and start the threads for( unsigned int i=0; i < gabors.size(); ++i ) { MP_Gabor *tmp = gabors[i]; functorWrap gw( tmp ); tmp->setImgStar( img_star ); boost::thread *thr = new boost::thread( gw ); threads.push_back( thr ); } // Wait until they all finish for( unsigned int i=0; i < threads.size(); ++i ) { threads[i]->join(); delete threads[i]; } // collect all the results for( unsigned int i=0; i < gabors.size(); ++i ) { std::vector step = gabors[i]->getAU(); // Add the results to yhat transform( yhat.begin(), yhat.end(), step.begin(), yhat.begin(), std::plus() ); if( gabors[i]->getLoadError() ) loadError = true; } return yhat; }