Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/Confidence.cpp
Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 1998-2010 George Tzanetakis <gtzan@cs.uvic.ca>
00003 **
00004 ** This program is free software; you can redistribute it and/or modify
00005 ** it under the terms of the GNU General Public License as published by
00006 ** the Free Software Foundation; either version 2 of the License, or
00007 ** (at your option) any later version.
00008 **
00009 ** This program is distributed in the hope that it will be useful,
00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 ** GNU General Public License for more details.
00013 **
00014 ** You should have received a copy of the GNU General Public License
00015 ** along with this program; if not, write to the Free Software
00016 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 */
00018 
00019 #include "Confidence.h"
00020 #include "../common_source.h"
00021 #include <marsyas/FileName.h>
00022 
00023 
00024 using std::ostringstream;
00025 using std::cout;
00026 using std::endl;
00027 using std::setprecision;
00028 using std::fixed;
00029 using std::ios;
00030 
00031 
00032 
00033 using namespace Marsyas;
00034 
00035 Confidence::Confidence(mrs_string name):MarSystem("Confidence",name)
00036 {
00037   print_ = false;
00038   forcePrint_ = false;
00039   csvOutput_ = false;
00040 
00041   predictions_ = 0;
00042   count_ = 0;
00043   write_=0;
00044   oriName_ = "MARSYAS_EMPTY";
00045   addControls();
00046 }
00047 
00048 Confidence::Confidence(const Confidence& a):MarSystem(a)
00049 {
00050   ctrl_memSize_ = getctrl("mrs_natural/memSize");
00051   ctrl_nLabels_ = getctrl("mrs_natural/nLabels");
00052   count_ = 0;
00053   print_ = false;
00054   csvOutput_ = false;
00055   forcePrint_ = false;
00056   write_=0;
00057   oriName_ = "MARSYAS_EMPTY";
00058 }
00059 
00060 Confidence::~Confidence()
00061 {
00062 }
00063 
00064 
00065 MarSystem*
00066 Confidence::clone() const
00067 {
00068   return new Confidence(*this);
00069 }
00070 
00071 void
00072 Confidence::addControls()
00073 {
00074   addctrl("mrs_natural/memSize", 40, ctrl_memSize_);
00075   addctrl("mrs_natural/nLabels", 2, ctrl_nLabels_);
00076   setctrlState("mrs_natural/nLabels", true);
00077   addctrl("mrs_string/labelNames", "Music,Speech");
00078   setctrlState("mrs_string/labelNames", true);
00079   addctrl("mrs_bool/print", false);
00080   setctrlState("mrs_bool/print", true);
00081   addctrl("mrs_bool/forcePrint", false);
00082   setctrlState("mrs_bool/forcePrint", true);
00083   addctrl("mrs_string/fileName", "MARSYAS_EMPTY");
00084   setctrlState("mrs_string/fileName", true);
00085   addctrl("mrs_natural/write", 0);
00086   setctrlState("mrs_natural/write", true);
00087   addctrl("mrs_natural/hopSize", 512);
00088   setctrlState("mrs_natural/hopSize", true);
00089   addctrl("mrs_bool/fileOutput", false);
00090   setctrlState("mrs_bool/fileOutput", true);
00091   addctrl("mrs_bool/csvOutput", false);
00092   setctrlState("mrs_bool/csvOutput", true);
00093 }
00094 
00095 void
00096 Confidence::myUpdate(MarControlPtr sender)
00097 {
00098   (void) sender;  //suppress warning of unused parameter(s)
00099   MRSDIAG("Confidence.cpp - Confidence:myUpdate");
00100   setctrl("mrs_natural/onSamples", getctrl("mrs_natural/inSamples"));
00101   setctrl("mrs_natural/onObservations", getctrl("mrs_natural/inObservations"));
00102   setctrl("mrs_real/osrate", getctrl("mrs_real/israte"));
00103 
00104   num_labels_ = getctrl("mrs_natural/nLabels")->to<mrs_natural>();
00105   confidences_.stretch(num_labels_);
00106   gtconfidences_.stretch(num_labels_+1);
00107 
00108   mrs_string labelNames = getctrl("mrs_string/labelNames")->to<mrs_string>();
00109 
00110   labelNames_.clear();
00111 
00112   print_ = getctrl("mrs_bool/print")->to<mrs_bool>();
00113   forcePrint_ = getctrl("mrs_bool/forcePrint")->to<mrs_bool>();
00114   csvOutput_ = getctrl("mrs_bool/csvOutput")->to<mrs_bool>();
00115 
00116   for (mrs_natural i = 0; i < getctrl("mrs_natural/nLabels")->to<mrs_natural>(); ++i)
00117   {
00118     mrs_string labelName;
00119     mrs_string temp;
00120 
00121     labelName = labelNames.substr(0, labelNames.find(","));
00122     temp = labelNames.substr(labelNames.find(",")+1, labelNames.length());
00123     labelNames = temp;
00124     labelNames_.push_back(labelName);
00125   }
00126 
00127   if (getctrl("mrs_bool/fileOutput")->to<mrs_bool>())
00128   {
00129     if(getctrl("mrs_string/fileName")->to<mrs_string>().compare(oriName_))
00130     {
00131       if(write_)
00132       {
00133         outputFileSyn_.close();
00134         outputFileTran_.close();
00135       }
00136       oriName_ = getctrl("mrs_string/fileName")->to<mrs_string>();
00137       FileName Sfname(oriName_);
00138       mrs_string tmp = Sfname.nameNoExt() +"_synSeg.txt";
00139       //      getchar();
00140       outputFileSyn_.open(tmp.c_str(), ios::out);
00141       tmp = Sfname.nameNoExt() +"_tranSeg.txt";
00142       outputFileTran_.open(tmp.c_str(), ios::out);
00143       write_ = 1;
00144     }
00145   }
00146   hopDuration_ = getctrl("mrs_natural/inSamples")->to<mrs_natural>() / getctrl("mrs_real/osrate")->to<mrs_real>();
00147   nbFrames_ = -getctrl("mrs_natural/memSize")->to<mrs_natural>()+1;
00148   nbCorrectFrames_ = 0;
00149   lastLabel_ = "MARSYAS_EMPTY";
00150 }
00151 
00152 void
00153 Confidence::myProcess(realvec& in, realvec& out)
00154 {
00155   mrs_natural o,t;
00156 
00157   bool mute = ctrl_mute_->to<mrs_bool>();
00158   mrs_natural memSize = ctrl_memSize_->to<mrs_natural>();
00159   mrs_natural nLabels = ctrl_nLabels_->to<mrs_natural>();
00160 
00161   mrs_natural label;
00162   mrs_natural gtlabel;
00163   mrs_natural l;
00164 
00165 
00166   if (mute == false)
00167   {
00168     for (o=0; o < inObservations_; o++)
00169       for (t = 0; t < inSamples_; t++)
00170       {
00171         out(o,t) = in(o,t);
00172         if (o==0)
00173         {
00174           label = (mrs_natural)in(0,t);
00175           confidences_(label) = confidences_(label) + 1;
00176           gtlabel = (mrs_natural)in(1,t);
00177           if (gtlabel < 0) {
00178             gtlabel = num_labels_;
00179           }
00180           gtconfidences_(gtlabel) = gtconfidences_(gtlabel)+1;
00181         }
00182       }
00183     count_++;
00184     bool cond = ((count_ % memSize) == 0);
00185 
00186     if (cond || forcePrint_)
00187     {
00188       mrs_real max_conf = 0;
00189       mrs_natural max_l = 0;
00190       mrs_real max_gtconf = 0;
00191       mrs_natural max_gtl = 0;
00192       for (l=0; l < nLabels; l++)
00193       {
00194         mrs_real conf = ((confidences_(l)) / count_);
00195         if (conf > max_conf)
00196         {
00197           max_conf = conf;
00198           max_l = l;
00199         }
00200       }
00201       mrs_string ground_truth_text;
00202       for (l=0; l < nLabels+1; l++)
00203       {
00204         mrs_real gtconf = ((gtconfidences_(l)) / count_);
00205         //cout<<"gtconf "<<gtconf<<endl;
00206         if (gtconf > max_gtconf)
00207         {
00208           max_gtconf = gtconf;
00209           max_gtl = l;
00210         }
00211       }
00212       if (max_gtl < num_labels_) {
00213         ground_truth_text = labelNames_[max_gtl];
00214       } else {
00215         ground_truth_text = "---";
00216       }
00217 
00218       if (getctrl("mrs_bool/fileOutput")->to<mrs_bool>())
00219       {
00220         cout << "fileOutput" << endl;
00221 
00222         if (write_)
00223         {
00224           outputFileSyn_ << fixed << setprecision(3) << nbFrames_*hopDuration_ << "\t";
00225           outputFileSyn_ << setprecision(0) << labelNames_[max_l] << "\t" <<
00226                          ((confidences_(max_l) / count_)) * 100.0 << endl;
00227 
00228           if(lastLabel_ == "MARSYAS_EMPTY" || lastLabel_ != labelNames_[max_l])
00229           {
00230             outputFileTran_ << fixed << setprecision(3) << nbFrames_*hopDuration_ << "\t" << labelNames_[max_l] << endl;
00231             lastLabel_ = labelNames_[max_l];
00232           }
00233         }
00234       }
00235       else
00236       {
00237         if (print_)
00238         {
00239           if (max_l == max_gtl)
00240           {
00241             nbCorrectFrames_ ++;
00242           }
00243 
00244           if (csvOutput_)
00245           {
00246             cout << fixed << setprecision(3) << nbFrames_*hopDuration_ << "\t";
00247             cout << fixed << setprecision(3) << ((nbFrames_+memSize)*hopDuration_) - 0.001 << "\t";
00248             cout << fixed << setprecision(0) << labelNames_[max_l] << "\t";
00249             cout << fixed << setprecision(3) << ((confidences_(max_l) / count_)) << endl;
00250           } else {
00251             cout << fixed << setprecision(3) << nbFrames_*hopDuration_ << "\t";
00252             cout << fixed << setprecision(0) << "PR = " << labelNames_[max_l] << "\t" <<
00253                  ((confidences_(max_l) / count_)) * 100.0 << setprecision(4) << "\t" << nbCorrectFrames_ * 1.0 / (nbFrames_/memSize+1);
00254             cout << "\t GT = " << ground_truth_text << endl;
00255           }
00256         }
00257 
00258       }
00259       if (cond || forcePrint_)
00260       {
00261         count_ = 0;
00262       }
00263 
00264       confidences_.setval(0.0);
00265       gtconfidences_.setval(0.0);
00266 
00267     }
00268   }
00269   nbFrames_++;
00270 }