-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·389 lines (313 loc) · 15.6 KB
/
main.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
from pickletools import optimize
import numpy as np
import matplotlib.pyplot as plt
from numpy.core.fromnumeric import shape
import pandas as pd
from pandas.io.stata import precision_loss_doc
from torch.utils.data import DataLoader
import torch.nn as nn
from torch.optim import lr_scheduler, SGD, Adam, AdamW
import torch
from sklearn.metrics import precision_recall_curve, auc
import os
from tqdm import tqdm
import matplotlib.pyplot as plt
from sklearn.metrics import precision_recall_curve, auc, roc_auc_score
import time
import random
from torch.profiler import profile, record_function, ProfilerActivity
import sys
sys.path.extend("./utils/")
from utils.models import densenet121, densenet201, resnet50, clip_resnet50, bit_resnet50, freeze_resnet50, freeze_densenet201, cbr_larget, alexnet, efficientnet, layer_wise_freeze_resnet50
from utils.HAM import HAM
from utils.BIMCV import BIMCV
from utils.settings import parse_opts
from utils.slim_resnet import slim_resnet50
from utils.layer_wise_slim_resnet import layer_wise_slim_resnet50
from utils.slim_densenet import slim_densenet201
try:
from torch.hub import load_state_dict_from_url
except ImportError:
from torch.utils.model_zoo import load_url as load_state_dict_from_url
resnet50_model_url = "https://download.pytorch.org/models/resnet50-0676ba61.pth"
densenet201_model_url = "https://download.pytorch.org/models/densenet201-c1103571.pth"
def plot(dict_loss, dict_acc, args):
epochs = len(dict_loss['train'])
plt.figure()
plt.subplot(211)
plt.plot(range(epochs), dict_loss['train'], label='train_loss')
plt.plot(range(epochs), dict_loss['val'], label='val_loss')
plt.legend()
plt.subplot(212)
plt.plot(range(epochs), dict_acc['train'], label='train_acc')
plt.plot(range(epochs), dict_acc['val'], label='val_acc')
plt.legend()
plt.title(args.saved_path.split("/")[-1])
plt.savefig(os.path.join(args.saved_path, "train_stat.png"))
def evaluate_single(model, valloader, criterion, args):
model.eval()
PRED = []
LABELS = []
LOSS = 0
m = nn.Softmax(dim=1)
total_run_time = 0
counter = 0
model.to(args.device)
for data, label in valloader:
input = data.to(args.device)
target = label.to(args.device).long()
output = m(model(input))
loss = criterion(output, target)
LABELS.extend(label.detach().cpu().numpy())
PRED.extend(output.detach().cpu().numpy())
LOSS += loss.detach().cpu().numpy() * data.shape[0]
LABELS = np.asarray(LABELS)
PRED = np.asarray(PRED)
TP = [0] * args.classes
FP = [0] * args.classes
PRED_LABEL = np.argmax(PRED, axis=1)
acc = np.mean(PRED_LABEL==LABELS)
LOSS = LOSS / valloader.dataset.__len__()
if args.test:
rocs = []
prcs = []
for i in range(LABELS.shape[0]):
if LABELS[i] == PRED_LABEL[i]:
TP[LABELS[i]] += 1
else:
FP[LABELS[i]] += 1
for i in range(PRED.shape[1]):
tmp_labels = (LABELS==i).astype(int)
roc = roc_auc_score(tmp_labels, PRED[:, i])
p, r, t = precision_recall_curve(tmp_labels, PRED[:, i])
prc = auc(r, p)
rocs.append(roc)
prcs.append(prc)
Precision = [TP[i]/(TP[i] + FP[i]) for i in range(args.classes)]
for p, r in zip(prcs, rocs):
print("{} {}".format(p, r))
return acc, LOSS
def evaluate_multi(model, valloader, criterion, args):
model.eval()
LABELS = []
PRED = []
LOSS = 0
for data, label in valloader:
input = data.to(args.device)
target = label.to(args.device)
output = model(input)
loss = criterion(output, target)
LOSS += loss.detach().cpu().numpy() * data.shape[0]
PRED.extend(output.detach().cpu().numpy())
LABELS.extend(label.detach().cpu().numpy())
PRED = np.asarray(PRED)
LABELS = np.asarray(LABELS)
acc = []
for i in range(LABELS.shape[1]):
tmp_pred = (PRED[:, i] > 0).astype(int)
tmp_labels = LABELS[:, i]
acc.append(np.sum(tmp_pred == tmp_labels)/LABELS.shape[0])
acc = np.mean(acc)
prc = []
for i in range(LABELS.shape[1]):
p, r, t = precision_recall_curve(LABELS[:, i], PRED[:, i])
prc.append(auc(r, p))
print(prc)
print("accuracy: ", acc)
prc = np.mean(prc)
LOSS = LOSS / valloader.dataset.__len__()
return prc, LOSS
def evaluate(model, valloader, criterion, args):
if args.dataset == "CheXpert" and (args.target == "all" or args.target == 'low' or args.target == 'high'):
return evaluate_multi(model, valloader, criterion, args)
elif args.dataset == "HAM" or args.dataset == "BIMCV" or (args.dataset == "CheXpert" and args.target != "all"):
return evaluate_single(model, valloader, criterion, args)
else:
exit("evaluation not supported yet")
def getWeights(labels, args):
new_labels = labels.detach().cpu().numpy()
count = [np.sum(new_labels == i) for i in range(args.classes)]
count = torch.Tensor(1/count)
print(count)
print(labels)
return count
def train(model, trainloader, valloader, args):
# training configs
model = model.to(args.device)
if args.data_parallel:
model = nn.DataParallel(model)
if "freeze" in args.model:
modules=list(model.children())[:-1]
base=nn.Sequential(*modules)
fc = list(model.children())[-1]
optimizer = Adam(
[
{"params": fc.parameters(), "lr": args.lr},
{"params": base.parameters()},
],
lr = args.lr/args.ptl_decay)
else:
modules=list(model.children())[:-1]
base=nn.Sequential(*modules)
fc = list(model.children())[-1]
optimizer = Adam(model.parameters(), lr = args.lr)
scheduler = lr_scheduler.ReduceLROnPlateau(optimizer, factor=0.5, patience=5, mode='min')
criterion = args.criterion
max_acc = 0
min_loss = 1e8
hist_loss = {"train":[], "val":[]}
hist_acc = {"train":[], "val":[]}
iter_count = 0
# Start Training
for epoch in range(args.max_epoch):
model.train()
for data, label in trainloader:
iter_count += 1
input = data.to(args.device)
target = label.to(args.device)
# for BCE loss only
if args.target != 'all' and args.target != 'low' and args.target != 'high':
target = target.long()
optimizer.zero_grad()
output = model(input)
loss = criterion(output, target)
loss.backward()
optimizer.step()
if args.eval_iter > 0 and (iter_count % args.eval_iter) == 0:
train_acc, train_loss = evaluate(model, trainloader, criterion, args=args)
val_acc, val_loss = evaluate(model, valloader, criterion, args=args)
log_config = {"epoch": epoch, "train_loss": train_loss, "train_acc": train_acc, "val_loss": val_loss, "val_acc": val_acc, "lr": optimizer.param_groups[0]['lr']}
print("epoch{epoch}: train: loss:{train_loss} \t acc:{train_acc} | test: loss:{val_loss} \t acc:{val_acc} \t lr:{lr}".format(**log_config))
scheduler.step(val_loss)
if min_loss > val_loss:
min_loss = val_loss
save_file_name = os.path.join(args.saved_path, "best.pt")
torch.save(model, save_file_name)
if optimizer.param_groups[0]['lr'] < 1e-6:
break
if args.eval_iter <= 0:
train_acc, train_loss = evaluate(model, trainloader, criterion, args=args)
val_acc, val_loss = evaluate(model, valloader, criterion, args=args)
log_config = {"epoch": epoch, "train_loss": train_loss, "train_acc": train_acc, "val_loss": val_loss, "val_acc": val_acc, "lr": optimizer.param_groups[0]['lr']}
print("epoch{epoch}: train: loss:{train_loss} \t acc:{train_acc} | test: loss:{val_loss} \t acc:{val_acc} \t lr:{lr}".format(**log_config))
scheduler.step(val_loss)
if min_loss > val_loss:
min_loss = val_loss
save_file_name = os.path.join(args.saved_path, "best.pt")
torch.save(model, save_file_name)
if optimizer.param_groups[0]['lr'] < 1e-6:
break
# save for plot
hist_loss['train'].append(train_loss)
hist_loss['val'].append(val_loss)
hist_acc['train'].append(train_acc)
hist_acc['val'].append(val_acc)
# save the final model
save_file_name = os.path.join(args.saved_path, "final.pt")
torch.save(model, save_file_name)
plot(hist_loss, hist_acc, args)
def set_random_seed(seed):
torch.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
if __name__ == "__main__":
set_random_seed(1)
torch.cuda.empty_cache()
args = parse_opts()
mode = "noisy"
if not args.test:
if args.dataset == "HAM":
if args.sub == 100:
train_df = pd.read_csv(os.path.join(args.root_path, str(args.exp), "train.csv"))
val_df = pd.read_csv(os.path.join(args.root_path, str(args.exp), "val.csv"))
else:
train_df = pd.read_csv(os.path.join(args.root_path, str(args.exp), "train_{}.csv".format(args.sub/100)))
val_df = pd.read_csv(os.path.join(args.root_path, str(args.exp), "val_{}.csv".format(args.sub/100)))
train_ds = HAM(train_df, root_dir=args.root_path+"jpgs/", mode='train', args=args)
train_dl = DataLoader(train_ds, batch_size=args.bs, shuffle=True, pin_memory=args.pin_memory, num_workers=args.num_workers, drop_last=True)
val_ds = HAM(val_df, root_dir=args.root_path+"jpgs/", mode='val', args=args)
val_dl = DataLoader(val_ds, batch_size=args.bs, shuffle=False, pin_memory=args.pin_memory, num_workers=args.num_workers)
elif args.dataset == "BIMCV":
if args.sub == 100:
train_df = pd.read_csv(os.path.join(args.root_path, mode, str(args.exp), "train.csv"))
val_df = pd.read_csv(os.path.join(args.root_path, mode, str(args.exp), "val.csv"))
else:
print("train with sub set")
train_df = pd.read_csv(os.path.join(args.root_path, mode, str(args.exp), "train_{}.csv".format(args.sub/100)))
val_df = pd.read_csv(os.path.join(args.root_path, mode, str(args.exp), "val_{}.csv".format(args.sub/100)))
train_ds = BIMCV(train_df, root_dir=args.root_path+"crop/", mode='train', args=args)
train_dl = DataLoader(train_ds, batch_size=args.bs, shuffle=True, pin_memory=args.pin_memory, num_workers=args.num_workers)
val_ds = BIMCV(val_df, root_dir=args.root_path+"crop/", mode='val', args=args)
val_dl = DataLoader(val_ds, batch_size=args.bs, shuffle=False, pin_memory=args.pin_memory, num_workers=args.num_workers)
elif args.dataset == "IMGNET":
imagenet_data = torchvision.datasets.ImageNet('path/to/imagenet_root/')
if args.model == "densenet121":
model = densenet121(pretrained=args.pretrained, trunc=args.trunc, classes=args.classes)
elif args.model == "densenet201":
model = densenet201(pretrained=args.pretrained, trunc=args.trunc, classes=args.classes)
elif args.model == "resnet50":
model = resnet50(pretrained=args.pretrained, trunc=args.trunc, classes=args.classes, args=args)
elif args.model == "resnet50_FAT":
model = resnet50(pretrained=args.pretrained, trunc=args.trunc, classes=args.classes, args=args, freeze=True)
elif args.model == "clip_resnet50":
model, preprocess = clip_resnet50(trunc=args.trunc, classes=args.classes)
args.preprocess = preprocess
elif args.model == "bit_resnet50":
model, preprocess = bit_resnet50(trunc=args.trunc, classes=args.classes)
args.preprocess = preprocess
elif args.model == "slim_resnet50":
model = slim_resnet50(shrink_coefficient=args.slim_factor, load_up_to=args.slim_from, num_classes=args.classes)
state_dict = load_state_dict_from_url(resnet50_model_url, progress=True)
model.load_up_to_block(state_dict)
elif args.model == "layer_wise_slim_resnet50":
model = layer_wise_slim_resnet50(shrink_coefficient=args.slim_factor, load_up_to=args.slim_from, num_classes=args.classes, pretrained=True)
elif args.model == "slim_densenet201":
model = slim_densenet201(shrink_coefficient=args.slim_factor, load_up_to=args.slim_from, num_classes=args.classes)
state_dict = load_state_dict_from_url(densenet201_model_url, progress=True)
model.load_up_to_block(state_dict)
elif args.model == "freeze_resnet50":
model = freeze_resnet50(finetune_from=args.finetune_from, classes=args.classes)
elif args.model == "layer_wise_freeze_resnet50":
model = net = layer_wise_freeze_resnet50(finetune_from=args.finetune_from, classes=args.classes)
elif args.model == "freeze_densenet201":
model = freeze_densenet201(finetune_from=args.finetune_from, classes=args.classes)
elif args.model == "cbr_larget":
model = cbr_larget(pretrained=args.pretrained, classes=args.classes)
elif args.model == 'alexnet':
model = alexnet(pretrained=args.pretrained, classes=args.classes)
elif args.model == 'layerttl_resnet50':
model = resnet50(pretrained=args.pretrained, trunc=args.trunc, layer_wise=True, classes=args.classes, args=args)
elif args.model == "efficientnet":
model = efficientnet(num_classes=args.classes, pretrained=args.pretrained, trunc=args.trunc)
else:
exit("model not found")
print("training with ", args.model)
train(model=model, trainloader=train_dl, valloader=val_dl, args=args)
else:
args.pin_memory = True
if args.dataset == "HAM":
test_df = pd.read_csv(os.path.join(args.root_path, str(args.exp), "test.csv"))
test_ds = HAM(test_df, root_dir=args.root_path+"jpgs/", mode='val', args=args)
test_dl = DataLoader(test_ds, batch_size=args.bs, shuffle=False, pin_memory=args.pin_memory, num_workers=args.num_workers)
elif args.dataset == "BIMCV":
test_df = pd.read_csv(os.path.join(args.root_path, mode, str(args.exp), "test.csv"))
test_ds = BIMCV(test_df, root_dir=args.root_path+"crop/", mode='val', args=args)
test_dl = DataLoader(test_ds, batch_size=args.bs, shuffle=False, pin_memory=args.pin_memory, num_workers=args.num_workers)
if args.model == "clip_resnet50":
_, preprocess = clip_resnet50(trunc=args.trunc, classes=args.classes)
args.preprocess = preprocess
elif args.model == "bit_resnet50":
_, preprocess = bit_resnet50(trunc=args.trunc, classes=args.classes)
args.preprocess = preprocess
else:
pass
print(os.path.join(args.saved_path, "best.pt"))
model = torch.load(os.path.join(args.saved_path, "best.pt"))
try:
model = model.module.to(args.device)
except:
model = model.to(args.device)
# model = model.module
criterion = nn.CrossEntropyLoss(reduction='mean')
acc, _ = evaluate(model, test_dl, criterion, args)
print("top one accuracy:", acc)