def __init__(self, fname, devicetype=None, device=None, context=None): self.image_rgb = scipy.misc.imread(fname) self.image_bw = 0.299 * self.image_rgb[:, :, 0] + 0.587 * self.image_rgb[:, :, 1] + 0.114 * self.image_rgb[:, :, 2] if feature: self._sift_cpp = feature.SiftAlignment() self._sift_ocl = sift.SiftPlan(template=self.image_rgb, device=device, devicetype=devicetype, context=context) self.kp_cpp = numpy.empty(0) self.kp_ocl = numpy.empty(0) if SHOW_FIGURES == True: self.fig = pylab.figure() self.sp1 = self.fig.add_subplot(1, 2, 1) self.im1 = self.sp1.imshow(self.image_rgb) self.sp1.set_title("OpenCL: %s keypoint" % self.kp_ocl.size) self.sp2 = self.fig.add_subplot(1, 2, 2) self.im2 = self.sp2.imshow(self.image_bw, cmap="gray") self.sp2.set_title("C++: %s keypoint" % self.kp_cpp.size) self.fig.show() self.timing_cpp = None self.timing_ocl = None self.speedups = numpy.zeros((1, 3), dtype=numpy.float32)
def __init__(self, filename=None, devicetype=None, device=None, context=None, profile=False): if filename and os.path.exists(filename): self.filename = filename else: self.filename = utilstest.UtilsTest.getimage("wikipedia/commons/9/94/Esrf_grenoble.jpg") try: self.image_rgb = scipy.misc.imread(self.filename) except: import fabio self.image_rgb = fabio.open(self.filename).data if self.image_rgb.ndim != 2: self.image_bw = 0.299 * self.image_rgb[:, :, 0] + 0.587 * self.image_rgb[:, :, 1] + 0.114 * self.image_rgb[:, :, 2] else: self.image_bw = self.image_rgb if feature: self._sift_cpp = feature.SiftAlignment() self._sift_ocl = sift.SiftPlan(template=self.image_rgb, device=device, devicetype=devicetype, context=context, profile=profile) self.ctx = self._sift_ocl.ctx self.kp_cpp = numpy.empty(0) self.kp_ocl = numpy.empty(0) self.fig = pylab.figure() self.sp1 = self.fig.add_subplot(1, 2, 1) self.im1 = self.sp1.imshow(self.image_rgb) self.sp1.set_title("OpenCL: %s keypoint" % self.kp_ocl.size) self.sp2 = self.fig.add_subplot(1, 2, 2) self.im2 = self.sp2.imshow(self.image_bw, cmap="gray") self.sp2.set_title("C++: %s keypoint" % self.kp_cpp.size) self.fig.show() self.timing_cpp = None self.timing_ocl = None
def __init__(self, filename=None, devicetype=None, device=None, profile=False): if filename and os.path.exists(filename): self.filename = filename else: self.filename = "Esrf_grenoble.jpg" data = urllib2.urlopen( "http://upload.wikimedia.org/wikipedia/commons/9/94/Esrf_grenoble.jpg" ).read() open(self.filename, "wb").write(data) self.image_rgb = scipy.misc.imread(self.filename) if self.image_rgb.ndim != 2: self.image_bw = 0.299 * self.image_rgb[:, :, 0] + 0.587 * self.image_rgb[:, :, 1] + 0.114 * self.image_rgb[:, :, 2] else: self.image_bw = self.image_rgb if feature: self._sift_cpp = feature.SiftAlignment() self._sift_ocl = sift.SiftPlan(template=self.image_rgb, device=device, devicetype=devicetype, profile=profile) self.kp_cpp = numpy.empty(0) self.kp_ocl = numpy.empty(0) self.fig = pylab.figure() self.sp1 = self.fig.add_subplot(1, 2, 1) self.sp2 = self.fig.add_subplot(1, 2, 2) #elf.sp3 = self.fig.add_subplot(1, 2, 3) #elf.sp4 = self.fig.add_subplot(1, 2, 4) self.im1 = self.sp1.imshow(self.image_rgb) self.sp1.set_title("OpenCL: %s keypoint" % self.kp_ocl.size) self.im2 = self.sp2.imshow(self.image_bw, cmap="gray") self.sp2.set_title("C++: %s keypoint" % self.kp_cpp.size) self.fig.show() self.timing_cpp = None self.timing_ocl = None
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)))
#!/usr/bin/python import fabio, feature, time print feature.__file__ ref0 = 1.6923630028440242 ref1 = 0.67084262245579773 img1 = fabio.open("Ti_slow_data_0000_0011_0000_norm.edf").data img2 = fabio.open("Ti_slow_data_0002_0055_0000_norm.edf").data #out = feature.sift2(img1, img2 , True) #assert abs((out[:, 2] - out[:, 0]).mean() - ref0) < 0.01 #assert abs((out[:, 3] - out[:, 1]).mean() - ref1) < 0.01 import threading dico = {} sift = feature.SiftAlignment() def oneImg(name): img1 = fabio.open(name).data dico[name] = sift.sift(img1) th1 = threading.Thread(target=oneImg, args=["Ti_slow_data_0000_0011_0000_norm.edf"]) th2 = threading.Thread(target=oneImg, args=["Ti_slow_data_0002_0055_0000_norm.edf"]) t0 = time.time() th1.start() th2.start() th1.join() th2.join()
import numpy import matplotlib from matplotlib import pylab import scipy import feature l = scipy.misc.lena().astype(numpy.float32) #l1 = l[5:, 6:] #l2 = l[:-5, :-6] l1 = l2 = l fig = pylab.figure() sp1 = fig.add_subplot(1, 2, 1) sp2 = fig.add_subplot(1, 2, 2) sp1.imshow(l1, interpolation="nearest", cmap="gray") sp2.imshow(l2, interpolation="nearest", cmap="gray") siftalignement = feature.SiftAlignment() kp1 = siftalignement.sift(l1) print("") kp2 = siftalignement.sift(l2) print kp1[:, :4], kp2[:, :4] print kp1.shape[0], kp2.shape[0] match = siftalignement.match(kp1, kp2) for i in range(kp1.shape[0]): x = kp1[i, 0] y = kp1[i, 1] scale = kp1[i, 2] angle = kp1[i, 3] if ((match[:, 1] == x) * (match[:, 0] == y)).sum() > 0: color = "blue" else:
def test_orientation(self): ''' #tests keypoints orientation assignment kernel ''' #orientation_setup : keypoints, nb_keypoints, updated_nb_keypoints, grad, ori, octsize = orientation_setup() keypoints, compact_cnt = my_compact(numpy.copy(keypoints),nb_keypoints) updated_nb_keypoints = compact_cnt if (USE_CPU): print("Using CPU-optimized kernels") wg = 1, shape = keypoints.shape[0]*wg[0], else: wg = 128, #FIXME : have to choose it for histograms #wg = max(self.wg), shape = keypoints.shape[0]*wg[0], #shape = calc_size(keypoints.shape, self.wg) gpu_keypoints = pyopencl.array.to_device(queue, keypoints) actual_nb_keypoints = numpy.int32(updated_nb_keypoints) print("Number of keypoints before orientation assignment : %s" % actual_nb_keypoints) gpu_grad = pyopencl.array.to_device(queue, grad) gpu_ori = pyopencl.array.to_device(queue, ori) orisigma = numpy.float32(1.5) #SIFT grad_height, grad_width = numpy.int32(grad.shape) keypoints_start = numpy.int32(0) keypoints_end = numpy.int32(actual_nb_keypoints) counter = pyopencl.array.to_device(queue, keypoints_end) #actual_nb_keypoints) t0 = time.time() k1 = self.program.orientation_assignment(queue, shape, wg, gpu_keypoints.data, gpu_grad.data, gpu_ori.data, counter.data, octsize, orisigma, nb_keypoints, keypoints_start, keypoints_end, grad_width, grad_height) res = gpu_keypoints.get() cnt = counter.get() t1 = time.time() if (USE_CPP_SIFT): import feature sc = feature.SiftAlignment() ref2 = sc.sift(scipy.misc.lena()) #ref2.x, ref2.y, ref2.scale, ref2.angle, ref2.desc --- ref2[numpy.argsort(ref2.y)]).desc ref = ref2.angle kp_ref = numpy.empty((ref2.size, 4), dtype=numpy.float32) kp_ref[:, 0] = ref2.x kp_ref[:, 1] = ref2.y kp_ref[:, 2] = ref2.scale kp_ref[:, 3] = ref2.angle else: ref, updated_nb_keypoints = my_orientation(keypoints, nb_keypoints, keypoints_start, keypoints_end, grad, ori, octsize, orisigma) t2 = time.time() if (PRINT_KEYPOINTS): # print("Keypoints after orientation assignment :") # print res[numpy.argsort(res[0:cnt,1])][0:cnt+10,3] #res[0:compact_cnt] print " " # print kp_ref[0:cnt+10] # print "Showing error (NOTE: significant error at position (i) should have its opposite at (i+1))" # print res[numpy.argsort(res[0:compact_cnt,1])][0:compact_cnt,3] - ref[0:compact_cnt] # print("Total keypoints for kernel : %s -- For Python : %s \t [octsize = %s]" % (cnt, updated_nb_keypoints, octsize)) # print("Opencl found %s keypoints (%s added)" %(cnt,cnt-compact_cnt)) #sort to compare added keypoints upbound = min(cnt,updated_nb_keypoints) d1, d2, d3, d4 = keypoints_compare(ref[0:upbound], res[0:upbound]) self.assert_(d1 < 1e-4, "delta_cols=%s" % (d1)) self.assert_(d2 < 1e-4, "delta_rows=%s" % (d2)) self.assert_(d3 < 1e-4, "delta_sigma=%s" % (d3)) self.assert_(d4 < 1e-1, "delta_angle=%s" % (d4)) #orientation has a poor precision logger.info("delta_cols=%s" % d1) logger.info("delta_rows=%s" % d2) logger.info("delta_sigma=%s" % d3) logger.info("delta_angle=%s" % d4) if PROFILE: logger.info("Global execution time: CPU %.3fms, GPU: %.3fms." % (1000.0 * (t2 - t1), 1000.0 * (t1 - t0))) logger.info("Orientation assignment took %.3fms" % (1e-6 * (k1.profile.end - k1.profile.start)))
def test_descriptor(self): ''' #tests keypoints descriptors creation kernel ''' #descriptor_setup : keypoints_o, nb_keypoints, actual_nb_keypoints, grad, ori, octsize = descriptor_setup() #keypoints should be a compacted vector of keypoints keypoints_o, compact_cnt = my_compact(numpy.copy(keypoints_o),nb_keypoints) actual_nb_keypoints = compact_cnt keypoints_start, keypoints_end = 0, actual_nb_keypoints keypoints = keypoints_o[keypoints_start:keypoints_end+52] #to check if we actually stop at keypoints_end print("Working on keypoints : [%s,%s] (octave = %s)" % (keypoints_start, keypoints_end-1,int(numpy.log2(octsize)+1))) if not(USE_CPP_SIFT) and (100 < keypoints_end-keypoints_start): print "NOTE: Python implementation of descriptors is slow. Do not handle more than 100 keypoints, or grab a coffee..." if (USE_CPU): print "Using CPU-optimized kernels" wg = 1, shape = keypoints.shape[0]*wg[0], else: # wg = (8, 8, 8) # shape = int(keypoints.shape[0]*wg[0]), 8, 8 wg = (8, 4, 4) shape = int(keypoints.shape[0]*wg[0]), 4, 4 gpu_keypoints = pyopencl.array.to_device(queue, keypoints) #NOTE: for the following line, use pyopencl.array.empty instead of pyopencl.array.zeros if the keypoints are compacted gpu_descriptors = pyopencl.array.zeros(queue, (keypoints_end - keypoints_start, 128), dtype=numpy.uint8, order="C") gpu_grad = pyopencl.array.to_device(queue, grad) gpu_ori = pyopencl.array.to_device(queue, ori) keypoints_start, keypoints_end = numpy.int32(keypoints_start), numpy.int32(keypoints_end) grad_height, grad_width = numpy.int32(grad.shape) counter = pyopencl.array.to_device(queue, keypoints_end) t0 = time.time() k1 = self.program.descriptor(queue, shape, wg, gpu_keypoints.data, gpu_descriptors.data, gpu_grad.data, gpu_ori.data, numpy.int32(octsize), keypoints_start, counter.data, grad_width, grad_height) res = gpu_descriptors.get() t1 = time.time() if (USE_CPP_SIFT): import feature sc = feature.SiftAlignment() ref2 = sc.sift(scipy.misc.lena()) #ref2.x, ref2.y, ref2.scale, ref2.angle, ref2.desc --- ref2[numpy.argsort(ref2.y)]).desc ref = ref2.desc ref_sort = ref else: ref = my_descriptor(keypoints_o, grad, ori, octsize, keypoints_start, keypoints_end) ref_sort = ref[numpy.argsort(keypoints[keypoints_start:keypoints_end,1])] t2 = time.time() if (PRINT_KEYPOINTS): res_sort = res[numpy.argsort(keypoints[keypoints_start:keypoints_end,1])] print res_sort[5:10]#keypoints_end-keypoints_start,0:15] # print res_sort[9] print "" print ref_sort[5:10] # numpy.savetxt("grrr_ocl_4_3.txt",res_sort,fmt='%d') # numpy.savetxt("grrr_cpp_4_3.txt",ref_sort,fmt='%d') # print ref[50:80,0:15]#[0:keypoints_end-keypoints_start,0:15] if (USE_CPP_SIFT and octsize == 1) or not(USE_CPP_SIFT): #this comparison is only relevant for the first keypoints print "Comparing descriptors (OpenCL and cpp) :" match, nulldesc = descriptors_compare(ref[keypoints_start:keypoints_end],res) print ("%s/%s match found" %(match,(keypoints_end-keypoints_start)-nulldesc)) # print ref[1,:] # print res[1,:].sum(), ref[1,:].sum() #append to existing text file # f_handle = file('desc_by_test_keypoints.txt', 'a') # numpy.savetxt(f_handle,res_sort,fmt='%d') # f_handle.close() ''' For now, the descriptor kernel is not precise enough to get exactly the same descriptors values (we have several difference of 1, but it is OK for the SIFT matching). Use descriptors_compare(ref,res) to count how many descriptors are exactly the same. #sort to compare added keypoints delta = abs(res_sort-ref_sort).max() self.assert_(delta <= 1, "delta=%s" % (delta)) 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("Descriptors computation took %.3fms" % (1e-6 * (k1.profile.end - k1.profile.start)))
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)))