-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmtranspose_layer.cpp
More file actions
80 lines (70 loc) · 2.59 KB
/
mtranspose_layer.cpp
File metadata and controls
80 lines (70 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <vector>
#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/filler.hpp"
#include "caffe/layer.hpp"
#include "caffe/util/math_functions.hpp"
#include "mtranspose_layer.hpp"
namespace caffe {
template <typename Dtype>
void MTransposeLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
MTransposeParameter mp = this->layer_param_.mtranspose_param();
groups_ = mp.groups();
}
template <typename Dtype>
void MTransposeLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
width_ = bottom[0]->width();
height_ = bottom[0]->height();
channel_ = bottom[0]->channels();
num_ = bottom[0]->num();
inner_size_ = width_*height_;
vector<int> shape;
shape.push_back(num_*channel_*height_*width_ / groups_);
shape.push_back(groups_);
top[0]->Reshape(shape);
}
template <typename Dtype>
void mtranspose_forward_cpu_kernel(const int num, const Dtype * const bottom, Dtype * const top,
const int inner_size, const int channel, const int groups) {
for (int i = 0; i < num; i++) {
int pn = i / inner_size / channel;
int pc = (i / inner_size) % channel;
int ps = i % inner_size;
int tidx = (pn*inner_size*channel/groups + pc/groups*inner_size+ps)*groups+pc%groups;
top[tidx] = bottom[i];
}
}
template <typename Dtype>
void MTransposeLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
const Dtype* bottom_data = bottom[0]->cpu_data();
mtranspose_forward_cpu_kernel<Dtype>(bottom[0]->count(), bottom_data,
top[0]->mutable_cpu_data(), inner_size_, channel_, groups_);
}
template <typename Dtype>
void mtranspose_backward_cpu_kernel(const int num, Dtype * const bottom, const Dtype * const top,
const int inner_size, const int channel, const int groups) {
for (int i = 0; i < num; i++) {
int pn = i / inner_size / channel;
int pc = (i / inner_size) % channel;
int ps = i % inner_size;
int tidx = (pn*inner_size*channel / groups + pc / groups*inner_size + ps)*groups + pc%groups;
bottom[i]=top[tidx];
}
}
template <typename Dtype>
void MTransposeLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down,
const vector<Blob<Dtype>*>& bottom) {
Dtype* bottom_diff = bottom[0]->mutable_cpu_diff();
mtranspose_backward_cpu_kernel<Dtype>(bottom[0]->count(), bottom_diff,
top[0]->cpu_diff(), inner_size_, channel_, groups_);
}
#ifdef CPU_ONLY
STUB_GPU(MTransposeLayer);
#endif
INSTANTIATE_CLASS(MTransposeLayer);
REGISTER_LAYER_CLASS(MTranspose);
} // namespace caffe