44import PIL .Image
55import numpy
66import numpycnn
7+ import threading
8+ import kivy .clock
79
810class AppGUI (kivy .uix .boxlayout .BoxLayout ):
911 layer_num = 1
12+ def run_cnn_thread (self ):
13+ print (self .layer_num % 3 )
14+ print ("Layer num : " , self .layer_num )
15+ self .curr_img = self .cnn_example (numpy_img = self .curr_img , layer = self .layer_num % 3 )
16+ self .refresh_GUI (layer = self .layer_num % 3 )
17+ self .ids .lbl_details .text = "Shape of output=" + str (self .curr_img .shape )
18+ self .layer_num = self .layer_num + 1
19+ print ("Output shape : " , self .curr_img .shape )
1020
1121 def start_cnn (self ):
1222 img1 = self .ids .img1 #Original Image
1323 im = PIL .Image .open (img1 .source ).convert ("L" )
1424 img_arr = numpy .asarray (im , dtype = numpy .uint8 )
1525 if self .layer_num % 3 == 1 :
16- self . curr_img = self .cnn_example ( numpy_img = img_arr , layer = 1 )
17- self . layer_num = self . layer_num + 1
18- self . ids . lbl_details . text = "Layer 1 (2ConvFilters,ReLU,Pool) \n " + "Shape of output=" + str ( self . curr_img . shape )
19- print ( "Layer num : " , self .layer_num )
20- print ( "Output shape : " , self .curr_img . shape )
21- self .ids .btn .text = "Run Second Conv. Layer "
26+ img1 = self .ids . img1 #Original Image
27+ im = PIL . Image . open ( img1 . source ). convert ( "L" )
28+ img_arr = numpy . asarray ( im , dtype = numpy . uint8 )
29+ self .curr_img = img_arr
30+ threading . Thread ( target = self .run_cnn_thread ). start ( )
31+ self .ids .btn .text = "Wait. "
2232 elif self .layer_num % 3 == 2 :
23- self .curr_img = self .cnn_example (numpy_img = self .curr_img , layer = 2 )
24- self .layer_num = self .layer_num + 1
25- self .ids .lbl_details .text = "Layer 2 (3ConvFilters,ReLU,Pool)\n " + "Shape of output=" + str (self .curr_img .shape )
26- print ("Layer num : " , self .layer_num )
27- print ("Output shape : " , self .curr_img .shape )
28- self .ids .btn .text = "Run Third Conv. Layer"
33+ threading .Thread (target = self .run_cnn_thread ).start ()
34+ self .ids .btn .text = "Wait."
2935 elif self .layer_num % 3 == 0 :
30- self .curr_img = self .cnn_example (numpy_img = self .curr_img , layer = 3 )
31- self .layer_num = self .layer_num + 1
32- self .ids .lbl_details .text = "Layer 3 (1ConvFilter,ReLU,Pool)\n " + "Shape of output=" + str (self .curr_img .shape )
33- print ("Layer num : " , self .layer_num )
34- print ("Output shape : " , self .curr_img .shape )
35- self .ids .btn .text = "Repeat Again"
36+ threading .Thread (target = self .run_cnn_thread ).start ()
37+ self .ids .btn .text = "Wait."
3638
3739 def cnn_example (self , numpy_img , layer = 1 ):
40+ if layer == 1 :
41+ #**Working with conv layer 1**
42+ l1_filter = numpy .zeros ((2 ,3 ,3 ))
43+ l1_filter [0 , :, :] = numpy .array ([[[- 1 , 0 , 1 ],
44+ [- 1 , 0 , 1 ],
45+ [- 1 , 0 , 1 ]]])
46+ l1_filter [1 , :, :] = numpy .array ([[[1 , 1 , 1 ],
47+ [0 , 0 , 0 ],
48+ [- 1 , - 1 , - 1 ]]])
49+
50+ l1_feature_map = numpycnn .conv (numpy_img , l1_filter )
51+ im = PIL .Image .fromarray (numpy .uint8 (l1_feature_map [:, :, 0 ]))
52+ im .save ("conv1_filter1.png" )
53+
54+ im = PIL .Image .fromarray (numpy .uint8 (l1_feature_map [:, :, 1 ]))
55+ im .save ("conv1_filter2.png" )
56+
57+ l1_feature_map_relu = numpycnn .relu (l1_feature_map )
58+ im = PIL .Image .fromarray (numpy .uint8 (l1_feature_map [:, :, 0 ]))
59+ im .save ("conv1_relu1.png" )
60+
61+ im = PIL .Image .fromarray (numpy .uint8 (l1_feature_map [:, :, 1 ]))
62+ im .save ("conv1_relu2.png" )
63+
64+ l1_feature_map_relu_pool = numpycnn .pooling (l1_feature_map_relu , 2 , 2 )
65+ im = PIL .Image .fromarray (numpy .uint8 (l1_feature_map_relu_pool [:, :, 0 ]))
66+ im .save ("conv1_relu_pool1.png" )
67+
68+ im = PIL .Image .fromarray (numpy .uint8 (l1_feature_map_relu_pool [:, :, 1 ]))
69+ im .save ("conv1_relu_pool2.png" )
70+
71+ return l1_feature_map_relu_pool
72+
73+ elif layer == 2 :
74+ #**Working with conv layer 2**
75+ l2_filter = numpy .random .rand (3 , 5 , 5 , numpy_img .shape [- 1 ])
76+ l2_feature_map = numpycnn .conv (numpy_img , l2_filter )
77+ im = PIL .Image .fromarray (numpy .uint8 (l2_feature_map [:, :, 0 ]))
78+ im .save ("conv2_filter1.png" )
79+ im = PIL .Image .fromarray (numpy .uint8 (l2_feature_map [:, :, 1 ]))
80+ im .save ("conv2_filter2.png" )
81+ im = PIL .Image .fromarray (numpy .uint8 (l2_feature_map [:, :, 2 ]))
82+ im .save ("conv2_filter3.png" )
83+
84+ l2_feature_map_relu = numpycnn .relu (l2_feature_map )
85+ im = PIL .Image .fromarray (numpy .uint8 (l2_feature_map_relu [:, :, 0 ]))
86+ im .save ("conv2_relu1.png" )
87+ im = PIL .Image .fromarray (numpy .uint8 (l2_feature_map_relu [:, :, 1 ]))
88+ im .save ("conv2_relu2.png" )
89+ im = PIL .Image .fromarray (numpy .uint8 (l2_feature_map_relu [:, :, 2 ]))
90+ im .save ("conv2_relu3.png" )
91+
92+ l2_feature_map_relu_pool = numpycnn .pooling (l2_feature_map_relu , 2 , 2 )
93+ im = PIL .Image .fromarray (numpy .uint8 (l2_feature_map_relu_pool [:, :, 0 ]))
94+ im .save ("conv2_relu_pool1.png" )
95+ im = PIL .Image .fromarray (numpy .uint8 (l2_feature_map_relu_pool [:, :, 1 ]))
96+ im .save ("conv2_relu_pool2.png" )
97+ im = PIL .Image .fromarray (numpy .uint8 (l2_feature_map_relu_pool [:, :, 2 ]))
98+ im .save ("conv2_relu_pool3.png" )
99+ return l2_feature_map_relu_pool
100+
101+ elif layer == 0 :#0 corresponds to layer 3
102+ #**Working with conv layer 3**
103+ l3_filter = numpy .random .rand (1 , 7 , 7 , numpy_img .shape [- 1 ])
104+ l3_feature_map = numpycnn .conv (numpy_img , l3_filter )
105+ im = PIL .Image .fromarray (numpy .uint8 (l3_feature_map [:, :, 0 ]))
106+ im .save ("conv3_filter1.png" )
107+ l3_feature_map_relu = numpycnn .relu (l3_feature_map )
108+ im = PIL .Image .fromarray (numpy .uint8 (l3_feature_map_relu [:, :, 0 ]))
109+ im .save ("conv3_relu1.png" )
110+ l3_feature_map_relu_pool = numpycnn .pooling (l3_feature_map_relu , 2 , 2 )
111+ im = PIL .Image .fromarray (numpy .uint8 (l3_feature_map_relu_pool [:, :, 0 ]))
112+ im .save ("conv3_relu_pool1.png" )
113+
114+ return l3_feature_map_relu_pool
115+
116+ @kivy .clock .mainthread
117+ def refresh_GUI (self , layer = 1 ):
38118 img1 = self .ids .img1
39119 img2 = self .ids .img2
40120 img3 = self .ids .img3
@@ -56,133 +136,84 @@ def cnn_example(self, numpy_img, layer=1):
56136 lbl9 = self .ids .lbl9
57137
58138 if layer == 1 :
59- #**Working with conv layer 1**
60- l1_filter = numpy .zeros ((2 ,3 ,3 ))
61- l1_filter [0 , :, :] = numpy .array ([[[- 1 , 0 , 1 ],
62- [- 1 , 0 , 1 ],
63- [- 1 , 0 , 1 ]]])
64- l1_filter [1 , :, :] = numpy .array ([[[1 , 1 , 1 ],
65- [0 , 0 , 0 ],
66- [- 1 , - 1 , - 1 ]]])
67-
68- l1_feature_map = numpycnn .conv (numpy_img , l1_filter )
69- im = PIL .Image .fromarray (numpy .uint8 (l1_feature_map [:, :, 0 ]))
70- im .save ("conv1_filter1.png" )
71139 lbl2 .text = "L1Map1"
72140 img2 .source = "conv1_filter1.png"
73141 img2 .reload ()
74142
75- im = PIL .Image .fromarray (numpy .uint8 (l1_feature_map [:, :, 1 ]))
76- im .save ("conv1_filter2.png" )
77143 lbl3 .text = "L1Map2"
78144 img3 .source = "conv1_filter2.png"
79145 img3 .reload ()
80146
81- l1_feature_map_relu = numpycnn .relu (l1_feature_map )
82- im = PIL .Image .fromarray (numpy .uint8 (l1_feature_map [:, :, 0 ]))
83- im .save ("conv1_relu1.png" )
84147 lbl4 .text = "L1Map1ReLU"
85148 img4 .source = "conv1_relu1.png"
86149 img4 .reload ()
87150
88- im = PIL .Image .fromarray (numpy .uint8 (l1_feature_map [:, :, 1 ]))
89- im .save ("conv1_relu2.png" )
90151 lbl5 .text = "L1Map2ReLU"
91152 img5 .source = "conv1_relu2.png"
92- img5 .reload ()
93-
94- l1_feature_map_relu_pool = numpycnn .pooling (l1_feature_map_relu , 2 , 2 )
95- im = PIL .Image .fromarray (numpy .uint8 (l1_feature_map_relu_pool [:, :, 0 ]))
96- im .save ("conv1_relu_pool1.png" )
153+ img5 .reload ()
154+
97155 lbl6 .text = "L1Map1ReLUPool"
98156 img6 .source = "conv1_relu_pool1.png"
99157 img6 .reload ()
100158
101- im = PIL .Image .fromarray (numpy .uint8 (l1_feature_map_relu_pool [:, :, 1 ]))
102- im .save ("conv1_relu_pool2.png" )
103159 lbl7 .text = "L1Map2ReLUPool"
104160 img7 .source = "conv1_relu_pool2.png"
105161 img7 .reload ()
106162
107- return l1_feature_map_relu_pool
163+ self . ids . btn . text = "Run Second Conv. Layer"
108164
109165 elif layer == 2 :
110- #**Working with conv layer 2**
111- l2_filter = numpy .random .rand (3 , 5 , 5 , numpy_img .shape [- 1 ])
112- l2_feature_map = numpycnn .conv (numpy_img , l2_filter )
113- im = PIL .Image .fromarray (numpy .uint8 (l2_feature_map [:, :, 0 ]))
114- im .save ("conv2_filter1.png" )
115166 lbl1 .text = "L2Map1"
116167 img1 .source = "conv2_filter1.png"
117168 img1 .reload ()
118- im = PIL .Image .fromarray (numpy .uint8 (l2_feature_map [:, :, 1 ]))
119- im .save ("conv2_filter2.png" )
169+
120170 lbl2 .text = "L2Map2"
121171 img2 .source = "conv2_filter2.png"
122172 img2 .reload ()
123- im = PIL .Image .fromarray (numpy .uint8 (l2_feature_map [:, :, 2 ]))
124- im .save ("conv2_filter3.png" )
173+
125174 lbl3 .text = "L2Map3"
126175 img3 .source = "conv2_filter3.png"
127176 img3 .reload ()
128177
129- l2_feature_map_relu = numpycnn .relu (l2_feature_map )
130- im = PIL .Image .fromarray (numpy .uint8 (l2_feature_map_relu [:, :, 0 ]))
131- im .save ("conv2_relu1.png" )
132178 lbl4 .text = "L2Map1ReLU"
133179 img4 .source = "conv2_relu1.png"
134180 img4 .reload ()
135- im = PIL .Image .fromarray (numpy .uint8 (l2_feature_map_relu [:, :, 1 ]))
136- im .save ("conv2_relu2.png" )
181+
137182 lbl5 .text = "L2Map3ReLU"
138183 img5 .source = "conv2_relu2.png"
139184 img5 .reload ()
140- im = PIL .Image .fromarray (numpy .uint8 (l2_feature_map_relu [:, :, 2 ]))
141- im .save ("conv2_relu3.png" )
185+
142186 lbl6 .text = "L2Map3ReLU"
143187 img6 .source = "conv2_relu3.png"
144188 img6 .reload ()
145189
146- l2_feature_map_relu_pool = numpycnn .pooling (l2_feature_map_relu , 2 , 2 )
147- im = PIL .Image .fromarray (numpy .uint8 (l2_feature_map_relu_pool [:, :, 0 ]))
148- im .save ("conv2_relu_pool1.png" )
149190 lbl7 .text = "L2Map1ReLU"
150191 img7 .source = "conv2_relu_pool1.png"
151192 img7 .reload ()
152- im = PIL .Image .fromarray (numpy .uint8 (l2_feature_map_relu_pool [:, :, 1 ]))
153- im .save ("conv2_relu_pool2.png" )
193+
154194 lbl8 .text = "L2Map2ReLU"
155195 img8 .source = "conv2_relu_pool2.png"
156196 img8 .reload ()
157- im = PIL .Image .fromarray (numpy .uint8 (l2_feature_map_relu_pool [:, :, 2 ]))
158- im .save ("conv2_relu_pool3.png" )
197+
159198 lbl9 .text = "L2Map3ReLU"
160199 img9 .source = "conv2_relu_pool3.png"
161200 img9 .reload ()
162- return l2_feature_map_relu_pool
163201
164- elif layer == 3 :
202+ self .ids .btn .text = "Run Third Conv. Layer"
203+
204+ elif layer == 0 :#0 corresponds to layer 3
165205 lbl1 .text = "Original"
166206 img1 .source = "input_image.jpg"
167207 img1 .reload ()
168208
169209 #**Working with conv layer 3**
170- l3_filter = numpy .random .rand (1 , 7 , 7 , numpy_img .shape [- 1 ])
171- l3_feature_map = numpycnn .conv (numpy_img , l3_filter )
172- im = PIL .Image .fromarray (numpy .uint8 (l3_feature_map [:, :, 0 ]))
173- im .save ("conv3_filter1.png" )
174210 lbl2 .text = "L3Map1"
175211 img2 .source = "conv3_filter1.png"
176212 img2 .reload ()
177- l3_feature_map_relu = numpycnn .relu (l3_feature_map )
178- im = PIL .Image .fromarray (numpy .uint8 (l3_feature_map_relu [:, :, 0 ]))
179- im .save ("conv3_relu1.png" )
213+
180214 lbl3 .text = "L3Map1ReLU"
181215 img3 .source = "conv3_relu1.png"
182216 img3 .reload ()
183- l3_feature_map_relu_pool = numpycnn .pooling (l3_feature_map_relu , 2 , 2 )
184- im = PIL .Image .fromarray (numpy .uint8 (l3_feature_map_relu_pool [:, :, 0 ]))
185- im .save ("conv3_relu_pool1.png" )
186217 lbl4 .text = "L3Map1ReLUPool"
187218 img4 .source = "conv3_relu_pool1.png"
188219 img4 .reload ()
@@ -204,7 +235,7 @@ def cnn_example(self, numpy_img, layer=1):
204235 lbl8 .text = ""
205236 lbl9 .text = ""
206237
207- return l3_feature_map_relu_pool
238+ self . ids . btn . text = "Repeat Again. Run First Conv. Layer"
208239
209240class NumPyCNNApp (kivy .app .App ):
210241 pass
0 commit comments