-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeras_mos_revised.py
146 lines (122 loc) · 4.85 KB
/
keras_mos_revised.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
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from DataPESQ import DataPESQ
import itertools as it
import os, sys, getopt
in_dir = ['data\PESQ_DB.xlsx', 'data\PESQ_DB_Male1_Seq4.xlsx']
out_dir = 'results'
cache_path = 'data/cache'
load_model = False
arg_list = ['--load-model', '--out-dir', '--in-dir', '--test-model', '--view-only']
arhitecture = [30, 30]
def plot_results(model, test_data, test_labels, rate=0.2):
test_labels_array = list(map(lambda x: [x], test_labels))
x = list(map(lambda x: x[0], test_data))
y = list(map(lambda x: x[1], test_data))
true_results = np.concatenate((test_data, test_labels_array), axis=1)
prediction = model.predict(test_data)
prediction_vector = np.concatenate((prediction),axis=None)
model_results = np.concatenate((test_data, prediction), axis=1)
diff = test_labels - prediction_vector
index_good = [i for i in range(len(diff)) if diff[i] <= rate]
index_bad = [i for i in range(len(diff)) if diff[i] > rate]
print(len(index_good)/len(diff))
fig = plt.figure()
ax = fig.add_subplot(211, projection='3d')
ax.scatter([x[i] for i in index_good], [y[i] for i in index_good], [diff[i] for i in index_good])
ax.scatter([x[i] for i in index_bad], [y[i] for i in index_bad], [diff[i] for i in index_bad], color='r')
plt.show()
return
def view_last_model():
raise NotImplementedError
def test_model(model, test_data, test_labels):
test_loss , test_mse, test_mae, test_mape = model.evaluate(test_data, test_labels)
print(test_loss, test_mse, test_mae, test_mape)
def get_data(path_list, training_size=0.8):
data = np.array([[1,1,1]])
# extract data from files
for path in path_list:
data_obj = DataPESQ(path)
data_from_ob = np.array(data_obj.get_data())
data = np.concatenate((data, data_from_ob), axis=0)
# np.random.shuffle(data)
data = data[1:]
np.random.shuffle(data)
# normalize data
data_tr = np.transpose(data)
for i in [0, 1, 2]:
xmin = data_tr[i].min()
xmax = data_tr[i].max()
print(xmin, xmax)
std = xmax - xmin
for elem in data_tr[i]:
elem = (elem - xmin) / std
data = np.transpose(data_tr)
# split data
sample_test = round(data.shape[0]*(1 - training_size))
training, test = data[sample_test:], data[:sample_test]
training_labels = np.array(list(it.chain.from_iterable(list(map(lambda x: x[:1], training)))))
training_data = np.array(list(map(lambda x: x[1:], training)))
test_labels = np.array(list(it.chain.from_iterable(list(map(lambda x: x[:1], test)))))
test_data = np.array(list(map(lambda x: x[1:], test)))
return training_data, training_labels, test_data, test_labels
def create_model(lst):
model = keras.Sequential()
for x in lst:
model.add(keras.layers.Dense(x, activation='relu'))
model.add(keras.layers.Dense(1))
model.compile(optimizer=tf.train.AdamOptimizer(0.001),
loss='mse',
metrics=['mse', 'mae', 'mape'])
return model
def main(argv):
try:
opts, args = getopt.getopt(argv, " ", arg_list)
except getopt.GetoptError:
print()
sys.exit(2)
# Look for options
for opt, arg in opts:
if opt in ('-l', '--load-model'):
print("model")
global load_model
load_model = True
if opt == '--out-dir':
global out_dir
out_dir = arg
if opt == '--in-dir':
global in_dir
in_dir = arg
# Look for commands
for opt, arg in opts:
if opt in ('-t', '--test-model'):
print("testing model")
return
if opt in ('-v', '--view-only'):
print("view results for last model tested")
return
training_data, training_labels, test_data, test_labels = get_data(in_dir)
if load_model:
print("load_model")
model = create_model(arhitecture)
model.fit(np.array([[1, 1]]), np.array([[1]]))
model.summary()
model.load_weights('my_model.h5')
else:
model = create_model(arhitecture)
tb_callback = keras.callbacks.TensorBoard(log_dir='./Graph', histogram_freq=0,
write_graph=True, write_images=True)
cp_callback = tf.keras.callbacks.ModelCheckpoint(
'./checkpoints/checkpoints', verbose=1, save_weights_only=True,
# Save weights, every 5-epochs.
period=5)
print(model)
model.fit(training_data, training_labels, epochs=100, validation_split=0.3, callbacks=[tb_callback])
model.save('my_model.h5')
test_model(model, test_data, test_labels)
plot_results(model, test_data, test_labels)
if __name__ == "__main__":
main(sys.argv[1:])