/
graphicDrawing.py
333 lines (244 loc) · 10.3 KB
/
graphicDrawing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
from PyQt5.QtWidgets import QMainWindow, QApplication ,QLabel, QMenu, QFrame, QMenuBar,QHBoxLayout,QVBoxLayout,QAction, QFileDialog, QWidget, QPushButton
from PyQt5.QtGui import QIcon, QImage, QPainter, QPen, QBrush,QPixmap,QLinearGradient,QRadialGradient
from PyQt5.QtCore import Qt, QPoint
import config
import numpy as np
import sys
import pickle
from random import randint
from nn import nn
class QLabelDraw(QLabel):
def __init__(self):
super().__init__()
top = 400
left = 400
width = 400
height = 400
self.setStyleSheet("margin:5px; border:1px solid rgb(0, 255, 0); ")
self.setGeometry(top, left, width, height)
self.image = QImage(self.size(), QImage.Format_Grayscale8)
self.image.fill(Qt.black)
self.image
self.drawing = False
self.brushSize =30
self.brushColor = Qt.white
self.brushColor2 = Qt.black
self.lastPoint = QPoint()
self.setStyleSheet("border: 10px solid black;")
self.mnist=[]
#funzioni di disegno tramite lo spostamento del mouse
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.drawing = True
self.lastPoint = event.pos()
#funzioni di disegno tramite lo spostamento del mouse
def mouseMoveEvent(self, event):
if(event.buttons() & Qt.LeftButton) & self.drawing:
painter = QPainter(self.image)
painter.setPen(QPen(self.brushColor, self.brushSize))
painter.drawLine(self.lastPoint, event.pos())
self.lastPoint = event.pos()
self.update()
#funzioni di disegno tramite lo spostamento del mouse
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton:
self.drawing = False
self.update()
def paintEvent(self, event):
canvasPainter = QPainter(self)
canvasPainter.drawImage(self.rect(),self.image, self.image.rect() )
self.update()
#salva l'immagine disegnata
def saveImage(self):
del self.mnist[:]
#resize dell'immagine da 400x400 a 28x28
self.img = QImage((self.image)).scaled(28, 28, Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
self.image = QImage((self.image)).scaled(28, 28, Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
#self.image=QImage((self.image)).scaled(28, 28, Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
#salvataggio del colore dei pixel nell'array msist
for i in range(28):
for j in range(28):
self.value=self.img.pixel(j,i)%256
self.mnist.append(self.value)
#inserimento del vettore mnist nella rete neurale
#config.x=[normalize(float(x)) for x in config.x]
self.mnist=[1 if int(x)>10 else 0 for x in self.mnist]
my_result=result(nn1.feedforward(self.mnist))
print(nn1.feedforward(self.mnist))
print(my_result)
return my_result
#cancella l'immagine disegnata
def cancelImage(self):
self.image.fill(Qt.black)
self.image = QImage((self.image)).scaled(400, 400, Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
self.update()
class Example(QWidget):
def __init__(self):
super().__init__()
self.mnist_copy=[]
self.initUI()
#richiama la funzione di salvataggio in QLabelDraw returnando il valore stampato dalla rete neurale
def save(self):
nn_result=self.QLabelDraw.saveImage()
#inserisce l'immagine .jpg rappresentante il numero finale nel labelImage
self.labelImage.clear()
final_image='number'+str(nn_result)+'.jpg'
self.pixmap = QPixmap(final_image)
self.labelImage.setPixmap(self.pixmap)
#print(result(nn1.feedforward(config.x)))
#richiama la funzione di cancellazione del disegno e cancella l'immagine .jpg sostituendola con una nera
def cancel(self):
self.QLabelDraw.cancelImage()
blackimage='none.jpg'
self.pixmap = QPixmap(blackimage)
self.labelImage.setPixmap(self.pixmap)
#inizializzazione dei layout e label che compongono l'applicazione
def initUI(self):
layout1 = QHBoxLayout()
layout2 = QVBoxLayout()
layout3 = QVBoxLayout()
#label dell'immagine .jpg
self.labelImage = QLabel(self)
#label disegnabile
self.QLabelDraw = QLabelDraw()
self.labelImage.setStyleSheet("border: 1px solid black;")
blackimage='none.jpg'
self.pixmap = QPixmap(blackimage)
self.QLabelDraw.setFixedSize(400, 400)
self.QLabelDraw.setStyleSheet("border: 100px solid black;")
self.labelImage.setPixmap(self.pixmap)
self.labelImage.setFixedSize(400, 400)
layout2.addWidget(self.QLabelDraw)
layout3.addWidget(self.labelImage)
saveButton = QPushButton('Save', self)
cancelButton = QPushButton('Cancel', self)
saveAction = QAction(QIcon("icons/save.jpg"), "Save",self)
saveAction.setShortcut("Ctrl+S")
saveButton.addAction(saveAction)
saveButton.clicked.connect(self.save)
cancelAction = QAction(QIcon("icons/clear.png"), "Clear", self)
cancelAction.setShortcut("Ctrl+C")
cancelButton.addAction(cancelAction)
cancelButton.clicked.connect(self.cancel)
layout2.addWidget(saveButton)
layout3.addWidget(cancelButton)
layout1.addLayout(layout2)
layout1.addLayout(layout3)
widget = QWidget(self)
widget.setLayout(layout1)
self.setGeometry(0, 0, 825, 450)
self.setWindowTitle('Number Recognition')
self.show()
def main():
app = QApplication(sys.argv)
ex = Example()
######################################################################################
#Reading
global nn1
nn1 = nn([70,16])
numberOfEpochs = 30 # 10
try:
# binary_file_pesi = open('Pesi.bin', mode='rb')
# binary_file_bias = open('Bias.bin', mode='rb')
# my_pesi=pickle.load(binary_file_pesi)
# nn1.setPesi(my_pesi)
#my_bias=pickle.load(binary_file_bias)
#nn1.setBias(my_bias)
#print("Pesi già presenti!")
binary_file_pesi = open('Data.bin', mode='rb')
t=[]
for _ in range(2):
t.append(pickle.load(binary_file_pesi))
nn1.setPesi(t[0])
nn1.setBias(t[1])
print("Pesi già presenti!")
#training if reading fails
except IOError:
print("Pesi mancanti, inizio training")
######################################################################################
# input dal file
targetsTrain = []
inputsTrain = []
#mydataset = open("data/mnistTrain.txt", "r")
mydataset = open(
r"C:\\Users\\bigfo\\OneDrive\\Desktop\\dati\\mnistTrain_copy.txt", "r")
for x in range(40000): # numberOfinputs 30000
targetTrain = int(mydataset.read(1))
#number = [normalize(float(x)) for x in next(mydataset).split()]
number = [1 if int(x)>90 else 0 for x in next(mydataset).split()]
targetsTrain.append(targetTrain)
inputsTrain.append(number)
# vettori di targetTrain
targetVectors = []
for i in range(len(targetsTrain)):
targetVectors.append(
[float(1) if x == targetsTrain[i] else float(0) for x in range(10)])
mydataset.close()
print("training terminato")
######################################################################################
#Testing data
targetsTest = []
inputsTest = []
#testDataset = open("data/mnistTest.txt", "r")
testDataset = open(
r"C:\\Users\\bigfo\\OneDrive\\Desktop\\dati\\mnistTest_copy.txt", "r")
for x in range(10000): # len(inputsTest) == 10000
targetTest = int(testDataset.read(1))
numberTest = [normalize(float(x)) for x in next(testDataset).split()]
targetsTest.append(targetTest)
inputsTest.append(numberTest)
# vettori di targetTrain
targetsTestVectors = []
for i in range(len(targetsTest)):
targetsTestVectors.append(
[float(1) if x == targetsTest[i] else float(0) for x in range(10)])
testDataset.close()
for l in range(numberOfEpochs):
nn1.TrainNet(inputsTrain, targetVectors)
print("EFFICIENCY", l+1, ": ", getEfficiency(inputsTest, targetsTest))
# tempPesi=pickle.dump(nn1.getPesi(),open('Pesi.bin', 'wb'))
#tempBias=pickle.dump(nn1.getBias(),open('Bias.bin', 'wb'))
Data=open('Data.bin', 'wb')
pickle.dump(nn1.getPesi(),Data)
pickle.dump(nn1.getBias(),Data)
######################################################################################
sys.exit(app.exec_())
######################################################################################
# funzioni ausiliarie
def result(output) :
max=0
count=0
for z in range(len(output)):
if output[z]>max:
max=output[z]
count=z
return count
def normalize(x) :
grey = 90
return 1.0/(1.0+np.exp(-x+grey))
def getEfficiency(inputsVector, targetScalars) :
c = 3
efficiency = 0.0
for x in range(len(inputsVector)):
output=nn1.feedforward(inputsVector[x])
#print("test", x+1, "(targetTrain, ris): ", targetVector[x], result(output), " ", targetsTrain[x] == result(output))
if targetScalars[x] == result(output) :
efficiency += 1.0
elif 300*c < x < 300*(c+1) : # elif temporaneo per vedere dove sbaglia
pass
#printNumber(inputsVector[x])
efficiency = efficiency/len(inputsVector)
return efficiency
def printNumber(n) :
s = normalize(110)
for i in range(28) :
for j in range(28) :
if(n[i * 28 + j] > s) :
print("o", end =" ")
else :
print(" ", end =" ")
print("")
if __name__ == '__main__':
main()
#print("prova")
####################################################################################################################################