-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtrain_and_evaluate.py
227 lines (190 loc) · 7.96 KB
/
train_and_evaluate.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
import copy
import os
import random
import time
import numpy as np
import torch
import torch.optim as optim
from utils.construct_hypergraph import construct_G_from_fts
from torch import nn
from config import get_config
from datasets import source_select
from models import model_select
def setup_seed(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.deterministic = True
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
def train_model(model, fts, lbls, idx_train, idx_val,
criterion, optimizer, scheduler, device,
num_epochs=100, print_freq=500):
"""
training method
:param model: model to be trained
:param fts: input features
:param lbls: input labels
:param idx_train: list of cross validation train set indicies
:param idx_val: list of cross validation validation set indices
:param criterion: loss function
:param optimizer:
:param scheduler:
:param device: CUDA device
:param num_epochs: epochs to train for
:param print_freq:
:return: best model on validation set
"""
since = time.time()
model_wts_best_val_acc = copy.deepcopy(model.cpu().state_dict())
model_wts_lowest_val_loss = copy.deepcopy(model.cpu().state_dict())
model = model.to(device)
best_acc = 0.0
loss_min = 100
for epoch in range(num_epochs):
if epoch % print_freq == 0:
print('-' * 10)
print(f'Epoch {epoch}/{num_epochs - 1}')
# Each epoch has a training and validation phase
for phase in ['train', 'val']:
if phase == 'train':
scheduler.step()
model.train() # Set model to training mode
else:
model.eval() # Set model to evaluate mode
running_loss = 0.0
running_corrects = 0
idx = idx_train if phase == 'train' else idx_val
optimizer.zero_grad()
with torch.set_grad_enabled(phase == 'train'):
outputs = model(feats=fts)
loss = criterion(outputs[idx], lbls[idx]) * len(idx)
_, preds = torch.max(outputs, 1)
# backward + optimize only if in training phase
if phase == 'train':
loss.backward()
optimizer.step()
# statistics
running_loss += loss
running_corrects += torch.sum(preds[idx] == lbls.data[idx])
epoch_loss = running_loss / len(idx)
epoch_acc = running_corrects.double() / len(idx)
if epoch % print_freq == 0:
print(f'{phase} Loss: {epoch_loss:.4f} Acc: {epoch_acc:.4f}')
# deep copy the model
if phase == 'val' and epoch_acc > best_acc:
best_acc = epoch_acc
model_wts_best_val_acc = copy.deepcopy(model.cpu().state_dict())
model = model.to(device)
if phase == 'val' and epoch_loss < loss_min:
loss_min = epoch_loss
model_wts_lowest_val_loss = copy.deepcopy(model.cpu().state_dict())
model = model.to(device)
if epoch % print_freq == 0 and phase == 'val':
print(f'Best val Acc: {best_acc:4f}, Min val loss: {loss_min:4f}')
print('-' * 20)
time_elapsed = time.time() - since
print(f'\nTraining complete in {time_elapsed // 60:.0f}m {time_elapsed % 60:.0f}s')
print(f'Best val Acc: {best_acc:4f}')
return model_wts_best_val_acc, model_wts_lowest_val_loss, best_acc, loss_min
def test_model(model, best_model_wts, fts, lbls, idx_test, device):
"""
testing method
:param model_best:
:param fts:
:param lbls:
:param idx_test:
:param edge_dict:
:param device:
:return:
"""
model.load_state_dict(best_model_wts)
model = model.to(device)
model.eval()
running_corrects = 0.0
with torch.no_grad():
outputs = model(feats=fts)
_, preds = torch.max(outputs, 1)
running_corrects += torch.sum(preds[idx_test] == lbls.data[idx_test])
test_acc = running_corrects.double() / len(idx_test)
test_sens = torch.sum(
preds[(min(idx_test) + lbls.data[idx_test].nonzero()).clone().detach().long()] == 1).item() / len(
lbls.data[idx_test].nonzero().data)
test_spec = torch.sum(
preds[(min(idx_test) + (lbls.data[idx_test] != 1).nonzero()).clone().detach().long()] == 0).item() / len(
(lbls.data[idx_test] != 1).nonzero().data)
print('*' * 20)
print('Test accuracy: %.2f' % test_acc)
print('Test sensitivity: %.2f' % test_sens)
print('Test specificity: %.2f' % test_spec)
print('*' * 20)
return test_acc
def get_source():
cfg = get_config('config/config.yaml')
source = source_select(cfg)
if cfg['data_type'] == 'simulated':
return source(cfg)
def train_test_HUNET():
device = torch.device('cuda:0')
cfg = get_config('config/config.yaml')
fts, lbls, idx_trains, idx_vals, idx_test, n_category = get_source()
H = construct_G_from_fts([fts], [cfg['k_construct_nn']])
fts = torch.Tensor(fts).to(device) # Convert to tensor and pass to device
lbls = torch.Tensor(lbls).squeeze().long().to(
device) # Squeeze along axis that are 1 and convert the values to 64 bit integers and pass to device
model = model_select(cfg['model']) \
(dim_feat=fts.size(1),
n_categories=n_category,
n_stack=cfg['n_stack'],
layer_spec=cfg['layer_spec'],
pool_ratios=cfg['pool_ratios'],
dropout_rate=cfg['drop_out'],
H_for_hunet=H,
hunet_depth=cfg['hunet_depth']
)
# initialize model
state_dict = model.state_dict()
for key in state_dict:
if 'weight' in key:
nn.init.xavier_uniform_(state_dict[key])
elif 'bias' in key:
state_dict[key] = state_dict[key].zero_()
# Wieght decay : prevents wieghts from going too large
optimizer = optim.Adam(model.parameters(), lr=cfg['lr'], weight_decay=cfg['weight_decay'])
# optimizer = optim.SGD(model.parameters(), lr=cfg['lr'], momentum=0.95, weight_decay=cfg['weight_decay'])
# Adaptive learning rate with 1 milestone
schedular = optim.lr_scheduler.MultiStepLR(optimizer,
milestones=cfg['milestones'],
gamma=cfg['gamma'])
criterion = torch.nn.NLLLoss()
trained_models = []
for idx_set in range(0, len(idx_trains)):
trained_models += [
train_model(model, fts, lbls, idx_trains[idx_set], idx_vals[idx_set], criterion, optimizer,
schedular, device,
cfg['max_epoch'], cfg['print_freq'])]
model_wts_best_val_acc = trained_models[0][0]
model_wts_lowest_val_loss = trained_models[0][1]
best_accuracy = trained_models[0][2]
loss_min = trained_models[0][3]
for trained_model in trained_models:
if best_accuracy < trained_model[2]:
model_wts_best_val_acc = trained_model[0]
if loss_min > trained_model[3]:
loss_min = trained_model[3]
model_wts_lowest_val_loss = trained_model[1]
print("stacked:{}\ndepth:{} \n pool_ratio: {}".format(cfg['n_stack'], cfg['hunet_depth'], cfg['pool_ratios']))
print('**** Cross Validation accuracy results ****')
print(np.asarray(trained_models)[:, 2])
print('**** Cross Validation val loss results ****')
print(np.asarray(trained_models)[:, 3])
if idx_test is not None:
print('**** Model of lowest val loss ****')
test_acc_lvl = test_model(model, model_wts_lowest_val_loss, fts, lbls, idx_test,
device)
print('**** Model of best val acc ****')
test_acc_bva = test_model(model, model_wts_best_val_acc, fts, lbls, idx_test, device)
return max(test_acc_lvl, test_acc_bva)
if __name__ == '__main__':
setup_seed(10000)
train_test_HUNET()