pair<Emotion, float> EmoDetector::predictBestWinsOneVsAll(cv::Mat& frame) { pair<Emotion, float> best(UNKNOWN, numeric_limits<float>::min()); if (detectors_ext.size() == 0) { return make_pair(UNKNOWN, 0.0f); } for (map<string, pair<vector<Emotion>, Classifier*> >::iterator ii = this->detectors_ext.begin(); ii != this->detectors_ext.end(); ++ii) { if (ii->second.first.size() != 1) { continue; } Emotion emo = ii->second.first[0]; Classifier* cl = ii->second.second; float prediction = cl->predict(frame); if (best.second < prediction) { best.first = emo; best.second = prediction; } } return best; }
pair<Emotion, float> EmoDetector::predictVotingOneVsAllExt(cv::Mat& frame) { map<Emotion,float> votes; if (detectors_ext.size() == 0) { return make_pair(UNKNOWN, 0.0f); } votes.insert(make_pair(NEUTRAL, 0.f)); votes.insert(make_pair(CONTEMPT , 0.f)); votes.insert(make_pair(DISGUST, 0.f)); votes.insert(make_pair(SADNESS, 0.f)); votes.insert(make_pair(ANGER, 0.f)); votes.insert(make_pair(HAPPY, 0.f)); votes.insert(make_pair(FEAR, 0.f)); votes.insert(make_pair(UNKNOWN, 0.f)); for(map<string, pair<vector<Emotion>, Classifier*> >::iterator ii = this->detectors_ext.begin(); ii != this->detectors_ext.end(); ++ii) { vector<Emotion> emo = ii->second.first; // detected emotions Classifier* cl = ii->second.second; float prediction = cl->predict(frame); for(vector<Emotion>::iterator emo_it = emo.begin(); emo_it != emo.end(); ++emo_it) { map<Emotion, float>::iterator it = votes.find(*emo_it); if (it == votes.end()) { votes.insert(make_pair(*emo_it, prediction)); } else{ if (prediction > 0.5) { it->second += 0.5; //1.0; } else { //for(map<Emotion,float>::iterator votes_it = votes.begin(); votes_it != votes.end(); ++votes_it) { // vector<Emotion>::iterator e_it = find(emo.begin(), emo.end(), votes_it->first); // if (e_it == emo.end()) { // // if I dont find emotion in detected emotion // votes_it->second+=1.0; // } //} it->second -= 0.0; } } } } pair<Emotion,float> max_pair = make_pair(UNKNOWN, numeric_limits<float>::min()); for( map<Emotion, float>::iterator ii = votes.begin(); ii != votes.end(); ++ii) { if (ii->second > max_pair.second) { max_pair.first = ii->first; max_pair.second = ii->second; } } return max_pair; }
pair<Emotion, float> EmoDetector::predictMayorityOneVsAll(cv::Mat& frame){ map<Emotion,float> votes; if (detectors_ext.size() == 0) { return make_pair(UNKNOWN, 0.0f); } for (map<string, pair<vector<Emotion>, Classifier*> >::iterator ii = this->detectors_ext.begin(); ii != this->detectors_ext.end(); ++ii) { if (ii->second.first.size() != 1) { continue; } Emotion emo = ii->second.first[0]; Classifier* cl = ii->second.second; float prediction = cl->predict(frame); map<Emotion, float>::iterator it = votes.find(emo); if (it == votes.end()) { votes.insert(make_pair(emo, prediction)); } else { it->second+=prediction; } } pair<Emotion,float> max_pair = make_pair(UNKNOWN, numeric_limits<float>::min()); for( map<Emotion, float>::iterator ii=votes.begin(); ii!=votes.end(); ++ii){ if (ii->second > max_pair.second){ max_pair.first=ii->first; max_pair.second=ii->second; } } return max_pair; }
int main(void) { double data[][4] = { {3.5, 5.3, 0.2, -1.2}, {4.4, 2.2, 0.3, 0.4}, {1.3, 0.5, 4.1, 3.2} }; int labels[] = {1, 2, 3}; Classifier* logReg = new LogisticRegression(); logReg->epsilon = 1e-5; logReg->feedData<4>(data, 3, 4); logReg->feedLabels(labels, 3); // Get elapsed time in seconds tic(); logReg->train(); fprintf("Elapsed time: %.3f seconds.\n", toc()); fprintf("W:\n"); printMatrix(*logReg->W); fprintf("b:\n"); printVector(logReg->b, 3); // double** dataTest = data; double dataTest[][4] = { {3.5, 5.3, 0.2, -1.2}, {4.4, 2.2, 0.3, 0.4}, {1.3, 0.5, 4.1, 3.2} }; fprintf("Ground truth:\n"); printMatrix(*logReg->Y); fprintf("Predicted probability matrix:\n"); Matrix& Prob_pred = logReg->predictLabelScoreMatrix<4>(dataTest, 3, 4); disp(Prob_pred); fprintf("Predicted label matrix:\n"); Matrix& Y_pred = logReg->predictLabelMatrix<4>(dataTest, 3, 4); printMatrix(Y_pred); int* pred_labels = logReg->predict(dataTest, 3, 4); Classifier::getAccuracy(pred_labels, labels, 3); tic(); std::string trainDataFilePath = "heart_scale"; logReg = new LogisticRegression(); DataSet& dataSet = readDataSetFromFile(trainDataFilePath); logReg->feedData(*dataSet.X); logReg->feedLabels(dataSet.Y, dataSet.nExample); logReg->train(); Matrix& XTest = *dataSet.X; pred_labels = logReg->predict(XTest); int nt = XTest.getRowDimension(); Classifier::getAccuracy(pred_labels, logReg->labels, nt); fprintf("Elapsed time: %.3f seconds.\n", toc()); return EXIT_SUCCESS; }