-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageVariationalMethods.cu
More file actions
314 lines (267 loc) · 11.7 KB
/
imageVariationalMethods.cu
File metadata and controls
314 lines (267 loc) · 11.7 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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include "imageVariationalMethods.cuh"
#include <stdint.h>
#define THREADS_PER_BLOCK 32
#define STABILITY_EPSILON 0.01
// the fast inverse sqrt algorithm adapted for doubles and cuda
// https://en.wikipedia.org/wiki/Fast_inverse_square_root
__device__ double fast_inverse_sqrt(double number) {
uint64_t i;
double x2, y;
const double threehalfs = 1.5F;
x2 = number * 0.5F;
y = number;
memcpy(&i, &y, sizeof(y));
i = 0x5FE6EB50C7B537A9 - (i >> 1); // magic constant
memcpy(&y, &i, sizeof(y));
y = y * (threehalfs - (x2 * y * y)); // 1st iteration
y = y * (threehalfs - (x2 * y * y)); // 2nd iteration, this can be removed
return y;
}
// calculates the element-wise inverse square root of a cubed entry
// result[i] = 1/((a[i]^3)^.5)
__global__ void inverse_three_halves(double *result, double *a, double epsilon, int width, int height) {
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < width * height) {
double f = a[idx] + epsilon; // adding epsilon avoids a numerical singularity
f = f * f * f; // cube step
result[idx] = fast_inverse_sqrt(f); // inverse square root step
}
}
// calculates centered difference approximation to partial derivative of u with respect to x
__global__ void calc_ux(double *u_x, double *u, int width, int height) {
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < width * height) {
int column = idx % width;
double u_left;
double u_right;
if (column == 0) {
u_right = u[idx + 1];
u_left = u[idx + 1];
} else if (column == width - 1) {
u_right = u[idx - 1];
u_left = u[idx - 1];
} else {
u_right = u[idx + 1];
u_left = u[idx - 1];
}
u_x[idx] = (u_right - u_left) / 2;
}
}
// calculates centered difference approximation to partial derivative of u with respect to y
__global__ void calc_uy(double *u_y, double *u, int width, int height) {
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < width * height) {
int column = idx % width;
int row = (idx - column) / width;
double u_up;
double u_down;
if (row == 0) {
u_up = u[idx + width];
u_down = u[idx + width];
} else if (row == height - 1) {
u_up = u[idx - width];
u_down = u[idx - width];
} else {
u_up = u[idx - width];
u_down = u[idx + width];
}
u_y[idx] = (u_up - u_down) / 2;
}
}
// calculates centered difference approximation to second partial derivative of u with respect to x
__global__ void calc_uxx(double *u_xx, double *u, int width, int height) {
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < width * height) {
int column = idx % width;
double u_left;
double u_right;
double u_center = u[idx];
if (column == 0) {
u_right = u[idx + 1];
u_left = u[idx + 1];
} else if (column == width - 1) {
u_right = u[idx - 1];
u_left = u[idx - 1];
} else {
u_right = u[idx + 1];
u_left = u[idx - 1];
}
u_xx[idx] = u_right + u_left - 2 * u_center;
}
}
// calculates centered difference approximation to second partial derivative of u with respect to y
__global__ void calc_uyy(double *u_yy, double *u, int width, int height) {
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < width * height) {
int column = idx % width;
int row = (idx - column) / width;
double u_up;
double u_down;
double u_center = u[idx];
if (row == 0) {
u_up = u[idx + width];
u_down = u[idx + width];
} else if (row == height - 1) {
u_up = u[idx - width];
u_down = u[idx - width];
} else {
u_up = u[idx - width];
u_down = u[idx + width];
}
u_yy[idx] = u_up + u_down - 2 * u_center;
}
}
// calculates result[i] = (s1 * a[i]) + (s2 * b[i]) for valid i
__global__ void scaledMatrixSum(double *result, double *a, double *b, double s1, double s2, int width, int height) {
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < width * height) {
result[idx] = (s1 * a[idx]) + (s2 * b[idx]);
}
}
// calculates result[i] = scalar * a[i] * b[i] for valid i
__global__ void
scaledMultiplyMatrixEntries(double *result, double *a, double *b, double scalar, int width, int height) {
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < width * height) {
result[idx] = scalar * a[idx] * b[idx];
}
}
// performs in-place variational diffusion on an image
void imageVariationalMethods::diffusion(double **pixelArrays, int width, int height, int depth, double deltaTime,
double lambda, int numSteps) {
int totalThreads = width * height;
int blockSize = THREADS_PER_BLOCK;
int numBlocks = (totalThreads + blockSize - 1) / blockSize;
// setup the device matrices that we will need
double *f;
double *u_curr;
double *u_t;
double *u_xx;
double *u_yy;
cudaMalloc(&f, width * height * sizeof(double));
cudaMalloc(&u_curr, width * height * sizeof(double));
cudaMalloc(&u_t, width * height * sizeof(double));
cudaMalloc(&u_xx, width * height * sizeof(double));
cudaMalloc(&u_yy, width * height * sizeof(double));
// perform variational diffusion on each image plane (see paper/report for math details)
for (int k = 0; k < depth; k++) {
// copy inputs to device
cudaMemcpy(f, pixelArrays[k], width * height * sizeof(double), cudaMemcpyHostToDevice);
cudaMemcpy(u_curr, pixelArrays[k], width * height * sizeof(double), cudaMemcpyHostToDevice);
// evolve the gradient descent flow
for (int i = 0; i < numSteps; i++) {
calc_uxx<<<numBlocks, blockSize>>>(u_xx, u_curr, width, height);
calc_uyy<<<numBlocks, blockSize>>>(u_yy, u_curr, width, height);
cudaDeviceSynchronize();
// u_t = u_xx + u_yy - the laplacian of u
scaledMatrixSum<<<numBlocks, blockSize>>>(u_t, u_xx, u_yy, 1.0, 1.0, width, height);
cudaDeviceSynchronize();
// u_t = f + lambda * u_t
scaledMatrixSum<<<numBlocks, blockSize>>>(u_t, f, u_t, 1.0, lambda, width, height);
cudaDeviceSynchronize();
// u_t = -u + u_t
scaledMatrixSum<<<numBlocks, blockSize>>>(u_t, u_curr, u_t, -1.0, 1.0, width, height);
cudaDeviceSynchronize();
// u_curr = deltat * u_t + u_curr (euler's method)
scaledMatrixSum<<<numBlocks, blockSize>>>(u_curr, u_t, u_curr, deltaTime, 1.0, width, height);
cudaDeviceSynchronize();
}
// copy results out
cudaMemcpy(pixelArrays[k], u_curr, width * height * sizeof(double), cudaMemcpyDeviceToHost);
}
// free resources
cudaFree(f);
cudaFree(u_curr);
cudaFree(u_t);
cudaFree(u_xx);
cudaFree(u_yy);
}
// performs a in-place total variation reduction on an image
void imageVariationalMethods::total(double **pixelArrays, int width, int height, int depth, double deltaTime,
double lambda, int numSteps) {
int totalThreads = width * height;
int blockSize = THREADS_PER_BLOCK;
int numBlocks = (totalThreads + blockSize - 1) / blockSize;
// setup the device matrices that we will need
double *f;
double *u_curr;
double *u_t;
double *u_x;
double *u_y;
double *u_xy;
double *u_xx;
double *u_yy;
cudaMalloc(&f, width * height * sizeof(double));
cudaMalloc(&u_curr, width * height * sizeof(double));
cudaMalloc(&u_t, width * height * sizeof(double));
cudaMalloc(&u_x, width * height * sizeof(double));
cudaMalloc(&u_y, width * height * sizeof(double));
cudaMalloc(&u_xy, width * height * sizeof(double));
cudaMalloc(&u_xx, width * height * sizeof(double));
cudaMalloc(&u_yy, width * height * sizeof(double));
// perform total variational descent on each image plane (see paper/report for math details)
for (int k = 0; k < depth; k++) {
// copy inputs to device
cudaMemcpy(f, pixelArrays[k], width * height * sizeof(double), cudaMemcpyHostToDevice);
cudaMemcpy(u_curr, pixelArrays[k], width * height * sizeof(double), cudaMemcpyHostToDevice);
// evolve the gradient descent flow
for (int i = 0; i < numSteps; i++) {
// calculate pure partials
calc_uxx<<<numBlocks, blockSize>>>(u_xx, u_curr, width, height);
calc_uyy<<<numBlocks, blockSize>>>(u_yy, u_curr, width, height);
calc_uy<<<numBlocks, blockSize>>>(u_y, u_curr, width, height);
calc_ux<<<numBlocks, blockSize>>>(u_x, u_curr, width, height);
cudaDeviceSynchronize();
// calculate mixed partial
calc_uy<<<numBlocks, blockSize>>>(u_xy, u_x, width, height);
cudaDeviceSynchronize();
// u_xy = -2 * u_xy * u_x * u_y (step 1)
scaledMultiplyMatrixEntries<<<numBlocks, blockSize>>>(u_xy, u_xy, u_x, -2.0, width, height);
cudaDeviceSynchronize();
scaledMultiplyMatrixEntries<<<numBlocks, blockSize>>>(u_xy, u_xy, u_y, 1, width, height);
cudaDeviceSynchronize();
// u_x = u_x^2, u_y = y_y^2 (step 2)
scaledMultiplyMatrixEntries<<<numBlocks, blockSize>>>(u_y, u_y, u_y, 1.0, width, height);
scaledMultiplyMatrixEntries<<<numBlocks, blockSize>>>(u_x, u_x, u_x, 1.0, width, height);
cudaDeviceSynchronize();
// u_yy = u_yy * u_x^2, u_xx = u_xx * u_y^2 (step 3)
scaledMultiplyMatrixEntries<<<numBlocks, blockSize>>>(u_yy, u_yy, u_x, 1.0, width, height);
scaledMultiplyMatrixEntries<<<numBlocks, blockSize>>>(u_xx, u_xx, u_y, 1.0, width, height);
cudaDeviceSynchronize();
// u_xy = u_xy + u_yy + u_xx forming the top part of the big eq... (step 4)
scaledMatrixSum<<<numBlocks, blockSize>>>(u_xy, u_xy, u_yy, 1.0, 1.0, width, height);
cudaDeviceSynchronize();
scaledMatrixSum<<<numBlocks, blockSize>>>(u_xy, u_xy, u_xx, 1.0, 1.0, width, height);
cudaDeviceSynchronize();
// u_x = u_x^2 + u_y^2 (step 5)
scaledMatrixSum<<<numBlocks, blockSize>>>(u_x, u_x, u_y, 1.0, 1.0, width, height);
cudaDeviceSynchronize();
// u_x = inverse three halves (step 6)
inverse_three_halves<<<numBlocks, blockSize>>>(u_x, u_x, STABILITY_EPSILON, width, height);
cudaDeviceSynchronize();
// u_x = u_x * u_xy (step 7)
scaledMultiplyMatrixEntries<<<numBlocks, blockSize>>>(u_x, u_xy, u_x, 1.0, width, height);
cudaDeviceSynchronize();
// u_y = -lambda*(u_curr - f) (step 8)
scaledMatrixSum<<<numBlocks, blockSize>>>(u_y, u_curr, f, -lambda, lambda, width, height);
cudaDeviceSynchronize();
// u_t = u_y + u_x (step 9)
scaledMatrixSum<<<numBlocks, blockSize>>>(u_t, u_x, u_y, 1.0, 1.0, width, height);
cudaDeviceSynchronize();
// u_curr = deltat*u_t + u_curr (euler's method)
scaledMatrixSum<<<numBlocks, blockSize>>>(u_curr, u_t, u_curr, deltaTime, 1.0, width, height);
cudaDeviceSynchronize();
}
// copy results out
cudaMemcpy(pixelArrays[k], u_curr, width * height * sizeof(double), cudaMemcpyDeviceToHost);
}
// free resources
cudaFree(f);
cudaFree(u_curr);
cudaFree(u_t);
cudaFree(u_x);
cudaFree(u_y);
cudaFree(u_xy);
cudaFree(u_xx);
cudaFree(u_yy);
}