Skip to content

Commit b384a37

Browse files
committed
Merge branch 'development'
2 parents c05b7fa + cc7f7ab commit b384a37

File tree

2 files changed

+74
-29
lines changed

2 files changed

+74
-29
lines changed

src/caffe/layers/depth_to_space_layer.cpp

Lines changed: 68 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,40 @@
55
namespace caffe {
66

77
template <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"){
21-
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+
this->output_top_shape.push_back(bottom_shape[1] /
24+
(this->block_size * this->block_size));
2225
this->output_top_shape.push_back(bottom_shape[2] * this->block_size);
2326
this->output_top_shape.push_back(bottom_shape[3] * this->block_size);
2427
}
2528
}
2629

2730
template <typename Dtype>
28-
void DepthToSpaceLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom,
29-
const vector<Blob<Dtype>*>& top) {
31+
void DepthToSpaceLayer<Dtype>::Reshape(const vector<Blob<Dtype> *> &bottom,
32+
const vector<Blob<Dtype> *> &top) {
3033
top[0]->Reshape(this->output_top_shape);
3134
CHECK_EQ(top[0]->count(), bottom[0]->count())
32-
<< "output count must match input count";
35+
<< "output count must match input count";
3336
}
3437

3538
template <typename Dtype>
36-
void DepthToSpaceLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
37-
const vector<Blob<Dtype>*>& top){
38-
if (this->data_format == "NHWC"){
39+
void DepthToSpaceLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype> *> &bottom,
40+
const vector<Blob<Dtype> *> &top) {
41+
if (this->data_format == "NHWC") {
3942
const int batch_size = this->output_top_shape[0];
4043
const int output_height = this->output_top_shape[1];
4144
const int output_width = this->output_top_shape[2];
@@ -46,8 +49,8 @@ void DepthToSpaceLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
4649
const int input_width = bottom_shape[2];
4750
const int input_depth = bottom_shape[3];
4851

49-
const Dtype* bottom_data = bottom[0]->cpu_data();
50-
Dtype* top_data = top[0]->mutable_cpu_data();
52+
const Dtype *bottom_data = bottom[0]->cpu_data();
53+
Dtype *top_data = top[0]->mutable_cpu_data();
5154

5255
for (int b = 0; b < batch_size; ++b) {
5356
for (int h = 0; h < output_height; ++h) {
@@ -57,17 +60,20 @@ void DepthToSpaceLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
5760
const int in_w = w / this->block_size;
5861
const int offset_w = (w % this->block_size);
5962
const int offset_d =
60-
(offset_h * this->block_size + offset_w) * output_depth;
63+
(offset_h * this->block_size + offset_w) * output_depth;
6164
for (int d = 0; d < output_depth; ++d) {
6265
const int in_d = d + offset_d;
63-
const int out_index = ((b*output_height + h)*output_width + w)*output_depth + d;
64-
const int in_index = ((b*input_height + in_h)*input_width + in_w)*input_depth + in_d;
66+
const int out_index =
67+
((b * output_height + h) * output_width + w) * output_depth + d;
68+
const int in_index =
69+
((b * input_height + in_h) * input_width + in_w) * input_depth +
70+
in_d;
6571
top_data[out_index] = bottom_data[in_index];
6672
}
6773
}
6874
}
6975
}
70-
} else {
76+
} else if (this->data_format == "NCHW" || this->data_format == "DCR") {
7177
const int batch_size = this->output_top_shape[0];
7278
const int output_depth = this->output_top_shape[1];
7379
const int output_height = this->output_top_shape[2];
@@ -78,8 +84,8 @@ void DepthToSpaceLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
7884
const int input_height = bottom_shape[2];
7985
const int input_width = bottom_shape[3];
8086

81-
const Dtype* bottom_data = bottom[0]->cpu_data();
82-
Dtype* top_data = top[0]->mutable_cpu_data();
87+
const Dtype *bottom_data = bottom[0]->cpu_data();
88+
Dtype *top_data = top[0]->mutable_cpu_data();
8389

8490
for (int b = 0; b < batch_size; ++b) {
8591
for (int h = 0; h < output_height; ++h) {
@@ -89,11 +95,48 @@ void DepthToSpaceLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
8995
const int in_w = w / this->block_size;
9096
const int offset_w = (w % this->block_size);
9197
const int offset_d =
92-
(offset_h * this->block_size + offset_w) * output_depth;
98+
(offset_h * this->block_size + offset_w) * output_depth;
9399
for (int d = 0; d < output_depth; ++d) {
94100
const int in_d = d + offset_d;
95-
const int out_index = ((b*output_depth + d)*output_height + h)*output_width + w;
96-
const int in_index = ((b*input_depth + in_d)*input_height + in_h)*input_width + in_w;
101+
const int out_index =
102+
((b * output_depth + d) * output_height + h) * output_width + w;
103+
const int in_index =
104+
((b * input_depth + in_d) * input_height + in_h) * input_width +
105+
in_w;
106+
top_data[out_index] = bottom_data[in_index];
107+
}
108+
}
109+
}
110+
}
111+
} else if (this->data_format == "CRD") {
112+
const int batch_size = this->output_top_shape[0];
113+
const int output_depth = this->output_top_shape[1];
114+
const int output_height = this->output_top_shape[2];
115+
const int output_width = this->output_top_shape[3];
116+
117+
vector<int> bottom_shape = bottom[0]->shape();
118+
const int input_depth = bottom_shape[1];
119+
const int input_height = bottom_shape[2];
120+
const int input_width = bottom_shape[3];
121+
122+
const Dtype *bottom_data = bottom[0]->cpu_data();
123+
Dtype *top_data = top[0]->mutable_cpu_data();
124+
125+
for (int b = 0; b < batch_size; ++b) {
126+
for (int h = 0; h < output_height; ++h) {
127+
const int in_h = h / this->block_size;
128+
const int offset_h = (h % this->block_size);
129+
for (int w = 0; w < output_width; ++w) {
130+
const int in_w = w / this->block_size;
131+
const int offset_w = (w % this->block_size);
132+
for (int d = 0; d < output_depth; ++d) {
133+
const int in_d =
134+
(d * this->block_size + offset_h) * this->block_size + offset_w;
135+
const int out_index =
136+
((b * output_depth + d) * output_height + h) * output_width + w;
137+
const int in_index =
138+
((b * input_depth + in_d) * input_height + in_h) * input_width +
139+
in_w;
97140
top_data[out_index] = bottom_data[in_index];
98141
}
99142
}
@@ -102,8 +145,7 @@ void DepthToSpaceLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
102145
}
103146
}
104147

105-
106148
INSTANTIATE_CLASS(DepthToSpaceLayer);
107149
REGISTER_LAYER_CLASS(DepthToSpace);
108150

109-
} // namespace caffe
151+
} // namespace caffe

