|
3 | 3 | import os
|
4 | 4 | import pytest
|
5 | 5 | import matplotlib.pyplot as plt
|
| 6 | +import sparse |
| 7 | +from util import ImagePydataSparseTensorLoader, safeCastPydataTensorToInts, plot_image |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.parametrize("num", list(range(1, 99))) |
| 12 | +@pytest.mark.parametrize("pt1", [0.5]) |
| 13 | +def bench_edge_detection_pydata(tacoBench, num, pt1, plot): |
| 14 | + loader = ImagePydataSparseTensorLoader() |
| 15 | + sparse_bin_img1 = safeCastPydataTensorToInts(loader.sparse_image(num, pt1, 1)) |
| 16 | + sparse_bin_img2 = safeCastPydataTensorToInts(loader.sparse_image(num, pt1+0.05, 2)) |
| 17 | + bin_img1 = loader.dense_image(num, pt1, 1) |
| 18 | + bin_img2 = loader.dense_image(num, pt1 + 0.05, 2) |
| 19 | + if plot: |
| 20 | + print(sparse_bin_img1.shape) |
| 21 | + print(sparse_bin_img2.shape) |
| 22 | + |
| 23 | + def sparse_bench(): |
| 24 | + sparse_xor_img = np.logical_xor(sparse_bin_img1, sparse_bin_img2).astype('int') |
| 25 | + return sparse_xor_img |
| 26 | + |
| 27 | + def dense_bench(): |
| 28 | + xor_img = np.logical_xor(bin_img1, bin_img2).astype('int') |
| 29 | + return xor_img |
| 30 | + ret = tacoBench(sparse_bench) |
| 31 | + sparse_xor_img = sparse_bench() |
| 32 | + xor_img = dense_bench() |
| 33 | + |
| 34 | + assert(sparse_xor_img.nnz == np.sum(xor_img != 0)) |
| 35 | + |
| 36 | + if plot: |
| 37 | + num_elements = float(np.prod(bin_img1.shape)) |
| 38 | + print("Sparse xor NNZ = ", sparse_xor_img.nnz, "\t", "Dense xor NNZ = ", np.sum(xor_img != 0)) |
| 39 | + print("Sparsity img 1 ", np.sum(bin_img1 != 0) / num_elements) |
| 40 | + print("Sparsity img 2 ", np.sum(bin_img2 != 0) / num_elements) |
| 41 | + print("Sparsity xor ", np.sum(xor_img != 0) / num_elements) |
| 42 | + sparse_xor_img = sparse_xor_img.todense() |
| 43 | + t1 = round(loader.max[num]*pt1, 2) |
| 44 | + t2 = round(loader.max[num]*(pt1 + 0.05), 2) |
| 45 | + plot_image(loader.img[num], bin_img1, bin_img2, xor_img, sparse_xor_img, t1, t2) |
| 46 | + |
| 47 | +@pytest.mark.parametrize("num", list(range(1, 99))) |
| 48 | +@pytest.mark.parametrize("pt1", [0.5]) |
| 49 | +def bench_edge_detection_dense(tacoBench, num, pt1): |
| 50 | + loader = ImagePydataSparseTensorLoader() |
| 51 | + bin_img1 = loader.dense_image(num, pt1, 1) |
| 52 | + bin_img2 = loader.dense_image(num, pt1 + 0.05, 2) |
| 53 | + |
| 54 | + def dense_bench(): |
| 55 | + xor_img = np.logical_xor(bin_img1, bin_img2).astype('int') |
| 56 | + return xor_img |
| 57 | + tacoBench(dense_bench) |
| 58 | + |
| 59 | +@pytest.mark.parametrize("num", list(range(1, 99))) |
| 60 | +@pytest.mark.parametrize("pt1", [0.5]) |
| 61 | +def bench_edge_detection_fused_pydata(tacoBench, num, pt1, plot): |
| 62 | + loader = ImagePydataSparseTensorLoader() |
| 63 | + sparse_bin_img1 = safeCastPydataTensorToInts(loader.sparse_image(num, pt1, 1)) |
| 64 | + sparse_bin_img2 = safeCastPydataTensorToInts(loader.sparse_image(num, pt1+0.05, 2)) |
| 65 | + sparse_bin_window = loader.sparse_window(num, 3) |
| 66 | + bin_img1 = loader.dense_image(num, pt1, 1) |
| 67 | + bin_img2 = loader.dense_image(num, pt1 + 0.05, 2) |
| 68 | + bin_window = loader.dense_window(num) |
| 69 | + |
| 70 | + if plot: |
| 71 | + print(sparse_bin_img1.shape) |
| 72 | + print(sparse_bin_img2.shape) |
| 73 | + |
| 74 | + def sparse_bench(): |
| 75 | + sbi1 = np.logical_and(sparse_bin_img1, sparse_bin_window) |
| 76 | + sbi2 = np.logical_and(sparse_bin_img2, sparse_bin_window) |
| 77 | + sparse_xor_img = np.logical_xor(sbi1, sbi2).astype('int') |
| 78 | + return sparse_xor_img |
| 79 | + |
| 80 | + def dense_bench(): |
| 81 | + bi1 = np.logical_and(bin_img1, bin_window).astype('int') |
| 82 | + bi2 = np.logical_and(bin_img2, bin_window).astype('int') |
| 83 | + xor_img = np.logical_xor(bi1, bi2).astype('int') |
| 84 | + return xor_img |
| 85 | + ret = tacoBench(sparse_bench) |
| 86 | + sparse_xor_img = sparse_bench() |
| 87 | + xor_img = dense_bench() |
| 88 | + |
| 89 | + if plot: |
| 90 | + num_elements = float(np.prod(bin_img1.shape)) |
| 91 | + print("Sparse xor NNZ = ", sparse_xor_img.nnz, "\t", "Dense xor NNZ = ", np.sum(xor_img != 0)) |
| 92 | + print("Sparsity img 1 ", np.sum(bin_img1 != 0) / num_elements) |
| 93 | + print("Sparsity img 2 ", np.sum(bin_img2 != 0) / num_elements) |
| 94 | + print("Sparsity xor ", np.sum(xor_img != 0) / num_elements) |
| 95 | + sparse_xor_img = sparse_xor_img.todense() |
| 96 | + t1 = round(loader.max[num]*pt1, 2) |
| 97 | + t2 = round(loader.max[num]*(pt1 + 0.05), 2) |
| 98 | + plot_image(loader.img[num], bin_img1, bin_img2, xor_img, sparse_xor_img, t1, t2, bin_window) |
| 99 | + |
| 100 | + assert(sparse_xor_img.nnz == np.sum(xor_img != 0)) |
| 101 | + |
| 102 | +@pytest.mark.parametrize("num", list(range(1, 99))) |
| 103 | +@pytest.mark.parametrize("pt1", [0.5]) |
| 104 | +def bench_edge_detection_fused_dense(tacoBench, num, pt1): |
| 105 | + loader = ImagePydataSparseTensorLoader() |
| 106 | + bin_img1 = loader.dense_image(num, pt1, 1) |
| 107 | + bin_img2 = loader.dense_image(num, pt1 + 0.05, 2) |
| 108 | + bin_window = loader.dense_window(num) |
| 109 | + |
| 110 | + def dense_bench(): |
| 111 | + bi1 = np.logical_and(bin_img1, bin_window).astype('int') |
| 112 | + bi2 = np.logical_and(bin_img2, bin_window).astype('int') |
| 113 | + xor_img = np.logical_xor(bin_img1, bin_img2).astype('int') |
| 114 | + return xor_img |
| 115 | + tacoBench(dense_bench) |
| 116 | + |
| 117 | +#TODO: Add in a benchmark that uses windowing for medical imaging as well. |
6 | 118 |
|
7 |
| -images_path = "./numpy/images" |
8 |
| - |
9 |
| -def load_dataset(image_folder): |
10 |
| - files = sorted(os.listdir(image_folder)) |
11 |
| - images = [] |
12 |
| - for f in files: |
13 |
| - path = os.path.join(image_folder, f) |
14 |
| - img = cv2.imread(path) |
15 |
| - img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) |
16 |
| - images.append(img) |
17 |
| - |
18 |
| - images = np.stack(images, axis=0) |
19 |
| - return images |
20 |
| - |
21 |
| -def thresh(images, t=85): |
22 |
| - if len(images.shape) < 3: |
23 |
| - images = np.expand_dims(images, axis=0) |
24 |
| - thresh_imgs = [] |
25 |
| - for i in range(images.shape[0]): |
26 |
| - img = images[i] |
27 |
| - ret, thresh_img = cv2.threshold(img, t, 255, cv2.THRESH_BINARY) |
28 |
| - thresh_imgs.append(thresh_img) |
29 |
| - |
30 |
| - thresh_imgs = np.stack(thresh_imgs, axis=0) |
31 |
| - return thresh_imgs |
32 |
| - |
33 |
| -def plot_image(img, img1, img2, xor_img, t1, t2): |
34 |
| - f, ax = plt.subplots(2, 2) |
35 |
| - ax[0, 0].imshow(img1, 'gray') |
36 |
| - ax[0, 0].title.set_text("Binned Image 1. t1 = " + str(t1)) |
37 |
| - |
38 |
| - ax[0, 1].imshow(img2, 'gray') |
39 |
| - ax[0, 1].title.set_text("Binned Image 2. t2 = " + str(t2)) |
40 |
| - |
41 |
| - ax[1, 0].imshow(img, 'gray') |
42 |
| - ax[1, 0].title.set_text("Saturdated Image") |
43 |
| - |
44 |
| - ax[1, 1].imshow(xor_img, 'gray') |
45 |
| - ax[1, 1].title.set_text("XOR Image") |
46 |
| - |
47 |
| - f.tight_layout() |
48 |
| - plt.show() |
49 |
| - |
50 |
| -@pytest.mark.parametrize("t1", [100, 150, 200, 250]) |
51 |
| -def bench_edge_detection(tacoBench, t1, plot): |
52 |
| - images = load_dataset(images_path) |
53 |
| - |
54 |
| - sat_images = images[:,:,:,1] |
55 |
| - |
56 |
| - img = sat_images[0] |
57 |
| - |
58 |
| - t2 = t1 - 50 |
59 |
| - |
60 |
| - bin_img1 = thresh(img, t1) |
61 |
| - bin_img2 = thresh(img, t2) |
62 |
| - num_elements = float(np.prod(bin_img1.shape)) |
63 |
| - |
64 |
| - def bench(): |
65 |
| - xor_img = np.logical_xor(bin_img1[0], bin_img2[0]).astype('int') |
66 |
| - return xor_img |
67 |
| - ret = tacoBench(bench) |
68 |
| - xor_img = bench() |
69 |
| - if plot: |
70 |
| - plot_image(img, bin_img1[0], bin_img2[0], xor_img, t1, t2) |
71 |
| - |
72 |
| - print("Sparsity img 1 ", np.sum(bin_img1 != 0) / num_elements) |
73 |
| - print("Sparsity img 2 ", np.sum(bin_img2 != 0) / num_elements) |
74 |
| - print("Sparsity xor ", np.sum(xor_img != 0) / num_elements) |
75 |
| - |
76 | 119 | if __name__=="__main__":
|
77 | 120 | main()
|
78 | 121 |
|
0 commit comments