(*top)[0]->Reshape(bottom[0]->num(), channels_, pooled_height_,
pooled_width_);
// If max pooling, we will initialize the vector index part.
- if (this->layer_param_.pooling_param().pool() == PoolingParameter_PoolMethod_MAX) {
+ if (this->layer_param_.pooling_param().pool() ==
+ PoolingParameter_PoolMethod_MAX) {
max_idx_.reset(new SyncedMemory((*top)[0]->count() * sizeof(int)));
}
// If stochastic pooling, we will initialize the random index part.
switch (this->layer_param_.pooling_param().pool()) {
case PoolingParameter_PoolMethod_MAX:
// Initialize
- mask = (int*)max_idx_->mutable_cpu_data();
+ mask = static_cast<int*>(max_idx_->mutable_cpu_data());
for (int i = 0; i < top_count; ++i) {
top_data[i] = -FLT_MAX;
mask[i] = -1;
int wstart = pw * stride_;
int hend = min(hstart + kernel_size_, height_);
int wend = min(wstart + kernel_size_, width_);
+ const int pool_index = ph * pooled_width_ + pw;
for (int h = hstart; h < hend; ++h) {
for (int w = wstart; w < wend; ++w) {
- if (bottom_data[h * width_ + w] > top_data[ph * pooled_width_ + pw]) {
- top_data[ph * pooled_width_ + pw] = bottom_data[h * width_ + w];
- mask[ph * pooled_width_ + pw] = h * width_ + w;
+ const int index = h * width_ + w;
+ if (bottom_data[index] > top_data[pool_index]) {
+ top_data[pool_index] = bottom_data[index];
+ mask[pool_index] = index;
}
}
}
// Different pooling methods. We explicitly do the switch outside the for
// loop to save time, although this results in more codes.
memset(bottom_diff, 0, (*bottom)[0]->count() * sizeof(Dtype));
- int* mask;
+ const int* mask;
switch (this->layer_param_.pooling_param().pool()) {
case PoolingParameter_PoolMethod_MAX:
// The main loop
- mask = (int*)max_idx_->cpu_data();
+ mask = static_cast<const int*>(max_idx_->cpu_data());
for (int n = 0; n < top[0]->num(); ++n) {
for (int c = 0; c < channels_; ++c) {
for (int ph = 0; ph < pooled_height_; ++ph) {
for (int pw = 0; pw < pooled_width_; ++pw) {
- bottom_diff[mask[ph * pooled_width_ + pw]]+=top_diff[ph * pooled_width_ + pw];
+ const int index = ph * pooled_width_ + pw;
+ bottom_diff[mask[index]] += top_diff[index];
}
}
bottom_diff += (*bottom)[0]->offset(0, 1);
using std::min;
namespace caffe {
-
+
template <typename Dtype>
__global__ void MaxPoolForward(const int nthreads, const Dtype* bottom_data,
const int num, const int channels, const int height,
maxidx = h * width + w;
maxval = bottom_data[maxidx];
}
-
}
}
top_data[index] = maxval;
int* mask;
switch (this->layer_param_.pooling_param().pool()) {
case PoolingParameter_PoolMethod_MAX:
+ mask = static_cast<int*>(max_idx_->mutable_gpu_data());
// NOLINT_NEXT_LINE(whitespace/operators)
- mask = (int*)max_idx_->mutable_gpu_data();
MaxPoolForward<Dtype><<<CAFFE_GET_BLOCKS(count), CAFFE_CUDA_NUM_THREADS>>>(
count, bottom_data, bottom[0]->num(), channels_,
height_, width_, pooled_height_, pooled_width_, kernel_size_, stride_,
Dtype gradient = 0;
top_diff += (n * channels + c) * pooled_height * pooled_width;
mask += (n * channels + c) * pooled_height * pooled_width;
- //bottom_diff[index] += top_diff[mask[index]];
for (int ph = phstart; ph < phend; ++ph) {
for (int pw = pwstart; pw < pwend; ++pw) {
if (mask[ph * pooled_width + pw] == h * width + w)
}
}
bottom_diff[index] = gradient;
- }
+ }
}
/*
int count = (*bottom)[0]->count();
CUDA_CHECK(cudaMemset(bottom_diff, 0, sizeof(Dtype) * count));
int* mask;
-
switch (this->layer_param_.pooling_param().pool()) {
case PoolingParameter_PoolMethod_MAX:
- mask = (int*)max_idx_->gpu_data();
+ mask = static_cast<int*>(max_idx_->mutable_gpu_data());
// Since we have the mask we only need count top_diff
- count = top[0]->count();
+ count = top[0]->count();
+ caffe_gpu_set(count, Dtype(0.), bottom_diff);
// NOLINT_NEXT_LINE(whitespace/operators)
- caffe_gpu_set(count,Dtype(0.),bottom_diff);
MaxPoolBackward<Dtype><<<CAFFE_GET_BLOCKS(count), CAFFE_CUDA_NUM_THREADS>>>(
count, top_diff, top[0]->num(), channels_,
height_, width_, pooled_height_, pooled_width_,
-// Copyright 2014 Sergio Guadarrama
+// Copyright 2014 BVLC and contributors.
#include <cstring>
-#include <cuda_runtime.h>
+#include <vector>
+
+#include "cuda_runtime.h"
#include "gtest/gtest.h"
#include "caffe/blob.hpp"
protected:
MaxPoolingDropoutTest()
: blob_bottom_(new Blob<Dtype>()),
- blob_top_(new Blob<Dtype>()) {};
+ blob_top_(new Blob<Dtype>()) {}
virtual void SetUp() {
Caffe::set_random_seed(1703);
blob_bottom_->Reshape(2, 3, 6, 5);
filler.Fill(this->blob_bottom_);
blob_bottom_vec_.push_back(blob_bottom_);
blob_top_vec_.push_back(blob_top_);
- };
+ }
virtual ~MaxPoolingDropoutTest() { delete blob_bottom_; delete blob_top_; }
Blob<Dtype>* const blob_bottom_;
Blob<Dtype>* const blob_top_;
const TypeParam* top_data = this->blob_top_->cpu_data();
TypeParam sum = 0.;
for (int i = 0; i < this->blob_top_->count(); ++i) {
- sum += top_data[i];
+ sum += top_data[i];
}
EXPECT_EQ(sum, this->blob_top_->count());
// Dropout in-place
TypeParam scale = 1. / (1. - layer_param.dropout_param().dropout_ratio());
top_data = this->blob_top_->cpu_data();
for (int i = 0; i < this->blob_top_->count(); ++i) {
- sum += top_data[i];
+ sum += top_data[i];
}
EXPECT_GE(sum, 0);
EXPECT_LE(sum, this->blob_top_->count()*scale);
const TypeParam* top_data = this->blob_top_->cpu_data();
TypeParam sum = 0.;
for (int i = 0; i < this->blob_top_->count(); ++i) {
- sum += top_data[i];
+ sum += top_data[i];
}
EXPECT_EQ(sum, this->blob_top_->count());
TypeParam scale = 1. / (1. - layer_param.dropout_param().dropout_ratio());
top_data = this->blob_top_->cpu_data();
for (int i = 0; i < this->blob_top_->count(); ++i) {
- sum += top_data[i];
+ sum += top_data[i];
}
EXPECT_GE(sum, 0);
EXPECT_LE(sum, this->blob_top_->count()*scale);
layer.Forward(this->blob_bottom_vec_, &(this->blob_top_vec_));
for (int i = 0; i < this->blob_top_->count(); ++i) {
this->blob_top_->mutable_cpu_diff()[i] = 1.;
- }
+ }
layer.Backward(this->blob_top_vec_, true, &(this->blob_bottom_vec_));
const TypeParam* bottom_diff = this->blob_bottom_->cpu_diff();
TypeParam sum = 0.;
for (int i = 0; i < this->blob_bottom_->count(); ++i) {
- sum += bottom_diff[i];
+ sum += bottom_diff[i];
}
EXPECT_EQ(sum, this->blob_top_->count());
// Dropout in-place
TypeParam sum_with_dropout = 0.;
bottom_diff = this->blob_bottom_->cpu_diff();
for (int i = 0; i < this->blob_bottom_->count(); ++i) {
- sum_with_dropout += bottom_diff[i];
+ sum_with_dropout += bottom_diff[i];
}
EXPECT_GE(sum_with_dropout, sum);
}
layer.Forward(this->blob_bottom_vec_, &(this->blob_top_vec_));
for (int i = 0; i < this->blob_top_->count(); ++i) {
this->blob_top_->mutable_cpu_diff()[i] = 1.;
- }
+ }
layer.Backward(this->blob_top_vec_, true, &(this->blob_bottom_vec_));
const TypeParam* bottom_diff = this->blob_bottom_->cpu_diff();
TypeParam sum = 0.;
for (int i = 0; i < this->blob_bottom_->count(); ++i) {
- sum += bottom_diff[i];
+ sum += bottom_diff[i];
}
EXPECT_EQ(sum, this->blob_top_->count());
// Dropout in-place
TypeParam sum_with_dropout = 0.;
bottom_diff = this->blob_bottom_->cpu_diff();
for (int i = 0; i < this->blob_bottom_->count(); ++i) {
- sum_with_dropout += bottom_diff[i];
+ sum_with_dropout += bottom_diff[i];
}
EXPECT_GE(sum_with_dropout, sum);
}
-}
\ No newline at end of file
+} // namespace caffe