Commit 58f925199adfe0b255eb9ac8e54dd326c955e7b0

Authored by Brendan Klare
1 parent 598a4160

Moving positive and negative sample extraction from SlidingWindow to BuildScales…

… to keep scale consistent
Showing 1 changed file with 75 additions and 75 deletions
openbr/plugins/slidingwindow.cpp
... ... @@ -40,21 +40,13 @@ class SlidingWindowTransform : public Transform
40 40 {
41 41 Q_OBJECT
42 42 Q_PROPERTY(br::Transform *transform READ get_transform WRITE set_transform RESET reset_transform STORED false)
43   - Q_PROPERTY(int minSize READ get_minSize WRITE set_minSize RESET reset_minSize STORED false)
44 43 Q_PROPERTY(int stepSize READ get_stepSize WRITE set_stepSize RESET reset_stepSize STORED false)
45 44 Q_PROPERTY(bool takeFirst READ get_takeFirst WRITE set_takeFirst RESET reset_takeFirst STORED false)
46   - Q_PROPERTY(bool negSamples READ get_negSamples WRITE set_negSamples RESET reset_negSamples STORED false)
47   - Q_PROPERTY(int negToPosRatio READ get_negToPosRatio WRITE set_negToPosRatio RESET reset_negToPosRatio STORED false)
48   - Q_PROPERTY(double maxOverlap READ get_maxOverlap WRITE set_maxOverlap RESET reset_maxOverlap STORED false)
49 45 Q_PROPERTY(int windowWidth READ get_windowWidth WRITE set_windowWidth RESET reset_windowWidth STORED false)
50 46 Q_PROPERTY(float threshold READ get_threshold WRITE set_threshold RESET reset_threshold STORED false)
51 47 BR_PROPERTY(br::Transform *, transform, NULL)
52   - BR_PROPERTY(int, minSize, 8)
53 48 BR_PROPERTY(int, stepSize, 1)
54 49 BR_PROPERTY(bool, takeFirst, false)
55   - BR_PROPERTY(bool, negSamples, true)
56   - BR_PROPERTY(int, negToPosRatio, 1)
57   - BR_PROPERTY(double, maxOverlap, 0)
58 50 BR_PROPERTY(int, windowWidth, 24)
59 51 BR_PROPERTY(float, threshold, 0)
60 52  
... ... @@ -65,11 +57,84 @@ private:
65 57  
66 58 void train(const TemplateList &data)
67 59 {
68   - // only calculate if the work hasn't been done
69   - aspectRatio = data.first().file.get<float>("aspectRatio", -1);
  60 + float aspectRatio = data.first().file.get<float>("aspectRatio", -1);
70 61 if (aspectRatio == -1)
71 62 aspectRatio = getAspectRatio(data);
72 63 windowHeight = (int) qRound((float) windowWidth / aspectRatio);
  64 + if (transform->trainable) {
  65 + transform->train(data);
  66 + }
  67 + }
  68 +
  69 + void project(const Template &src, Template &dst) const
  70 + {
  71 + dst = src;
  72 + // no need to slide a window over ground truth data
  73 + if (src.file.getBool("Train", false)) return;
  74 +
  75 + dst.file.clearRects();
  76 + float scale = src.file.get<float>("scale", 1);
  77 + Template windowTemplate(src.file, src);
  78 + for (double y = 0; y + windowHeight < src.m().rows; y += stepSize) {
  79 + for (double x = 0; x + windowWidth < src.m().cols; x += stepSize) {
  80 + Mat windowMat(src, Rect(x, y, windowWidth, windowHeight));
  81 + windowTemplate.replace(0,windowMat);
  82 + Template detect;
  83 + transform->project(windowTemplate, detect);
  84 + float conf = detect.m().at<float>(0);
  85 +
  86 + // the result will be in the Label
  87 + if (conf > threshold) {
  88 + dst.file.appendRect(QRectF((float) x * scale, (float) y * scale, (float) windowWidth * scale, (float) windowHeight * scale));
  89 + QList<float> confidences = dst.file.getList<float>("Confidences", QList<float>());
  90 + confidences.append(conf);
  91 + dst.file.setList<float>("Confidences", confidences);
  92 + if (takeFirst)
  93 + return;
  94 + }
  95 + }
  96 + }
  97 + }
  98 +};
  99 +
  100 +BR_REGISTER(Transform, SlidingWindowTransform)
  101 +
  102 +/*!
  103 + * \ingroup transforms
  104 + * \brief .
  105 + * \author Austin Blanton \cite imaus10
  106 + */
  107 +class BuildScalesTransform : public Transform
  108 +{
  109 + Q_OBJECT
  110 + Q_PROPERTY(br::Transform *transform READ get_transform WRITE set_transform RESET reset_transform STORED false)
  111 + Q_PROPERTY(double scaleFactor READ get_scaleFactor WRITE set_scaleFactor RESET reset_scaleFactor STORED false)
  112 + Q_PROPERTY(bool takeLargestScale READ get_takeLargestScale WRITE set_takeLargestScale RESET reset_takeLargestScale STORED false)
  113 + Q_PROPERTY(int windowWidth READ get_windowWidth WRITE set_windowWidth RESET reset_windowWidth STORED false)
  114 + Q_PROPERTY(int negToPosRatio READ get_negToPosRatio WRITE set_negToPosRatio RESET reset_negToPosRatio STORED false)
  115 + Q_PROPERTY(int minSize READ get_minSize WRITE set_minSize RESET reset_minSize STORED false)
  116 + Q_PROPERTY(double maxOverlap READ get_maxOverlap WRITE set_maxOverlap RESET reset_maxOverlap STORED false)
  117 + Q_PROPERTY(bool negSamples READ get_negSamples WRITE set_negSamples RESET reset_negSamples STORED false)
  118 + BR_PROPERTY(br::Transform *, transform, NULL)
  119 + BR_PROPERTY(double, scaleFactor, 0.75)
  120 + BR_PROPERTY(bool, takeLargestScale, false)
  121 + BR_PROPERTY(int, windowWidth, 24)
  122 + BR_PROPERTY(int, negToPosRatio, 1)
  123 + BR_PROPERTY(int, minSize, 8)
  124 + BR_PROPERTY(double, maxOverlap, 0)
  125 + BR_PROPERTY(bool, negSamples, true)
  126 +
  127 +public:
  128 + BuildScalesTransform() : Transform(false, true) {}
  129 +private:
  130 + int windowHeight;
  131 +
  132 + void train(const TemplateList &_data)
  133 + {
  134 + TemplateList data = _data; // have to make a copy b/c data is const
  135 + aspectRatio = getAspectRatio(data);
  136 + data.first().file.set("aspectRatio", aspectRatio);
  137 + windowHeight = (int) qRound((float) windowWidth / aspectRatio);
73 138  
74 139 if (transform->trainable) {
75 140 TemplateList full;
... ... @@ -142,71 +207,6 @@ private:
142 207 return false;
143 208 }
144 209  
145   - void project(const Template &src, Template &dst) const
146   - {
147   - dst = src;
148   - // no need to slide a window over ground truth data
149   - if (src.file.getBool("Train", false)) return;
150   -
151   - dst.file.clearRects();
152   - int scale = src.file.get<float>("scale", 1);
153   - Template windowTemplate(src.file, src);
154   - for (double y = 0; y + windowHeight < src.m().rows; y += stepSize) {
155   - for (double x = 0; x + windowWidth < src.m().cols; x += stepSize) {
156   - Mat windowMat(src, Rect(x, y, windowWidth, windowHeight));
157   - windowTemplate.replace(0,windowMat);
158   - Template detect;
159   - transform->project(windowTemplate, detect);
160   - float conf = detect.m().at<float>(0);
161   -
162   - // the result will be in the Label
163   - if (conf > threshold) {
164   - dst.file.appendRect(QRectF((float) x * scale, (float) y * scale, (float) windowWidth * scale, (float) windowHeight * scale));
165   - QList<float> confidences = dst.file.getList<float>("Confidences", QList<float>());
166   - confidences.append(conf);
167   - dst.file.setList<float>("Confidences", confidences);
168   - if (takeFirst)
169   - return;
170   - }
171   - }
172   - }
173   - }
174   -
175   - float aspectRatio;
176   -};
177   -
178   -BR_REGISTER(Transform, SlidingWindowTransform)
179   -
180   -/*!
181   - * \ingroup transforms
182   - * \brief .
183   - * \author Austin Blanton \cite imaus10
184   - */
185   -class BuildScalesTransform : public Transform
186   -{
187   - Q_OBJECT
188   - Q_PROPERTY(br::Transform *transform READ get_transform WRITE set_transform RESET reset_transform STORED false)
189   - Q_PROPERTY(double scaleFactor READ get_scaleFactor WRITE set_scaleFactor RESET reset_scaleFactor STORED false)
190   - Q_PROPERTY(bool takeLargestScale READ get_takeLargestScale WRITE set_takeLargestScale RESET reset_takeLargestScale STORED false)
191   - Q_PROPERTY(int windowWidth READ get_windowWidth WRITE set_windowWidth RESET reset_windowWidth STORED false)
192   - BR_PROPERTY(br::Transform *, transform, NULL)
193   - BR_PROPERTY(double, scaleFactor, 0.75)
194   - BR_PROPERTY(bool, takeLargestScale, false)
195   - BR_PROPERTY(int, windowWidth, 24)
196   -
197   -public:
198   - BuildScalesTransform() : Transform(false, true) {}
199   -private:
200   -
201   - void train(const TemplateList &data)
202   - {
203   - aspectRatio = getAspectRatio(data);
204   - // have to make a copy b/c data is const
205   - TemplateList cp = data;
206   - cp.first().file.set("aspectRatio", aspectRatio);
207   - if (transform->trainable)
208   - transform->train(cp);
209   - }
210 210  
211 211 void project(const Template &src, Template &dst) const
212 212 {
... ...