3
3
import os
4
4
import pytest
5
5
import matplotlib .pyplot as plt
6
+ import sparse
7
+ from util import ImagePydataSparseTensorLoader , safeCastPydataTensorToInts
6
8
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 )
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 )
35
14
ax [0 , 0 ].imshow (img1 , 'gray' )
36
15
ax [0 , 0 ].title .set_text ("Binned Image 1. t1 = " + str (t1 ))
37
16
@@ -43,36 +22,200 @@ def plot_image(img, img1, img2, xor_img, t1, t2):
43
22
44
23
ax [1 , 1 ].imshow (xor_img , 'gray' )
45
24
ax [1 , 1 ].title .set_text ("XOR Image" )
46
-
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
+
47
33
f .tight_layout ()
48
34
plt .show ()
49
35
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
-
36
+
37
+ @pytest .mark .parametrize ("num" , list (range (1 , 99 )))
38
+ @pytest .mark .parametrize ("pt1" , [0.5 ])
39
+ def bench_edge_detection_pydata (tacoBench , num , pt1 , plot ):
40
+ loader = ImagePydataSparseTensorLoader ()
41
+ sparse_bin_img1 = safeCastPydataTensorToInts (loader .sparse_image (num , pt1 , 1 ))
42
+ sparse_bin_img2 = safeCastPydataTensorToInts (loader .sparse_image (num , pt1 + 0.05 , 2 ))
43
+ bin_img1 = loader .dense_image (num , pt1 , 1 )
44
+ bin_img2 = loader .dense_image (num , pt1 + 0.05 , 2 )
45
+ if plot :
46
+ print (sparse_bin_img1 .shape )
47
+ print (sparse_bin_img2 .shape )
48
+
49
+ def sparse_bench ():
50
+ sparse_xor_img = np .logical_xor (sparse_bin_img1 , sparse_bin_img2 ).astype ('int' )
51
+ return sparse_xor_img
52
+
53
+ def dense_bench ():
54
+ xor_img = np .logical_xor (bin_img1 , bin_img2 ).astype ('int' )
55
+ return xor_img
56
+ ret = tacoBench (sparse_bench )
57
+ sparse_xor_img = sparse_bench ()
58
+ xor_img = dense_bench ()
59
+
60
+ assert (sparse_xor_img .nnz == np .sum (xor_img != 0 ))
61
+
62
+ if plot :
63
+ num_elements = float (np .prod (bin_img1 .shape ))
64
+ print ("Sparse xor NNZ = " , sparse_xor_img .nnz , "\t " , "Dense xor NNZ = " , np .sum (xor_img != 0 ))
65
+ print ("Sparsity img 1 " , np .sum (bin_img1 != 0 ) / num_elements )
66
+ print ("Sparsity img 2 " , np .sum (bin_img2 != 0 ) / num_elements )
67
+ print ("Sparsity xor " , np .sum (xor_img != 0 ) / num_elements )
68
+ sparse_xor_img = sparse_xor_img .todense ()
69
+ t1 = round (loader .max [num ]* pt1 , 2 )
70
+ t2 = round (loader .max [num ]* (pt1 + 0.05 ), 2 )
71
+ plot_image (loader .img [num ], bin_img1 , bin_img2 , xor_img , sparse_xor_img , t1 , t2 )
72
+
73
+ @pytest .mark .parametrize ("num" , list (range (1 , 99 )))
74
+ @pytest .mark .parametrize ("pt1" , [0.5 ])
75
+ def bench_edge_detection_dense (tacoBench , num , pt1 ):
76
+ loader = ImagePydataSparseTensorLoader ()
77
+ bin_img1 = loader .dense_image (num , pt1 , 1 )
78
+ bin_img2 = loader .dense_image (num , pt1 + 0.05 , 2 )
79
+
80
+ def dense_bench ():
81
+ xor_img = np .logical_xor (bin_img1 , bin_img2 ).astype ('int' )
82
+ return xor_img
83
+ tacoBench (dense_bench )
84
+
85
+ @pytest .mark .parametrize ("num" , list (range (1 , 99 )))
86
+ @pytest .mark .parametrize ("pt1" , [0.5 ])
87
+ def bench_edge_detection_fused_pydata (tacoBench , num , pt1 , plot ):
88
+ loader = ImagePydataSparseTensorLoader ()
89
+ sparse_bin_img1 = safeCastPydataTensorToInts (loader .sparse_image (num , pt1 , 1 ))
90
+ sparse_bin_img2 = safeCastPydataTensorToInts (loader .sparse_image (num , pt1 + 0.05 , 2 ))
91
+ sparse_bin_window = loader .sparse_window (num , 3 )
92
+ bin_img1 = loader .dense_image (num , pt1 , 1 )
93
+ bin_img2 = loader .dense_image (num , pt1 + 0.05 , 2 )
94
+ bin_window = loader .dense_window (num )
95
+
96
+ if plot :
97
+ print (sparse_bin_img1 .shape )
98
+ print (sparse_bin_img2 .shape )
99
+
100
+ def sparse_bench ():
101
+ sbi1 = np .logical_and (sparse_bin_img1 , sparse_bin_window )
102
+ sbi2 = np .logical_and (sparse_bin_img2 , sparse_bin_window )
103
+ sparse_xor_img = np .logical_xor (sbi1 , sbi2 ).astype ('int' )
104
+ return sparse_xor_img
105
+
106
+ def dense_bench ():
107
+ bi1 = np .logical_and (bin_img1 , bin_window ).astype ('int' )
108
+ bi2 = np .logical_and (bin_img2 , bin_window ).astype ('int' )
109
+ xor_img = np .logical_xor (bi1 , bi2 ).astype ('int' )
110
+ return xor_img
111
+ ret = tacoBench (sparse_bench )
112
+ sparse_xor_img = sparse_bench ()
113
+ xor_img = dense_bench ()
114
+
115
+ if plot :
116
+ num_elements = float (np .prod (bin_img1 .shape ))
117
+ print ("Sparse xor NNZ = " , sparse_xor_img .nnz , "\t " , "Dense xor NNZ = " , np .sum (xor_img != 0 ))
118
+ print ("Sparsity img 1 " , np .sum (bin_img1 != 0 ) / num_elements )
119
+ print ("Sparsity img 2 " , np .sum (bin_img2 != 0 ) / num_elements )
120
+ print ("Sparsity xor " , np .sum (xor_img != 0 ) / num_elements )
121
+ sparse_xor_img = sparse_xor_img .todense ()
122
+ t1 = round (loader .max [num ]* pt1 , 2 )
123
+ t2 = round (loader .max [num ]* (pt1 + 0.05 ), 2 )
124
+ plot_image (loader .img [num ], bin_img1 , bin_img2 , xor_img , sparse_xor_img , t1 , t2 , bin_window )
125
+
126
+ assert (sparse_xor_img .nnz == np .sum (xor_img != 0 ))
127
+
128
+ @pytest .mark .parametrize ("num" , list (range (1 , 99 )))
129
+ @pytest .mark .parametrize ("pt1" , [0.5 ])
130
+ def bench_edge_detection_fused_dense (tacoBench , num , pt1 ):
131
+ loader = ImagePydataSparseTensorLoader ()
132
+ bin_img1 = loader .dense_image (num , pt1 , 1 )
133
+ bin_img2 = loader .dense_image (num , pt1 + 0.05 , 2 )
134
+ bin_window = loader .dense_window (num )
135
+
136
+ def dense_bench ():
137
+ bi1 = np .logical_and (bin_img1 , bin_window ).astype ('int' )
138
+ bi2 = np .logical_and (bin_img2 , bin_window ).astype ('int' )
139
+ xor_img = np .logical_xor (bin_img1 , bin_img2 ).astype ('int' )
140
+ return xor_img
141
+ tacoBench (dense_bench )
142
+
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 )
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
+
76
219
if __name__ == "__main__" :
77
220
main ()
78
221
0 commit comments