Skip to content

Commit fd5c1c8

Browse files
committed
Merge branch 'develop' of https://github.com/PaddlePaddle/paddle into enhance-lookup_table_op-padidx
2 parents 1d715c6 + cb17dd2 commit fd5c1c8

File tree

3 files changed

+206
-42
lines changed

3 files changed

+206
-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)

paddle/operators/math/sampler.cc

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#include "sampler.h"
16+
17+
namespace paddle {
18+
namespace random {
19+
20+
Sampler::~Sampler() {}
21+
22+
UniformSampler::UniformSampler(int64 range)
23+
: Sampler(range), inv_range_(1.0 / range) {
24+
random_engine_ = std::make_shared<std::mt19937>(seed_);
25+
dist_ = std::make_shared<std::uniform_int_distribution<>>(0, range);
26+
}
27+
28+
UniformSampler::UniformSampler(int64 range, unsigned int seed)
29+
: Sampler(range, seed), inv_range_(1.0 / range) {
30+
random_engine_ = std::make_shared<std::mt19937>(seed_);
31+
dist_ = std::make_shared<std::uniform_int_distribution<>>(0, range);
32+
}
33+
34+
int64 UniformSampler::Sample() const { return (*dist_)(*random_engine_); }
35+
36+
float UniformSampler::Probability(int64 value) const { return inv_range_; }
37+
38+
LogUniformSampler::LogUniformSampler(int64 range)
39+
: Sampler(range), log_range_(log(range + 1)) {
40+
random_engine_ = std::make_shared<std::mt19937>(seed_);
41+
dist_ = std::make_shared<std::uniform_real_distribution<>>(0, 1);
42+
}
43+
44+
LogUniformSampler::LogUniformSampler(int64 range, unsigned int seed)
45+
: Sampler(range, seed), log_range_(log(range + 1)) {
46+
random_engine_ = std::make_shared<std::mt19937>(seed_);
47+
dist_ = std::make_shared<std::uniform_real_distribution<>>(0, 1);
48+
}
49+
int64 LogUniformSampler::Sample() const {
50+
// Got Log Uniform distribution from uniform distribution by
51+
// inverse_transform_sampling method
52+
// More details:
53+
// https://wanghaoshuang.github.io/2017/11/Log-uniform-distribution-sampler/
54+
const int64 value =
55+
static_cast<int64>(exp((*dist_)(*random_engine_) * log_range_)) - 1;
56+
// Mathematically, value should be <= range_, but might not be due to some
57+
// floating point roundoff, so we mod by range_.
58+
return value % range_;
59+
}
60+
61+
float LogUniformSampler::Probability(int64 value) const {
62+
// Given f(x) = 1/[(x+1) * log_range_]
63+
// The value's probability is integral of f(x) from value to (value + 1)
64+
// More details:
65+
// https://wanghaoshuang.github.io/2017/11/Log-uniform-distribution-sampler
66+
return (log((value + 2.0) / (value + 1.0))) / log_range_;
67+
}
68+
69+
} // namespace random
70+
} // namespace paddle

paddle/operators/math/sampler.h

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#pragma once
16+
#include <memory>
17+
#include <random>
18+
typedef long int64;
19+
namespace paddle {
20+
namespace operators {
21+
namespace math {
22+
23+
// TODO(wanghaoshuang): Support for GPU
24+
25+
/**
26+
* Sample integers from [0, range).
27+
*/
28+
class Sampler {
29+
public:
30+
explicit Sampler(int64 range) : range_(range) {
31+
PADDLE_ENFORCE_GT(range, 0);
32+
std::random_device r;
33+
seed_ = r();
34+
}
35+
explicit Sampler(int64 range, unsigned int seed)
36+
: range_(range), seed_(seed) {
37+
PADDLE_ENFORCE_GT(range, 0);
38+
}
39+
virtual ~Sampler();
40+
// Sample a single value
41+
virtual int64 Sample() const = 0;
42+
// The probability that a single call to Sample() returns the given value.
43+
virtual float Probability(int64 value) const = 0;
44+
45+
int64 range() { return range_; };
46+
47+
protected:
48+
const int64 range_;
49+
unsigned int seed_;
50+
};
51+
52+
/**
53+
* Sample integers from [0, range).
54+
* And the distribution function is:
55+
* P(x) = 1 / range
56+
*/
57+
class UniformSampler : public Sampler {
58+
public:
59+
explicit UniformSampler(int64 range);
60+
61+
explicit UniformSampler(int64 range, unsigned int seed);
62+
63+
~UniformSampler() override {}
64+
65+
int64 Sample() const override;
66+
67+
float Probability(int64 value) const override;
68+
69+
private:
70+
const float inv_range_;
71+
std::shared_ptr<std::mt19937_64> random_engine_;
72+
std::shared_ptr<std::uniform_int_distribution<>> dist_;
73+
};
74+
75+
/**
76+
* Sample integers from [0, range).
77+
* And the distribution function is:
78+
* P(x) = (1/ln(range+1)) * ln(1 + 1/(x + 1))
79+
*/
80+
class LogUniformSampler : public Sampler {
81+
public:
82+
explicit LogUniformSampler(int64 range);
83+
84+
explicit LogUniformSampler(int64 range, unsigned int seed);
85+
86+
~LogUniformSampler() override {}
87+
88+
int64 Sample() const override;
89+
90+
float Probability(int64 value) const override;
91+
92+
private:
93+
const float log_range_;
94+
std::shared_ptr<std::mt19937_64> random_engine_;
95+
std::shared_ptr<std::uniform_real_distribution<>> dist_;
96+
};
97+
98+
} // math
99+
} // namespace operators
100+
} // namespace paddle

0 commit comments

Comments
 (0)