-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
executable file
·670 lines (601 loc) · 19.9 KB
/
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
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
"""
Project: Advancing Surgical VQA with Scene Graph Knowledge
-----
Copyright (c) University of Strasbourg, All Rights Reserved.
"""
import os
import argparse
import pandas as pd
from lib2to3.pytree import convert
from torch import nn
from torch import optim
import torch.utils.data
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
from transformers import BertTokenizer
from torch.utils.data import DataLoader
from utils.utils import *
from utils.dataloaderClassification import *
from models.VisualBertClassification_ssgqa import VisualBertClassification
import warnings
warnings.simplefilter(action="ignore", category=FutureWarning)
"""
Seed randoms
"""
def seed_everything(seed=27):
"""
Set random seed for reproducible experiments
Inputs: seed number
"""
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
os.environ["PYTHONHASHSEED"] = str(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
def train(
args, train_dataloader, model, criterion, optimizer, epoch, tokenizer, device
):
model.train()
total_loss = 0.0
label_true = None
label_pred = None
label_score = None
import time
for i, (_, visual_features, q, labels) in enumerate(train_dataloader, 0):
# prepare questions
questions = []
for question in q:
questions.append(question)
inputs = tokenizer(
questions,
return_tensors="pt",
padding="max_length",
truncation=True,
max_length=args.question_len,
)
# GPU / CPU
visual_features = visual_features.to(device)
labels = labels.to(device)
if args.transformer_ver == "pure_language":
outputs = model(inputs)
else:
outputs = model(inputs, visual_features)
loss = criterion(outputs, labels)
# zero the parameter gradients
optimizer.zero_grad()
loss.backward()
optimizer.step()
# print statistics
total_loss += loss.item()
scores, predicted = torch.max(F.softmax(outputs, dim=1).data, 1)
label_true = (
labels.data.cpu()
if label_true == None
else torch.cat((label_true, labels.data.cpu()), 0)
)
label_pred = (
predicted.data.cpu()
if label_pred == None
else torch.cat((label_pred, predicted.data.cpu()), 0)
)
label_score = (
scores.data.cpu()
if label_score == None
else torch.cat((label_score, scores.data.cpu()), 0)
)
# loss and acc
acc, c_acc = calc_acc(label_true, label_pred), calc_classwise_acc(
label_true, label_pred
)
precision, recall, fscore = calc_precision_recall_fscore(label_true, label_pred)
print(
"Train: epoch: %d loss: %.6f | Acc: %.6f | Precision: %.6f | Recall: %.6f | FScore: %.6f"
% (epoch, total_loss, acc, precision, recall, fscore)
)
return acc
def validate(
args, val_loader, model, criterion, epoch, tokenizer, device, save_output=False
):
model.eval()
total_loss = 0.0
label_true = None
label_pred = None
label_score = None
file_names = list()
criterion = nn.CrossEntropyLoss()
with torch.no_grad():
for i, (file_name, visual_features, q, labels) in enumerate(val_loader, 0):
# prepare questions
questions = []
for question in q:
questions.append(question)
inputs = tokenizer(
questions,
return_tensors="pt",
padding="max_length",
truncation=True,
max_length=args.question_len,
)
# GPU / CPU
visual_features = visual_features.to(device)
labels = labels.to(device)
if args.transformer_ver == "pure_language":
outputs = model(inputs)
else:
outputs = model(inputs, visual_features)
loss = criterion(outputs, labels)
total_loss += loss.item()
scores, predicted = torch.max(F.softmax(outputs, dim=1).data, 1)
label_true = (
labels.data.cpu()
if label_true == None
else torch.cat((label_true, labels.data.cpu()), 0)
)
label_pred = (
predicted.data.cpu()
if label_pred == None
else torch.cat((label_pred, predicted.data.cpu()), 0)
)
label_score = (
scores.data.cpu()
if label_score == None
else torch.cat((label_score, scores.data.cpu()), 0)
)
for f in file_name:
file_names.append(f)
mAP, mAR, mAf1, wf1, acc = eval_for_f1_et_all(label_true, label_pred)
c_acc = 0.0
print(
"Test: epoch: %d loss: %.6f | Acc: %.6f | mAP: %.6f | mAR: %.6f | mAf1: %.6f | wf1: %.6f"
% (epoch, total_loss, acc, mAP, mAR, mAf1, wf1)
)
if save_output:
"""
Saving predictions
"""
if os.path.exists(args.checkpoint_dir + "text_files") == False:
os.mkdir(args.checkpoint_dir + "text_files")
file1 = open(args.checkpoint_dir + "text_files/labels.txt", "w")
file1.write(str(label_true))
file1.close()
file1 = open(args.checkpoint_dir + "text_files/predictions.txt", "w")
file1.write(str(label_pred))
file1.close()
if "ssg-qa" in args.dataset_type:
convert_arr = [
"0",
"1",
"10",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"False",
"True",
"abdominal_wall_cavity",
"adhesion",
"anatomy",
"aspirate",
"bipolar",
"blood_vessel",
"blue",
"brown",
"clip",
"clipper",
"coagulate",
"cut",
"cystic_artery",
"cystic_duct",
"cystic_pedicle",
"cystic_plate",
"dissect",
"fluid",
"gallbladder",
"grasp",
"grasper",
"gut",
"hook",
"instrument",
"irrigate",
"irrigator",
"liver",
"omentum",
"pack",
"peritoneum",
"red",
"retract",
"scissors",
"silver",
"specimen_bag",
"specimenbag",
"white",
"yellow",
]
else:
raise NotImplementedError
df = pd.DataFrame(columns=["Img", "Ground Truth", "Prediction"])
for i in range(len(label_true)):
df = df.append(
{
"Img": file_names[i],
"Ground Truth": convert_arr[label_true[i]],
"Prediction": convert_arr[label_pred[i]],
},
ignore_index=True,
)
df.to_csv(
args.checkpoint_dir
+ args.checkpoint_dir.split("/")[1]
+ "_"
+ args.checkpoint_dir.split("/")[2]
+ "_eval.csv"
)
return (acc, c_acc, mAP, mAR, mAf1)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="SSGQA VQA Classification")
# Model parameters
parser.add_argument(
"--emb_dim", type=int, default=300, help="dimension of word embeddings."
)
parser.add_argument("--n_heads", type=int, default=8, help="Multi-head attention.")
parser.add_argument("--dropout", type=float, default=0.1, help="dropout")
parser.add_argument(
"--encoder_layers",
type=int,
default=6,
help="the number of layers of encoder in Transformer.",
)
# Training parameters
parser.add_argument(
"--epochs",
type=int,
default=80,
help="number of epochs to train for (if early stopping is not triggered).",
) # 80, 26
parser.add_argument("--batch_size", type=int, default=64, help="batch_size")
parser.add_argument(
"--workers",
type=int,
default=4,
help="for data-loading; right now, only 1 works with h5pys.",
)
parser.add_argument(
"--print_freq",
type=int,
default=100,
help="print training/validation stats every __ batches.",
)
parser.add_argument(
"--checkpoint", default=None, help="path to checkpoint, None if none."
)
# existing checkpoint
parser.add_argument(
"--lr", type=float, default=0.000005, help="0.000005, 0.00001, 0.000005"
)
parser.add_argument(
"--checkpoint_dir",
default="./checkpoints/final_vb/",
help="med_vqa_c$version$/m18/c80//m18_vid$temporal_size$/c80_vid$temporal_size$",
) # clf_v1_2_1x1/med_vqa_c3
parser.add_argument(
"--dataset_type",
default="ssg-qa-roi_coord",
help="med_vqa/m18/c80/m18_vid/c80_vid/ssg-qa-full/img/roi/rot_coord",
)
parser.add_argument("--dataset_cat", default="None", help="cat1/cat2/cat3")
parser.add_argument(
"--transformer_ver",
default="vb",
help="vb/vbrm/two/vbrm_visual/pure_visual/pure_language",
)
parser.add_argument("--tokenizer_ver", default="v2", help="v2/v3")
parser.add_argument("--patch_size", default=1, help="1/2/3/4/5")
parser.add_argument("--temporal_size", default=3, help="1/2/3/4/5")
parser.add_argument("--question_len", default=77, help="25")
parser.add_argument("--num_class", default=2, help="25")
parser.add_argument(
"--validate", default=False, help="When only validation required False/True"
)
parser.add_argument(
"--analysis_type",
default=["exist"],
help="Which type of question are evaluated",
)
args = parser.parse_args()
# load checkpoint, these parameters can't be modified
final_args = {
"emb_dim": args.emb_dim,
"n_heads": args.n_heads,
"dropout": args.dropout,
"encoder_layers": args.encoder_layers,
}
seed_everything()
# GPU or CPU
device = torch.device(
"cuda" if torch.cuda.is_available() else "cpu"
) # sets device for model and PyTorch tensors
cudnn.benchmark = True # set to true only if inputs to model are fixed size; otherwise lot of computational overhead
print("device =", device)
# best model initialize
start_epoch = 1
best_epoch = [0]
best_results = [0.0]
epochs_since_improvement = 0
# # dataset
if args.dataset_type == "ssg-qa-roi_coord":
"""
Train and test for cholec dataset
"""
# tokenizer
if args.tokenizer_ver == "v2":
tokenizer = BertTokenizer.from_pretrained("emilyalsentzer/Bio_ClinicalBERT")
elif args.tokenizer_ver == "v3":
tokenizer = BertTokenizer.from_pretrained(
"emilyalsentzer/Bio_ClinicalBERT", do_lower_case=True
)
elif args.tokenizer_ver == "v4":
tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
# dataloader
train_seq = [
"VID73",
"VID40",
"VID62",
"VID42",
"VID29",
"VID56",
"VID50",
"VID78",
"VID66",
"VID13",
"VID52",
"VID06",
"VID36",
"VID05",
"VID12",
"VID26",
"VID68",
"VID32",
"VID49",
"VID65",
"VID47",
"VID04",
"VID23",
"VID79",
"VID51",
"VID10",
"VID57",
"VID75",
"VID25",
"VID14",
"VID15",
"VID08",
"VID80",
"VID27",
"VID70",
]
val_seq = ["VID18", "VID48", "VID01", "VID35", "VID31"]
test_seq = ["VID22", "VID74", "VID60", "VID02", "VID43"] #
folder_head = "./data/"
folder_tail = "/*.txt"
# dataloader
train_dataset = SSGVQAClassification_full_roi_coord(
train_seq, folder_head, folder_tail, patch_size=args.patch_size
)
train_dataloader = DataLoader(
dataset=train_dataset,
batch_size=args.batch_size,
shuffle=False,
num_workers=args.workers,
)
val_dataset = SSGVQAClassification_full_roi_coord(
val_seq, folder_head, folder_tail, patch_size=args.patch_size
)
val_dataloader = DataLoader(
dataset=val_dataset,
batch_size=args.batch_size,
shuffle=False,
num_workers=args.workers,
)
test_dataset = SSGVQAClassification_full_roi_coord(
test_seq, folder_head, folder_tail, patch_size=args.patch_size
)
test_dataloader = DataLoader(
dataset=test_dataset,
batch_size=args.batch_size,
shuffle=False,
num_workers=args.workers,
)
# num_classes
args.num_class = 51
elif args.dataset_type == "ssg-qa-roi-analysis":
"""
Train and test for cholec dataset
"""
if args.tokenizer_ver == "v2":
tokenizer = BertTokenizer.from_pretrained("emilyalsentzer/Bio_ClinicalBERT")
elif args.tokenizer_ver == "v3":
tokenizer = BertTokenizer.from_pretrained(
"emilyalsentzer/Bio_ClinicalBERT", do_lower_case=True
)
# dataloader
test_seq = ["VID22", "VID74", "VID02", "VID60", "VID43"]
folder_head = "./data/"
folder_tail = "/*.txt"
# dataloader
test_sets = [
SSGVQAClassification_full_roi_analysis(
test_seq,
folder_head,
folder_tail,
ana_type=["zero_hop.json"],
patch_size=args.patch_size,
),
SSGVQAClassification_full_roi_analysis(
test_seq,
folder_head,
folder_tail,
ana_type=["one_hop.json"],
patch_size=args.patch_size,
),
SSGVQAClassification_full_roi_analysis(
test_seq,
folder_head,
folder_tail,
ana_type=["single_and.json"],
patch_size=args.patch_size,
),
SSGVQAClassification_full_roi_analysis(
test_seq,
folder_head,
folder_tail,
ana_type=["query_color", "query_type", "query_location"],
patch_size=args.patch_size,
),
SSGVQAClassification_full_roi_analysis(
test_seq,
folder_head,
folder_tail,
ana_type=["query_component"],
patch_size=args.patch_size,
),
SSGVQAClassification_full_roi_analysis(
test_seq,
folder_head,
folder_tail,
ana_type=["exist"],
patch_size=args.patch_size,
),
SSGVQAClassification_full_roi_analysis(
test_seq,
folder_head,
folder_tail,
ana_type=["count"],
patch_size=args.patch_size,
),
]
test_dataloader = [
DataLoader(dataset=test_dataset, batch_size=args.batch_size, shuffle=False)
for test_dataset in test_sets
]
# num_classes
args.num_class = 51
# model
if args.transformer_ver == "vb":
print("loading VisualBert")
model = VisualBertClassification(
vocab_size=len(tokenizer),
layers=args.encoder_layers,
n_heads=args.n_heads,
num_class=args.num_class,
)
else:
raise NotImplementedError
# Initialize / load checkpoint
if args.checkpoint is None:
# optimizer
optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)
else:
print("loading from checkpoint")
checkpoint = torch.load(args.checkpoint, map_location=str(device))
start_epoch = checkpoint["epoch"]
epochs_since_improvement = checkpoint["epochs_since_improvement"]
best_Acc = checkpoint["Acc"]
model_dict = checkpoint["model"]
model.load_state_dict(model_dict)
optimizer = checkpoint["optimizer"]
final_args = checkpoint["final_args"]
for key in final_args.keys():
args.__setattr__(key, final_args[key])
# Move to GPU, if available
model = model.to(device)
print(final_args)
pytorch_total_params = sum(p.numel() for p in model.parameters())
print("model params: ", pytorch_total_params)
# print(model)
# Loss function
criterion = nn.CrossEntropyLoss().to(device)
# validation
if args.validate:
if "analysis" in args.dataset_type:
for i in test_dataloader:
(
test_acc,
test_c_acc,
test_precision,
test_recall,
test_fscore,
) = validate(
args,
val_loader=i,
model=model,
criterion=criterion,
epoch=(args.epochs - 1),
tokenizer=tokenizer,
device=device,
save_output=True,
)
else:
test_acc, test_c_acc, test_precision, test_recall, test_fscore = validate(
args,
val_loader=test_dataloader,
model=model,
criterion=criterion,
epoch=(args.epochs - 1),
tokenizer=tokenizer,
device=device,
save_output=True,
)
else:
for epoch in range(start_epoch, args.epochs):
print(epoch)
if epochs_since_improvement > 0 and epochs_since_improvement % 5 == 0:
adjust_learning_rate(optimizer, 0.8)
# train
train_acc = train(
args,
train_dataloader=train_dataloader,
model=model,
criterion=criterion,
optimizer=optimizer,
epoch=epoch,
tokenizer=tokenizer,
device=device,
)
# validation
test_acc, test_c_acc, test_precision, test_recall, test_fscore = validate(
args,
val_loader=test_dataloader,
model=model,
criterion=criterion,
epoch=epoch,
tokenizer=tokenizer,
device=device,
)
if test_acc >= best_results[0]:
epochs_since_improvement = 0
best_results[0] = test_acc
best_epoch[0] = epoch
print(
"Best epoch: %d | Best acc: %.6f" % (best_epoch[0], best_results[0])
)
save_clf_checkpoint(
args.checkpoint_dir,
epoch,
epochs_since_improvement,
model,
optimizer,
best_results[0],
final_args,
)
else:
epochs_since_improvement += 1
print(
"\nEpochs since last improvement: %d\n"
% (epochs_since_improvement,)
)
if train_acc >= 1.0:
break