In January 2021 we will introduce a 10 GB quota for project repositories. Higher limits for individual projects will be available on request. Please see https://doku.lrz.de/display/PUBLIC/GitLab for more information.

Commit c7ffa509 authored by Alessio Netti's avatar Alessio Netti

Analytics: changes to clustering plugin

- Implemented "means" REST API action
- Integrated the Mahalanobis distance outlier cut
parent 02fcff78
......@@ -60,6 +60,8 @@ restResponse_t ClusteringOperator::REST(const string& action, const unordered_ma
if(action=="train") {
resp.response = "Re-training triggered for gaussian mixture model " + this->_name + "!\n";
this->_trainingPending = true;
} else if(action=="means") {
resp.response = printMeans();
} else
throw invalid_argument("Unknown plugin action " + action + " requested!");
return resp;
......@@ -124,12 +126,19 @@ void ClusteringOperator::compute(U_Ptr unit) {
std::vector<std::shared_ptr<UnitTemplate<ClusteringSensorBase>>> subUnits = unit->getSubUnits();
cv::Vec2d res;
int64_t label;
bool outlier;
std::vector<cv::Mat> covs;
_gmm->getCovs(covs);
reading_t predict;
predict.timestamp = getTimestamp();
for(unsigned int idx=0; idx<subUnits.size(); idx++) {
res = _gmm->predict2(_currentfVector, cv::noArray());
predict.value = (int64_t)res[1];
res = _gmm->predict2(_trainingSet.row(idx), cv::noArray());
label = (int64_t)res[1];
outlier = isOutlier(_trainingSet.row(idx), _gmm->getMeans().row(label), covs[label]);
predict.value = outlier ? OUTLIER_ID : label;
subUnits[idx]->getOutputs()[0]->storeReading(predict);
}
}
......@@ -153,3 +162,26 @@ void ClusteringOperator::computeFeatureVector(U_Ptr unit) {
_currentfVector.at<float>(idx) = (float)_mean;
}
}
bool ClusteringOperator::isOutlier(cv::Mat vec1, cv::Mat vec2, cv::Mat cov) {
double dist = 0.0;
try {
cv::Mat iCov;
cv::invert(cov, iCov, cv::DECOMP_SVD);
dist = cv::Mahalanobis(vec1, vec2, iCov);
} catch(const std::exception& e) {
return false;
}
return dist > _outlierCut;
}
std::string ClusteringOperator::printMeans() {
std::ostringstream out;
if(_gmm.empty() || !_gmm->isTrained())
out << "Model is uninitialized or not trained.\n";
else {
for(size_t idx=0; idx<_gmm->getMeans().size().height; idx++)
out << "Component " << idx << " :" << _gmm->getMeans().row(idx) << "\n";
}
return out.str();
}
......@@ -79,6 +79,8 @@ protected:
virtual void compute(U_Ptr unit) override;
void computeFeatureVector(U_Ptr unit);
bool isOutlier(cv::Mat vec1, cv::Mat vec2, cv::Mat cov);
std::string printMeans();
std::string _modelOut;
std::string _modelIn;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment