-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel_train.py
231 lines (189 loc) · 8.27 KB
/
model_train.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
import torch
import torch.utils.data as data_utils
import torch.optim as optim
import torch.nn as nn
import torch.nn.functional as F
import sys
import time
import copy
import numpy as np
class ModelTrainer:
'''class containing all the info about the training process and handling the actual
training function'''
def __init__(
self,
model,
dataloaders,
epochs,
optimizer,
scheduler,
class_count,
device,
early_stop=20):
self.model = model
self.dataloaders = dataloaders
self.epochs = epochs
self.optimizer = optimizer
self.scheduler = scheduler
self.class_count = class_count
self.early_stop = early_stop
self.device = device
def launch_training(self):
'''initializes training process.'''
best_loss = 10 # high value, so that future loss values will always be lower
no_improvement_for = 0
best_model = copy.deepcopy(self.model.state_dict())
for ep in range(self.epochs):
# perform train/val iteration
loss, acc, conf_matrix, data_obj = self.dataset_to_model(
ep, 'train')
loss, acc, conf_matrix, data_obj = self.dataset_to_model(ep, 'val')
no_improvement_for += 1
loss = loss.cpu().numpy()
# if improvement, reset counter
if(loss < best_loss):
best_model = copy.deepcopy(self.model.state_dict())
best_loss = loss
no_improvement_for = 0
print("Best!")
# break if X times no improvement
if(no_improvement_for == self.early_stop):
break
# scheduler (optional)
if not (self.scheduler is None):
if isinstance(
self.scheduler,
optim.lr_scheduler.ReduceLROnPlateau):
self.scheduler.step(loss)
else:
self.scheduler.step()
# load best performing model, and launch on test set
self.model.load_state_dict(best_model)
loss, acc, conf_matrix, data_obj = self.dataset_to_model(ep, 'test')
return self.model, conf_matrix, data_obj
def dataset_to_model(self, epoch, split, backprop_every=20):
'''launch iteration for 1 epoch on specific dataset object, with backprop being optional
- epoch: epoch count, is only printed and thus not super important
- split: if equal to 'train', apply backpropagation. Otherwise, don`t.
- backprop_every: only apply backpropagation every n patients. Allows for gradient accumulation over
multiple patients, like in batch processing in a regular neural network.'''
if(split == 'train'):
backpropagation = True
self.model.train()
else:
backpropagation = False
self.model.eval()
# initialize data structures to store results
corrects = 0
train_loss = 0.
time_pre_epoch = time.time()
confusion_matrix = np.zeros(
(self.class_count, self.class_count), np.int16)
data_obj = DataMatrix()
self.optimizer.zero_grad()
backprop_counter = 0
for batch_idx, (bag, label, path_full) in enumerate(
self.dataloaders[split]):
# send to gpu
label = label.to(self.device)
bag = bag.to(self.device)
# forward pass
prediction, att_raw, att_softmax, bag_feature_stack = self.model(
bag)
# calculate and store loss
# loss_func = nn.BCELoss()
# loss_out = loss_func(prediction, label)
# pred_log = torch.log(prediction)
loss_func = nn.CrossEntropyLoss()
loss_out = loss_func(prediction, label[0])
train_loss += loss_out.data
# apply backpropagation if indicated
if(backpropagation):
loss_out.backward()
backprop_counter += 1
# counter makes sure only every X samples backpropagation is
# excluded (resembling a training batch)
if(backprop_counter % backprop_every == 0):
self.optimizer.step()
# print_grads(self.model)
self.optimizer.zero_grad()
# transforms prediction tensor into index of position with highest
# value
label_prediction = torch.argmax(prediction, dim=1).item()
label_groundtruth = label[0].item()
# store patient information for potential later analysis
data_obj.add_patient(
label_groundtruth,
path_full[0],
att_raw,
att_softmax,
label_prediction,
F.softmax(
prediction,
dim=1),
loss_out,
bag_feature_stack)
# store predictions accordingly in confusion matrix
if(label_prediction == label_groundtruth):
corrects += 1
confusion_matrix[label_groundtruth, label_prediction] += int(1)
# print('----- loss: {:.3f}, gt: {} , pred: {}, prob: {}'.format(loss_out, label_groundtruth, label_prediction, prediction.detach().cpu().numpy()))
samples = len(self.dataloaders[split])
train_loss /= samples
accuracy = corrects / samples
print('- ep: {}/{}, loss: {:.3f}, acc: {:.3f}, {}s, {}'.format(
epoch + 1, self.epochs, train_loss.cpu().numpy(),
accuracy, int(time.time() - time_pre_epoch), split))
return train_loss, accuracy, confusion_matrix, data_obj.return_data()
class DataMatrix():
'''DataMatrix contains all information about patient classification for later storage.
Data is stored within a dictionary:
self.data_dict[true entity] contains another dictionary with all patient paths for
the patients of one entity (e.g. AML-PML-RARA, SCD, ...)
--> In this dictionary, the paths form the keys to all the data of that patient
and it's classification, stored as a tuple:
- attention_raw: attention for all single cell images before softmax transform
- attention: attention after softmax transform
- prediction: Numeric position of predicted label in the prediction vector
- prediction_vector:Prediction vector containing the softmax-transformed activations
of the last AMiL layer
- loss: Loss for that patients' classification
- out_features: Aggregated bag feature vectors after attention calculation and
softmax transform. '''
def __init__(self):
self.data_dict = dict()
def add_patient(
self,
entity,
path_full,
attention_raw,
attention,
prediction,
prediction_vector,
loss,
out_features):
'''Add a new patient into the data dictionary. Enter all the data packed into a tuple into the dictionary as:
self.data_dict[entity][path_full] = (attention_raw, attention, prediction, prediction_vector, loss, out_features)
accepts:
- entity: true patient label
- path_full: path to patient folder
- attention_raw: attention before softmax transform
- attention: attention after softmax transform
- prediction: numeric bag label
- prediction_vector: output activations of AMiL model
- loss: loss calculated from output actiations
- out_features: bag features after attention calculation and matrix multiplication
returns: Nothing
'''
if not (entity in self.data_dict):
self.data_dict[entity] = dict()
self.data_dict[entity][path_full] = (
attention_raw.detach().cpu().numpy(),
attention.detach().cpu().numpy(),
prediction,
prediction_vector.data.cpu().numpy()[0],
float(
loss.data.cpu()),
out_features.detach().cpu().numpy())
def return_data(self):
return self.data_dict