From 823d84a77a6565ceb98ba7c1f52cc0264ff88206 Mon Sep 17 00:00:00 2001 From: Tiago Freitas Pereira <tiagofrepereira@gmail.com> Date: Tue, 26 Apr 2022 17:24:23 +0200 Subject: [PATCH] Removed bob.sp test requirement --- bob/bio/vein/tests/test_tools.py | 155 ------------------------------- 1 file changed, 155 deletions(-) diff --git a/bob/bio/vein/tests/test_tools.py b/bob/bio/vein/tests/test_tools.py index 86d876b..dda3ac2 100644 --- a/bob/bio/vein/tests/test_tools.py +++ b/bob/bio/vein/tests/test_tools.py @@ -646,161 +646,6 @@ def test_intersection_ratio(): ) -def test_correlation(): - - # A test for convolution performance. Correlations are used on the Miura - # Match algorithm, therefore we want to make sure we can perform them as fast - # as possible. - import numpy - import scipy.signal - import bob.sp - - # Rough example from Vera fingervein database - Y = 250 - X = 600 - CH = 80 - CW = 90 - - def gen_ab(): - a = numpy.random.randint(256, size=(Y, X)).astype(float) - b = numpy.random.randint(256, size=(Y - CH, X - CW)).astype(float) - return a, b - - def bob_function(a, b): - - # rotate input image by 180 degrees - b = numpy.rot90(b, k=2) - - # Determine padding size in x and y dimension - size_a = numpy.array(a.shape) - size_b = numpy.array(b.shape) - outsize = size_a + size_b - 1 - - # Determine 2D cross correlation in Fourier domain - a2 = numpy.zeros(outsize) - a2[0 : size_a[0], 0 : size_a[1]] = a - Fa = bob.sp.fft(a2.astype(numpy.complex128)) - - b2 = numpy.zeros(outsize) - b2[0 : size_b[0], 0 : size_b[1]] = b - Fb = bob.sp.fft(b2.astype(numpy.complex128)) - - conv_ab = numpy.real(bob.sp.ifft(Fa * Fb)) - - h, w = size_a - size_b + 1 - - Nm = conv_ab[ - size_b[0] - 1 : size_b[0] - 1 + h, size_b[1] - 1 : size_b[1] - 1 + w - ] - - t0, s0 = numpy.unravel_index(Nm.argmax(), Nm.shape) - - # this is our output - Nmm = Nm[t0, s0] - - # normalizes the output by the number of pixels lit on the input - # matrices, taking into consideration the surface that produced the - # result (i.e., the eroded model and part of the probe) - h, w = b.shape - return Nmm / ( - sum(sum(b)) + sum(sum(a[t0 : t0 + h - 2 * CH, s0 : s0 + w - 2 * CW])) - ) - - def scipy_function(a, b): - b = numpy.rot90(b, k=2) - - Nm = scipy.signal.convolve2d(a, b, "valid") - - # figures out where the maximum is on the resulting matrix - t0, s0 = numpy.unravel_index(Nm.argmax(), Nm.shape) - - # this is our output - Nmm = Nm[t0, s0] - - # normalizes the output by the number of pixels lit on the input - # matrices, taking into consideration the surface that produced the - # result (i.e., the eroded model and part of the probe) - h, w = b.shape - return Nmm / ( - sum(sum(b)) + sum(sum(a[t0 : t0 + h - 2 * CH, s0 : s0 + w - 2 * CW])) - ) - - def scipy2_function(a, b): - b = numpy.rot90(b, k=2) - Nm = scipy.signal.fftconvolve(a, b, "valid") - - # figures out where the maximum is on the resulting matrix - t0, s0 = numpy.unravel_index(Nm.argmax(), Nm.shape) - - # this is our output - Nmm = Nm[t0, s0] - - # normalizes the output by the number of pixels lit on the input - # matrices, taking into consideration the surface that produced the - # result (i.e., the eroded model and part of the probe) - h, w = b.shape - return Nmm / ( - sum(sum(b)) + sum(sum(a[t0 : t0 + h - 2 * CH, s0 : s0 + w - 2 * CW])) - ) - - def scipy3_function(a, b): - Nm = scipy.signal.correlate2d(a, b, "valid") - - # figures out where the maximum is on the resulting matrix - t0, s0 = numpy.unravel_index(Nm.argmax(), Nm.shape) - - # this is our output - Nmm = Nm[t0, s0] - - # normalizes the output by the number of pixels lit on the input - # matrices, taking into consideration the surface that produced the - # result (i.e., the eroded model and part of the probe) - h, w = b.shape - return Nmm / ( - sum(sum(b)) + sum(sum(a[t0 : t0 + h - 2 * CH, s0 : s0 + w - 2 * CW])) - ) - - a, b = gen_ab() - - assert numpy.allclose(bob_function(a, b), scipy_function(a, b)) - assert numpy.allclose(scipy_function(a, b), scipy2_function(a, b)) - assert numpy.allclose(scipy2_function(a, b), scipy3_function(a, b)) - - # if you want to test timings, uncomment the following section - """ - import time - - start = time.clock() - N = 10 - for i in range(N): - a, b = gen_ab() - bob_function(a, b) - total = time.clock() - start - print('bob implementation, %d iterations - %.2e per iteration' % (N, total/N)) - - start = time.clock() - for i in range(N): - a, b = gen_ab() - scipy_function(a, b) - total = time.clock() - start - print('scipy+convolve, %d iterations - %.2e per iteration' % (N, total/N)) - - start = time.clock() - for i in range(N): - a, b = gen_ab() - scipy2_function(a, b) - total = time.clock() - start - print('scipy+fftconvolve, %d iterations - %.2e per iteration' % (N, total/N)) - - start = time.clock() - for i in range(N): - a, b = gen_ab() - scipy3_function(a, b) - total = time.clock() - start - print('scipy+correlate2d, %d iterations - %.2e per iteration' % (N, total/N)) - """ - - def test_hamming_distance(): from ..algorithm.HammingDistance import HammingDistance -- GitLab