]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - jacinto-ai/caffe-jacinto.git/blob - src/caffe/layers/im2col_layer.cpp
a52829c648dfd6c6f654ff029ce964b0a31e2a0a
[jacinto-ai/caffe-jacinto.git] / src / caffe / layers / im2col_layer.cpp
1 #include "caffe/layer.hpp"
2 #include "caffe/util/im2col.hpp"
3 #include "caffe/vision_layers.hpp"
4 #include "caffe/common.hpp"
6 namespace caffe {
8 template <typename Dtype>
9 void Im2colLayer<Dtype>::SetUp(const vector<Blob<Dtype>*>& bottom,
10       vector<Blob<Dtype>*>* top) {
11   CHECK_EQ(bottom.size(), 1) << "Im2col Layer takes a single blob as input.";
12   CHECK_EQ(top->size(), 1) << "Im2col Layer takes a single blob as output.";
13   KSIZE_ = this->layer_param_.kernelsize();
14   STRIDE_ = this->layer_param_.stride();
15   CHANNELS_ = bottom[0]->channels();
16   HEIGHT_ = bottom[0]->height();
17   WIDTH_ = bottom[0]->width();
18   (*top)[0]->Reshape(bottom[0]->num(), CHANNELS_ * KSIZE_ * KSIZE_,
19       (HEIGHT_ - KSIZE_) / STRIDE_ + 1, (WIDTH_ - KSIZE_) / STRIDE_ + 1);
20 };
22 template <typename Dtype>
23 void Im2colLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
24       vector<Blob<Dtype>*>* top) {
25   const Dtype* bottom_data = bottom[0]->cpu_data();
26   Dtype* top_data = (*top)[0]->mutable_cpu_data();
27   for (int n = 0; n < bottom[0]->num(); ++n) {
28     im2col_cpu(bottom_data + bottom[0]->offset(n), CHANNELS_, HEIGHT_,
29         WIDTH_, KSIZE_, STRIDE_, top_data + (*top)[0]->offset(n));
30   }
31 }
33 template <typename Dtype>
34 void Im2colLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
35       vector<Blob<Dtype>*>* top) {
36   const Dtype* bottom_data = bottom[0]->gpu_data();
37   Dtype* top_data = (*top)[0]->mutable_gpu_data();
38   for (int n = 0; n < bottom[0]->num(); ++n) {
39     im2col_gpu(bottom_data + bottom[0]->offset(n), CHANNELS_, HEIGHT_,
40         WIDTH_, KSIZE_, STRIDE_, top_data + (*top)[0]->offset(n));
41   }
42 }
44 template <typename Dtype>
45 Dtype Im2colLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
46       const bool propagate_down, vector<Blob<Dtype>*>* bottom) {
47   const Dtype* top_diff = top[0]->cpu_diff();
48   Dtype* bottom_diff = (*bottom)[0]->mutable_cpu_diff();
49   for (int n = 0; n < top[0]->num(); ++n) {
50     col2im_cpu(top_diff + top[0]->offset(n), CHANNELS_, HEIGHT_,
51         WIDTH_, KSIZE_, STRIDE_, bottom_diff + (*bottom)[0]->offset(n));
52   }
53   return Dtype(0.);
54 }
57 template <typename Dtype>
58 Dtype Im2colLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
59       const bool propagate_down, vector<Blob<Dtype>*>* bottom) {
60   const Dtype* top_diff = top[0]->gpu_diff();
61   Dtype* bottom_diff = (*bottom)[0]->mutable_gpu_diff();
62   for (int n = 0; n < top[0]->num(); ++n) {
63     col2im_gpu(top_diff + top[0]->offset(n), CHANNELS_, HEIGHT_,
64         WIDTH_, KSIZE_, STRIDE_, bottom_diff + (*bottom)[0]->offset(n));
65   }
66   return Dtype(0.);
67 }
69 INSTANTIATE_CLASS(Im2colLayer);
71 }  // namespace caffe