-
Notifications
You must be signed in to change notification settings - Fork 0
/
charm_trainer.py
317 lines (237 loc) · 12.1 KB
/
charm_trainer.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
from autocommand import autocommand
#from torch.utils.tensorboard import SummaryWriter
import rn_model, datetime, os, signal, torch, cnn_model, lstm, conv_lstm, sys, conv_rn
#import deep_gambler as dg
import numpy as np
import readCharmDataset as riq
import torch.nn as nn
import torch.optim as optim
from tqdm import tqdm
import pandas as pd
def compute_conf_matrix(labels, acc_mat):
conf_mat = {"Classes": labels}
for label in labels:
conf_mat.update({label: []})
for c in range(len(labels)):
for j in range(len(labels)):
conf_mat[labels[c]].append(acc_mat[c, j])
return conf_mat
def compute_metrics(labels, acc_mat, avg_loss, best_val_accuracy):
classes = acc_mat.shape[0]
ones = np.ones((classes, 1)).squeeze(-1)
corrects = np.diag(acc_mat)
acc = corrects.sum()/acc_mat.sum()
recall = (corrects/acc_mat.dot(ones)).round(4)
precision = (corrects/ones.dot(acc_mat)).round(4)
f1 = (2*recall*precision/(recall+precision)).round(4)
print(f"Accuracy: {acc}")
print(f"\t\tRecall\tPrecision\tF1")
results = {"acc": acc, "avg_loss": avg_loss, "best_val_accuracy": best_val_accuracy,
"overall_precision": np.mean(precision[:-1]), "overall_recall": np.mean(recall[:-1]),
"overall_f1": np.mean(f1[:-1])}
conf_mat = compute_conf_matrix(labels, acc_mat)
for c in range(classes):
print(f"Class {c}\t\t{recall[c]}\t{precision[c]}\t\t{f1[c]}")
results.update({"recall_%s"%(labels[c]): recall[c], "precision_%s"%(labels[c]): precision[c],
"f1_%s"%(labels[c]): f1[c]})
return results, conf_mat
def tensorboard_parse(tensorboard):
'''
tensorboard: a string with comma separated <key>=<value> substrings, each of
them mapping to a tensorboard.SummaryWriter constructor parameter.
E.g.,
log_dir='./runs',comment='',purge_step=None,max_queue=10,flush_secs=120,filename_suffix=''
'''
writer = None
if tensorboard:
conf = {}
for tok in tensorboard.split(','):
kv = tok.split('=')
if len(kv) == 2:
if kv[1] == 'None':
kv[1] = None
conf[kv[0]] = kv[1]
writer = SummaryWriter(**conf)
return writer
class EarlyExitException(Exception):
def __str__(self):
return "Received termination signal"
class CharmTrainer(object):
def __init__(self, model_name="rn", id_gpu="0", data_folder=".", modelPath=".", resultPath=".", batch_size=64, chunk_size=200000,
sample_stride=0, loaders=8, dg_coverage=0.999, tensorboard=None):
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = id_gpu
self.device = (torch.device('cuda') if torch.cuda.is_available()
else torch.device('cpu'))
self.model_name = model_name
print(self.model_name)
self.history_path = os.path.join(resultPath, "history_%s_og2.csv"%(self.model_name))
self.modelSavePath = os.path.join(modelPath, "%s_model_og2.pt"%(self.model_name))
self.metricsEvaluationPath = os.path.join(resultPath, "dnn_metrics_performance_test_set_final_final.csv")
self.confMatrixPath = os.path.join(resultPath, "%s_confusion_matrix.csv"%(self.model_name))
self.labels = ['Clear', 'LTE', 'WiFi', 'Other']
print(f"Training on {self.device}")
signal.signal(signal.SIGINT, self.exit_gracefully)
signal.signal(signal.SIGTERM, self.exit_gracefully)
self.chunk_size = chunk_size
self.loss_fn = nn.CrossEntropyLoss() #dg.GamblerLoss(3)
self.dg_coverage = dg_coverage
self.train_data = riq.IQDataset(data_folder=data_folder, chunk_size=chunk_size, stride=sample_stride)
self.train_data.normalize(torch.tensor([-2.7671e-06, -7.3102e-07]), torch.tensor([0.0002, 0.0002]))
self.train_loader = torch.utils.data.DataLoader(self.train_data, batch_size=batch_size, shuffle=True, num_workers=loaders, pin_memory=True)
self.val_data = riq.IQDataset(data_folder=data_folder, chunk_size=chunk_size, stride=sample_stride, subset='validation')
self.val_data.normalize(torch.tensor([-2.7671e-06, -7.3102e-07]), torch.tensor([0.0002, 0.0002]))
self.val_loader = torch.utils.data.DataLoader(self.val_data, batch_size=batch_size, shuffle=False, num_workers=loaders, pin_memory=True)
self.test_data = riq.IQDataset(data_folder=data_folder, chunk_size=chunk_size, stride=sample_stride, subset='test')
self.test_data.normalize(torch.tensor([-2.7671e-06, -7.3102e-07]), torch.tensor([0.0002, 0.0002]))
self.test_loader = torch.utils.data.DataLoader(self.val_data, batch_size=batch_size, shuffle=False, num_workers=loaders, pin_memory=True)
self.running = False
self.best_val_accuracy = 0.0
self.tensorboard = tensorboard_parse(tensorboard)
print("Init OK")
def save_history(self, metrics, epoch, subset):
metrics.update({"epoch": epoch, "subset": subset})
df = pd.DataFrame([metrics])
df.to_csv(self.history_path, mode='a', header=not os.path.exists(self.history_path))
def save_metrics_performance_test(self, metrics):
metrics.update({"model_name": self.model_name})
df = pd.DataFrame([metrics])
df.to_csv(self.metricsEvaluationPath, mode='a', header=not os.path.exists(self.metricsEvaluationPath))
def save_metrics_conf_matrix(self, conf_matrix):
df = pd.DataFrame(conf_matrix)
df.to_csv(self.confMatrixPath, mode='a', header=not os.path.exists(self.confMatrixPath))
def save_model(self, metrics):
'''
load your model with:
>>> model = brain.CharmBrain()
>>> model.load_state_dict(torch.load(filename))
'''
save_dict = {}
save_dict.update(metrics)
save_dict.update({"best_val_accuracy": self.best_val_accuracy})
save_dict.update({"model_state_dict": self.model.state_dict()})
torch.save(save_dict, self.modelSavePath)
def init(self):
if(self.model_name == "rn"):
self.model = rn_model.CharmBrain(self.chunk_size).to(self.device)
elif(self.model_name == "cnn"):
self.model = cnn_model.ConvModel().to(self.device)
elif(self.model_name == "lstm"):
self.model = lstm.SequenceModel(n_features=2, n_classes=3).to(self.device)
elif(self.model_name == "conv_rn"):
self.model = conv_rn.ConvRNN().to(self.device)
#self.model = rn_model.CharmBrain(self.chunk_size).to(self.device)
self.optimizer = optim.Adam(self.model.parameters())
self.best_val_accuracy = 0.0
def load_model(self):
self.init()
self.model.load_state_dict(torch.load(self.modelSavePath)["model_state_dict"])
def training_loop(self, n_epochs):
for self.loss_fn.o in [1.7]:
self.init()
self.model.train()
for epoch in range(n_epochs):
loss_train = 0.0
for chunks, labels in tqdm(self.train_loader):
if not self.running:
raise EarlyExitException
chunks = chunks.to(self.device, non_blocking=True)
labels = labels.to(self.device, non_blocking=True)
output = self.model(chunks)
loss = self.loss_fn(output, labels)
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
loss_train += loss.item()
torch.cuda.empty_cache()
#if self.tensorboard:
# self.tensorboard.add_scalar("Loss/train", loss_train/len(self.train_loader), epoch)
#if True:
print(f"{datetime.datetime.now()} Epoch {epoch}, loss {loss_train/len(self.train_loader)}")
#print(f"Coverage: {self.dg_coverage}, o-parameter {self.loss_fn.o}")
self.validate(epoch, train=True)
self.model.train()
def validate(self, epoch, train=True):
loaders = [('val', self.val_loader)]
if train:
loaders.append(('train', self.train_loader))
self.model.eval()
for name, loader in loaders:
correct = 0
total = 0
loss_total = 0
acc_mat = np.zeros((len(self.train_data.label), len(self.train_data.label)))
#acc_mat = np.zeros((len(loader.label), len(loader.label)))
with torch.no_grad():
for chunks, labels in tqdm(loader):
if not self.running:
raise EarlyExitException
chunks = chunks.to(self.device, non_blocking=True)
labels = labels.to(self.device, non_blocking=True)
output = self.model(chunks)
loss = self.loss_fn(output, labels)
#predicted = dg.output2class(output, self.dg_coverage, 3)
loss_total += loss.item()
_, predicted = torch.max(output, dim=1)
total += labels.shape[0]
correct += int((predicted == labels).sum())
for i in range(labels.shape[0]):
acc_mat[labels[i]][predicted[i]] += 1
accuracy = correct/total
avg_loss = loss_total/len(loader)
print(f"Epoch {epoch} on {name} dataset")
print(f"{name} accuracy: {accuracy}")
metrics, _ = compute_metrics(self.labels, acc_mat, avg_loss, self.best_val_accuracy)
self.save_history(metrics, epoch, subset=name)
if name == 'val' and accuracy>self.best_val_accuracy:
self.best_val_accuracy = accuracy
self.save_model(metrics)
def test(self):
self.running = True
self.model.eval()
correct = 0
total = 0
loss_total = 0
acc_mat = np.zeros((len(self.train_data.label), len(self.train_data.label)))
with torch.no_grad():
for chunks, labels in tqdm(self.test_loader):
if not self.running:
raise EarlyExitException
chunks = chunks.to(self.device, non_blocking=True)
labels = labels.to(self.device, non_blocking=True)
output = self.model(chunks)
loss = self.loss_fn(output, labels)
#predicted = dg.output2class(output, self.dg_coverage, 3)
loss_total += loss.item()
_, predicted = torch.max(output, dim=1)
total += labels.shape[0]
correct += int((predicted == labels).sum())
for i in range(labels.shape[0]):
acc_mat[labels[i]][predicted[i]] += 1
accuracy = correct/total
avg_loss = loss_total/len(self.test_loader)
print(f"Test Accuracy: {accuracy}")
metrics, conf_matrix = compute_metrics(self.labels, acc_mat, avg_loss, self.best_val_accuracy)
self.save_metrics_performance_test(metrics)
self.save_metrics_conf_matrix(conf_matrix)
def execute(self, n_epochs):
self.running = True
try:
self.training_loop(n_epochs)
#self.validate(n_epochs-1, train=True)
self.test()
except EarlyExitException:
pass
if self.tensorboard:
self.tensorboard.close()
print("[Done]")
def exit_gracefully(self, signum, frame):
self.running = False
@autocommand(__name__)
def charm_trainer(model_name="cnn", id_gpu="0", data_folder="./",
modelPath="./models", resultPath="./results", n_epochs=25, batch_size=512,
chunk_size=20000, sample_stride=0, loaders=6, dg_coverage=0.75, tensorboard=None):
ct = CharmTrainer(model_name=model_name, id_gpu=id_gpu, data_folder=data_folder, modelPath=modelPath, resultPath=resultPath,
batch_size=batch_size, chunk_size=chunk_size, sample_stride=sample_stride,
loaders=loaders, dg_coverage=dg_coverage, tensorboard=tensorboard)
ct.execute(n_epochs=n_epochs)