def match(self): if self.kp_ocl.size > 0 and self.kp_cpp > 0: t0 = time.time() match = feature.sift_match(self.kp_ocl, self.kp_cpp) t1 = time.time() print("Number of Keypoints with OpenCL: %i, With C++: %i, Matching keypoints: %i, Processing time: %.3fs" % (self.kp_ocl.size, self.kp_cpp.size, match.shape[0], (t1 - t0))) self.fig.canvas.draw() else: print("No keypoints, cannot match")
logger.error("Feature is not available to compare results with C++ implementation") feature = None res = numpy.empty(0) img1 = scipy.misc.imread("testimages/img1.jpg") img2 = scipy.misc.imread("testimages/img2.jpg") plan = sift.SiftPlan(template=img1, devicetype="gpu") kp1 = plan.keypoints(img1) kp2 = plan.keypoints(img2) fig = pylab.figure() sp1 = fig.add_subplot(122) sp2 = fig.add_subplot(121) sp1.imshow(img1) sp2.imshow(img2) match = feature.sift_match(kp1, kp2) fig.show() # raw_input("Enter") for m in match: con = ConnectionPatch( xyA=(m["x0"], m["y0"]), xyB=(m["x1"], m["y1"]), coordsA="data", coordsB="data", axesA=sp1, axesB=sp2, color="red", ) sp1.add_artist(con) # sp2.add_artist(con)
def test_matching(self): ''' tests keypoints matching kernel ''' if hasattr(scipy.misc, "ascent"): image = scipy.misc.ascent().astype(numpy.float32) else: image = scipy.misc.lena().astype(numpy.float32) if (feature is not None) and USE_CPP_SIFT: # get the struct keypoints : (x,y,s,angle,[descriptors]) sc = feature.SiftAlignment() ref_sift = sc.sift(image) else: sp = SiftPlan(template=image) ref_sift = sp.keypoints(image) ref_sift_2 = numpy.recarray((ref_sift.shape), dtype=ref_sift.dtype) ref_sift_2[:] = (ref_sift[::-1]) if (feature is not None and USE_CPP_MATCH): t0_matching = time.time() siftmatch = feature.sift_match(ref_sift, ref_sift_2) t1_matching = time.time() reference = "CPP" else: t0_matching = time.time() siftmatch = match_py(ref_sift, ref_sift_2, raw_results=True) t1_matching = time.time() reference = "NumPy" if (USE_CPU): wg = 1, else: wg = 64, shape = ref_sift.shape[0] * wg[0], ratio_th = numpy.float32(0.5329) # sift.cpp : 0.73*0.73 keypoints_start, keypoints_end = 0, min(ref_sift.shape[0], ref_sift_2.shape[0]) gpu_keypoints1 = pyopencl.array.to_device(self.queue, ref_sift) gpu_keypoints2 = pyopencl.array.to_device(self.queue, ref_sift_2) gpu_matchings = pyopencl.array.zeros( self.queue, (keypoints_end - keypoints_start, 2), dtype=numpy.int32, order="C") keypoints_start, keypoints_end = numpy.int32( keypoints_start), numpy.int32(keypoints_end) nb_keypoints = numpy.int32(10000) counter = pyopencl.array.zeros(self.queue, (1, 1), dtype=numpy.int32, order="C") t0 = time.time() k1 = self.program.matching(self.queue, shape, wg, gpu_keypoints1.data, gpu_keypoints2.data, gpu_matchings.data, counter.data, nb_keypoints, ratio_th, keypoints_end, keypoints_end) res = gpu_matchings.get() cnt = counter.get() t1 = time.time() res_sort = res[numpy.argsort(res[:, 0])] logger.debug("%s", res_sort[0:20]) logger.debug("%s Matching took %.3f ms", reference, 1000.0 * (t1_matching - t0_matching)) logger.debug("OpenCL: %d match / %s : %d match", cnt, reference, siftmatch.shape[0]) # sort to compare added keypoints self.assertEqual(cnt, siftmatch.shape[0], "number of matching element is the same") delta = abs(res_sort - siftmatch).max() self.assertEqual(delta, 0, "Matching keypoints are actually the same") #logger.info("delta=%s" % delta) if self.PROFILE: logger.debug("Global execution time: %.3fms." % (1000.0 * (t1 - t0))) logger.debug("Matching on device took %.3fms" % (1e-6 * (k1.profile.end - k1.profile.start)))
y = ref[i, 1] scale = ref[i, 2] angle = ref[i, 3] sp2.annotate("", xy=(x, y), xytext=(x + scale * cos(angle), y + scale * sin(angle)), color="red", arrowprops=dict(facecolor='red', edgecolor='red', width=1),) sp1.annotate("", xy=(x, y), xytext=(x + scale * cos(angle), y + scale * sin(angle)), color="red", arrowprops=dict(facecolor='red', edgecolor='red', width=1),) for i in range(kp.shape[0]): x = kp[i, 0] y = kp[i, 1] scale = kp[i, 2] angle = kp[i, 3] sp1.annotate("", xy=(x, y), xytext=(x + scale * cos(angle), y + scale * sin(angle)), color="blue", arrowprops=dict(facecolor='blue', edgecolor='blue', width=1),) #print numpy.degrees((ref[numpy.argsort(res.scale)][:392] - kp[numpy.argsort(kpg.scale)][:392])[:,3]) #print res[:5] #print ""*80 #print kpg[:5] match = feature.sift_match(res, kpg) #print match print match.shape fig.show() fig.show() raw_input()
def test_matching(self): ''' tests keypoints matching kernel ''' image = scipy.misc.ascent().astype(numpy.float32) try: import feature except: logger.error("WARNING: feature module is not available to compare results with C++ implementation. Matching cannot be tested.") feature = None if (feature is not None): # get the struct keypoints : (x,y,s,angle,[descriptors]) sc = feature.SiftAlignment() ref_sift = sc.sift(image) ref_sift_2 = numpy.recarray((ref_sift.shape), dtype=ref_sift.dtype) ref_sift_2[:] = (ref_sift[::-1]) t0_matching = time.time() siftmatch = feature.sift_match(ref_sift, ref_sift_2) t1_matching = time.time() ref = ref_sift.desc if (USE_CPU): wg = 1, else: wg = 64, shape = ref_sift.shape[0] * wg[0], ratio_th = numpy.float32(0.5329) # sift.cpp : 0.73*0.73 keypoints_start, keypoints_end = 0, min(ref_sift.shape[0], ref_sift_2.shape[0]) gpu_keypoints1 = pyopencl.array.to_device(self.queue, ref_sift) gpu_keypoints2 = pyopencl.array.to_device(self.queue, ref_sift_2) gpu_matchings = pyopencl.array.zeros(self.queue, (keypoints_end - keypoints_start, 2), dtype=numpy.int32, order="C") keypoints_start, keypoints_end = numpy.int32(keypoints_start), numpy.int32(keypoints_end) nb_keypoints = numpy.int32(10000) counter = pyopencl.array.zeros(self.queue, (1, 1), dtype=numpy.int32, order="C") t0 = time.time() k1 = self.program.matching(self.queue, shape, wg, gpu_keypoints1.data, gpu_keypoints2.data, gpu_matchings.data, counter.data, nb_keypoints, ratio_th, keypoints_end, keypoints_end) res = gpu_matchings.get() cnt = counter.get() t1 = time.time() # ref_python, nb_match = my_matching(kp1, kp2, keypoints_start, keypoints_end) t2 = time.time() res_sort = res[numpy.argsort(res[:, 1])] # ref_sort = ref[numpy.argsort(ref[:,1])] logger.info("%s", res[0:20]) # print ref_sort[0:20] logger.info("C++ Matching took %.3f ms" , 1000.0 * (t1_matching - t0_matching)) logger.info("OpenCL: %d match / C++ : %d match" , cnt, siftmatch.shape[0]) # sort to compare added keypoints ''' delta = abs(res_sort-ref_sort).max() self.assert_(delta == 0, "delta=%s" % (delta)) #integers logger.info("delta=%s" % delta) ''' if self.PROFILE: logger.info("Global execution time: CPU %.3fms, GPU: %.3fms." % (1000.0 * (t2 - t1), 1000.0 * (t1 - t0))) logger.info("Matching took %.3fms" % (1e-6 * (k1.profile.end - k1.profile.start)))
logger.error("Feature is not available to compare results with C++ implementation") feature = None res = numpy.empty(0) img1 = scipy.misc.imread("testimages/img1.jpg") img2 = scipy.misc.imread("testimages/img2.jpg") plan = sift.SiftPlan(template=img1, devicetype="gpu") kp1 = plan.keypoints(img1) kp2 = plan.keypoints(img2) fig = pylab.figure() sp1 = fig.add_subplot(122) sp2 = fig.add_subplot(121) sp1.imshow(img1) sp2.imshow(img2) match = feature.sift_match(kp1, kp2) fig.show() #raw_input("Enter") for m in match: con = ConnectionPatch(xyA=(m["x0"], m["y0"]), xyB=(m["x1"], m["y1"]), coordsA="data", coordsB="data", axesA=sp1, axesB=sp2, color="red") sp1.add_artist(con) # sp2.add_artist(con) x = m["x0"] y = m["y0"] scale = m["scale0"] angle = m["angle0"] x0 = x + scale * cos(angle) y0 = y + scale * sin(angle) sp1.annotate("", xy=(x, y), xytext=(x0, y0), color="blue", arrowprops=dict(facecolor='blue', edgecolor='blue', width=1),)
def test_matching(self): ''' tests keypoints matching kernel ''' image = scipy.misc.lena().astype(numpy.float32) try: import feature except: logger.error( "WARNING: feature module is not available to compare results with C++ implementation. Matching cannot be tested." ) feature = None if (feature != None): #get the struct keypoints : (x,y,s,angle,[descriptors]) sc = feature.SiftAlignment() ref_sift = sc.sift(image) ref_sift_2 = numpy.recarray((ref_sift.shape), dtype=ref_sift.dtype) ref_sift_2[:] = (ref_sift[::-1]) t0_matching = time.time() siftmatch = feature.sift_match(ref_sift, ref_sift_2) t1_matching = time.time() ref = ref_sift.desc if (USE_CPU): wg = 1, else: wg = 64, shape = ref_sift.shape[0] * wg[0], ratio_th = numpy.float32(0.5329) #sift.cpp : 0.73*0.73 keypoints_start, keypoints_end = 0, min(ref_sift.shape[0], ref_sift_2.shape[0]) gpu_keypoints1 = pyopencl.array.to_device(queue, ref_sift) gpu_keypoints2 = pyopencl.array.to_device(queue, ref_sift_2) gpu_matchings = pyopencl.array.zeros( queue, (keypoints_end - keypoints_start, 2), dtype=numpy.int32, order="C") keypoints_start, keypoints_end = numpy.int32( keypoints_start), numpy.int32(keypoints_end) nb_keypoints = numpy.int32(10000) counter = pyopencl.array.zeros(queue, (1, 1), dtype=numpy.int32, order="C") t0 = time.time() k1 = self.program.matching(queue, shape, wg, gpu_keypoints1.data, gpu_keypoints2.data, gpu_matchings.data, counter.data, nb_keypoints, ratio_th, keypoints_end, keypoints_end) res = gpu_matchings.get() cnt = counter.get() t1 = time.time() # ref_python, nb_match = my_matching(kp1, kp2, keypoints_start, keypoints_end) t2 = time.time() res_sort = res[numpy.argsort(res[:, 1])] # ref_sort = ref[numpy.argsort(ref[:,1])] print res[0:20] print "" # print ref_sort[0:20] print("C++ Matching took %.3f ms" % (1000.0 * (t1_matching - t0_matching))) print("OpenCL: %d match / C++ : %d match" % (cnt, siftmatch.shape[0])) #sort to compare added keypoints ''' delta = abs(res_sort-ref_sort).max() self.assert_(delta == 0, "delta=%s" % (delta)) #integers logger.info("delta=%s" % delta) ''' if PROFILE: logger.info("Global execution time: CPU %.3fms, GPU: %.3fms." % (1000.0 * (t2 - t1), 1000.0 * (t1 - t0))) logger.info("Matching took %.3fms" % (1e-6 * (k1.profile.end - k1.profile.start)))
xy=(x, y), xytext=(x + scale * cos(angle), y + scale * sin(angle)), color="red", arrowprops=dict(facecolor='red', edgecolor='red', width=1), ) for i in range(kp.shape[0]): x = kp[i, 0] y = kp[i, 1] scale = kp[i, 2] angle = kp[i, 3] sp1.annotate( "", xy=(x, y), xytext=(x + scale * cos(angle), y + scale * sin(angle)), color="blue", arrowprops=dict(facecolor='blue', edgecolor='blue', width=1), ) #print numpy.degrees((ref[numpy.argsort(res.scale)][:392] - kp[numpy.argsort(kpg.scale)][:392])[:,3]) #print res[:5] #print ""*80 #print kpg[:5] match = feature.sift_match(res, kpg) #print match print match.shape fig.show() fig.show() raw_input()