-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbscan.cu
375 lines (324 loc) · 12 KB
/
dbscan.cu
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include <opencv2/opencv.hpp>
#include <iostream>
#define WHITE_VAL 255
#define SIZE 64
struct Point{
Point() = default;
Point(int i, int j): x(i), y(j){}
int x, y;
__device__ __host__ float euclidean_distance(Point &other){
return sqrt((float)((this->x - other.x)*(this->x - other.x) + (this->y - other.y)*(this->y - other.y)));
}
__device__ __host__ int manhattan_distance(Point &other){
return abs(this->x - other.x) + abs(this->y - other.y);
}
};
__global__ void num_neighbors(int *count_list, Point *points, int no_of_nodes, float eps){
int index = blockIdx.x*SIZE + threadIdx.x;
if(index < no_of_nodes){
int temp=0;
for(int i=0;i < no_of_nodes; ++i){
if(i == index)
continue;
if(points[index].euclidean_distance(points[i]) <= eps){
temp++;
}
}
count_list[index]=temp;
}
}
__global__ void make_graph(int *adj_list, int *offset, Point *points, int no_of_nodes, int eps){
int index = blockIdx.x*SIZE + threadIdx.x;
// //debug
// if(index == 0){
// printf("testing : %d\n", offset[1]);//points[0].euclidean_distance(points[1]));
// }
if(index < no_of_nodes){
int curr_ind = 0;
for(int i=0;i < no_of_nodes; ++i){
if(i == index)
continue;
if(points[index].euclidean_distance(points[i]) <= eps){
adj_list[offset[index] + curr_ind] = i;
curr_ind++;
}
}
}
}
class Graph{
public:
Graph() = default;
~Graph(){
cudaFree(adj_list);
cudaFree(dev_prefix);
delete []prefix_sum;
}
Graph(cv::Mat &binary_image, float eps) : eps(eps){
//filter black points
find_nodes(binary_image);
std::vector<int> neighbor_list = std::vector<int>(nodes.size(), 0);
int *dev_neighbor_list;
//allocate nodes on device
Point *dev_nodes;
cudaMalloc(&dev_nodes, sizeof(Point)*nodes.size());
cudaMemcpy(dev_nodes, nodes.data(), sizeof(Point)*nodes.size(), cudaMemcpyHostToDevice);
// std::cout << (int)nodes[0].x << '\n';
// std::cout <<"size : " << nodes.size() << '\n';
cudaMalloc(&dev_neighbor_list, sizeof(int)*neighbor_list.size());
//find neighbors
dim3 dim_block(SIZE, 1);
dim3 dim_grid((nodes.size() + SIZE-1)/SIZE, 1);
num_neighbors<<<dim_grid, dim_block>>>(dev_neighbor_list, dev_nodes, nodes.size(), eps);
//back to host
cudaMemcpy(neighbor_list.data(), dev_neighbor_list, sizeof(int)*neighbor_list.size(), cudaMemcpyDeviceToHost);
// //debug
// for(int i=0;i < nodes.size(); ++i){
// std::cout << neighbor_list[i] << ' ';
// }
// std::cout << '\n';
//allocating memory to adjacency list
prefix_sum = new int[nodes.size()+1];
prefix_sum[0] = 0;
for(int i=1;i < nodes.size()+1; ++i){
prefix_sum[i] = prefix_sum[i-1] + neighbor_list[i-1];
}
// std::cout << "prefix : " << prefix_sum[nodes.size()] << '\n';
cudaMalloc(&adj_list, sizeof(int)*(prefix_sum[nodes.size()]));
cudaMalloc(&dev_prefix, sizeof(int)*(nodes.size()+1));
cudaMemcpy(dev_prefix, prefix_sum, sizeof(int)*(nodes.size()+1), cudaMemcpyHostToDevice);
make_graph<<<dim_grid, dim_block>>>(adj_list, dev_prefix, dev_nodes, nodes.size(), eps);
// //debug
// std::cout << "adj list :\n";
// int adj[prefix_sum[nodes.size()]];
// cudaMemcpy(adj, adj_list, sizeof(int)*prefix_sum[nodes.size()], cudaMemcpyDeviceToHost);
// for(int i=0;i < nodes.size(); ++i){
// for(int j=prefix_sum[i]; j < prefix_sum[i+1]; ++j){
// std::cout << adj[j] << ' ';
// }
// std::cout << '\n';
// }
cudaFree(dev_nodes);
cudaFree(dev_neighbor_list);
}
size_t size(){
return nodes.size();
}
Point node(int index){
return nodes[index];
}
private:
std::vector<Point> nodes;
int *adj_list;
int *dev_prefix;
int *prefix_sum;
float eps;
void find_nodes(cv::Mat &img){
uchar *row;
cv::MatIterator_<uchar> itr, end;
for(int i=0; i < img.rows; ++i){
row = img.ptr<uchar>(i);
for(int j=0; j < img.cols; ++j){
if(row[j] == WHITE_VAL){
nodes.push_back(Point(i,j));
}
}
}
}
friend class DBSCAN;
};
__global__ void search(int *adj_list, int *offset, uchar *frontier, uchar *v, float eps, int min_pts, int no_of_nodes, int *true_count){
int index = blockIdx.x*SIZE + threadIdx.x;
if(index < no_of_nodes){
if(frontier[index]){ //if node is a frontier
frontier[index] = 0;
// v[index]=1;
for(int neighbor=offset[index];neighbor < offset[index+1]; ++neighbor){ //set all its neighbors as frontiers
if(!v[adj_list[neighbor]]){
//if border point
if(offset[adj_list[neighbor]+1]-offset[adj_list[neighbor]] >= min_pts){
frontier[adj_list[neighbor]] = 1;
}
v[adj_list[neighbor]] = 1;
}
}
}
//the first thread sums the frontier array
int sum=0;
if(index == 0){
for(int i=0;i < no_of_nodes; ++i){
sum+=frontier[i];
}
*true_count = sum;
}
}
}
__global__ void reset(int node, uchar *frontier, uchar *v){
/*
* kernel code to do bfs
*/
int index = blockIdx.x*SIZE + threadIdx.x;
int val=0;
if(index == node)
val=1;
frontier[index] = val;
v[index] = val;
}
class DBSCAN{
public:
DBSCAN(Graph *g, float eps, int min_pts) : graph(g), no_nodes(g->nodes.size()), eps(eps), min_pts(min_pts){
//unified memory
// cudaMallocManaged(&visited, sizeof(int)*no_nodes);
// cudaMallocManaged(&labels, sizeof(int)*no_nodes);
visited = new uchar[no_nodes];
labels = new uchar[no_nodes];
cudaMallocManaged(&true_count, sizeof(int));
}
~DBSCAN(){
delete []visited;
delete []labels;
cudaFree(true_count);
}
void identify_cluster(float eps, int min_pts){
int cluster_id = 1;
for(int i=0;i < no_nodes; ++i){
visited[i] = 0;
labels[i]=0;
}
// allocating memory
uchar *frontier;
uchar *v;
cudaMalloc(&frontier, sizeof(uchar)*no_nodes);
cudaMalloc(&v, sizeof(uchar)*no_nodes);
int neighbors;
for(int node=0;node < no_nodes; ++node){
neighbors = graph->prefix_sum[node+1] - graph->prefix_sum[node];
if(!visited[node] && neighbors >= min_pts){
// std::cout << "n : " << neighbors << '\n';
// std::cout << "hi";
visited[node] = 1;
labels[node] = cluster_id;
bfs(frontier, v, node, eps, min_pts, cluster_id++);
}
}
cudaFree(frontier);
cudaFree(v);
}
void show_labels(){
std::cout << "labels :\n";
for(int i=0;i < no_nodes; ++i){
std::cout << (int)labels[i] << ' ';
}
std::cout << '\n';
}
uchar label(int index){
return labels[index];
}
private:
Graph *graph;
int no_nodes;
uchar *visited;
uchar *labels;
float eps;
int min_pts;
int *true_count;
void bfs(uchar *frontier, uchar *v, int node, float eps, int min_pts, int cluster_id){
/*
* start from a node and do bfs
*/
// //debug
// int adj[graph->prefix_sum[graph->nodes.size()]];
// cudaMemcpy(adj, graph->adj_list, sizeof(int)*graph->prefix_sum[graph->nodes.size()], cudaMemcpyDeviceToHost);
// for(int i=0;i < graph->nodes.size(); ++i){
// for(int j=graph->prefix_sum[i]; j < graph->prefix_sum[i+1]; ++j){
// std::cout << adj[j] << ' ';
// }
// std::cout << '\n';
// }
dim3 dim_block(SIZE, 1);
dim3 dim_grid((no_nodes + SIZE-1)/SIZE);
reset<<<dim_grid,dim_block>>>(node, frontier, v);
*true_count = 1;
//debug
// int counter=1;
while(*true_count){
search<<<dim_grid, dim_block>>>(graph->adj_list, graph->dev_prefix, frontier, v, eps, min_pts, no_nodes, true_count);
cudaDeviceSynchronize();
// std::cout << *true_count << '\n';
//debug
// if(counter == 10){
// break;
// }
// counter++;
}
//back to host
uchar V[no_nodes];
cudaMemcpy(V, v, sizeof(uchar)*no_nodes, cudaMemcpyDeviceToHost);
for(int node=0;node < no_nodes; ++node){
if(V[node]){
labels[node] = cluster_id;
visited[node] = 1;
}
}
}
};
int main(){
cv::Mat img = imread("/home/krutarth/Desktop/gdbscan/test.jpeg", cv::IMREAD_COLOR);
cv::Mat grey_img;
cv::Mat binary_img;
cvtColor(img, grey_img, cv::COLOR_BGR2GRAY);
threshold(grey_img, binary_img, 200, 255, cv::THRESH_BINARY);
float eps = 10.0f;
int min_pts = 10;
Graph graph(binary_img, eps);
DBSCAN scanner(&graph, 0.1f, 10);
scanner.identify_cluster(eps, min_pts);
scanner.show_labels();
cv::Mat final(binary_img.size(), CV_8UC3, cv::Scalar(0));
std::cout << final.rows << ' '<< final.cols << '\n';
std::cout << binary_img.rows << ' '<< binary_img.cols << '\n';
int max_label_ = 0;
for(int i=0;i < graph.size(); ++i){
if(scanner.label(i) > max_label_){
max_label_ = scanner.label(i);
}
}
// int count[max_label_+1]={0};
// for(int i=0;i < graph.size(); ++i){
// count[scanner.label(i)]++;
// }
// int max_count = 0;
// int max_count_label= 0;
// for(int i=1;i <= max_label_; ++i){
// if(count[i] > max_count)
// max_count = count[i];
// max_count_label= i;
// }
// std::cout << max_count << '\n';
// for(int i=0;i < graph.size(); ++i){
// if(scanner.label(i) == max_count_label)
// final.at<uchar>(graph.node(i).x, graph.node(i).y) = 255;
// }
uchar color = 255;
uchar color_diff = (255)/max_label_;
for(int label=1; label <= max_label_; ++label){
for(int j=0; j < graph.size(); ++j){
if(scanner.label(j) == label){
final.at<uchar>(graph.node(j).x, graph.node(j).y*3) = color;
final.at<uchar>(graph.node(j).x, graph.node(j).y*3+1) = color/2;
final.at<uchar>(graph.node(j).x, graph.node(j).y*3+2) = color/3;
}
}
color-=color_diff;
}
// std::cout << "debug : \n";
// for(int j=0; j < graph.size(); ++j){
// final.at<uchar>(graph.node(j).x, graph.node(j).y) = 255;
// std::cout << graph.node(j).x << ' '<< graph.node(j).y << ' ';
// }
cv::namedWindow("final", cv::WINDOW_FULLSCREEN);
// cv::namedWindow("binary", cv::WINDOW_NORMAL);
imshow("final", final);
// imshow("binary", binary_img);
cv::waitKey(0);
// return 0;
}