20 #include <yarp/os/Log.h>
22 #include "classifierHandling.h"
26 void Classifier::push(
const Score &s)
29 if (window.size()>winLen)
35 void Classifier::push(
const bool isPositive)
38 s.isPositive=isPositive;
46 double Classifier::update()
51 bool posExitCond=
false;
52 bool negExitCond=
false;
53 for (
size_t i=0; i<window.size(); i++)
55 if (window[i].isPositive)
60 if (posExitCond && negExitCond)
64 double bestTheta=threshold;
65 if (posExitCond && negExitCond)
67 size_t minPenalty=window.size()+1;
69 for (
size_t i=0; i<window.size(); i++)
71 double theta=window[i].score;
74 for (
size_t j=0; j<window.size(); j++)
75 if ((window[j].isPositive && (window[j].score<theta)) ||
76 (!window[j].isPositive && (window[j].score>theta)))
79 if (penalty<=minPenalty)
92 void Classifier::init(
const double thres)
102 Classifier::Classifier(
const double thres)
109 Classifier::Classifier(
const Classifier &classifier)
111 name=classifier.name;
112 threshold=classifier.threshold;
113 newScore=classifier.newScore;
114 winLen=classifier.winLen;
115 window=classifier.window;
120 Classifier::Classifier(
const string &name,
const double thres)
128 Classifier::Classifier(
const Bottle &options)
135 bool Classifier::isThis(
const double val)
const
137 return (val>=threshold);
142 void Classifier::prepare(
const double newScore)
144 yInfo(
"Classifier %s: stored score %g",name.c_str(),newScore);
145 this->newScore=newScore;
150 void Classifier::declare(
const bool isPositive)
153 yInfo(
"Classifier %s: score %g declared as %s",name.c_str(),
154 newScore,isPositive?
"positive":
"negative");
156 double thresholdLatched=threshold;
158 yInfo(
"Updating threshold of classifier %s: %g => (%g) => %g",
159 name.c_str(),thresholdLatched,newScore,threshold);
164 void Classifier::negative()
171 void Classifier::positive()
178 void Classifier::fromBottle(
const Bottle &options)
182 if (options.check(
"name"))
183 name=options.find(
"name").asString();
185 if (options.check(
"threshold"))
186 threshold=options.find(
"threshold").asFloat64();
188 if (options.check(
"winLen"))
189 winLen=options.find(
"winLen").asInt32();
191 if (Bottle *item_list=options.find(
"items").asList())
193 for (
int i=0; i<item_list->size()-1; i+=2)
196 s.isPositive=(item_list->get(i).asString()==
"pos");
197 s.score=item_list->get(i+1).asFloat64();
205 Bottle Classifier::toBottle()
210 Bottle &name_list=options.addList();
211 name_list.addString(
"name");
212 name_list.addString(name);
215 Bottle &threshold_list=options.addList();
216 threshold_list.addString(
"threshold");
217 threshold_list.addFloat64(threshold);
220 Bottle &winLen_list=options.addList();
221 winLen_list.addString(
"winLen");
222 winLen_list.addInt32(winLen);
225 Bottle &window_list=options.addList();
226 window_list.addString(
"items");
227 Bottle &item_list=window_list.addList();
228 for (
size_t i=0; i<window.size(); i++)
230 item_list.addString(window[i].isPositive?
"pos":
"neg");
231 item_list.addFloat64(window[i].score);
239 ClassifiersDataBase::~ClassifiersDataBase()
246 void ClassifiersDataBase::clear()
248 for (
auto it=begin(); it!=end(); it++)
249 if (it->second!=NULL)
252 map<string,Classifier*>::clear();
257 void ClassifiersDataBase::erase(iterator it)
259 if (it->second!=NULL)
262 map<string,Classifier*>::erase(it);
267 int ClassifiersDataBase::processScores(Classifier *pClassifier,
268 const Bottle &scores)
271 double maxScoreObj=0.0;
273 for (
int i=0; i<scores.size(); i++)
278 double maxScoreNoObj=0.0;
281 Bottle *blobScores=scores.find(tag.str()).asList();
282 if (blobScores==NULL)
285 for (
int j=0; j<blobScores->size(); j++)
287 Bottle *item=blobScores->get(j).asList();
291 string name=item->get(0).asString();
292 double score=item->get(1).asFloat64();
294 if (name==pClassifier->getName())
296 if (pClassifier->isThis(score))
299 else if (score>=maxScoreNoObj)
303 if ((scoreObj>maxScoreNoObj) && (scoreObj>maxScoreObj))
305 pClassifier->prepare(maxScoreObj=scoreObj);
315 string ClassifiersDataBase::findName(
const Bottle &scores,
319 string retName=OBJECT_UNKNOWN;
320 if(score) *score = 0;
321 Bottle *blobScores=scores.find(tag).asList();
322 if (blobScores==NULL)
326 double maxScore=0.0;
int imax=0;
327 vector<double> s(blobScores->size(),-1.0);
328 for (
int i=0; i<blobScores->size(); i++)
330 Bottle *item=blobScores->get(i).asList();
334 string name=item->get(0).asString();
335 double score=item->get(1).asFloat64();
341 if (it->second->isThis(score) && (score>maxScore))
352 if (retName!=OBJECT_UNKNOWN)
354 for (
size_t i=0; i<s.size(); i++)
356 if ((i!=imax) && (s[i]>0.75*maxScore))
357 return OBJECT_UNKNOWN;
361 if(score) *score = maxScore;