/* * Linear Emotion Emotions2 * * Author: Andrew Salamon * * Copyright (c) 2010 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 "Emotions2.h" #include #include #include #include #include #include #ifdef WIN32 #define _USE_MATH_DEFINES #include #else #include #endif #include // For basename and extension namespace bf = boost::filesystem; namespace mpt { Emotions2::Emotions2( const std::string &file11, const std::string &file8, const std::string &more, const std::string &thresh ) : acttype(SOFTMAX) //softmax(false), sigmoid(true) { if( !readMatrixFromFile( weights11, auCount, file11 ) || !readMatrixFromFile( weights8, addAuCount , file8 ) || !readMatrixFromFile( weightsMore, moreCount , more ) || !readMatrixFromFile( threshold, 1, thresh ) ) { clear(); std::cerr << "Unable to read one or more weights files in mpt::Emotions2" << std::endl; } } void Emotions2::clear() { weights11.resize( boost::extents[0][0] ); weights8.resize( boost::extents[0][0] ); threshold.resize( boost::extents[0][0] ); } void Emotions2::setActivation( activationtype _acttype ) { acttype = _acttype; } Emotions2::activationtype Emotions2::getActivation() { return acttype; } /* void Emotions2::setSoftmax( bool _softmax ) { if( softmax != _softmax ) { softmax = _softmax; if( softmax ) sigmoid = false; } } void Emotions2::setSigmoid( bool _sigmoid ) { if( sigmoid != _sigmoid ) { sigmoid = _sigmoid; if( sigmoid ) softmax = false; } } */ bool Emotions2::readMatrixFromFile( Matrix2d &matrix, unsigned int lineCount, const std::string &file ) { bf::path weightPath( file, bf::native ); std::string ext = bf::extension(weightPath); if( (".txt" == ext) || (".TXT" == ext) || (".text" == ext) || (".TEXT" == ext) ) { // read a plain text file, probably exported from Matlab with something like: // save emotions1.xml Wemo -ASCII std::ifstream weightsStream( file.c_str() ); std::string line; unsigned int lineCnt = 0; matrix.resize( boost::extents[lineCount][emotionCount] ); while( (lineCnt < lineCount) && std::getline( weightsStream, line ) ) { std::istringstream iss( line, std::istringstream::in ); float val; for( unsigned int cnt = 0; cnt < emotionCount; ++cnt ) { if( iss >> val ) { matrix[lineCnt][cnt] = val; } else { std::cerr << "Couldn't read emotion weight value: " << lineCnt << " x " << cnt << std::endl; std::cerr << "Line was: " << line << std::endl; } } ++lineCnt; } if( lineCount != lineCnt ) { std::cerr << "Wrong number of AUs in weights file: " << file << std::endl; return false; } } else { throw std::runtime_error( "Unsupported file type in Emotions2." ); } // printMatrix( weights, "Weights" ); return true; } /* load Wemo_cert4_4 u=(X11*W11)+(X8*W8)+threshold; u=exp(u); emotion=u/sum(u); */ std::vector Emotions2::calc( const std::vector &aus, const std::vector &add, const std::vector &more ) { if( (auCount != aus.size()) || (addAuCount != add.size() ) || (moreCount != more.size() ) || (auCount != weights11.shape()[0]) || (addAuCount != weights8.shape()[0]) || (moreCount != weightsMore.shape()[0]) ) { // If inputs are correct, then this probably means the weights weren't read in correctly. std::vector res; return res; } // Convert the inputs to matrixes Matrix2d X11; X11.resize( boost::extents[1][auCount] ); for( unsigned int cnt = 0; cnt < auCount; ++cnt ) X11[0][cnt] = aus[cnt]; Matrix2d X8; X8.resize( boost::extents[1][addAuCount] ); for( unsigned int cnt = 0; cnt < addAuCount; ++cnt ) X8[0][cnt] = add[cnt]; Matrix2d Xmore; Xmore.resize( boost::extents[1][moreCount] ); for( unsigned int cnt = 0; cnt < moreCount; ++cnt ) Xmore[0][cnt] = more[cnt]; // printMatrix( X11, "11 AUs" ); // printMatrix( X8, "8 AUs" ); // printMatrix( Xmore, "more AUs" ); Matrix2d outputs1; SimpleMatrixOps::multiply( X11, weights11, outputs1 ); Matrix2d outputs2; SimpleMatrixOps::multiply( X8, weights8, outputs2 ); Matrix2d outputs3; SimpleMatrixOps::multiply( Xmore, weightsMore, outputs3 ); // printMatrix( outputs1, "Outputs11" ); // printMatrix( outputs2, "Outputs8" ); // printMatrix( outputs3, "Outputs3" ); std::vector res( emotionCount, 0.0 ); if( acttype==SOFTMAX ) { float sum = 0.0; for( unsigned int idx = 0; idx < emotionCount; ++idx ) { float tmp = exp( outputs1[0][idx] + outputs2[0][idx] + outputs3[0][idx] + threshold[0][idx] ); res[idx] = tmp; sum += tmp; } for( unsigned int idx = 0; idx < emotionCount; ++idx ) { res[idx] /= sum; } } else if( acttype==SIGMOID ) { for( unsigned int idx = 0; idx < emotionCount; ++idx ) { float tmp = outputs1[0][idx] + outputs2[0][idx] + outputs3[0][idx] + threshold[0][idx]; res[idx] = 1.0 / (1.0 + exp( -tmp ) ); } } else { for( unsigned int idx = 0; idx < emotionCount; ++idx ) { res[idx] = outputs1[0][idx] + outputs2[0][idx] + outputs3[0][idx] + threshold[0][idx]; } } return res; } void printMatrix( const Matrix2d &matrix, const std::string &title ) { if( title.length() > 0 ) std::cout << title << ":" << std::endl; for( unsigned int row = 0; row < matrix.shape()[0]; ++row ) { for( unsigned int col = 0; col < matrix.shape()[1]; ++col ) { if( title.length() > 0 ) std::cout << " "; std::cout << matrix[row][col]; if( col < (matrix.shape()[1] - 1) ) std::cout << " "; } std::cout << std::endl; } } } // end namespace mpt