-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainer.py
165 lines (144 loc) · 6.33 KB
/
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
import torch
import tqdm
from utils import get_optimizer
import cxr_dataset as CXR
from torchvision import datasets, models, transforms, utils
from torch.utils.data import Dataset, DataLoader
from torch.autograd import Variable
from readData import ChestXrayDataSet
import torch.backends.cudnn as cudnn
from sklearn import metrics
import AmoebaNet as amoeba
import numpy as np
import random
import torch
import torch.nn as nn
##########################################################################################
##########################################################################################
# About trainer.py
#
# Trainer is responsible for loading, training, evaluating and saving a given model
#
##########################################################################################
##########################################################################################
class Trainer:
def __init__(self, model, normal_ops, reduction_ops, optimizer, data_path, loss_fn=None, device=None):
self.model = model
self.model = nn.DataParallel(self.model)
self.normal_ops = normal_ops
self.reduction_ops = reduction_ops
self.optimizer = optimizer
self.data_path = data_path
self.loss_fn = loss_fn
self.task_id = None
self.device = device
self.loss_fn = self.loss_fn.to(device)
def set_id(self, num):
self.task_id = num
# Save model to checkpoint_path
def save_checkpoint(self, checkpoint_path):
checkpoint = dict(model_state_dict=self.model.state_dict(),
optim_state_dict=self.optimizer.state_dict(),
normal_ops=self.normal_ops,
reduction_ops=self.reduction_ops)
torch.save(checkpoint, checkpoint_path)
# Load model from checkpoint_path
def load_checkpoint(self, checkpoint_path):
checkpoint = torch.load(checkpoint_path)
self.optimizer = get_optimizer(self.model ,0.01)
self.normal_ops = checkpoint['normal_ops']
self.reduction_ops = checkpoint['reduction_ops']
self.model = amoeba.amoebanet(14, 3, 100, self.normal_ops, self.reduction_ops)
self.model = self.model.to(self.device)
self.model = nn.DataParallel(self.model)
# Train model for 20 epochs
def train(self):
torch.manual_seed(0)
cudnn.benchmark = True
self.model.train()
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
data_transforms = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.Resize(224),
transforms.CenterCrop(224),
transforms.ColorJitter(brightness=0.1, contrast=0.1, saturation=0.1, hue=0.1),
transforms.RandomAffine(degrees=10, scale=(.95, 1.05), shear=0),
transforms.ToTensor(),
transforms.Normalize(mean, std)
])
transformed_dataset = CXR.CXRDataset(
path_to_images=self.data_path,
fold='train',
transform=data_transforms)
dataloader = torch.utils.data.DataLoader(
transformed_dataset,
batch_size=16,
shuffle=True,
num_workers=8)
lr = 0.01
print(f"About to begin training on device: {self.device}")
for epoch in range(20):
for x, y, _ in dataloader:
x, y = Variable(x.to(self.device)), Variable(y.to(self.device)).float()
output = self.model(x)
loss = self.loss_fn(output, y)
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
lr *= 0.95
self.optimizer = get_optimizer(self.model, lr)
print(f"Completed epoch on {self.device}")
# Compute AUC scores
def compute_AUCs(self, gt, pred):
AUROCs = []
gt_np = gt.cpu().numpy()
pred_np = pred.cpu().numpy()
for i in range(14):
try:
AUROCs.append(metrics.roc_auc_score(gt_np[:, i], pred_np[:, i]))
except ValueError:
pass
return AUROCs
# Evaluate the trained model
def eval(self):
self.model.eval()
cudnn.benchmark = True
"""Evaluate model on the provided validation or test set."""
gt = torch.FloatTensor()
gt = gt.to(self.device)
pred = torch.FloatTensor()
pred = pred.to(self.device)
normalize = transforms.Normalize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])
test_dataset = ChestXrayDataSet(data_dir=self.data_path, image_list_file="./labels/test_list.txt",
transform=transforms.Compose([
transforms.Resize(224),
transforms.TenCrop(224),
transforms.Lambda
(lambda crops: torch.stack([transforms.ToTensor()(crop) for crop in crops])),
transforms.Lambda
(lambda crops: torch.stack([normalize(crop) for crop in crops]))
]))
test_loader = DataLoader(dataset=test_dataset, batch_size=16,
shuffle=False, num_workers=0, pin_memory=True)
print(f"About to evaluate on device {self.device}")
for i, (inp, target) in enumerate(test_loader):
with torch.no_grad():
target = target.to(self.device)
gt = torch.cat((gt, target), 0)
bs, n_crops, c, h, w = inp.size()
input_var = torch.autograd.Variable(inp.view(-1, c, h, w))
input_var = input_var.to(self.device)
self.model = self.model.to(self.device)
output = self.model(input_var)
output_mean = output.view(bs, n_crops, -1).mean(1)
pred = torch.cat((pred, output_mean.data), 0)
AUROCs = self.compute_AUCs(gt, pred)
AUROC_avg = np.array(AUROCs).mean()
print("Finished eval aucroc is: ", AUROC_avg)
print("Normal ops:")
print(*self.normal_ops, sep = ', ')
print("Reduction ops: ")
print(*self.reduction_ops, sep = '- ')
return AUROC_avg