-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathdensecrf_loss_layer.cpp
181 lines (151 loc) · 5.82 KB
/
densecrf_loss_layer.cpp
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// DenseCRF loss layer
#include <vector>
#include <stdio.h>
#include <float.h>
#include <math.h>
#include "caffe/layers/densecrf_loss_layer.hpp"
#include <omp.h>
namespace caffe{
template <typename Dtype>
DenseCRFLossLayer<Dtype>::~DenseCRFLossLayer() {
delete AS;
delete cropping_batch;
}
template <typename Dtype>
void DenseCRFLossLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top){
// blob size
N = bottom[0]->shape(0);
C = bottom[1]->shape(1);
H = bottom[0]->shape(2);
W = bottom[0]->shape(3);
// Gaussian kernel parameters
DenseCRFLossParameter densecrf_loss_param = this->layer_param_.densecrf_loss_param();
bi_xy_std_ = densecrf_loss_param.bi_xy_std();
bi_rgb_std_ = densecrf_loss_param.bi_rgb_std();
printf("LayerSetup\n");
AS = new Blob<Dtype>(N,C,H,W);
cropping_batch = new Dtype[N*H*W];
permutohedrals = vector<Permutohedral>(N);
const int maxNumThreads = omp_get_max_threads();
printf("Maximum number of threads for this machine: %i\n", maxNumThreads);
printf("Total number of cores in the CPU: %ld\n", sysconf(_SC_NPROCESSORS_ONLN));
omp_set_num_threads(std::min(maxNumThreads,30));
}
template <typename Dtype>
void DenseCRFLossLayer<Dtype>::Reshape(const vector<Blob<Dtype>*> & bottom,
const vector<Blob<Dtype>*> & top)
{
top[0]->Reshape(1,1,1,1);
}
template <typename Dtype>
void DenseCRFLossLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*> & bottom,
const vector<Blob<Dtype>*> & top)
{
const Dtype* images = bottom[0]->cpu_data();
const Dtype* segmentations = bottom[1]->cpu_data();
// cropping
caffe_set(N*H*W, Dtype(1.0), cropping_batch);
for(int n=0;n<N;n++){
const Dtype * image = images + H*W*3*n;
for(int h=0;h<H;h++){
for(int w=0;w<W;w++){
if(((int)(image[0*H*W + h*W + w])==0)&&((int)(image[1*H*W + h*W + w])==0)&&((int)(image[2*H*W + h*W + w])==0))
cropping_batch[n*H*W + h*W + w] = Dtype(0);
}
}
}
//printf("DenseCRF forward\n");
//printf("bi std %.2f %.2f\n", bi_xy_std_, bi_rgb_std_);
// initialize permutohedrals
#pragma omp parallel for
//printf("Permutohedral Number of threads: %d\n", omp_get_num_threads());
for(int n=0;n<N;n++){
const Dtype * image = images + H*W*3*n;
initializePermutohedral((float *)image, W, H, bi_rgb_std_, bi_xy_std_, permutohedrals[n]);
//printf("Permutohedral Thread %d: %d\n", omp_get_thread_num(), n);
}
Dtype densecrf_loss = Dtype(0);
#pragma omp parallel for reduction(+: densecrf_loss)
for(int n=0;n<N;n++){
const Dtype * image = images + H*W*3*n;
//printf("size of Dtype %d\n", sizeof(new Dtype[1]));
//exit(-1);
densecrf_loss = densecrf_loss + Compute_DenseCRF(image, segmentations + H*W*C*n, AS->mutable_cpu_data() + n*C*H*W, cropping_batch + n*H*W, permutohedrals[n]);
//exit(-1);
//printf(" Thread %d: %d\n", omp_get_thread_num(), n);
}
densecrf_loss = densecrf_loss / N;
Dtype* top_data = top[0]->mutable_cpu_data();
top_data[0] = densecrf_loss;
}
template <typename Dtype>
void DenseCRFLossLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*> & top,
const vector<bool> & propagate_down,
const vector<Blob<Dtype>*> & bottom)
{
if (propagate_down[0]) {
LOG(FATAL) << this->type()
<< " Layer cannot backpropagate to image inputs.";
}
if (propagate_down[1]) {
//printf("DenseCRF backward\n");
const Dtype* images = bottom[0]->cpu_data();
const Dtype* segmentations = bottom[1]->cpu_data();
Dtype* bottom_diff = bottom[1]->mutable_cpu_diff();
#pragma omp parallel for
for(int n=0;n<N;n++){
const Dtype * image = images + H*W*3*n;
Gradient_DenseCRF(image, segmentations + H*W*C*n, bottom_diff + H*W*C*n, AS->cpu_data()+n*C*H*W, cropping_batch + n*H*W);
}
// Scale gradient
Dtype loss_weight = top[0]->cpu_diff()[0] / N;
caffe_scal(bottom[1]->count(), loss_weight, bottom_diff);
//printf("loss_weight is %.2f\n", loss_weight);
}
}
template <typename Dtype>
Dtype DenseCRFLossLayer<Dtype>::Compute_DenseCRF(const Dtype * image, const Dtype * segmentation, Dtype * AS_data, const Dtype * cropping, Permutohedral & permutohedral)
{
Dtype densecrf_loss = 0;
// segmentation in cropping, zero outside
Dtype * temp = new Dtype[H*W];
for(int c=0;c<C;c++){
caffe_mul(H*W, segmentation+c*W*H, cropping, temp);
//printf("sum of segmentation of this channel %.8f\n", caffe_cpu_dot(H*W, temp, allones->cpu_data()));
permutohedral.compute((float *)AS_data + c*W*H, (float *)temp, 1);
Dtype SAS = caffe_cpu_dot(H*W, temp, AS_data + c*W*H);
densecrf_loss -= SAS;
if(isnan(SAS))
LOG(FATAL) << this->type()
<< " Layer SAS: "<< SAS <<std::endl;
}
delete [] temp;
return densecrf_loss;
}
template <typename Dtype>
void DenseCRFLossLayer<Dtype>::Gradient_DenseCRF(const Dtype * image, const Dtype * segmentation, Dtype * gradients, const Dtype * AS_data, const Dtype * cropping){
caffe_set(H*W*C, Dtype(0), gradients);
// segmentation in cropping, zero outside
Dtype * temp = new Dtype[H*W];
for(int c=0;c<C;c++){
caffe_mul(H*W, segmentation+c*W*H, cropping, temp);
for(int i=0;i<H * W;i++){
gradients[H*W*c + i] = - 2 * AS_data[i+c*H*W];
if(isnan(gradients[H*W*c + i]))
LOG(FATAL) << this->type()
<< " Layer gradient is nan!"<<std::endl;
}
caffe_mul(H*W, gradients + c*H*W, cropping, gradients + c*H*W);
}
delete temp;
for(int c=0;c<C;c++)
caffe_mul(H*W, gradients + c*H*W, cropping, gradients + c*H*W);
//printf("end of gradient_densecrf\n");
}
#ifdef CPU_ONLY
STUB_GPU(DenseCRFLossLayer);
#endif
INSTANTIATE_CLASS(DenseCRFLossLayer);
REGISTER_LAYER_CLASS(DenseCRFLoss);
} // namespace caffe