55namespace caffe {
66
77template <typename Dtype>
8- void DepthToSpaceLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,
9- const vector<Blob<Dtype>*>& top) {
8+ void DepthToSpaceLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype> *> & bottom,
9+ const vector<Blob<Dtype> *> & top) {
1010 CHECK_NE (top[0 ], bottom[0 ]) << this ->type () << " Layer does not "
11- " allow in-place computation." ;
11+ " allow in-place computation." ;
1212 this ->block_size = this ->layer_param_ .depth_to_space_param ().block_size ();
1313 this ->data_format = this ->layer_param_ .depth_to_space_param ().data_format ();
1414 vector<int > bottom_shape = bottom[0 ]->shape ();
1515 this ->output_top_shape .push_back (bottom_shape[0 ]);
16- if (this ->data_format == " NHWC" ){
16+ if (this ->data_format == " NHWC" ) {
1717 this ->output_top_shape .push_back (bottom_shape[1 ] * this ->block_size );
1818 this ->output_top_shape .push_back (bottom_shape[2 ] * this ->block_size );
19- this ->output_top_shape .push_back (bottom_shape[3 ] / (this ->block_size *this ->block_size ));
20- } else if (this ->data_format == " NCHW" || this ->data_format == " CRD" || this ->data_format == " DCR" ){
21- std::cout << " the second output shape" << std::endl;
22- this ->output_top_shape .push_back (bottom_shape[1 ] / (this ->block_size *this ->block_size ));
19+ this ->output_top_shape .push_back (bottom_shape[3 ] /
20+ (this ->block_size * this ->block_size ));
21+ } else if (this ->data_format == " NCHW" || this ->data_format == " CRD" ||
22+ this ->data_format == " DCR" ) {
23+ std::cout << " the second output shape" << std::endl;
24+ this ->output_top_shape .push_back (bottom_shape[1 ] /
25+ (this ->block_size * this ->block_size ));
2326 this ->output_top_shape .push_back (bottom_shape[2 ] * this ->block_size );
2427 this ->output_top_shape .push_back (bottom_shape[3 ] * this ->block_size );
2528 }
2629}
2730
2831template <typename Dtype>
29- void DepthToSpaceLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom,
30- const vector<Blob<Dtype>*>& top) {
32+ void DepthToSpaceLayer<Dtype>::Reshape(const vector<Blob<Dtype> *> & bottom,
33+ const vector<Blob<Dtype> *> & top) {
3134 top[0 ]->Reshape (this ->output_top_shape );
3235 CHECK_EQ (top[0 ]->count (), bottom[0 ]->count ())
33- << " output count must match input count" ;
36+ << " output count must match input count" ;
3437}
3538
3639template <typename Dtype>
37- void DepthToSpaceLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
38- const vector<Blob<Dtype>*>& top){
39- if (this ->data_format == " NHWC" ){
40+ void DepthToSpaceLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype> *> & bottom,
41+ const vector<Blob<Dtype> *> & top) {
42+ if (this ->data_format == " NHWC" ) {
4043 const int batch_size = this ->output_top_shape [0 ];
4144 const int output_height = this ->output_top_shape [1 ];
4245 const int output_width = this ->output_top_shape [2 ];
@@ -47,8 +50,8 @@ void DepthToSpaceLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
4750 const int input_width = bottom_shape[2 ];
4851 const int input_depth = bottom_shape[3 ];
4952
50- const Dtype* bottom_data = bottom[0 ]->cpu_data ();
51- Dtype* top_data = top[0 ]->mutable_cpu_data ();
53+ const Dtype * bottom_data = bottom[0 ]->cpu_data ();
54+ Dtype * top_data = top[0 ]->mutable_cpu_data ();
5255
5356 for (int b = 0 ; b < batch_size; ++b) {
5457 for (int h = 0 ; h < output_height; ++h) {
@@ -58,11 +61,14 @@ void DepthToSpaceLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
5861 const int in_w = w / this ->block_size ;
5962 const int offset_w = (w % this ->block_size );
6063 const int offset_d =
61- (offset_h * this ->block_size + offset_w) * output_depth;
64+ (offset_h * this ->block_size + offset_w) * output_depth;
6265 for (int d = 0 ; d < output_depth; ++d) {
6366 const int in_d = d + offset_d;
64- const int out_index = ((b*output_height + h)*output_width + w)*output_depth + d;
65- const int in_index = ((b*input_height + in_h)*input_width + in_w)*input_depth + in_d;
67+ const int out_index =
68+ ((b * output_height + h) * output_width + w) * output_depth + d;
69+ const int in_index =
70+ ((b * input_height + in_h) * input_width + in_w) * input_depth +
71+ in_d;
6672 top_data[out_index] = bottom_data[in_index];
6773 }
6874 }
@@ -79,8 +85,8 @@ void DepthToSpaceLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
7985 const int input_height = bottom_shape[2 ];
8086 const int input_width = bottom_shape[3 ];
8187
82- const Dtype* bottom_data = bottom[0 ]->cpu_data ();
83- Dtype* top_data = top[0 ]->mutable_cpu_data ();
88+ const Dtype * bottom_data = bottom[0 ]->cpu_data ();
89+ Dtype * top_data = top[0 ]->mutable_cpu_data ();
8490
8591 for (int b = 0 ; b < batch_size; ++b) {
8692 for (int h = 0 ; h < output_height; ++h) {
@@ -90,52 +96,57 @@ void DepthToSpaceLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
9096 const int in_w = w / this ->block_size ;
9197 const int offset_w = (w % this ->block_size );
9298 const int offset_d =
93- (offset_h * this ->block_size + offset_w) * output_depth;
99+ (offset_h * this ->block_size + offset_w) * output_depth;
94100 for (int d = 0 ; d < output_depth; ++d) {
95- const int in_d = d + offset_d;
96- const int out_index = ((b*output_depth + d)*output_height + h)*output_width + w;
97- const int in_index = ((b*input_depth + in_d)*input_height + in_h)*input_width + in_w;
98- top_data[out_index] = bottom_data[in_index];
101+ const int in_d = d + offset_d;
102+ const int out_index =
103+ ((b * output_depth + d) * output_height + h) * output_width + w;
104+ const int in_index =
105+ ((b * input_depth + in_d) * input_height + in_h) * input_width +
106+ in_w;
107+ top_data[out_index] = bottom_data[in_index];
108+ }
109+ }
110+ }
111+ }
112+ } else if (this ->data_format == " CRD" ) {
113+ const int batch_size = this ->output_top_shape [0 ];
114+ const int output_depth = this ->output_top_shape [1 ];
115+ const int output_height = this ->output_top_shape [2 ];
116+ const int output_width = this ->output_top_shape [3 ];
117+
118+ vector<int > bottom_shape = bottom[0 ]->shape ();
119+ const int input_depth = bottom_shape[1 ];
120+ const int input_height = bottom_shape[2 ];
121+ const int input_width = bottom_shape[3 ];
122+
123+ const Dtype *bottom_data = bottom[0 ]->cpu_data ();
124+ Dtype *top_data = top[0 ]->mutable_cpu_data ();
125+
126+ for (int b = 0 ; b < batch_size; ++b) {
127+ for (int h = 0 ; h < output_height; ++h) {
128+ const int in_h = h / this ->block_size ;
129+ const int offset_h = (h % this ->block_size );
130+ for (int w = 0 ; w < output_width; ++w) {
131+ const int in_w = w / this ->block_size ;
132+ const int offset_w = (w % this ->block_size );
133+ for (int d = 0 ; d < output_depth; ++d) {
134+ const int in_d =
135+ (d * this ->block_size + offset_h) * this ->block_size + offset_w;
136+ const int out_index =
137+ ((b * output_depth + d) * output_height + h) * output_width + w;
138+ const int in_index =
139+ ((b * input_depth + in_d) * input_height + in_h) * input_width +
140+ in_w;
141+ top_data[out_index] = bottom_data[in_index];
99142 }
100143 }
101144 }
102145 }
103146 }
104- else if (this ->data_format == " CRD" ) {
105- const int batch_size = this ->output_top_shape [0 ];
106- const int output_depth = this ->output_top_shape [1 ];
107- const int output_height = this ->output_top_shape [2 ];
108- const int output_width = this ->output_top_shape [3 ];
109-
110- vector<int > bottom_shape = bottom[0 ]->shape ();
111- const int input_depth = bottom_shape[1 ];
112- const int input_height = bottom_shape[2 ];
113- const int input_width = bottom_shape[3 ];
114-
115- const Dtype* bottom_data = bottom[0 ]->cpu_data ();
116- Dtype* top_data = top[0 ]->mutable_cpu_data ();
117-
118- for (int b = 0 ; b < batch_size; ++b) {
119- for (int h = 0 ; h < output_height; ++h) {
120- const int in_h = h / this ->block_size ;
121- const int offset_h = (h % this ->block_size );
122- for (int w = 0 ; w < output_width; ++w) {
123- const int in_w = w / this ->block_size ;
124- const int offset_w = (w % this ->block_size );
125- for (int d = 0 ; d < output_depth; ++d) {
126- const int in_d = (d * this ->block_size + offset_h) * this ->block_size + offset_w;
127- const int out_index = ((b*output_depth + d)*output_height + h)*output_width + w;
128- const int in_index = ((b*input_depth + in_d)*input_height + in_h)*input_width + in_w;
129- top_data[out_index] = bottom_data[in_index];
130- }
131- }
132- }
133- }
134- }
135147}
136148
137-
138149INSTANTIATE_CLASS (DepthToSpaceLayer);
139150REGISTER_LAYER_CLASS (DepthToSpace);
140151
141- } // namespace caffe
152+ } // namespace caffe
0 commit comments