/* * 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 #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. * Create multiple Gabors, giving each one a range of kernels to work on. * Three Gabors seems to be optimal, at least on a quad G5. */ 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 ); } } mp_threadedGabor::~mp_threadedGabor() { for( unsigned int i=0; i < gabors.size(); ++i ) { MP_Gabor *gabor = gabors[i]; delete gabor; } } /** Sets the new image for each of the Gabors. * /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 ); } void mp_threadedGabor::setCategories( std::vector< std::string > &_categories ) { MP_Gabor::setCategories( _categories ); for( unsigned int i=0; i < gabors.size(); ++i ) gabors[i]->setCategories( _categories ); } void mp_threadedGabor::clearYhat() { MP_Gabor::clearYhat(); for( unsigned int i=0; i < gabors.size(); ++i ) gabors[i]->clearYhat(); } /** Main AU calculation. * Create one Boost thread for each Gabor. * Each Gabor is wrapped in a simple wrapper so it won't be deleted when the * thread is finished. * Run all threads and accumulated all of the results. */ bool mp_threadedGabor::CERT_Gabor() { loadError = false; clearYhat(); // Don't do anything if we don't have any SVM categories to run. if( categories.size() > 0 ) { boost::thread_group grp; 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 ); grp.create_thread( gw ); // creates a thread and starts running it. } grp.join_all(); // Wait for all threads to finish // collect all the results for( unsigned int i=0; i < gabors.size(); ++i ) { // Accumulate results for each of the SVM categories we processed. for( std::vector< std::string >::iterator iter = categories.begin(), lastIter = categories.end(); iter != lastIter; ++iter ) { std::vector accum = catResults[ *iter ]; std::vector step = gabors[i]->resultsForCategory( *iter ); transform( accum.begin(), accum.end(), step.begin(), accum.begin(), std::plus() ); catResults[ *iter ] = accum; frequencies[ *iter ] += gabors[i]->frequenciesForCategory( *iter ); } if( gabors[i]->getLoadError() ) loadError = true; } #ifdef DEBUG for( std::vector< std::string >::iterator iter = categories.begin(), lastIter = categories.end(); iter != lastIter; ++iter ) { std::string filename = "/tmp/" + *iter + "_results."; filename += mp_AUResults::extensionForFiletype( mp_AUResults::xml ); std::ofstream ofs( filename.c_str() ); if( ofs.good() ) frequencies[ *iter ].save( ofs ); } #endif } return !loadError; }