4
4
import PIL .Image
5
5
import numpy
6
6
import numpycnn
7
+ import threading
8
+ import kivy .clock
7
9
8
10
class AppGUI (kivy .uix .boxlayout .BoxLayout ):
9
11
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 )
10
20
11
21
def start_cnn (self ):
12
22
img1 = self .ids .img1 #Original Image
13
23
im = PIL .Image .open (img1 .source ).convert ("L" )
14
24
img_arr = numpy .asarray (im , dtype = numpy .uint8 )
15
25
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. "
22
32
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."
29
35
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."
36
38
37
39
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 ):
38
118
img1 = self .ids .img1
39
119
img2 = self .ids .img2
40
120
img3 = self .ids .img3
@@ -56,133 +136,84 @@ def cnn_example(self, numpy_img, layer=1):
56
136
lbl9 = self .ids .lbl9
57
137
58
138
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" )
71
139
lbl2 .text = "L1Map1"
72
140
img2 .source = "conv1_filter1.png"
73
141
img2 .reload ()
74
142
75
- im = PIL .Image .fromarray (numpy .uint8 (l1_feature_map [:, :, 1 ]))
76
- im .save ("conv1_filter2.png" )
77
143
lbl3 .text = "L1Map2"
78
144
img3 .source = "conv1_filter2.png"
79
145
img3 .reload ()
80
146
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" )
84
147
lbl4 .text = "L1Map1ReLU"
85
148
img4 .source = "conv1_relu1.png"
86
149
img4 .reload ()
87
150
88
- im = PIL .Image .fromarray (numpy .uint8 (l1_feature_map [:, :, 1 ]))
89
- im .save ("conv1_relu2.png" )
90
151
lbl5 .text = "L1Map2ReLU"
91
152
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
+
97
155
lbl6 .text = "L1Map1ReLUPool"
98
156
img6 .source = "conv1_relu_pool1.png"
99
157
img6 .reload ()
100
158
101
- im = PIL .Image .fromarray (numpy .uint8 (l1_feature_map_relu_pool [:, :, 1 ]))
102
- im .save ("conv1_relu_pool2.png" )
103
159
lbl7 .text = "L1Map2ReLUPool"
104
160
img7 .source = "conv1_relu_pool2.png"
105
161
img7 .reload ()
106
162
107
- return l1_feature_map_relu_pool
163
+ self . ids . btn . text = "Run Second Conv. Layer"
108
164
109
165
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" )
115
166
lbl1 .text = "L2Map1"
116
167
img1 .source = "conv2_filter1.png"
117
168
img1 .reload ()
118
- im = PIL .Image .fromarray (numpy .uint8 (l2_feature_map [:, :, 1 ]))
119
- im .save ("conv2_filter2.png" )
169
+
120
170
lbl2 .text = "L2Map2"
121
171
img2 .source = "conv2_filter2.png"
122
172
img2 .reload ()
123
- im = PIL .Image .fromarray (numpy .uint8 (l2_feature_map [:, :, 2 ]))
124
- im .save ("conv2_filter3.png" )
173
+
125
174
lbl3 .text = "L2Map3"
126
175
img3 .source = "conv2_filter3.png"
127
176
img3 .reload ()
128
177
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" )
132
178
lbl4 .text = "L2Map1ReLU"
133
179
img4 .source = "conv2_relu1.png"
134
180
img4 .reload ()
135
- im = PIL .Image .fromarray (numpy .uint8 (l2_feature_map_relu [:, :, 1 ]))
136
- im .save ("conv2_relu2.png" )
181
+
137
182
lbl5 .text = "L2Map3ReLU"
138
183
img5 .source = "conv2_relu2.png"
139
184
img5 .reload ()
140
- im = PIL .Image .fromarray (numpy .uint8 (l2_feature_map_relu [:, :, 2 ]))
141
- im .save ("conv2_relu3.png" )
185
+
142
186
lbl6 .text = "L2Map3ReLU"
143
187
img6 .source = "conv2_relu3.png"
144
188
img6 .reload ()
145
189
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" )
149
190
lbl7 .text = "L2Map1ReLU"
150
191
img7 .source = "conv2_relu_pool1.png"
151
192
img7 .reload ()
152
- im = PIL .Image .fromarray (numpy .uint8 (l2_feature_map_relu_pool [:, :, 1 ]))
153
- im .save ("conv2_relu_pool2.png" )
193
+
154
194
lbl8 .text = "L2Map2ReLU"
155
195
img8 .source = "conv2_relu_pool2.png"
156
196
img8 .reload ()
157
- im = PIL .Image .fromarray (numpy .uint8 (l2_feature_map_relu_pool [:, :, 2 ]))
158
- im .save ("conv2_relu_pool3.png" )
197
+
159
198
lbl9 .text = "L2Map3ReLU"
160
199
img9 .source = "conv2_relu_pool3.png"
161
200
img9 .reload ()
162
- return l2_feature_map_relu_pool
163
201
164
- elif layer == 3 :
202
+ self .ids .btn .text = "Run Third Conv. Layer"
203
+
204
+ elif layer == 0 :#0 corresponds to layer 3
165
205
lbl1 .text = "Original"
166
206
img1 .source = "input_image.jpg"
167
207
img1 .reload ()
168
208
169
209
#**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" )
174
210
lbl2 .text = "L3Map1"
175
211
img2 .source = "conv3_filter1.png"
176
212
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
+
180
214
lbl3 .text = "L3Map1ReLU"
181
215
img3 .source = "conv3_relu1.png"
182
216
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" )
186
217
lbl4 .text = "L3Map1ReLUPool"
187
218
img4 .source = "conv3_relu_pool1.png"
188
219
img4 .reload ()
@@ -204,7 +235,7 @@ def cnn_example(self, numpy_img, layer=1):
204
235
lbl8 .text = ""
205
236
lbl9 .text = ""
206
237
207
- return l3_feature_map_relu_pool
238
+ self . ids . btn . text = "Repeat Again. Run First Conv. Layer"
208
239
209
240
class NumPyCNNApp (kivy .app .App ):
210
241
pass
0 commit comments