Commit 8dba64925f1ea09e718881c7ef6e5858131aee54
1 parent
602042ab
removed knn
Showing
1 changed file
with
0 additions
and
100 deletions
openbr/plugins/cluster/knn.cpp deleted
| 1 | -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | ||
| 2 | - * Copyright 2012 The MITRE Corporation * | ||
| 3 | - * * | ||
| 4 | - * Licensed under the Apache License, Version 2.0 (the "License"); * | ||
| 5 | - * you may not use this file except in compliance with the License. * | ||
| 6 | - * You may obtain a copy of the License at * | ||
| 7 | - * * | ||
| 8 | - * http://www.apache.org/licenses/LICENSE-2.0 * | ||
| 9 | - * * | ||
| 10 | - * Unless required by applicable law or agreed to in writing, software * | ||
| 11 | - * distributed under the License is distributed on an "AS IS" BASIS, * | ||
| 12 | - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * | ||
| 13 | - * See the License for the specific language governing permissions and * | ||
| 14 | - * limitations under the License. * | ||
| 15 | - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | ||
| 16 | - | ||
| 17 | -#include <openbr/plugins/openbr_internal.h> | ||
| 18 | -#include <openbr/core/common.h> | ||
| 19 | - | ||
| 20 | -using namespace cv; | ||
| 21 | - | ||
| 22 | -namespace br | ||
| 23 | -{ | ||
| 24 | - | ||
| 25 | -/*! | ||
| 26 | - * \ingroup transforms | ||
| 27 | - * \brief K nearest neighbors classifier. | ||
| 28 | - * \author Josh Klontz \cite jklontz | ||
| 29 | - */ | ||
| 30 | -class KNNTransform : public Transform | ||
| 31 | -{ | ||
| 32 | - Q_OBJECT | ||
| 33 | - Q_PROPERTY(int k READ get_k WRITE set_k RESET reset_k STORED false) | ||
| 34 | - Q_PROPERTY(br::Distance *distance READ get_distance WRITE set_distance RESET reset_distance STORED false) | ||
| 35 | - Q_PROPERTY(bool weighted READ get_weighted WRITE set_weighted RESET reset_weighted STORED false) | ||
| 36 | - Q_PROPERTY(int numSubjects READ get_numSubjects WRITE set_numSubjects RESET reset_numSubjects STORED false) | ||
| 37 | - Q_PROPERTY(QString inputVariable READ get_inputVariable WRITE set_inputVariable RESET reset_inputVariable STORED false) | ||
| 38 | - Q_PROPERTY(QString outputVariable READ get_outputVariable WRITE set_outputVariable RESET reset_outputVariable STORED false) | ||
| 39 | - Q_PROPERTY(QString galleryName READ get_galleryName WRITE set_galleryName RESET reset_galleryName STORED false) | ||
| 40 | - BR_PROPERTY(int, k, 1) | ||
| 41 | - BR_PROPERTY(br::Distance*, distance, NULL) | ||
| 42 | - BR_PROPERTY(bool, weighted, false) | ||
| 43 | - BR_PROPERTY(int, numSubjects, 1) | ||
| 44 | - BR_PROPERTY(QString, inputVariable, "Label") | ||
| 45 | - BR_PROPERTY(QString, outputVariable, "KNN") | ||
| 46 | - BR_PROPERTY(QString, galleryName, "") | ||
| 47 | - | ||
| 48 | - TemplateList gallery; | ||
| 49 | - | ||
| 50 | - void train(const TemplateList &data) | ||
| 51 | - { | ||
| 52 | - distance->train(data); | ||
| 53 | - gallery = data; | ||
| 54 | - } | ||
| 55 | - | ||
| 56 | - void project(const Template &src, Template &dst) const | ||
| 57 | - { | ||
| 58 | - QList< QPair<float, int> > sortedScores = Common::Sort(distance->compare(gallery, src), true); | ||
| 59 | - | ||
| 60 | - QStringList subjects; | ||
| 61 | - for (int i=0; i<numSubjects; i++) { | ||
| 62 | - QHash<QString, float> votes; | ||
| 63 | - const int max = (k < 1) ? sortedScores.size() : std::min(k, sortedScores.size()); | ||
| 64 | - for (int j=0; j<max; j++) | ||
| 65 | - votes[gallery[sortedScores[j].second].file.get<QString>(inputVariable)] += (weighted ? sortedScores[j].first : 1); | ||
| 66 | - subjects.append(votes.keys()[votes.values().indexOf(Common::Max(votes.values()))]); | ||
| 67 | - | ||
| 68 | - // Remove subject from consideration | ||
| 69 | - if (subjects.size() < numSubjects) | ||
| 70 | - for (int j=sortedScores.size()-1; j>=0; j--) | ||
| 71 | - if (gallery[sortedScores[j].second].file.get<QString>(inputVariable) == subjects.last()) | ||
| 72 | - sortedScores.removeAt(j); | ||
| 73 | - } | ||
| 74 | - | ||
| 75 | - dst.file.set(outputVariable, subjects.size() > 1 ? "[" + subjects.join(",") + "]" : subjects.first()); | ||
| 76 | - dst.file.set("Nearest", gallery[sortedScores[0].second].file.name); | ||
| 77 | - } | ||
| 78 | - | ||
| 79 | - void store(QDataStream &stream) const | ||
| 80 | - { | ||
| 81 | - stream << gallery; | ||
| 82 | - } | ||
| 83 | - | ||
| 84 | - void load(QDataStream &stream) | ||
| 85 | - { | ||
| 86 | - stream >> gallery; | ||
| 87 | - } | ||
| 88 | - | ||
| 89 | - void init() | ||
| 90 | - { | ||
| 91 | - if (!galleryName.isEmpty()) | ||
| 92 | - gallery = TemplateList::fromGallery(galleryName); | ||
| 93 | - } | ||
| 94 | -}; | ||
| 95 | - | ||
| 96 | -BR_REGISTER(Transform, KNNTransform) | ||
| 97 | - | ||
| 98 | -} // namespace br | ||
| 99 | - | ||
| 100 | -#include "cluster/knn.moc" |