-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
317 lines (237 loc) · 8.88 KB
/
server.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
from flask import Flask, render_template
from flask_socketio import SocketIO, send, emit
from DrawData import GraphHandler
from Model import LineTrainer, PatternTrainer
import json
import os
import os.path as osp
from os import listdir
from config import config
import random
import numpy as np
from pathlib import Path
import torch
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
#create folders if they dont exist yet
Path("./baseData").mkdir(exist_ok=True)
Path("./lineModels").mkdir(exist_ok=True)
gh = GraphHandler()
#path = osp.join(osp.dirname(osp.realpath(__file__)), 'data', 'dataset-test')
#dataset = MyOwnDataset("testdata", path)
def tensor2Points(x):
points = []
for i in range(x.shape[0]):
points.append({
'x': x[i][0].item(),
'y': x[i][1].item()
})
return points
def prediction2obj(pred, lineTrainer, ref_id=None):
line_obj = {}
tensor = lineTrainer.decode_latent_vector(pred["latVec"])
line_obj['points'] = tensor2Points(tensor)
line_obj['scale'] = pred["scale"]
line_obj['rotation'] = pred["rot"]
line_obj['position'] = {
"x": pred['posX'],
"y": pred['posY']
}
if ref_id:
line_obj['reference_id'] = ref_id
return line_obj
@socketio.event
def connect():
print("User connected")
emit('init', config)
mlist = getModels()
print(mlist)
emit('models', mlist)
#@socketio.on('new line')
#def new_line(points):
# print("new line received", points)
# bd.createData(points)
@socketio.on('deleteModel')
def delete_model(data):
model_name = data['modelName']
model_path = osp.join("./lineModels", model_name)
model_path_basedata = osp.join("./baseData", model_name + "-line.pt")
if osp.exists(model_path):
try:
os.remove(model_path)
os.remove(model_path_basedata)
print(f"Model {model_name} deleted successfully.")
emit('modelDeleted', {'status': 'success', 'message': f'Model {model_name} deleted successfully.'})
except Exception as e:
print(f"Error deleting model {model_name}: {str(e)}")
emit('modelDeleted', {'status': 'error', 'message': f'Error deleting model {model_name}: {str(e)}'})
else:
print(f"Model {model_name} not found.")
emit('modelDeleted', {'status': 'error', 'message': f'Model {model_name} not found.'})
# Update the list of available models
mlist = getModels()
emit('models', mlist)
@socketio.on('raw data')
def raw_data(data):
gh.init_raw(data)
print('raw data recieved')
@socketio.on('new dataset')
def new_dataset(data):
#print("socket received:")
#print(data)
if len(data['list']) == 0:
print("ERROR: no drawing data transmitted!")
else:
gh.init_raw(data)
gh.save_line_training_data(data['name'])
lineTrainer = LineTrainer(data['name'])
lineTrainer.trainModel(send_progress)
def send_progress(trainer, text, label=None):
if isinstance(text, list):
pointlist = []
for z in text:
print(z)
tensor = trainer.decode_latent_vector(z)
pointlist.append(tensor2Points(tensor))
#print(pointlist)
emit('progress', {'lines': pointlist} )
else:
print("sending progress", text)
emit('progress', {'percent':text, 'label':label} )
def getLatentspaceLine(data):
lineTrainer = LineTrainer(data['name'])
x, edge_index = gh.create_line_graph(data['points'])
z = lineTrainer.encodeLineVector(x, edge_index)
line = lineTrainer.decode_latent_vector(z)
def getModels():
onlyfiles = [f for f in listdir("./lineModels") if osp.isfile(osp.join("./lineModels", f))]
return onlyfiles
@socketio.on('generate')
def generate(data):
print(data)
print("RAW", gh.raw_data)
if data['name'] == "random":
print("choosing RANDOM model")
trainer = LineTrainer(random.choice(getModels()))
else:
trainer = LineTrainer(data['name'])
tensors, scales, rotations = trainer.generate(data['nr'], 0.5)
pointlist = []
for tensor in tensors:
pointlist.append(tensor2Points(tensor))
emit('result', {'list': pointlist, 'scales':scales, 'rotations':rotations})
@socketio.on('convertToLatentspace')
def convertToLatentspace(data):
print(data)
if data['name'] == "random":
print("choosing RANDOM model")
trainer = LineTrainer(random.choice(getModels()))
else:
trainer = LineTrainer(data['name'])
pointlist = []
for line in data['list']:
x, edge_index = gh.create_line_graph(line['points'])
z = trainer.encodeLineVector(x, edge_index)
zMatch = trainer.getClosestMatch(z)
latentLine = trainer.decode_latent_vector(zMatch)
pointlist.append(tensor2Points(latentLine))
#for tensor in tensors:
# pointlist.append(tensor2Points(tensor))
emit('latentLine', {'list': pointlist })
@socketio.on('compare')
def compare(data):
trainer = LineTrainer(data['name'])
pointlist = []
originlist = []
tensors, originpoints = trainer.extractOriginLineVectors()
for z in tensors:
print(z)
tensor = trainer.decode_latent_vector(z)
pointlist.append(tensor2Points(tensor))
for ori in originpoints:
originlist.append(tensor2Points(ori))
emit('result', {'list': pointlist, "origins":originlist})
@socketio.on('train pattern')
def new_pattern(data):
print("hallo", data)
lineTrainer = LineTrainer(data['name'])
gh.add_line_latentspace(lineTrainer)
gh.save_pattern_training_data(data['name'], data['name'])
#todo training hier ist broken ||||| ist das so?
pt = PatternTrainer(data['name'])
pt.trainModel()
@socketio.on('generate pattern')
def generate_pattern(data):
pt = PatternTrainer(data['name'])
lineTrainer = LineTrainer(data['name'])
z,y,x = pt.generate()
pred = GraphHandler.decompose_node_hidden_state(z)
ground_truth = GraphHandler.decompose_node_hidden_state(y)
base_list = []
for i in range(x.size()[0]):
n = GraphHandler.decompose_node_hidden_state(x[i])
base_list.append( prediction2obj(n, lineTrainer) )
info = {}
info["prediction"] = prediction2obj(pred, lineTrainer)
info["ground_truth"] = prediction2obj(ground_truth, lineTrainer)
info["base_list"] = base_list
emit('prediction', info)
@socketio.on('extend pattern')
def extend_pattern(data):
#prediction durch alle linien laufen lassen, clustern und durchschnitt bilden oder in die richtung "wandern" lassen
#für die ähnlihckeit distanz im vectorspace nehmen?
#später dropout?
print("LINES:", len(data["list"]))
pt = PatternTrainer(data['name'])
lineTrainer = LineTrainer(data['name'])
info = {}
if(len(data["list"]) <=0 ):
gh.clear()
#starten mit einem einzelnen set aus dem datensatz
x = pt.getDatasetSample()
base_list = []
for i in range(x.size()[0]):
n = GraphHandler.decompose_node_hidden_state(x[i])
base_list.append( prediction2obj(n, lineTrainer) )
#info["prediction"] = prediction2obj(pred, lineTrainer)
#info["ground_truth"] = prediction2obj(ground_truth, lineTrainer)
info["base_list"] = base_list
emit('extention', info)
else:
#prediction durch alle linien laufen lassen, clustern und durchschnitt bilden oder in die richtung "wandern" lassen
#bisherige linien in den graphhandler laden
gh.add_raw(data)
#latentspace auf den gleichen datensatz setzen
gh.add_line_latentspace(lineTrainer)
#alle linien abgehen, als referenz nehmen und prediction einsammeln
samples = gh.sample_complete_graph(data['name'])
predictions = []
for sample in samples:
x = sample["x"]
edge_index = sample["edge_index"]
ref_id = sample["ref_id"]
z = pt.predict(x, edge_index)
pred = GraphHandler.decompose_node_hidden_state(z)
predictions.append( prediction2obj(pred, lineTrainer, ref_id) )
info["prediction"] = predictions
emit('extention', info)
#@socketio.on('train')
#def train(data):
# trainer.trainModel(data['name'])
###### ROUTES
@app.route("/")
def start():
return render_template('webcam.html')
@app.route("/train")
def website_train():
return render_template('train.html')
@app.route("/webcam")
def website_webcam():
return render_template('webcam.html')
#@app.route("/photo")
#def website_photo():
# return render_template('photo.html')
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
socketio.run(app, host='0.0.0.0', port=port, allow_unsafe_werkzeug=True)