-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_manager.py
225 lines (177 loc) · 6.43 KB
/
run_manager.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
import time
from torch.utils.tensorboard import SummaryWriter
from utils import *
class RunManager:
"""
This class creates manages different parameters based on each run.
"""
def __init__(self, prune_ite, checkpoint_path, tb_path, train_loader, val_loader):
"""
Initialized each parameters of each run.
"""
self.prune_ite = prune_ite
self.checkpoint_path = checkpoint_path
self.tb_path = tb_path
self.train_loader = train_loader
self.val_loader = val_loader
self.epoch_id = 0
self.epoch_train_loss = 0
self.epoch_val_loss = 0
self.epoch_id_total_train_correct = 0
self.epoch_id_total_val_correct = 0
self.best_val_accuracy = 0
self.epoch_start_time = None
self.run_params = None
self.run_id = 0
self.run_data = []
self.run_start_time = None
self.epoch_duration = None
self.tb = None
self.train_loss = None
self.val_loss = None
self.train_accuracy = None
self.val_accuracy = None
def begin_run(self, run):
"""
Records all the parameters at the start of each run.
:param run:
:param network: cnn model
:param loader: pytorch data loader
:param device: {cpu or gpu}
:param type_of_bn: whether {batch normalization, no batch normalization or dropout}
:return: none
"""
self.run_start_time = time.time()
self.run_id += 1
self.run_params = run
self.tb = SummaryWriter(f"{self.tb_path}/{run}")
def end_run(self):
"""
Records all the parameters at the end of each run.
:return: none
"""
self.tb.close()
self.epoch_id = 0
def begin_epoch(self):
"""
Records all the parameters at the start of each epoch.
:return: none
"""
self.epoch_start_time = time.time()
self.epoch_id += 1
self.epoch_train_loss = 0
self.epoch_val_loss = 0
self.epoch_id_total_train_correct = 0
self.epoch_id_total_val_correct = 0
def end_epoch(self, model):
"""
Records all the parameters at the end of each epoch.
:return: none
"""
self.epoch_duration = time.time() - self.epoch_start_time
run_duration = time.time() - self.run_start_time
self.train_loss = self.epoch_train_loss / len(self.train_loader.dataset)
self.val_loss = self.epoch_val_loss / len(self.val_loader.dataset)
self.train_accuracy = self.epoch_id_total_train_correct / len(self.train_loader.dataset)
self.val_accuracy = self.epoch_id_total_val_correct / len(self.val_loader.dataset)
self.tb.add_scalar(
"Plots/Train_Loss",
self.train_loss,
self.epoch_id
)
self.tb.add_scalar(
"Plots/Val_Loss",
self.val_loss,
self.epoch_id
)
self.tb.add_scalar(
"Plots/Train_correct",
self.epoch_id_total_train_correct,
self.epoch_id
)
self.tb.add_scalar(
"Plots/Val_correct",
self.epoch_id_total_val_correct,
self.epoch_id
)
self.tb.add_scalar("Plots/Train_accuracy", self.train_accuracy, self.epoch_id)
self.tb.add_scalar("Plots/Val_accuracy", self.val_accuracy, self.epoch_id)
# for name, param in model.named_parameters():
# self.tb.add_histogram(name, param, self.epoch_id)
# self.tb.add_histogram(f'{name}.grad', param.grad, self.epoch_id)
torch.save(
model.state_dict(),
f"{self.checkpoint_path}/"
f"seq_epoch_{self.epoch_id}_prune_iteration_{self.prune_ite}.pth.tar"
)
if self.val_accuracy > self.best_val_accuracy:
torch.save(
model.state_dict(),
f"{self.checkpoint_path}/"
f"best_prune_iteration_{self.prune_ite}.pth.tar"
)
print(f"\n Old best val accuracy: {self.best_val_accuracy} || "
f"New best val accuracy: {self.val_accuracy} , and new model saved..\n")
self.best_val_accuracy = self.val_accuracy
def track_train_loss(self, loss):
"""
Calculates the loss at the each iteration of batch.
:param loss:
:return: calculated loss
"""
self.epoch_train_loss += loss * self.train_loader.batch_size
def track_total_train_correct_per_epoch(self, preds, labels, num_classes):
"""
Calculates the correct prediction at the each iteration of batch.
:param preds: predicted labels
:param labels: true labels
:return: the totalcorrect prediction at the each iteration of batch
"""
self.epoch_id_total_train_correct += get_correct(preds, labels, num_classes)
def track_val_loss(self, loss):
"""
Calculates the loss at the each iteration of batch.
:param loss:
:return: calculated loss
"""
self.epoch_val_loss += loss * self.val_loader.batch_size
def track_total_val_correct_per_epoch(self, preds, labels, num_classes):
"""
Calculates the correct prediction at the each iteration of batch.
:param preds: predicted labels
:param labels: true labels
:return: the totalcorrect prediction at the each iteration of batch
"""
self.epoch_id_total_val_correct += get_correct(preds, labels, num_classes)
def get_final_val_loss(self):
"""
Gets the final loss value.
:return: the final loss value
"""
return self.val_loss
def get_final_train_loss(self):
"""
Gets the final loss value.
:return: the final loss value
"""
return self.train_loss
def get_final_best_val_accuracy(self):
"""
Gets the final loss value.
:return: the final loss value
"""
return self.best_val_accuracy
def get_final_val_accuracy(self):
"""
Gets the final accuracy value.
:return: the final accuracy value
"""
return self.val_accuracy
def get_final_train_accuracy(self):
"""
Gets the final accuracy value.
:return: the final accuracy value
"""
return self.train_accuracy
def get_epoch_duration(self):
return self.epoch_duration