-
Notifications
You must be signed in to change notification settings - Fork 29
/
sp.h
252 lines (197 loc) · 7.12 KB
/
sp.h
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include <stdio.h>
#ifndef MY_SP_STRUCT
#define MY_SP_STRUCT
#include "my_sp_struct.h"
#endif
__device__ __forceinline__ float atomicMaxFloat2 (float * addr, float value) {
float old;
old = (value >= 0) ? __int_as_float(atomicMax((int *)addr, __float_as_int(value))) :
__uint_as_float(atomicMin((unsigned int *)addr, __float_as_uint(value)));
return old;
}
/*bool nums_bool[256];
int nums_indices[48] = {2, 3, 6, 7, 8, 9, 11, 15, 16, 20, 22, 23, 31,
40, 41, 43, 47, 63, 64, 96, 104, 105, 107, 111, 144, 148,
150, 151, 159, 191, 192, 208, 212, 214, 215, 224, 232, 233, 235,
239, 240, 244, 246, 247, 248, 249, 252, 253};
for (int i = 0; i<256; i++){
nums_bool[i] = 0;
for(int j = 0; j< 48; j++){
if(nums_indices[i]==j) nums_bool[i] =1;
}
*/
//bass table
__device__ const bool bass_lut[256] = {0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0,
1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1,
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0,
0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0,
0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0};
/*
__device__ inline int ischangbale_by_nbrs(bool* nbrs){
// This function does the following:
// 1) converts the arrray of binary labels of the 8 nbrs into an integer btwn 0 and 255
// 2) does a lookup check and count number of different labels, than set the last cell of nbrs array as
// 0 or 1 which means if it is valid or not.
int num,count_diff = 0;
#pragma unroll
for (int i=7; i>=0; i--){
num <<= 1;
if (nbrs[i]) num++;
else count_diff++;
}
if (num == 0)
return 0;
else {
nbrs[8] = 0;
// indexes of valid configuration
int nums [48] = {2, 3, 6, 7, 8, 9, 11, 15, 16, 20, 22, 23, 31,
40, 41, 43, 47, 63, 64, 96, 104, 105, 107, 111, 144, 148,
150, 151, 159, 191, 192, 208, 212, 214, 215, 224, 232, 233, 235,
239, 240, 244, 246, 247, 248, 249, 252, 253};
for(int j = 0; j< 48; j++){
if(num == nums[j]){nbrs[8] = 1;}
}
//nbrs[8] = lut[num];
return count_diff;
}
//return ischangbale_by_num(num);
}
*/
__device__ inline int ischangbale_by_nbrs(bool* nbrs){
int num,count_diff = 0;
#pragma unroll
for (int i=7; i>=0; i--)
{
num <<= 1;
if (nbrs[i]) num++;
else count_diff++;
}
nbrs[8] = bass_lut[num];
return count_diff;
}
/*
* Set the elements in nbrs "array" to 1 if corresponding neighbor pixel has the same superpixel as "label"
*/
__device__ inline void set_nbrs(int NW,
int N,
int NE,
int W,
int E,
int SW,
int S,
int SE,
int label, bool* nbrs)
{
nbrs[0] = (label ==NW);
nbrs[1] = (label == N);
nbrs[2] = (label == NE);
nbrs[3] = (label == W);
nbrs[4] = (label == E);
nbrs[5] = (label == SW);
nbrs[6] = (label == S);
nbrs[7] = (label == SE);
return;
}
__device__ inline float2 cal_posterior_new(
float* img, int* seg,
int x, int y,
superpixel_params* sp_params,
int idx,
int seg_idx,
float3 J_i, float logdet_Sigma_i, float i_std, int s_std,
post_changes_helper* post_changes, float potts, float beta, float2 res_max)
{
float res = -1000; // some large negative number
float* imgC = img + idx * 3;
//if (idx>154064)
// printf("%d ,%d , %d\n",idx_inside, idx, seg_idx);
const float x0 = __ldg(&imgC[0])-__ldg(&sp_params[seg_idx].mu_i.x);
const float x1 = __ldg(&imgC[1])-__ldg(&sp_params[seg_idx].mu_i.y);
const float x2 = __ldg(&imgC[2])-__ldg(&sp_params[seg_idx].mu_i.z);
const int d0 = x - __ldg(&sp_params[seg_idx].mu_s.x);
const int d1 = y - __ldg(&sp_params[seg_idx].mu_s.y);
//color component
const float J_i_x = J_i.x;
const float J_i_y = J_i.y;
const float J_i_z = J_i.z;
const float sigma_s_x = __ldg(&sp_params[seg_idx].sigma_s.x);
const float sigma_s_y = __ldg(&sp_params[seg_idx].sigma_s.y);
const float sigma_s_z = __ldg(&sp_params[seg_idx].sigma_s.z);
const float logdet_sigma_s = __ldg(&sp_params[seg_idx].logdet_Sigma_s);
res = res - (x0*x0*J_i_x + x1*x1*J_i_y + x2*x2*J_i_z); //res = -calc_squared_mahal_3d(imgC,mu_i,J_i);
res = res -logdet_Sigma_i;
//space component
res = res - d0*d0*sigma_s_x;
res = res - d1*d1*sigma_s_z;
res = res - 2*d0*d1*sigma_s_y; // res -= calc_squared_mahal_2d(pt,mu_s,J_s);
res = res - logdet_sigma_s;
res = res -beta*potts;
/*if (res > atomicMaxFloat2(&post_changes[idx].post[4],res))
{
seg[idx] = seg_idx;
}*/
if( res>res_max.x)
{
res_max.x = res;
res_max.y = seg_idx;
}
return res_max;
}
/*__device__ inline float cal_posterior(
bool isValid,
float* imgC,
int x, int y, double* pt,
superpixel_params* sp_params,
int seg,
float3 J_i, float logdet_Sigma_i,
bool cal_cov, float i_std, int s_std)
{
float res = -1000; // some large negative number
if (isValid){
const float3 mu_i = sp_params[seg].mu_i;
const double3 sigma_s = sp_params[seg].sigma_s;
const double2 mu_s = sp_params[seg].mu_s;
float x0 = imgC[0]-mu_i.x;
float x1 = imgC[1]-mu_i.y;
float x2 = imgC[2]-mu_i.z;
double d0 = x - mu_s.x;
double d1 = y - mu_s.y;
const double logdet_Sigma_s = sp_params[seg].logdet_Sigma_s;
const double log_count = sp_params[seg].log_count;
//color component
res = - (x0*x0*J_i.x + x1*x1*J_i.y + x2*x2*J_i.z); //res = -calc_squared_mahal_3d(imgC,mu_i,J_i);
res -= logdet_Sigma_i;
//space component
res -= d0*d0*sigma_s.x;
res -= d1*d1*sigma_s.z;
res -= 2*d0*d1*sigma_s.y; // res -= calc_squared_mahal_2d(pt,mu_s,J_s);
res -= logdet_Sigma_s;
//res += potts_res;
//add in prior prob
#if USE_COUNTS
const double prior_weight = 0.5;
res *= (1-prior_weight);
double prior = prior_weight * log_count;
res += prior;
#endif
}
return res;
}
*/
__device__ inline float calc_total(float posterior, float potts_term){
float total = 0;
total = posterior +potts_term;
return total;
}
__device__ inline float calc_potts(float beta, int count_diff){
float potts;
potts = -beta*count_diff;
return potts;
}