-
Notifications
You must be signed in to change notification settings - Fork 5
/
ComputeTrainedScores.java
144 lines (123 loc) · 5.46 KB
/
ComputeTrainedScores.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package org.genericsystem.cv.comparator;
import java.util.ArrayList;
import java.util.List;
import org.genericsystem.common.Generic;
import org.genericsystem.common.Root;
import org.genericsystem.cv.Levenshtein;
import org.genericsystem.cv.model.Doc;
import org.genericsystem.cv.model.Doc.DocInstance;
import org.genericsystem.cv.model.DocClass;
import org.genericsystem.cv.model.ImgFilter;
import org.genericsystem.cv.model.ImgFilter.ImgFilterInstance;
import org.genericsystem.cv.model.MeanLevenshtein;
import org.genericsystem.cv.model.Score;
import org.genericsystem.cv.model.Score.ScoreInstance;
import org.genericsystem.cv.model.ZoneGeneric;
import org.genericsystem.cv.model.ZoneGeneric.ZoneInstance;
import org.genericsystem.cv.model.ZoneText;
import org.genericsystem.cv.model.ZoneText.ZoneTextInstance;
import org.genericsystem.kernel.Engine;
import org.opencv.core.Core;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The ComputeTrainedScores class computes the {@link Score} and the
* {@link MeanLevenshtein} for each zone and each filter.
*
* The data is retrieved from GS, and stored in GS.
*
* @author Pierrik Lassalas
*
*/
public class ComputeTrainedScores {
private final static String gsPath = System.getenv("HOME") + "/genericsystem/gs-cv_model3/";
private static Logger log = LoggerFactory.getLogger(ComputeTrainedScores.class);
public static void main(String[] mainArgs) {
final Engine engine = new Engine(gsPath, Doc.class, ImgFilter.class, ZoneGeneric.class, ZoneText.class,
Score.class, MeanLevenshtein.class);
engine.newCache().start();
compute(engine);
engine.close();
}
public static void compute(Root engine) {
final String docType = "id-fr-front";
compute(engine, docType);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void compute(Root engine, String docType) {
Generic currentDocClass = engine.find(DocClass.class).getInstance(docType);
ImgFilter imgFilter = engine.find(ImgFilter.class);
ZoneText zoneText = engine.find(ZoneText.class);
Score score = engine.find(Score.class);
MeanLevenshtein meanLevenshtein = engine.find(MeanLevenshtein.class);
log.info("Current doc class : {} ", currentDocClass);
List<DocInstance> docInstances = (List) currentDocClass.getHolders(engine.find(Doc.class)).toList();
List<ZoneInstance> zoneInstances = (List) currentDocClass.getHolders(engine.find(ZoneGeneric.class)).toList();
List<ImgFilterInstance> imgFilterInstances = (List) imgFilter.getInstances()
.filter(f -> !"reality".equals(f.getValue()) && !"best".equals(f.getValue())).toList();
ImgFilterInstance realityInstance = imgFilter.getImgFilter("reality");
// Loop over all zone instances
for (ZoneInstance zoneInstance : zoneInstances) {
log.info("=> Zone {}", zoneInstance);
List<Float> meanLevDistances = new ArrayList<Float>();
List<Float> probabilities = new ArrayList<Float>();
// Loop over all filter instances
for (ImgFilterInstance imgFilterInstance : imgFilterInstances) {
int lev = 0; // contains the sum of all Levenshtein
// distances for a given zone
int count = 0; // contains the number of "perfect" matches
int totalDocs = docInstances.size(); // contains the number of
// documents
// Loop over all documents in this class
for (DocInstance docInstance : docInstances) {
ZoneTextInstance realZti = zoneText.getZoneText(docInstance, zoneInstance, realityInstance);
// Do not attempt the computation if the document was not
// supervised
if (realZti == null) {
log.debug("Document {} on zone {} was not supervised (passed)", docInstance.getValue(),
zoneInstance.getValue());
totalDocs--; // Decrement the total size, since this
// value will not be accounted for in
// the statistics
} else {
String realText = (String) realZti.getValue();
ZoneTextInstance zti = zoneText.getZoneText(docInstance, zoneInstance, imgFilterInstance);
// Do not proceed if the zoneText does not exists (i.e.,
// the algorithm was not applied to this image)
if (zti == null) {
log.debug("No text found for {} => zone n°{}, {}", docInstance.getValue(),
zoneInstance.getValue(), imgFilterInstance.getValue());
totalDocs--; // Decrement the total size, since this
// value will not be accounted for
// in the statistics
} else {
String text = (String) zti.getValue();
// TODO : manipulate the Strings before comparison?
int dist = Levenshtein.distance(text.replaceAll("[\n ,.]", "").trim(),
realText.replaceAll("[\n ,.]", "").trim());
count += (dist == 0) ? 1 : 0;
lev += dist;
}
}
}
if (totalDocs > 0) {
float probability = (float) count / (float) totalDocs;
float meanDistance = (float) lev / (float) totalDocs;
ScoreInstance scoreInstance = score.setScore(probability, zoneInstance, imgFilterInstance);
meanLevenshtein.setMeanLev(meanDistance, scoreInstance);
engine.getCurrentCache().flush();
meanLevDistances.add(meanDistance);
probabilities.add(probability);
} else {
log.error("An error has occured while processing the score computation of zone n°{} (class: {})",
zoneInstance.getValue(), docType);
}
}
engine.getCurrentCache().flush();
for (int i = 0; i < imgFilterInstances.size(); i++) {
log.info("{}: {} (meanLev: {})", imgFilterInstances.get(i), probabilities.get(i),
meanLevDistances.get(i));
}
}
}
}