/* * SVMPlugin * * Author: Andrew Salamon * * Copyright (c) 2008 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 "SVMPlugin.h" #include namespace CERT { SVMPlugin::SVMPlugin( ConfigItem *_config ) : PluginBase(_config) { std::vector temp = config->getValueForKey( "post" ); if( 1 == temp.size() ) post = temp[0]; temp = config->getValueForKey( "dir" ); if( 1 != temp.size() ) throw PluginExceptions::configError( "Missing directory in SVM config." ); weightsDir = temp[0]; labels = config->getValueForKey( "labels" ); for( unsigned int i = 0; i < labels.size(); ++i ) nans += "\tNaN"; } SVMPlugin::~SVMPlugin() { } PluginBase * SVMPlugin::copy() { return new SVMPlugin(*this); } bool SVMPlugin::areResultsValid() { return ( (results.size() > 0) && (results.size() == labels.size()) ); } std::string SVMPlugin::invalidResultsString() const { return nans; } void SVMPlugin::postProcess( const std::vector &_results ) { // check that results and labels are both the same size // See what the value of the 'post' property is and do that kind of post processing if( "softmax" == post ) results = postSoftmax( _results ); else results = _results; } std::vector SVMPlugin::postSoftmax( const std::vector &results ) { /* If the 6 emotion outputs for a single frame is Yout_em y=exp(Yout_em*k); (k is a constant which may need tweeking. Try k=10;) s=sum(y)+1; yout=y/s; */ static const double k = 10.0; double s = 0.0; std::vector newResults; for( unsigned int ind = 0; ind < results.size(); ++ind ) { double value = results[ind]; value = std::exp( value * k ); s += value; newResults.push_back(value); } for( unsigned int ind = 0; ind < newResults.size(); ++ind ) { double value = newResults[ind]; value /= s; newResults[ind] = value; } return newResults; } } // end namespace CERT