Skip to content

Commit 629de80

Browse files
committed
Fix window benchmark for medical imaging
1 parent 0b09f34 commit 629de80

File tree

2 files changed

+101
-27
lines changed

2 files changed

+101
-27
lines changed

numpy/image.py

+101-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,35 @@
44
import pytest
55
import matplotlib.pyplot as plt
66
import sparse
7-
from util import ImagePydataSparseTensorLoader, safeCastPydataTensorToInts, plot_image
7+
from util import ImagePydataSparseTensorLoader, safeCastPydataTensorToInts
88

99

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+
1036

1137
@pytest.mark.parametrize("num", list(range(1, 99)))
1238
@pytest.mark.parametrize("pt1", [0.5])
@@ -115,7 +141,81 @@ def dense_bench():
115141
tacoBench(dense_bench)
116142

117143
#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)
118153

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+
119219
if __name__=="__main__":
120220
main()
121221

numpy/util.py

-26
Original file line numberDiff line numberDiff line change
@@ -311,32 +311,6 @@ def load_image(image_folder, num):
311311
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
312312
return img
313313

314-
# plot_image plots the given original, binned, xor, and sparse xor images
315-
# for the numpy/image.py. Used for debugging only with the --plot flag
316-
def plot_image(img, img1, img2, xor_img, sparse_xor_img, t1, t2, window=None):
317-
f, ax = plt.subplots(2, 3)
318-
ax[0, 0].imshow(img1, 'gray')
319-
ax[0, 0].title.set_text("Binned Image 1. t1 = " + str(t1))
320-
321-
ax[0, 1].imshow(img2, 'gray')
322-
ax[0, 1].title.set_text("Binned Image 2. t2 = " + str(t2))
323-
324-
ax[1, 0].imshow(img, 'gray')
325-
ax[1, 0].title.set_text("Saturdated Image")
326-
327-
ax[1, 1].imshow(xor_img, 'gray')
328-
ax[1, 1].title.set_text("XOR Image")
329-
330-
ax[1, 2].imshow(sparse_xor_img, 'gray')
331-
ax[1, 2].title.set_text("Sparse XOR Image")
332-
333-
if window is not None:
334-
ax[0, 2].imshow(window, 'gray')
335-
ax[0, 2].title.set_text("Fused Window Image")
336-
337-
f.tight_layout()
338-
plt.show()
339-
340314
# thresh thresholdes the given image by a threshold
341315
def thresh(images, t=85):
342316
if len(images.shape) < 3:

0 commit comments

Comments
 (0)