-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathutils.py
186 lines (150 loc) · 5.71 KB
/
utils.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
import csv
import torch
import numpy as np
from tqdm import tqdm
from PIL import Image
import torchvision.transforms as transforms
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
dirs = {
'train': 'data/COVID-19_Radiography_Dataset/train',
'val': 'data/COVID-19_Radiography_Dataset/val',
'test': 'data/COVID-19_Radiography_Dataset/test'
}
transform = {
'train': transforms.Compose([
transforms.Resize((224, 224)),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]
)
]),
'eval': transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]
)
])
}
def get_num_correct(preds, labels):
return preds.argmax(dim=1).eq(labels).sum().item()
def get_all_preds(model, loader):
model.eval()
with torch.no_grad():
all_preds = torch.tensor([], device=device)
for batch in loader:
images = batch[0].to(device)
preds = model(images)
all_preds = torch.cat((all_preds, preds), dim=0)
return all_preds
def get_confmat(targets, preds):
stacked = torch.stack(
(torch.as_tensor(targets, device=device),
preds.argmax(dim=1)), dim=1
).tolist()
confmat = torch.zeros(4, 4, dtype=torch.int16)
for t, p in stacked:
confmat[t, p] += 1
return confmat
def get_results(confmat, classes):
results = {}
d = confmat.diagonal()
for i, l in enumerate(classes):
tp = d[i].item()
tn = d.sum().item() - tp
fp = confmat[i].sum().item() - tp
fn = confmat[:, i].sum().item() - tp
accuracy = (tp+tn)/(tp+tn+fp+fn)
recall = tp/(tp+fn)
precision = tp/(tp+fp)
f1score = (2*precision*recall)/(precision+recall)
results[l] = [accuracy, recall, precision, f1score]
return results
def fit(epochs, model, criterion, optimizer, train_dl, valid_dl):
model_name = type(model).__name__.lower()
valid_loss_min = np.Inf
len_train, len_valid = 20685, 240
fields = [
'epoch', 'train_loss', 'train_acc', 'valid_loss', 'valid_acc'
]
rows = []
for epoch in range(epochs):
train_loss, train_correct = 0, 0
train_loop = tqdm(train_dl)
model.train()
for batch in train_loop:
images, labels = batch[0].to(device), batch[1].to(device)
preds = model(images)
loss = criterion(preds, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
train_loss += loss.item() * labels.size(0)
train_correct += get_num_correct(preds, labels)
train_loop.set_description(f'Epoch [{epoch+1:2d}/{epochs}]')
train_loop.set_postfix(
loss=loss.item(), acc=train_correct/len_train
)
train_loss = train_loss/len_train
train_acc = train_correct/len_train
model.eval()
with torch.no_grad():
valid_loss, valid_correct = 0, 0
for batch in valid_dl:
images, labels = batch[0].to(device), batch[1].to(device)
preds = model(images)
loss = criterion(preds, labels)
valid_loss += loss.item() * labels.size(0)
valid_correct += get_num_correct(preds, labels)
valid_loss = valid_loss/len_valid
valid_acc = valid_correct/len_valid
rows.append([epoch, train_loss, train_acc, valid_loss, valid_acc])
train_loop.write(
f'\n\t\tAvg train loss: {train_loss:.6f}', end='\t'
)
train_loop.write(f'Avg valid loss: {valid_loss:.6f}\n')
# save model if validation loss has decreased
# (sometimes also referred as "Early stopping")
if valid_loss <= valid_loss_min:
train_loop.write('\t\tvalid_loss decreased', end=' ')
train_loop.write(f'({valid_loss_min:.6f} -> {valid_loss:.6f})')
train_loop.write('\t\tsaving model...\n')
torch.save(
model.state_dict(),
f'models/lr3e-5_{model_name}_{device}.pth'
)
valid_loss_min = valid_loss
# write running results for plots
with open(f'outputs/CSVs/{model_name}.csv', 'w') as csv_file:
csv_writer = csv.writer(csv_file)
csv_writer.writerow(fields)
csv_writer.writerows(rows)
# worker init function for randomness in multiprocess dataloading
# https://github.com/pytorch/pytorch/issues/5059#issuecomment-817392562
def wif(id):
process_seed = torch.initial_seed()
base_seed = process_seed - id
ss = np.random.SeedSequence([id, base_seed])
# More than 128 bits (4 32-bit words) would be overkill.
np.random.seed(ss.generate_state(4))
def load_image(path):
image = Image.open(path)
image = transform['eval'](image).unsqueeze(0)
return image
def deprocess_image(image):
image = image.cpu().numpy()
image = np.squeeze(np.transpose(image[0], (1, 2, 0)))
image = image * np.array((0.229, 0.224, 0.225)) + \
np.array((0.485, 0.456, 0.406)) # un-normalize
image = image.clip(0, 1)
return image
def save_image(image, path):
# while saving PIL assumes the image is in BGR, and saves it as RGB.
# But here the image is in RGB, therefore it is converted to BGR first.
image = image[:, :, ::-1] # RGB -> BGR
image = Image.fromarray(image)
image.save(path) # saved as RGB
print(f'GradCAM masked image saved to "{path}".')