Commit 9047d863 authored by Michal Balazia's avatar Michal Balazia
Browse files

classifier linear

parent 8ee9ed78
#Wed, 12 Oct 2016 16:31:41 +0200
#Wed, 12 Oct 2016 19:10:42 +0200
D\:\\School\\PHD\\PROJECTS\\IMPLEMENTATION\\GaitRecognition=
......@@ -11,7 +11,7 @@ public abstract class Classifier implements Serializable {
private DistanceTemplates distanceTemplates;
private DistanceMatrix distanceMatrix;
private Matrix transformationMatrix;
public Classifier() {
}
......@@ -31,7 +31,6 @@ public abstract class Classifier implements Serializable {
this.distanceMatrix = distanceMatrix;
}
public Matrix getTransformationMatrix() {
return transformationMatrix;
}
......@@ -41,4 +40,8 @@ public abstract class Classifier implements Serializable {
}
public abstract int classify(Template templateQuery, List<Template> templatesGallery);
public double getDistance(Template template1, Template template2) {
return distanceMatrix == null ? distanceTemplates.getDistance(template1, template2) : distanceMatrix.getDistance(template1, template2);
}
}
package algorithms;
import java.util.List;
import objects.DistanceMatrix;
import objects.Template;
public class Classifier1NN extends Classifier {
@Override
public int classify(Template templateQuery, List<Template> templatesGallery) {
// get k closest descriptors
Template closestTemplate = new Template(0, "", null);
DistanceTemplates distanceTemplates = getDistanceTemplates();
DistanceMatrix distanceMatrix = getDistanceMatrix();
double minDistance = Double.MAX_VALUE;
for (Template template : templatesGallery) {
double distance = getDistanceMatrix() == null ? distanceTemplates.getDistance(template, templateQuery) : distanceMatrix.getDistance(template, templateQuery);
double distance = getDistance(template, templateQuery);
if (minDistance > distance) {
minDistance = distance;
closestTemplate = template;
......
......@@ -5,7 +5,6 @@ import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import objects.DistanceMatrix;
import objects.Template;
public class ClassifierKNNMV extends Classifier {
......@@ -21,11 +20,9 @@ public class ClassifierKNNMV extends Classifier {
// get k closest descriptors
Map<Double, Template> kClosestTemplates = new HashMap(k);
DistanceTemplates distanceTemplates = getDistanceTemplates();
DistanceMatrix distanceMatrix = getDistanceMatrix();
double maxDistance = 0;
for (Template template : templatesGallery) {
double distance = getDistanceMatrix() == null ? distanceTemplates.getDistance(template, templateQuery) : distanceMatrix.getDistance(template, templateQuery);
double distance = getDistance(template, templateQuery);
if (kClosestTemplates.size() < k || distance < maxDistance) {
kClosestTemplates.put(distance, template);
if (kClosestTemplates.size() > k) {
......
package algorithms;
import java.util.ArrayList;
import java.util.List;
import objects.ClassifiableList;
import objects.Template;
public class ClassifierLinear extends Classifier {
@Override
public int classify(Template templateQuery, List<Template> templatesGallery) {
List<Template> centroids = new ArrayList();
List<List<Template>> templatesByClass = new ClassifiableList(templatesGallery).splitClasses();
for (List<Template> templatesOfClass : templatesByClass) {
Template centroid = new Template(0, "", null);
if (templatesOfClass.isEmpty()) {
System.out.println("empty class");
} else {
double minSum = Double.MAX_VALUE;
for (Template centroidCandidate : templatesOfClass) {
double sum = 0;
for (Template template : templatesOfClass) {
sum += getDistance(template, centroidCandidate);
}
if (minSum > sum) {
minSum = sum;
centroid = centroidCandidate;
}
}
}
centroids.add(centroid);
}
Template closestCentroid = new Template(0, "", null);
double minDistance = Double.MAX_VALUE;
for (Template centroid : centroids) {
double distance = getDistance(centroid, templateQuery);
if (minDistance > distance) {
minDistance = distance;
closestCentroid = centroid;
}
}
return closestCentroid.getSubject();
}
}
......@@ -13,21 +13,17 @@ public abstract class Retriever implements Serializable {
public Retriever() {
}
public DistanceTemplates getDistanceTemplates() {
return distanceTemplates;
}
public void setDistanceTemplates(DistanceTemplates distanceTemplates) {
this.distanceTemplates = distanceTemplates;
}
public DistanceMatrix getDistanceMatrix() {
return distanceMatrix;
}
public void setDistanceMatrix(DistanceMatrix distanceMatrix) {
this.distanceMatrix = distanceMatrix;
}
public abstract List<Template> retrieve(Template sampleTest, List<Template> samplesGallery);
public double getDistance(Template template1, Template template2) {
return distanceMatrix == null ? distanceTemplates.getDistance(template1, template2) : distanceMatrix.getDistance(template1, template2);
}
}
......@@ -5,7 +5,6 @@ import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import objects.DistanceMatrix;
import objects.Template;
public class RetrieverKNNQuery extends Retriever {
......@@ -18,12 +17,10 @@ public class RetrieverKNNQuery extends Retriever {
@Override
public List<Template> retrieve(Template templateTest, List<Template> templatesGallery) {
DistanceTemplates distanceTemplates = getDistanceTemplates();
DistanceMatrix distanceMatrix = getDistanceMatrix();
Map<Double, Template> kClosestTemplates = new HashMap(k);
double maxDistance = 0;
for (Template template : templatesGallery) {
double distance = getDistanceMatrix() == null ? distanceTemplates.getDistance(template, templateTest) : distanceMatrix.getDistance(template, templateTest);
double distance = getDistance(template, templateTest);
if (kClosestTemplates.size() < k || distance < maxDistance) {
kClosestTemplates.put(distance, template);
if (kClosestTemplates.size() > k) {
......
Supports Markdown
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