|
4 | 4 | import pytest
|
5 | 5 | import matplotlib.pyplot as plt
|
6 | 6 | import sparse
|
7 |
| -from util import ImagePydataSparseTensorLoader, safeCastPydataTensorToInts, plot_image |
| 7 | +from util import ImagePydataSparseTensorLoader, safeCastPydataTensorToInts |
8 | 8 |
|
9 | 9 |
|
| 10 | +# plot_image plots the given original, binned, xor, and sparse xor images |
| 11 | +# for the numpy/image.py. Used for debugging only with the --plot flag |
| 12 | +def plot_image(img, img1, img2, xor_img, sparse_xor_img, t1, t2, window=None): |
| 13 | + f, ax = plt.subplots(2, 3) |
| 14 | + ax[0, 0].imshow(img1, 'gray') |
| 15 | + ax[0, 0].title.set_text("Binned Image 1. t1 = " + str(t1)) |
| 16 | + |
| 17 | + ax[0, 1].imshow(img2, 'gray') |
| 18 | + ax[0, 1].title.set_text("Binned Image 2. t2 = " + str(t2)) |
| 19 | + |
| 20 | + ax[1, 0].imshow(img, 'gray') |
| 21 | + ax[1, 0].title.set_text("Saturdated Image") |
| 22 | + |
| 23 | + ax[1, 1].imshow(xor_img, 'gray') |
| 24 | + ax[1, 1].title.set_text("XOR Image") |
| 25 | + |
| 26 | + ax[1, 2].imshow(sparse_xor_img, 'gray') |
| 27 | + ax[1, 2].title.set_text("Sparse XOR Image") |
| 28 | + |
| 29 | + if window is not None: |
| 30 | + ax[0, 2].imshow(window, 'gray') |
| 31 | + ax[0, 2].title.set_text("Fused Window Image") |
| 32 | + |
| 33 | + f.tight_layout() |
| 34 | + plt.show() |
| 35 | + |
10 | 36 |
|
11 | 37 | @pytest.mark.parametrize("num", list(range(1, 99)))
|
12 | 38 | @pytest.mark.parametrize("pt1", [0.5])
|
@@ -115,7 +141,81 @@ def dense_bench():
|
115 | 141 | tacoBench(dense_bench)
|
116 | 142 |
|
117 | 143 | #TODO: Add in a benchmark that uses windowing for medical imaging as well.
|
| 144 | +@pytest.mark.parametrize("num", list(range(1, 99))) |
| 145 | +@pytest.mark.parametrize("pt1", [0.5]) |
| 146 | +@pytest.mark.parametrize("window_size", [0.25, 0.2, 0.15, 0.1]) |
| 147 | +def bench_edge_detection_window_pydata(tacoBench, num, pt1, window_size, plot): |
| 148 | + loader = ImagePydataSparseTensorLoader() |
| 149 | + sparse_bin_img1 = safeCastPydataTensorToInts(loader.sparse_image(num, pt1, 1)) |
| 150 | + sparse_bin_img2 = safeCastPydataTensorToInts(loader.sparse_image(num, pt1+0.05, 2)) |
| 151 | + bin_img1 = loader.dense_image(num, pt1, 1) |
| 152 | + bin_img2 = loader.dense_image(num, pt1 + 0.05, 2) |
118 | 153 |
|
| 154 | + mid0 = int(bin_img1.shape[0]/2) |
| 155 | + mid1 = int(bin_img1.shape[1]/2) |
| 156 | + |
| 157 | + win_len0 = int(window_size * bin_img1.shape[0]) |
| 158 | + win_len1 = int(window_size * bin_img1.shape[1]) |
| 159 | + |
| 160 | + if plot: |
| 161 | + print(sparse_bin_img1.shape) |
| 162 | + print(sparse_bin_img2.shape) |
| 163 | + |
| 164 | + def sparse_bench(): |
| 165 | + swin1 = sparse_bin_img1[mid0 - win_len0:mid0 + win_len0, mid1 - win_len1:mid1 + win_len1] |
| 166 | + swin2 = sparse_bin_img2[mid0 - win_len0:mid0 + win_len0, mid1 - win_len1:mid1 + win_len1] |
| 167 | + sparse_xor_img = np.logical_xor(swin1, swin2).astype('int') |
| 168 | + return sparse_xor_img |
| 169 | + |
| 170 | + def dense_bench(): |
| 171 | + win1 = bin_img1[mid0 - win_len0:mid0 + win_len0, mid1 - win_len1:mid1 + win_len1] |
| 172 | + win2 = bin_img2[mid0 - win_len0:mid0 + win_len0, mid1 - win_len1:mid1 + win_len1] |
| 173 | + xor_img = np.logical_xor(win1, win2).astype('int') |
| 174 | + return xor_img |
| 175 | + |
| 176 | + ret = tacoBench(sparse_bench) |
| 177 | + sparse_xor_img = sparse_bench() |
| 178 | + xor_img = dense_bench() |
| 179 | + |
| 180 | + if plot: |
| 181 | + print(sparse_xor_img) |
| 182 | + print("sparse img1 nnz =", sparse_bin_img1.nnz, " ", np.sum(bin_img1 != 0)) |
| 183 | + print("sparse img2 nnz =", sparse_bin_img2.nnz, " ", np.sum(bin_img2 != 0)) |
| 184 | + num_elements = float(np.prod(bin_img1.shape)) |
| 185 | + print("Sparse xor NNZ = ", sparse_xor_img.nnz, "\t", "Dense xor NNZ = ", np.sum(xor_img != 0)) |
| 186 | + print("Sparsity img 1 ", np.sum(bin_img1 != 0) / num_elements) |
| 187 | + print("Sparsity img 2 ", np.sum(bin_img2 != 0) / num_elements) |
| 188 | + print("Sparsity xor ", np.sum(xor_img != 0) / num_elements) |
| 189 | + sparse_xor_img = sparse_xor_img.todense() |
| 190 | + t1 = round(loader.max[num]*pt1, 2) |
| 191 | + t2 = round(loader.max[num]*(pt1 + 0.05), 2) |
| 192 | + print(xor_img) |
| 193 | + plot_image(loader.img[num], bin_img1, bin_img2, xor_img, sparse_xor_img, t1, t2) |
| 194 | + |
| 195 | + assert(sparse_xor_img.nnz == np.sum(xor_img != 0)) |
| 196 | + |
| 197 | +@pytest.mark.parametrize("num", list(range(1, 99))) |
| 198 | +@pytest.mark.parametrize("pt1", [0.5]) |
| 199 | +@pytest.mark.parametrize("window_size", [0.25, 0.2, 0.15, 0.1]) |
| 200 | +def bench_edge_detection_window_dense(tacoBench, num, pt1, window_size): |
| 201 | + loader = ImagePydataSparseTensorLoader() |
| 202 | + bin_img1 = loader.dense_image(num, pt1, 1) |
| 203 | + bin_img2 = loader.dense_image(num, pt1 + 0.05, 2) |
| 204 | + |
| 205 | + mid0 = int(bin_img1.shape[0]/2) |
| 206 | + mid1 = int(bin_img1.shape[1]/2) |
| 207 | + |
| 208 | + win_len0 = int(window_size * bin_img1.shape[0]) |
| 209 | + win_len1 = int(window_size * bin_img1.shape[1]) |
| 210 | + |
| 211 | + def dense_bench(): |
| 212 | + win1 = bin_img1[mid0 - win_len0:mid0 + win_len0, mid1 - win_len1:mid1 + win_len1] |
| 213 | + win2 = bin_img2[mid0 - win_len0:mid0 + win_len0, mid1 - win_len1:mid1 + win_len1] |
| 214 | + xor_img = np.logical_xor(win1, win2).astype('int') |
| 215 | + return xor_img |
| 216 | + |
| 217 | + tacoBench(dense_bench) |
| 218 | + |
119 | 219 | if __name__=="__main__":
|
120 | 220 | main()
|
121 | 221 |
|
0 commit comments