5 #ifndef CAFFE_FILLER_HPP 6 #define CAFFE_FILLER_HPP 10 #include "caffe/blob.hpp" 11 #include "caffe/proto/caffe.pb.h" 12 #include "caffe/syncedmem.hpp" 13 #include "caffe/util/math_functions.hpp" 18 template <
typename Dtype>
21 explicit Filler(
const FillerParameter& param) : filler_param_(param) {}
25 FillerParameter filler_param_;
30 template <
typename Dtype>
36 Dtype* data = blob->mutable_cpu_data();
37 const int count = blob->count();
38 const Dtype value = this->filler_param_.value();
40 for (
int i = 0; i < count; ++i) {
43 CHECK_EQ(this->filler_param_.sparse(), -1)
44 <<
"Sparsity not supported by this Filler.";
49 template <
typename Dtype>
56 caffe_rng_uniform<Dtype>(blob->count(), Dtype(this->filler_param_.min()),
57 Dtype(this->filler_param_.max()), blob->mutable_cpu_data());
58 CHECK_EQ(this->filler_param_.sparse(), -1)
59 <<
"Sparsity not supported by this Filler.";
64 template <
typename Dtype>
70 Dtype* data = blob->mutable_cpu_data();
72 caffe_rng_gaussian<Dtype>(blob->count(), Dtype(this->filler_param_.mean()),
73 Dtype(this->filler_param_.std()), blob->mutable_cpu_data());
74 int sparse = this->filler_param_.sparse();
81 CHECK_GE(blob->num_axes(), 1);
82 const int num_outputs = blob->shape(0);
83 Dtype non_zero_probability = Dtype(sparse) / Dtype(num_outputs);
84 rand_vec_.reset(
new SyncedMemory(blob->count() *
sizeof(int)));
85 int* mask =
reinterpret_cast<int*
>(rand_vec_->mutable_cpu_data());
86 caffe_rng_bernoulli(blob->count(), non_zero_probability, mask);
87 for (
int i = 0; i < blob->count(); ++i) {
94 shared_ptr<SyncedMemory> rand_vec_;
100 template <
typename Dtype>
106 Dtype* data = blob->mutable_cpu_data();
107 DCHECK(blob->count());
108 caffe_rng_uniform<Dtype>(blob->count(), 0, 1, blob->mutable_cpu_data());
111 int dim = blob->count() / blob->
num();
113 for (
int i = 0; i < blob->
num(); ++i) {
115 for (
int j = 0; j < dim; ++j) {
116 sum += data[i * dim + j];
118 for (
int j = 0; j < dim; ++j) {
119 data[i * dim + j] /= sum;
122 CHECK_EQ(this->filler_param_.sparse(), -1)
123 <<
"Sparsity not supported by this Filler.";
143 template <
typename Dtype>
149 CHECK(blob->count());
150 int fan_in = blob->count() / blob->
num();
151 int fan_out = blob->count() / blob->
channels();
153 if (this->filler_param_.variance_norm() ==
154 FillerParameter_VarianceNorm_AVERAGE) {
155 n = (fan_in + fan_out) / Dtype(2);
156 }
else if (this->filler_param_.variance_norm() ==
157 FillerParameter_VarianceNorm_FAN_OUT) {
160 Dtype scale = sqrt(Dtype(3) / n);
161 caffe_rng_uniform<Dtype>(blob->count(), -scale, scale,
162 blob->mutable_cpu_data());
163 CHECK_EQ(this->filler_param_.sparse(), -1)
164 <<
"Sparsity not supported by this Filler.";
185 template <
typename Dtype>
188 explicit MSRAFiller(
const FillerParameter& param)
191 CHECK(blob->count());
192 int fan_in = blob->count() / blob->
num();
193 int fan_out = blob->count() / blob->
channels();
195 if (this->filler_param_.variance_norm() ==
196 FillerParameter_VarianceNorm_AVERAGE) {
197 n = (fan_in + fan_out) / Dtype(2);
198 }
else if (this->filler_param_.variance_norm() ==
199 FillerParameter_VarianceNorm_FAN_OUT) {
202 Dtype std = sqrt(Dtype(2) / n);
203 caffe_rng_gaussian<Dtype>(blob->count(), Dtype(0), std,
204 blob->mutable_cpu_data());
205 CHECK_EQ(this->filler_param_.sparse(), -1)
206 <<
"Sparsity not supported by this Filler.";
243 template <
typename Dtype>
249 CHECK_EQ(blob->num_axes(), 4) <<
"Blob must be 4 dim.";
250 CHECK_EQ(blob->
width(), blob->
height()) <<
"Filter must be square";
251 Dtype* data = blob->mutable_cpu_data();
252 int f = ceil(blob->
width() / 2.);
253 float c = (2 * f - 1 - f % 2) / (2. * f);
254 for (
int i = 0; i < blob->count(); ++i) {
255 float x = i % blob->
width();
257 data[i] = (1 - fabs(x / f - c)) * (1 - fabs(y / f - c));
259 CHECK_EQ(this->filler_param_.sparse(), -1)
260 <<
"Sparsity not supported by this Filler.";
270 template <
typename Dtype>
272 const std::string& type = param.type();
273 if (type ==
"constant") {
275 }
else if (type ==
"gaussian") {
277 }
else if (type ==
"positive_unitball") {
279 }
else if (type ==
"uniform") {
281 }
else if (type ==
"xavier") {
283 }
else if (type ==
"msra") {
285 }
else if (type ==
"bilinear") {
288 CHECK(
false) <<
"Unknown filler name: " << param.type();
295 #endif // CAFFE_FILLER_HPP_ int channels() const
Deprecated legacy shape accessor channels: use shape(1) instead.
Definition: blob.hpp:134
Fills a Blob with constant or randomly-generated data.
Definition: filler.hpp:19
Fills a Blob with values such that .
Definition: filler.hpp:101
A layer factory that allows one to register layers. During runtime, registered layers can be called b...
Definition: blob.hpp:14
int height() const
Deprecated legacy shape accessor height: use shape(2) instead.
Definition: blob.hpp:136
Fills a Blob with coefficients for bilinear interpolation.
Definition: filler.hpp:244
Filler< Dtype > * GetFiller(const FillerParameter ¶m)
Get a specific filler from the specification given in FillerParameter.
Definition: filler.hpp:271
Fills a Blob with values where is set inversely proportional to number of incoming nodes...
Definition: filler.hpp:144
Fills a Blob with Gaussian-distributed values .
Definition: filler.hpp:65
Manages memory allocation and synchronization between the host (CPU) and device (GPU).
Definition: syncedmem.hpp:57
Fills a Blob with values where is set inversely proportional to number of incoming nodes...
Definition: filler.hpp:186
int num() const
Deprecated legacy shape accessor num: use shape(0) instead.
Definition: blob.hpp:132
int width() const
Deprecated legacy shape accessor width: use shape(3) instead.
Definition: blob.hpp:138
Fills a Blob with constant values .
Definition: filler.hpp:31
A wrapper around SyncedMemory holders serving as the basic computational unit through which Layers...
Definition: blob.hpp:24