src/caffe/layers/space_to_batch_nd_layer.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ void SpaceToBatchNDLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
6262
const vector<Blob<Dtype>*>& top) {
6363
const Dtype* bottom_data = bottom[0]->cpu_data();
6464
Dtype* top_data = top[0]->mutable_cpu_data();
65+
const int count = top[0]->count();
66+
caffe_set(count, Dtype(0), top_data);
6567
vector<int> bottom_shape = bottom[0]->shape();
6668
vector<int> top_shape = bottom_shape;
6769

@@ -105,11 +107,12 @@ void SpaceToBatchNDLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
105107
for(int i=0; i<top_temp.size(); i++){
106108
vector<int> coord_old = indices(i, top_shape);
107109
vector<int> coord_permuted = coord_old;
108-
for(int i=0; i<coord_old.size(); i++){
109-
coord_permuted[i] = coord_old[permuted_order[i]];
110+
for(int j=0; j<coord_old.size(); j++){
111+
coord_permuted[j] = coord_old[permuted_order[j]];
110112
}
111113
int position_permuted = offset(coord_permuted, permuted_shape);
112-
copy_n(top_temp.begin()+i, 1, top_data+position_permuted);
114+
//copy_n(top_temp.begin()+i, 1, top_data+position_permuted);
115+
top_data[position_permuted] = top_temp[i];
113116
}
114117

115118
// 4. Reshape permuted_reshaped_padded to flatten block_shape into the batch dimension, producing an output tensor of shape:

0 commit comments

Comments
 (0)