-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
524 lines (429 loc) · 18.8 KB
/
app.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
from flask import Flask, render_template, request, redirect, url_for
from werkzeug.utils import secure_filename
from database.models import DbDatasetCatalog, DbDatasetMeta, DbConfig, db, DbModel
from library.DataMetaDataCreator import MetaDataCreator
from library.DatasetExplorer import DatasetExplorer
from library.FeatureExtractor import FeatureExtractor
from library.ModelBuilder import NewModelBuilder
from library.ModelTrainer import ModelTrainer
UPLOAD_FOLDER = 'uploads'
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database/data.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
db.init_app(app)
@app.route('/')
def page_welcome():
init_config()
db.session.commit()
return render_template("index.html")
@app.route('/select_dataset')
def web_select_dataset():
import os
path = "Datasets"
if not os.path.exists(path):
try:
os.mkdir(path)
except OSError:
print("Creation of the directory %s failed" % path)
else:
print("Successfully created the directory %s " % path)
path = "Downloads"
if not os.path.exists(path):
try:
os.mkdir(path)
except OSError:
print("Creation of the directory %s failed" % path)
else:
print("Successfully created the directory %s " % path)
return render_template("select_dataset.html", datasets=DbDatasetCatalog.query.all())
@app.route('/download_dataset', methods=['POST'])
def web_download_dataset():
download_datasets(list(request.form.keys()))
return redirect(url_for("web_select_dataset"))
@app.route('/delete_datasets')
def web_delete_datasets():
import shutil
try:
shutil.rmtree('Datasets')
except OSError as e:
print("Error: %s - %s." % (e.filename, e.strerror))
try:
shutil.rmtree('Downloads')
except OSError as e:
print("Error: %s - %s." % (e.filename, e.strerror))
for data in DbDatasetCatalog.query.all():
data.isdownloaded = 0
db.session.commit()
return redirect(url_for("web_select_dataset"))
@app.route('/select_metadata')
def web_select_metadata():
return render_template("select_metadata.html", datasets=DbDatasetCatalog.query.all())
@app.route('/create_metadata', methods=['POST'])
def web_create_metadata():
metadata_creator = MetaDataCreator(DbConfig.query.first().__dict__)
metadata_creator.create_csv(list(request.form.keys()))
datametacsv_to_database()
for meta in list(request.form.keys()):
DbDatasetCatalog.query.filter(DbDatasetCatalog.name == meta).first().ismeta = 1
db.session.commit()
return redirect(url_for("web_select_metadata"))
@app.route('/delete_metadata')
def web_delete_metadata():
DbDatasetMeta.query.delete()
for x in DbDatasetCatalog.query.all():
x.ismeta = 0
db.session.commit()
return redirect(url_for('web_select_metadata'))
@app.route("/select_features")
def web_select_features():
import os.path
temp = []
temp2 = []
if os.path.isfile(DbConfig.query.first().SAVE_RUNTIME_FEATURES + 'FeaturesX.npy') and os.path.isfile(
DbConfig.query.first().SAVE_RUNTIME_FEATURES + 'FeaturesY.npy'):
temp = True
else:
temp = False
return render_template("select_features.html", temp=temp)
@app.route("/create_features", methods=['POST'])
def web_create_features():
from distutils.util import strtobool
# Bu işlem selectbox üzerinden gelen verilerin flatten dict üzerinde gösterilememesinden dolayı yazılmıştır.
a = request.form.to_dict(flat=True)
a['features'] = request.form.getlist("features")
a['augmentations'] = request.form.getlist("augmentations")
a['sampling_rate'] = int(a['sampling_rate'])
a['duration'] = int(a['duration'])
a['n_mfcc'] = int(a['n_mfcc'])
a['pitch_pm'] = int(a['pitch_pm'])
a['bins_per_octave'] = int(a['bins_per_octave'])
a['shift_rate'] = int(a['shift_rate'])
a['speed_change'] = float(a['speed_change'])
a['trim_long_data'] = bool(strtobool(a['trim_long_data']))
# Çıkartılan özniteliklerin prediction aşamasında kullanılabilmesi için dump edilmiştir.
import pickle
with open('TEMP/initFeatureExtractor', 'wb') as file:
pickle.dump(a, file)
f = FeatureExtractor(a, a, DbDatasetMeta.query.all()) # feature_extraction_dict {} yolla
f.extract_with_database()
return redirect(url_for("web_select_features"))
@app.route("/delete_features")
def web_delete_features():
import os
try:
os.remove(DbConfig.query.first().SAVE_RUNTIME_FEATURES + 'FeaturesX.npy')
os.remove(DbConfig.query.first().SAVE_RUNTIME_FEATURES + 'FeaturesY.npy')
except:
print("Dosyalar bulunamadı. Silme işleminde problem oluştu.")
return redirect(url_for('web_select_features'))
@app.route('/create_model')
def web_create_model():
return render_template("create_model.html", columns=DbModel.__table__.columns.keys(),
layers=DbModel.query.all())
@app.route('/create_model_conv_1d', methods=['POST', 'GET'])
def web_create_model_conv_1d():
if request.method == 'POST':
model_conv1d = DbModel(type="conv_1d", filters=request.form.get("filters"),
kernel_size=request.form.get("kernel_size"), padding=request.form.get("padding"),
activation=request.form.get("activation"))
db.session.add(model_conv1d)
db.session.flush()
db.session.commit()
return redirect(url_for('web_create_model'))
# print(request.form.to_dict(flat=True))
else:
return render_template("create_model_conv_1d.html")
@app.route('/create_model_dropout', methods=['POST', 'GET'])
def web_create_model_dropout():
if request.method == 'POST':
model_dropout = DbModel(type="dropout", rate=request.form.get("rate"))
db.session.add(model_dropout)
db.session.flush()
db.session.commit()
return redirect(url_for('web_create_model'))
# print(request.form.to_dict(flat=True))
else:
return render_template("create_model_dropout.html")
@app.route('/create_model_dense', methods=['POST', 'GET'])
def web_create_model_dense():
if request.method == 'POST':
model_dense = DbModel(type="dense", units=request.form.get("units"), activation=request.form.get("activation"))
db.session.add(model_dense)
db.session.flush()
db.session.commit()
return redirect(url_for('web_create_model'))
# print(request.form.to_dict(flat=True))
else:
return render_template("create_model_dense.html")
@app.route('/create_model_batch_normalization')
def web_create_model_batch_normalization():
model_batch = DbModel(type="batch_normalization")
db.session.add(model_batch)
db.session.flush()
db.session.commit()
return redirect(url_for('web_create_model'))
@app.route('/create_model_max_pooling_1d')
def web_create_model_max_pooling_1d():
model_batch = DbModel(type="max_pooling_1d")
db.session.add(model_batch)
db.session.flush()
db.session.commit()
return redirect(url_for('web_create_model'))
@app.route('/create_model_flatten')
def web_create_model_flatten():
model_flatten = DbModel(type="flatten")
db.session.add(model_flatten)
db.session.flush()
db.session.commit()
return redirect(url_for('web_create_model'))
@app.route('/delete_layer/<layer_id>')
def web_delete_model_layer(layer_id):
db.session.delete(DbModel.query.get(layer_id))
db.session.commit()
return redirect(url_for('web_create_model'))
@app.route('/delete_layer')
def web_delete_all_layers():
DbModel.query.delete()
db.session.commit()
return redirect(url_for('web_create_model'))
@app.route("/select_compile_config")
def web_select_compile_config():
import os
temp = []
temp2 = []
if os.path.isfile(DbConfig.query.first().SAVE_RUNTIME_FEATURES + 'FeaturesX.npy') and os.path.isfile(
DbConfig.query.first().SAVE_RUNTIME_FEATURES + 'FeaturesY.npy'):
temp = True
else:
temp = False
if os.path.isfile(DbConfig.query.first().SAVE_RUNTIME_FEATURES + 'runtime_model'):
temp2 = True
else:
temp2 = False
return render_template("select_compile_config.html", temp=temp, temp2=temp2)
@app.route("/create_compile_config", methods=['POST'])
def web_create_compile_config():
a = request.form.to_dict(flat=True)
model_builder = NewModelBuilder(DbConfig.query.first().__dict__, DbModel.query.all(), a)
model_builder.build()
db.session.commit()
return redirect(url_for("web_select_compile_config"))
@app.route("/delete_compile_config")
def web_delete_compile_config():
import os
try:
os.remove('TEMP/runtime_model')
except OSError as e:
print("Error: %s - %s." % (e.filename, e.strerror))
return redirect(url_for('web_select_compile_config'))
@app.route("/select_model_trainer")
def web_select_model_trainer():
import os
temp = []
if os.path.isfile(DbConfig.query.first().SAVE_RUNTIME_FEATURES + 'runtime_model'):
temp = True
else:
temp = False
return render_template("select_model_trainer.html", temp=temp)
@app.route("/model_trainer", methods=['POST'])
def web_model_trainer():
from distutils.util import strtobool
a = request.form.to_dict(flat=True)
a['save_model'] = bool(strtobool(a['save_model']))
a['use_random_state'] = bool(strtobool(a['use_random_state']))
a['use_tensorboard'] = bool(strtobool(a['use_tensorboard']))
a['test_split_rate'] = float(a['test_split_rate'])
a['batch_size'] = int(a['batch_size'])
a['epochs'] = int(a['epochs'])
a['validation_split_rate'] = float(a['validation_split_rate'])
model_trainer = ModelTrainer(model_train_config=a, path_dict=DbConfig.query.first().__dict__,
tensorboard_config=a)
model_trainer.train_with_temp_features()
db.session.commit()
print(a)
# model_builder = NewModelBuilder(DbConfig.query.first().__dict__, DbModel.query.all(), a)
# model_builder.build()
return "finished"
@app.route('/features_reshape', methods=['POST', 'GET'])
def web_features_reshape():
import numpy as np
features = np.load("TEMP/FeaturesX.npy")
print(features.shape)
if request.method == "GET":
features_shape = str(features.shape).translate({ord(i): None for i in '() '})
return render_template('select_features_shape.html', features_shape=features_shape)
if request.method == "POST":
new_shape = request.form.get('new_shape')
new_shape = tuple(map(int, new_shape.split(',')))
features = features.reshape(new_shape)
np.save("TEMP/FeaturesX.npy", features)
return redirect(url_for('web_features_reshape'))
@app.route('/prediction', methods=['POST', 'GET'])
def web_prediction():
if request.method == 'GET':
return render_template('select_model_prediction.html')
if request.method == 'POST':
import os
file = request.files['file']
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
from tensorflow.keras.models import load_model
import pickle
try:
model = load_model('TEMP/model.h5')
# print(model.summary())
except:
print("model dosyasi bulunamadı")
# return
try:
with open('TEMP/initFeatureExtractor', 'rb') as file:
a = pickle.load(file)
f = FeatureExtractor(a, a, None) # feature_extraction_dict {} yolla
except:
print('initFeatureExtractor bulunamadi')
extracted_features, lenght = f.extract('uploads/' + filename, False)
import numpy as np
extracted_features = np.expand_dims(extracted_features , axis = 1)
extracted_features = np.expand_dims(extracted_features , axis = 0)
print(extracted_features.shape)
predicted = model.predict(extracted_features)
predict_dict = {}
dic = {0:'neutral', 1:'happy', 2:'sad', 3:'angry', 4:'fear', 5:'disgust', 6:'surprise', 7:'bored'}
for key, value in dic.items():
# print('{0:.1f}'.format(predicted[0][value] * 100), key)
try:
predict_dict[value] = '{0:.1f}'.format(predicted[0][key] * 100)
except:
pass
return render_template('select_model_prediction.html', predict=True, predict_dict=predict_dict)
@app.teardown_appcontext
def shutdown_session(exception=None):
db.session.remove()
"""
1-Program veritabanı dosyasını kontrol eder.
2-Program veritabanı üzerinde Config tablosu üzerinde aramalar yapar ve bozulma var ise alttaki fonksiyonlar çalışır.
"""
# Config olarak kullanılan kesinleştirilmiş veri yolları veri tabanına eğer kayıt yok ise kaydediliyor.
# Bu kayıtlar buradan değiştirilip veritabanı silindiği zaman otomatik olarak değiştirilmiş olarak kaydedilecektir.
def init_config():
import os
import sys
print("----------------")
print("Config işlemi başlatılıyor...")
if (DbConfig.query.all().__len__() == 0):
print("Config Bulunamadı , Oluşturuluyor.")
working_dir_path = os.path.dirname(os.path.abspath(__file__))
if sys.platform.startswith('win32'):
# DATASET INDIRME KISMI
DOWNLOADS_FOLDER = 'Downloads\\'
DATASETS_FOLDER = 'Datasets\\'
RAVDESS_FILES_PATH = 'Datasets\\Ravdess\\'
CREMA_D_FILES_PATH = 'Datasets\\Crema-D\\'
SAVEE_FILES_PATH = 'Datasets\\SAVEE\\'
EMODB_FILES_PATH = 'Datasets\\emoDB\\'
TRAINING_FILES_PATH = 'pass\\'
TRAINING_FILES_SPECTOGRAMS = 'TEMP\\Spectogram\\'
SAVE_RUNTIME_FEATURES = 'TEMP\\'
SAVE_RUNTIME_FEATURES_X = 'TEMP\\featuresX.npy'
SAVE_RUNTIME_FEATURES_Y = 'TEMP\\featuresY.npy'
MODEL_FEATURES_PATH = 'ImplementedModels\\'
MODEL_WEIGHTS_PATH = 'ModelWeights\\'
MODEL_TRAINING_PLOTS = 'TEMP\\Plots\\'
TEST_FILES_PATH = 'pass\\'
DATA_METADATA_DF_PATH = 'TEMP\\metadata_table.csv'
db_config = DbConfig(TRAINING_FILES_PATH, TRAINING_FILES_SPECTOGRAMS, SAVE_RUNTIME_FEATURES,
SAVE_RUNTIME_FEATURES_X, SAVE_RUNTIME_FEATURES_Y, MODEL_FEATURES_PATH,
MODEL_WEIGHTS_PATH, MODEL_TRAINING_PLOTS, TEST_FILES_PATH, RAVDESS_FILES_PATH,
CREMA_D_FILES_PATH, SAVEE_FILES_PATH, EMODB_FILES_PATH, DATA_METADATA_DF_PATH,
DOWNLOADS_FOLDER, DATASETS_FOLDER)
db.session.add(db_config)
init_DbDatasetCatalog()
db.session.flush()
else:
# DATASET INDIRME KISMI
DOWNLOADS_FOLDER = 'Downloads/'
RAVDESS_FILES_PATH = 'Datasets/Ravdess/'
CREMA_D_FILES_PATH = 'Datasets/Crema-D/'
SAVEE_FILES_PATH = 'Datasets/SAVEE/'
EMODB_FILES_PATH = 'Datasets/emoDB/'
DATASETS_FOLDER = 'Datasets/'
TRAINING_FILES_PATH = 'pass/'
TRAINING_FILES_SPECTOGRAMS = 'TEMP/Spectogram/'
MODEL_DIR_PATH = 'ImplementedModels/'
MODEL_WEIGHTS_PATH = 'ModelWeights/'
MODEL_TRAINING_PLOTS = 'TEMP/Plots/'
SAVE_RUNTIME_FEATURES = 'TEMP/'
SAVE_RUNTIME_FEATURES_X = 'TEMP/featuresX.npy'
SAVE_RUNTIME_FEATURES_Y = 'TEMP/featuresY.npy'
MODEL_FEATURES_PATH = 'ImplementedModels/'
TEST_FILES_PATH = 'pass/'
DATA_METADATA_DF_PATH = 'TEMP/metadata_table.csv'
db_config = DbConfig(TRAINING_FILES_PATH, TRAINING_FILES_SPECTOGRAMS, SAVE_RUNTIME_FEATURES,
SAVE_RUNTIME_FEATURES_X, SAVE_RUNTIME_FEATURES_Y, MODEL_FEATURES_PATH,
MODEL_WEIGHTS_PATH, MODEL_TRAINING_PLOTS, TEST_FILES_PATH, RAVDESS_FILES_PATH,
CREMA_D_FILES_PATH, SAVEE_FILES_PATH, EMODB_FILES_PATH, DATA_METADATA_DF_PATH,
DOWNLOADS_FOLDER, DATASETS_FOLDER)
db.session.add(db_config)
init_DbDatasetCatalog()
db.session.commit()
else:
print("Config Dosyası bulundu.")
print("Config İşlemi Tamamlandı.")
path = "TEMP"
if not os.path.exists(path):
try:
os.mkdir(path)
except OSError:
print("Creation of the directory %s failed" % path)
else:
print("Successfully created the directory %s " % path)
print("----------------")
# Şu anda analiz edilip kullanılması için dosya tanımlama algoritmaları yazılmış veri setleri tanımlamalarıdır.
# Yeni bir veri seti algoritması eklendiği zaman ortak karar ile buraya eklemeler yapılabilir.
# Veritabanı dosyası silindiği taktirde kendisini güncel verilerle oluşturacaktır.
def init_DbDatasetCatalog():
print("----------------")
print("Catalog işlemi başlatılıyor...")
DbDatasetCatalog.query.delete()
db.session.add(DbDatasetCatalog("Crema-D", 0, 0))
db.session.add(DbDatasetCatalog("emoDB", 0, 0))
db.session.add(DbDatasetCatalog("Ravdess", 0, 0))
db.session.add(DbDatasetCatalog("SAVEE", 0, 0))
print("Catalog İşlemi Tamamlandı.")
print("----------------")
def download_datasets(datasets):
print("----------------")
print("Dataset indirme işlemi başlatılıyor...")
# print(DbConfig.query.first().__dict__)
dataset_explorer = DatasetExplorer(datasets, DbConfig.query.first().__dict__)
dataset_explorer.scan()
dataset_explorer.download_datasets()
for data in datasets:
DbDatasetCatalog.query.filter(DbDatasetCatalog.name == data).first().isdownloaded = 1
db.session.commit()
print("Dataset indirme işlemi tamamlandı.")
print("----------------")
del dataset_explorer
def datametacsv_to_database():
DbDatasetMeta.query.delete()
db.session.commit()
import pandas as pd
metadata_df = pd.read_csv(DbConfig.query.first().DATA_METADATA_DF_PATH)
metadata_df = metadata_df.loc[:, ~metadata_df.columns.str.contains('^Unnamed')]
metadatas = []
for index, row in metadata_df.iterrows():
dataset = DbDatasetCatalog.query.filter(DbDatasetCatalog.name == row['source']).first()
metadatas.append(DbDatasetMeta(path=row['path'], gender=row["gender"], emotion=row["emotion"],
dataset_catalog_id=dataset.id))
print(metadatas.__len__())
db.session.bulk_save_objects(metadatas)
db.session.commit()
from os import remove
remove(DbConfig.query.first().DATA_METADATA_DF_PATH)
DbDatasetMeta.query.filter(DbDatasetMeta.emotion == "unknown").delete()
db.session.commit()
if __name__ == '__main__':
db.app = app
db.create_all()
app.run(threaded=True, debug=True)