Skip to content

Commit cb17dd2

Browse files
authored
Merge pull request #7698 from will-am/fix_priorbox
Fix priorbox layer when multiple values given in min_size
2 parents 32cc11e + 871b0e6 commit cb17dd2

File tree

1 file changed

+36
-42
lines changed

1 file changed

+36
-42
lines changed

paddle/gserver/layers/PriorBox.cpp

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,19 @@ bool PriorBoxLayer::init(const LayerMap& layerMap,
6565
std::copy(pbConf.aspect_ratio().begin(),
6666
pbConf.aspect_ratio().end(),
6767
std::back_inserter(tmp));
68-
// flip
69-
int inputRatioLength = tmp.size();
70-
for (int index = 0; index < inputRatioLength; index++) {
71-
aspectRatio_.push_back(tmp[index]);
72-
aspectRatio_.push_back(1 / tmp[index]);
68+
69+
if (maxSize_.size() > 0) CHECK_EQ(minSize_.size(), maxSize_.size());
70+
71+
// flip aspect ratios
72+
for (int index = 0; index < tmp.size(); index++) {
73+
real ar = tmp[index];
74+
if (fabs(ar - 1.) < 1e-6) continue;
75+
aspectRatio_.push_back(ar);
76+
aspectRatio_.push_back(1. / ar);
7377
}
74-
numPriors_ = aspectRatio_.size();
75-
if (maxSize_.size() > 0) numPriors_++;
78+
79+
numPriors_ = aspectRatio_.size() * minSize_.size() + maxSize_.size();
80+
7681
return true;
7782
}
7883

@@ -99,50 +104,39 @@ void PriorBoxLayer::forward(PassType passType) {
99104
for (int w = 0; w < layerWidth; ++w) {
100105
real centerX = (w + 0.5) * stepW;
101106
real centerY = (h + 0.5) * stepH;
102-
real minSize = 0;
103107
for (size_t s = 0; s < minSize_.size(); s++) {
104-
// first prior.
105-
minSize = minSize_[s];
108+
real minSize = minSize_[s];
106109
real boxWidth = minSize;
107110
real boxHeight = minSize;
108-
// xmin, ymin, xmax, ymax.
109-
tmpPtr[idx++] = (centerX - boxWidth / 2.) / imageWidth;
110-
tmpPtr[idx++] = (centerY - boxHeight / 2.) / imageHeight;
111-
tmpPtr[idx++] = (centerX + boxWidth / 2.) / imageWidth;
112-
tmpPtr[idx++] = (centerY + boxHeight / 2.) / imageHeight;
113-
// set the variance.
114-
for (int t = 0; t < 4; t++) tmpPtr[idx++] = variance_[t];
111+
112+
// priors with different aspect ratios
113+
for (size_t r = 0; r < aspectRatio_.size(); r++) {
114+
real ar = aspectRatio_[r];
115+
boxWidth = minSize * sqrt(ar);
116+
boxHeight = minSize / sqrt(ar);
117+
tmpPtr[idx++] = (centerX - boxWidth / 2.) / imageWidth;
118+
tmpPtr[idx++] = (centerY - boxHeight / 2.) / imageHeight;
119+
tmpPtr[idx++] = (centerX + boxWidth / 2.) / imageWidth;
120+
tmpPtr[idx++] = (centerY + boxHeight / 2.) / imageHeight;
121+
// set the variance.
122+
for (int t = 0; t < 4; t++) tmpPtr[idx++] = variance_[t];
123+
}
115124

116125
if (maxSize_.size() > 0) {
117-
CHECK_EQ(minSize_.size(), maxSize_.size());
118-
// second prior.
119-
for (size_t s = 0; s < maxSize_.size(); s++) {
120-
real maxSize = maxSize_[s];
121-
boxWidth = boxHeight = sqrt(minSize * maxSize);
122-
tmpPtr[idx++] = (centerX - boxWidth / 2.) / imageWidth;
123-
tmpPtr[idx++] = (centerY - boxHeight / 2.) / imageHeight;
124-
tmpPtr[idx++] = (centerX + boxWidth / 2.) / imageWidth;
125-
tmpPtr[idx++] = (centerY + boxHeight / 2.) / imageHeight;
126-
// set the variance.
127-
for (int t = 0; t < 4; t++) tmpPtr[idx++] = variance_[t];
128-
}
126+
// square prior with size sqrt(minSize * maxSize)
127+
real maxSize = maxSize_[s];
128+
boxWidth = boxHeight = sqrt(minSize * maxSize);
129+
tmpPtr[idx++] = (centerX - boxWidth / 2.) / imageWidth;
130+
tmpPtr[idx++] = (centerY - boxHeight / 2.) / imageHeight;
131+
tmpPtr[idx++] = (centerX + boxWidth / 2.) / imageWidth;
132+
tmpPtr[idx++] = (centerY + boxHeight / 2.) / imageHeight;
133+
// set the variance.
134+
for (int t = 0; t < 4; t++) tmpPtr[idx++] = variance_[t];
129135
}
130136
}
131-
// rest of priors.
132-
for (size_t r = 0; r < aspectRatio_.size(); r++) {
133-
real ar = aspectRatio_[r];
134-
if (fabs(ar - 1.) < 1e-6) continue;
135-
real boxWidth = minSize * sqrt(ar);
136-
real boxHeight = minSize / sqrt(ar);
137-
tmpPtr[idx++] = (centerX - boxWidth / 2.) / imageWidth;
138-
tmpPtr[idx++] = (centerY - boxHeight / 2.) / imageHeight;
139-
tmpPtr[idx++] = (centerX + boxWidth / 2.) / imageWidth;
140-
tmpPtr[idx++] = (centerY + boxHeight / 2.) / imageHeight;
141-
// set the variance.
142-
for (int t = 0; t < 4; t++) tmpPtr[idx++] = variance_[t];
143-
}
144137
}
145138
}
139+
146140
// clip the prior's coordidate such that it is within [0, 1]
147141
for (int d = 0; d < dim * 2; ++d)
148142
if ((d % 8) < 4)

0 commit comments

Comments
 (0)