-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathclassification.py
More file actions
138 lines (117 loc) · 5.67 KB
/
classification.py
File metadata and controls
138 lines (117 loc) · 5.67 KB
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
"""
#################################
# Classification after training the Model
Keras
GPU: Nvidia RTX 2080 Ti
OS: Ubuntu 18.04
#################################
"""
#########################################################
# import libraries
import pickle
import platform
import itertools
import numpy as np
import matplotlib.pyplot as plt
from keras.models import load_model
from sklearn.metrics import confusion_matrix
from sklearn.preprocessing import LabelBinarizer
from config import Config_Queue
from utils import dir_string_to_num
from config import Config_General as General
#########################################################
# General Parameters
num_ue = General.get('NUM_UE')
num_run = General.get('NUM_RUN')
# num_run = 1
num_frm = General.get('NUM_FRM')
cbr_rate = General.get('CBR_RATE')
num_actions = General.get('Actions')
num_event = General.get('Sim_Events')
num_angles = General.get('NUM_ANGLE')
queue_lim = Config_Queue.get('Queue_limit')
num_features = num_ue + num_ue + num_ue + 1 # number of queues + number of dist + number of dir + active_user
x_data_state_vec = np.empty([num_run * num_frm * num_event, num_features], dtype=object)
y_action_vec = np.zeros([num_run * num_frm * num_event, num_actions], dtype=int) - 1
#########################################################
# Function definition
def classify():
"""
This function loads data from the local drive to evaluate the performance on the collected information from the
expert.
:return: None
"""
print(" --------- Classification --------- ")
run_list = range(0, num_run)
for run in run_list:
if platform.system() == "Windows":
output_file = \
"D:\\SimulationData\\TestData\\num_UE_%d_num_angles_%d_queue_lim_%d_Run_%d_Frame_%d" \
"_cbr_rate_%d_Event_%d.npz" % (num_ue, num_angles, queue_lim, run, num_frm, cbr_rate, num_event)
elif platform.system() == "Linux":
output_file = \
"SimulationData/TestData/num_UE_%d_num_angles_%d_queue_lim_%d_Run_%d_Frame_%d" \
"_cbr_rate_%d_Event_%d.npz" % (num_ue, num_angles, queue_lim, run, num_frm, cbr_rate, num_event)
else:
print("Nor Linux neither Windows")
return
readfile = np.load(output_file, allow_pickle=True)
x_data_state_vec[run * num_frm * num_event:(run + 1) * num_frm * num_event, :] = \
readfile['state_feature_vec'].reshape(num_frm * num_event, num_features)
y_action_vec[run * num_frm * num_event:(run + 1) * num_frm * num_event, :] = \
readfile['action_vec'].reshape(num_frm * num_event, num_actions)
x_queue_vec = x_data_state_vec[:, 0:num_ue]
x_dir_vec = x_data_state_vec[:, 2 * num_ue:3 * num_ue]
x_dir_vec_num = dir_string_to_num(x_dir_vec)
x_dir_vec_con = np.concatenate((x_dir_vec_num, np.reshape(y_action_vec[:, 0], [y_action_vec[:, 0].shape[0], 1])),
axis=1)
print("[INFO] data matrix: ({:.2f}MB)".format(
x_data_state_vec.nbytes / (1024 * 1000.0)))
y_action_lb = LabelBinarizer()
y_action_user = y_action_lb.fit_transform(y_action_vec[:, 0])
y_action_dir = y_action_lb.fit_transform(y_action_vec[:, 1])
model_queue = load_model('Output/Models/model_queue_5_layers_[40, 80, 160, 80, 5]_units.model')
model_dir = load_model('Output/Models/model_dir_3_layers_[8, 10, 3]_units.model')
loss_queue, accuracy_queue = model_queue.evaluate(x_queue_vec, y_action_user)
print('accuracy_queue: %.2f' % (accuracy_queue * 100), "loss_queue: %.5f" % loss_queue)
loss_dir, accuracy_dir = model_dir.evaluate(x_dir_vec_con, y_action_dir)
print('accuracy_dir: %.2f' % (accuracy_dir * 100), "loss_dir: %.5f" % loss_dir)
predictions_queue = model_queue.predict_classes(x_queue_vec)
for i in range(10):
print('%s => %d (expected %d)' % (x_queue_vec[i].tolist(), predictions_queue[i], y_action_vec[i, 0]))
predictions_dir = model_dir.predict_classes(x_dir_vec_con)
for i in range(10):
print('%s => %d (expected %d)' % (x_dir_vec_con[i].tolist(), predictions_dir[i], y_action_vec[i, 1]))
# predictions_not_rounded = model_queue.predict(x_queue_vec)
# rounded = [round(x[0]) for x in predictions_not_rounded]
cm = confusion_matrix(y_true=y_action_vec[:, 0], y_pred=predictions_queue)
cm_plot_labels = ['1', '2', '3', '4', '5']
plot_confusion_matrix(cm=cm, classes=cm_plot_labels, title='Confusion Matrix')
def plot_confusion_matrix(cm, classes, normalize=False, title='Confusion matrix', cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
fig_conf = plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
print(cm)
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, cm[i, j], horizontalalignment="center", color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label', size=12, fontweight='bold')
plt.xlabel('Predicted label', size=12, fontweight='bold')
# file_pdf = 'Output/Figures/confusion_matrix.pdf'
file_figobj = 'Output/FigureObject/confusion_matrix_40_epochs.fig.pickle'
pickle.dump(fig_conf, open(file_figobj, 'wb'))
# plt.savefig(file_pdf)
# plt.imsave(file_pdf)