-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.py
326 lines (298 loc) · 10.1 KB
/
Main.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
from Tkinter import *
from dialog import *
from multiprocessing import Process, Value, Array
from threading import Thread
import Proc
import time
#import zmq
import socket
import os
from tkFileDialog import *
from NET import *
from other import *
from dialog import *
class APP:
def __init__(self):
self.root = Tk()
self.root.title("MAin 10x10 BackProp")
self.lframe = Frame(self.root)
self.rframe = Frame(self.root)
self.StartButtonName = StringVar()
self.StartButtonName.set("Start Network")
self.StartButton = Button(self.root, textvariable = self.StartButtonName, command=self.StartNN)
self.StartButton.grid()
self.TestButton = Button(self.root, text = "Load Nn and Test", command = self.TestNN)
self.TestButton.grid()
self.TestText = StringVar()
self.MaxSimpleMul = MaxSimpleMul(10) ###################
self.PrepareFS()
self.PrepareView()
self.Mainproc = [None]*self.MaxSimpleMul
self.startedProc = [0] *self.MaxSimpleMul
for i in xrange(self.MaxSimpleMul) :
self.startedProc[i] = []
self.socket = socket.socket()
self.socket.bind(("", 8011))
self.socket.listen(100)#(self.MaxSimpleMul)
self.Exit = False
#self.learningrate = 0.1
self.threads = [0]*self.MaxSimpleMul
self.learningrate = [0.01]*self.MaxSimpleMul
b=[]
for i in xrange(7):
b.append(Value("d", 1.))
self.sharedmemory = [b]* self.MaxSimpleMul
for i in xrange(self.MaxSimpleMul):
self.threads[i] = Thread(target=self.Twork, args=(i, os.getcwd()+"/%s"%i))
self.threads[i].daemon = True
self.threads[i].start()
self.root.protocol('WM_DELETE_WINDOW', self.Quit)
self.root.mainloop()
def PrepareFS(self):
for i in xrange(self.MaxSimpleMul):
if os.path.isdir(os.getcwd()+"/%s"%i) == False:
os.mkdir(os.getcwd()+"/%s"%i)
fw = open(os.getcwd()+"/%s/dMSE.err"%i, "w")
fw.write("%s"%[1])
fw.close()
out = read_file_data_troika("data.txt")
res = [0 for i in xrange(len(out)/3)]
j=0
for i in xrange(0, len(out), 3):
res[j] = tuple(IntListToTrans_10(out[i:i+3]))
j+=1
res = np.array(res)
for i in xrange(self.MaxSimpleMul):
np.savetxt(os.getcwd()+"/%s/data.txt"%i, res[:,i*(10/self.MaxSimpleMul):i*(10/self.MaxSimpleMul)+(10/self.MaxSimpleMul)]) #############
fw = open(os.getcwd()+"/%s/data.len"%i, "w")
fw.write("%s"%list(res[:,i*(10/self.MaxSimpleMul):i*(10/self.MaxSimpleMul)+(10/self.MaxSimpleMul)].shape)) ##################
fw.close()
def PrepareView(self):
self.SummuryInfo = Frame(self.root)
Label(self.SummuryInfo, text= "Structure NN").grid( row=0, column=0)
Label(self.SummuryInfo, text= "Iteration").grid( row=0, column=1)
Label(self.SummuryInfo, text= "Error").grid( row=0, column=2)
Label(self.SummuryInfo, text= "Mean Errors").grid( row=0, column=3)
self.TextStructure = [0]*self.MaxSimpleMul
self.TextIterations = [0]*self.MaxSimpleMul
self.TextError = [0]*self.MaxSimpleMul
self.TextMean = [0]*self.MaxSimpleMul
for i in xrange(self.MaxSimpleMul):
self.TextStructure[i] = StringVar()
self.TextIterations[i] = StringVar()
self.TextError[i] = StringVar()
self.TextMean[i] = StringVar()
for j in xrange(self.MaxSimpleMul):
Label(self.SummuryInfo, textvariable = self.TextStructure[j]).grid(row=j+1, column=0)
Label(self.SummuryInfo, textvariable = self.TextIterations[j]).grid(row=j+1, column=1)
Label(self.SummuryInfo, textvariable = self.TextError[j]).grid(row=j+1, column=2)
Label(self.SummuryInfo, textvariable = self.TextMean[j]).grid(row=j+1, column=3)
self.SummuryInfo.grid(sticky=N+S+E+W)
def StartNN(self, x = [1], iter=None):
if self.StartButtonName.get() == "Start Network":
self.StartButtonName.set("Pause Network")
self.root.title("MAin 10x10 RPMSProp") ##############
if iter == None:
arg =[]
arg += [10/self.MaxSimpleMul] ############
arg +=x
arg += [10/self.MaxSimpleMul] #########
self.dMSE = [0]*self.MaxSimpleMul
for i in xrange(self.MaxSimpleMul):
#threads[i] = Thread(target=self.Twork, args=(i, os.getcwd()+"/%s"%i))
self.Mainproc[i] = Process(target=Proc.main, args = (arg, os.getcwd()+"/%s"%i, "main", i, 0.1))
self.dMSE[i] = []
for i in xrange(self.MaxSimpleMul):
#threads[i].start()
self.Mainproc[i].start()
if iter != None:
self.dMSE[iter] = []
arg =[]
arg += [10/self.MaxSimpleMul] ############
arg +=x
arg += [10/self.MaxSimpleMul] ###########
self.Mainproc[iter] = Process(target=Proc.main, args = (arg, os.getcwd()+"/%s"%iter, "main", iter, self.learningrate[iter], None, self.sharedmemory))
self.Mainproc[iter].start()
def TestNN(self):
options = {}
options['defaultextension'] = '.txt'
options['filetypes'] = [('all files', '.*'), ('text files', '.txt')]
options['initialdir'] = os.getcwd()
options['initialfile'] = 'myfile.txt'
options['parent'] = self.root
options['title'] = 'This is a title'
p = askopenfile( **options)
Net = NET(1,1)
Net.LoadNet(p.name)
d = DialogTestNN(self.root)
res = IntListToTrans_52(d.value)
#print len(res)
t = Net.TestNet(res)
print ("sizeinput = %s\n out = %s"%(Net.inputsize, t))
t1 = t.tolist()
s1 = []
for i in xrange(len(t1)):
s1.append((float(t1[i]), i+1))
dd = dict(s1)
s = dd.keys()
s = np.sort(np.array(s, dtype = "float"))
r = []
for i in range(1,7):
r.append(dd[s[-i]])
print (dd[s[-i]], s[-i])
k = "%s\n%s"%(TransToIntList(t.tolist()), r)
self.TestText.set("%s"%k)
def UpdateButtons(self):
if self.startedProc[iter] != []:
try:
msg = self.socket.recv()
k = msg.split(" ")
self.ButtonsText[int(k[0])].set("Net: %s, %s, %s\nError: %s\nProgress: %s"%(self.ParamForText[int(k[0])][0], self.ParamForText[int(k[0])][1], self.ParamForText[int(k[0])][2], float(k[1]), int(k[2])))
self.root.update()
except: pass
def UpdateMainProcNN(self, iter):
#self.TextStructure[int(m[2])].set("%s"%m[7])
self.TextIterations[iter].set("%s"%self.sharedmemory[iter][3].value)
self.TextError[iter].set("%s"%self.sharedmemory[iter][1].value)
self.TextMean[iter].set("%s"%self.sharedmemory[iter][5].value)
#def MainLoop(self):
def DellFiles(self, path, fend, all = False):
#path = os.getcwd()
dirlist = os.listdir(path)
l = []
for i in dirlist:
#if os.path.isfile(i):
if i[-len(fend):] == fend:
l.append(os.path.basename(i))
l1=[]
for i in xrange(len(l)):
l1.append(l[i][:-(len(fend)+1)].split(" "))
for i in xrange(len(l1)):
l1[i][0] = float(l1[i][0])
d = dict(l1[:])
l2 = d.keys()
l2 = np.sort(np.array(l2,dtype = "float"))
for i in l:
if all == True:
os.remove(path+"/"+i)
elif i.find("%s"%l2[0]) == -1:
try:
os.remove(path+"/"+i)
except:
print path, i, "CAN'T REMOVE"
def Twork(self, iter, path):
#path = os.getcwd()
while self.Exit == False:
if self.Mainproc[iter] != None:
#print self.Mainproc.is_alive()
if self.Mainproc[iter].is_alive() == False and self.startedProc[iter] == []:
dirlist = os.listdir(path)
#print iter, dirlist
l = []
for i in dirlist:
#print i
if i[-4:]== ".xml":
l.append(i)
#print iter, l, "L"
l1=[]
for i in xrange(len(l)):
l1.append(l[i][:-4].split(" "))
for i in xrange(len(l1)):
l1[i][0] = float(l1[i][0])
d = dict(l1[:])
l2 = d.keys()
l2 = np.sort(np.array(l2,dtype = "float64"))
#print l2
if l2[0]<0.000000000000001:
self.Exit = True
break
fw = open("dMSE.err","w")
fw.write("%s"%self.dMSE[iter][:-1])
fw.close()
self.dMSE[iter] = []
learningrate = float("{0:.9f}".format(float(l2[0])/10))
if self.learningrate[iter] > learningrate:
self.learningrate[iter] = learningrate
param = self.ParamFromText(d[l2[0]].split("_")[1])
alter = self.Alternate(param)
self.AddProc(iter, alter[:])
p = 0
if self.startedProc[iter] != []:
for i in self.startedProc[iter]:
if i.is_alive() == True:
p+=1
if p == 0 and self.Mainproc[iter].is_alive() == False:
#print "YES"
dirlist = os.listdir(path)
l = []
for i in dirlist:
if i[-4:] == ".upd":
l.append(i)
l1=[]
for i in xrange(len(l)):
l1.append(l[i][:-4].split(" "))
for i in xrange(len(l1)):
l1[i][0] = float(l1[i][0])
d = dict(l1[:])
l2 = d.keys()
l2 = np.sort(np.array(l2,dtype = "float64"))
print d[l2[0]].split("_")
arg = self.ParamFromText(d[l2[0]].split("_")[1])
self.StartNN(arg, iter)
k =[10/self.MaxSimpleMul]
k+=arg
k.append(10/self.MaxSimpleMul)
self.TextStructure[iter].set("%s"%k)
#self.root.title("MAin 52x52 BackProp %s"%d[l2[0]].split("_")[1])
self.StopProc(iter)
self.startedProc[iter] = []
self.DellFiles(path, "xml")
self.DellFiles(path, "upd", True)
self.DellFiles(path, "work")
self.UpdateMainProcNN(iter)
def AddProc(self, iter, alter):
self.startedProc[iter] = []
for i in xrange(len(alter)):
arg = []
arg+=[10/self.MaxSimpleMul] #############
arg+=alter[i]
arg+=[10/self.MaxSimpleMul] #############
self.startedProc[iter].append(Process(target=Proc.main, args=(arg, os.getcwd()+"/%s"%iter, i, iter, self.learningrate[iter], i)))
for i in self.startedProc[iter]:
#print iter, i
i.start()
def ParamFromText(self, s):
s2 = []
if s.find(",") == -1:
s2.append(int(s[1:-1]))
else:
s1 = s[1:-1]
s4 = s1.split(",")
for i in s4:
s2.append(int(i))
return s2[:]
def Alternate(self, m):
r = []
for i in xrange(len(m)):
m[i]= m[i]+1
r.append(m[:])
m[i]= m[i]-1
m.append(1)
r.append(m)
#print r
return r
def CommandStopProc(self, event):
for i in xrange(len(self.ProcessButtons)):
print (self.ProcessButtons[i], event.widget)
if self.ProcessButtons[i] == event.widget:
self.startedProc[iter][i].terminate()
event.widget.destroy()
break
def Quit(self):
self.Exit = True
for i in xrange(self.MaxSimpleMul):
self.threads[i].join()
self.Mainproc[i].join()
sys.exit()
if __name__ == "__main__":
a = APP()