Skip to content

Commit 1c4a399

Browse files
authored
Merge pull request #10606 from pkuyym/fix-10605
Fix order of prior boxes.
2 parents 9923be5 + 1c44650 commit 1c4a399

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

paddle/gserver/layers/PriorBox.cpp

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ namespace paddle {
2828
*/
2929

3030
class PriorBoxLayer : public Layer {
31-
public:
31+
public: // NOLINT
3232
explicit PriorBoxLayer(const LayerConfig& config) : Layer(config) {}
3333
bool init(const LayerMap& layerMap,
3434
const ParameterMap& parameterMap) override;
3535

3636
void forward(PassType passType) override;
3737
void backward(const UpdateCallback& callback) override {}
3838

39-
protected:
39+
protected: // NOLINT
4040
int numPriors_;
4141
std::vector<int> minSize_;
4242
std::vector<int> maxSize_;
@@ -109,11 +109,18 @@ void PriorBoxLayer::forward(PassType passType) {
109109
real boxWidth = minSize;
110110
real boxHeight = minSize;
111111

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);
112+
// first prior: aspect_ratio == 1.0, compatible to old logic
113+
tmpPtr[idx++] = (centerX - boxWidth / 2.) / imageWidth;
114+
tmpPtr[idx++] = (centerY - boxHeight / 2.) / imageHeight;
115+
tmpPtr[idx++] = (centerX + boxWidth / 2.) / imageWidth;
116+
tmpPtr[idx++] = (centerY + boxHeight / 2.) / imageHeight;
117+
// set the variance.
118+
for (int t = 0; t < 4; t++) tmpPtr[idx++] = variance_[t];
119+
120+
if (maxSize_.size() > 0) {
121+
// square prior with size sqrt(minSize * maxSize)
122+
real maxSize = maxSize_[s];
123+
boxWidth = boxHeight = sqrt(minSize * maxSize);
117124
tmpPtr[idx++] = (centerX - boxWidth / 2.) / imageWidth;
118125
tmpPtr[idx++] = (centerY - boxHeight / 2.) / imageHeight;
119126
tmpPtr[idx++] = (centerX + boxWidth / 2.) / imageWidth;
@@ -122,10 +129,14 @@ void PriorBoxLayer::forward(PassType passType) {
122129
for (int t = 0; t < 4; t++) tmpPtr[idx++] = variance_[t];
123130
}
124131

125-
if (maxSize_.size() > 0) {
126-
// square prior with size sqrt(minSize * maxSize)
127-
real maxSize = maxSize_[s];
128-
boxWidth = boxHeight = sqrt(minSize * maxSize);
132+
// priors with different aspect ratios
133+
for (size_t r = 0; r < aspectRatio_.size(); r++) {
134+
real ar = aspectRatio_[r];
135+
if (fabs(ar - 1.0) < 1e-6) {
136+
continue;
137+
}
138+
boxWidth = minSize * sqrt(ar);
139+
boxHeight = minSize / sqrt(ar);
129140
tmpPtr[idx++] = (centerX - boxWidth / 2.) / imageWidth;
130141
tmpPtr[idx++] = (centerY - boxHeight / 2.) / imageHeight;
131142
tmpPtr[idx++] = (centerX + boxWidth / 2.) / imageWidth;

0 commit comments

Comments
 (0)