-
Notifications
You must be signed in to change notification settings - Fork 7
/
tryGPUFeatureMatcher.cu
181 lines (140 loc) · 6.53 KB
/
tryGPUFeatureMatcher.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
#include <iostream>
#include <cassert>
#include "gpuFeatureMatcher.cuh"
/*
*Pre-conditions:
* `arr1` and `arr2` are each arrays of length `length`
*Post-conditions:
* Returns the L2 distance of the two arrs interpreted as vectors.
*/
__device__ float deviceComputeL2Distance(float* arr1, float* arr2, int length) {
float ssd = 0; // sum of squared distance
for (int i = 0; i < length; ++i) {
float currDistance = arr1[i] - arr2[i];
ssd += currDistance * currDistance;
}
return sqrtf(ssd);
}
int test(void) {
int numDescriptors[] = {10, 20, 30, 40, 50};
const int numImages = sizeof(numDescriptors) / sizeof(numDescriptors[0]);
int k = 32; // size of one descriptor
float matchConf = 0.1;
// Compute cumulative sum as input downstream
int cumNumDescriptors[numImages];
cumsum(numDescriptors, cumNumDescriptors, numImages);
int n = cumNumDescriptors[numImages-1]; // sum of all feature counts
cout << "Num descriptors:\n\t";
for (int i=0;i<numImages;++i) { cout << numDescriptors[i] << " "; }
cout << endl;
printf("n=%i, k=%i, numImages=%i\n", n, k, numImages);
Matrix<float> descriptorsH = AllocateMatrix<float>(n, k, 1);
// Initialize descriptors in device
Matrix<float> descriptorsD = AllocateDeviceMatrix<float>(descriptorsH);
CopyToDeviceMatrix(descriptorsD, descriptorsH);
// Compute distance matrix in device
Matrix<float> distanceMatD = AllocateDeviceMatrix<float>(AllocateMatrix<float>(n, n, 2));
gpuComputeDistanceMat(descriptorsD, distanceMatD);
cout << "Computed distance mat with GPU\n";
// Compute distance matrix in host
Matrix<float> distanceMatH = AllocateMatrix<float>(n, n, 0);
cout << "Allocated distance matrix in host\n";
cout << "address of descriptorsH = " << descriptorsH.elements << endl;
cout << "address of distanceMatH = " << distanceMatH.elements << endl;
cpuComputeDistanceMat(descriptorsH, distanceMatH);
cout << "Computed distance mat with CPU\n";
// Compute correspondence matrix in host
Matrix<int> correspondenceMatH = AllocateMatrix<int>(numImages, n, 0);
computeCorrespondenceMat(distanceMatH, cumNumDescriptors, numImages,
correspondenceMatH, matchConf);
// Compute correspondence matrix in device
Matrix<int> correspondenceMatD =
AllocateDeviceMatrix<int>(correspondenceMatH);
gpuComputeCorrespondenceMat(distanceMatD, cumNumDescriptors, numImages,
correspondenceMatD, matchConf);
/*printf("Host correspondence mat:\n");*/
/*printMatrix(correspondenceMatH);*/
/*printf("Device correspondence mat:\n");*/
/*printMatrixD(correspondenceMatD);*/
printf("Error between correspondence mat in D and H = %f\n",
getRMSEHostAndDevice(correspondenceMatH, correspondenceMatD));
/*printf("Host descriptor mat:\n");*/
/*printMatrix(descriptorsH);*/
/*printf("Device descriptor mat:\n");*/
/*printMatrixD(descriptorsD);*/
printf("Error between descriptor in D and H = %f\n",
getRMSEHostAndDevice(descriptorsH, descriptorsD));
/*printf("Host distance mat:\n");*/
/*printMatrix(distanceMatH);*/
/*printf("Device distance mat:\n");*/
/*printMatrixD(distanceMatD);*/
printf("Error between distance mat in D and H = %f\n",
getRMSEHostAndDevice(distanceMatH, distanceMatD));
/*printCorrespondence(descriptorsH, numDescriptors, numImages, matchConf);*/
/*cpuComputeDistanceMat(descriptors, distanceMat);*/
/*computeCorrespondenceMat(distanceMat, cumNumDescriptors, numImages,*/
/*corrMat, matchConf);*/
/*std::cout << "corrMat:\n";*/
/*printMatrix(corrMat);*/
FreeMatrix(&descriptorsH);
FreeMatrix(&distanceMatH);
FreeMatrix(&correspondenceMatH);
FreeDeviceMatrix(&descriptorsD);
FreeDeviceMatrix(&distanceMatD);
FreeDeviceMatrix(&correspondenceMatD);
/*cv::Mat image = cv::imread( "outputImages/result.jpg", 1 );*/
/*printf("size = (%i, %i)\n", image.rows, image.cols);*/
return 0;
}
int main(void) {
int numDescriptors[] = {2, 3, 4, 5};
const int numImages = sizeof(numDescriptors) / sizeof(numDescriptors[0]);
int k = 32; // size of one descriptor
float matchConf = 0.1;
// Compute cumulative sum as input downstream
int cumNumDescriptors[numImages];
cumsum(numDescriptors, cumNumDescriptors, numImages);
int n = cumNumDescriptors[numImages-1]; // sum of all feature counts
cout << "Num descriptors:\n\t";
for (int i=0;i<numImages;++i) { cout << numDescriptors[i] << " "; }
cout << endl;
printf("n=%i, k=%i, numImages=%i\n", n, k, numImages);
Matrix<float> descriptorsH = AllocateMatrix<float>(n, k, 1);
// Initialize descriptors in device
Matrix<float> descriptorsD = AllocateDeviceMatrix<float>(descriptorsH);
CopyToDeviceMatrix(descriptorsD, descriptorsH);
// Compute correspondence matrix in host
Matrix<int> correspondenceMatH = AllocateMatrix<int>(numImages, n, 0);
computeCorrespondenceMatFromDescriptors(descriptorsH, cumNumDescriptors,
numImages, correspondenceMatH, matchConf);
// Compute correspondence matrix in device
Matrix<int> correspondenceMatD =
AllocateDeviceMatrix<int>(correspondenceMatH);
gpuComputeCorrespondenceMatFromDescriptors(descriptorsD, cumNumDescriptors,
numImages, correspondenceMatD, matchConf);
/*printf("Host correspondence mat:\n");*/
/*printMatrix(correspondenceMatH);*/
/*printf("Device correspondence mat:\n");*/
/*printMatrixD(correspondenceMatD);*/
printf("Error between correspondence mat in D and H = %f\n",
getRMSEHostAndDevice(correspondenceMatH, correspondenceMatD));
/*printf("Host descriptor mat:\n");*/
/*printMatrix(descriptorsH);*/
/*printf("Device descriptor mat:\n");*/
/*printMatrixD(descriptorsD);*/
printf("Error between descriptor in D and H = %f\n",
getRMSEHostAndDevice(descriptorsH, descriptorsD));
/*printCorrespondence(descriptorsH, numDescriptors, numImages, matchConf);*/
/*cpuComputeDistanceMat(descriptors, distanceMat);*/
/*computeCorrespondenceMat(distanceMat, cumNumDescriptors, numImages,*/
/*corrMat, matchConf);*/
/*std::cout << "corrMat:\n";*/
/*printMatrix(corrMat);*/
FreeMatrix(&descriptorsH);
FreeMatrix(&correspondenceMatH);
FreeDeviceMatrix(&descriptorsD);
FreeDeviceMatrix(&correspondenceMatD);
/*cv::Mat image = cv::imread( "outputImages/result.jpg", 1 );*/
/*printf("size = (%i, %i)\n", image.rows, image.cols);*/
return 0;
}