-
Notifications
You must be signed in to change notification settings - Fork 88
/
train_tcga.py
184 lines (168 loc) · 8.95 KB
/
train_tcga.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
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from torch.autograd import Variable
import torchvision.transforms.functional as VF
from torchvision import transforms
import sys, argparse, os, copy, itertools, glob, datetime
import pandas as pd
import numpy as np
from sklearn.utils import shuffle
from sklearn.metrics import roc_curve, roc_auc_score, precision_recall_fscore_support
from sklearn.datasets import load_svmlight_file
from collections import OrderedDict
def get_bag_feats(csv_file_df, args):
if args.dataset == 'TCGA-lung-default':
feats_csv_path = 'datasets/tcga-dataset/tcga_lung_data_feats/' + csv_file_df.iloc[0].split('/')[1] + '.csv'
else:
feats_csv_path = csv_file_df.iloc[0]
df = pd.read_csv(feats_csv_path)
feats = shuffle(df).reset_index(drop=True)
feats = feats.to_numpy()
label = np.zeros(args.num_classes)
if args.num_classes==1:
label[0] = csv_file_df.iloc[1]
else:
if int(csv_file_df.iloc[1])<=(len(label)-1):
label[int(csv_file_df.iloc[1])] = 1
return label, feats
def train(train_df, milnet, criterion, optimizer, args):
csvs = shuffle(train_df).reset_index(drop=True)
total_loss = 0
bc = 0
Tensor = torch.cuda.FloatTensor
for i in range(len(train_df)):
optimizer.zero_grad()
label, feats = get_bag_feats(train_df.iloc[i], args)
bag_label = Variable(Tensor([label]))
bag_feats = Variable(Tensor([feats]))
bag_feats = bag_feats.view(-1, args.feats_size)
ins_prediction, bag_prediction, _, _ = milnet(bag_feats)
max_prediction, _ = torch.max(ins_prediction, 0)
bag_loss = criterion(bag_prediction.view(1, -1), bag_label.view(1, -1))
max_loss = criterion(max_prediction.view(1, -1), bag_label.view(1, -1))
loss = 0.5*bag_loss + 0.5*max_loss
loss.backward()
optimizer.step()
total_loss = total_loss + loss.item()
sys.stdout.write('\r Training bag [%d/%d] bag loss: %.4f' % (i, len(train_df), loss.item()))
return total_loss / len(train_df)
def test(test_df, milnet, criterion, optimizer, args):
csvs = shuffle(test_df).reset_index(drop=True)
total_loss = 0
test_labels = []
test_predictions = []
Tensor = torch.cuda.FloatTensor
with torch.no_grad():
for i in range(len(test_df)):
label, feats = get_bag_feats(test_df.iloc[i], args)
bag_label = Variable(Tensor([label]))
bag_feats = Variable(Tensor([feats]))
bag_feats = bag_feats.view(-1, args.feats_size)
ins_prediction, bag_prediction, _, _ = milnet(bag_feats)
max_prediction, _ = torch.max(ins_prediction, 0)
bag_loss = criterion(bag_prediction.view(1, -1), bag_label.view(1, -1))
max_loss = criterion(max_prediction.view(1, -1), bag_label.view(1, -1))
loss = 0.5*bag_loss + 0.5*max_loss
total_loss = total_loss + loss.item()
sys.stdout.write('\r Testing bag [%d/%d] bag loss: %.4f' % (i, len(test_df), loss.item()))
test_labels.extend([label])
test_predictions.extend([(0.0*torch.sigmoid(max_prediction)+1.0*torch.sigmoid(bag_prediction)).squeeze().cpu().numpy()])
test_labels = np.array(test_labels)
test_predictions = np.array(test_predictions)
auc_value, _, thresholds_optimal = multi_label_roc(test_labels, test_predictions, args.num_classes, pos_label=1)
if args.num_classes==1:
class_prediction_bag = test_predictions
class_prediction_bag[class_prediction_bag>=thresholds_optimal[0]] = 1
class_prediction_bag[class_prediction_bag<thresholds_optimal[0]] = 0
test_predictions = class_prediction_bag
test_labels = np.squeeze(test_labels)
else:
for i in range(args.num_classes):
class_prediction_bag = test_predictions[:, i]
class_prediction_bag[class_prediction_bag>=thresholds_optimal[i]] = 1
class_prediction_bag[class_prediction_bag<thresholds_optimal[i]] = 0
test_predictions[:, i] = class_prediction_bag
bag_score = 0
for i in range(0, len(test_df)):
bag_score = np.array_equal(test_labels[i], test_predictions[i]) + bag_score
avg_score = bag_score / len(test_df)
return total_loss / len(test_df), avg_score, auc_value, thresholds_optimal
def multi_label_roc(labels, predictions, num_classes, pos_label=1):
fprs = []
tprs = []
thresholds = []
thresholds_optimal = []
aucs = []
if len(predictions.shape)==1:
predictions = predictions[:, None]
for c in range(0, num_classes):
label = labels[:, c]
prediction = predictions[:, c]
fpr, tpr, threshold = roc_curve(label, prediction, pos_label=1)
fpr_optimal, tpr_optimal, threshold_optimal = optimal_thresh(fpr, tpr, threshold)
c_auc = roc_auc_score(label, prediction)
aucs.append(c_auc)
thresholds.append(threshold)
thresholds_optimal.append(threshold_optimal)
return aucs, thresholds, thresholds_optimal
def optimal_thresh(fpr, tpr, thresholds, p=0):
loss = (fpr - tpr) - p * tpr / (fpr + tpr + 1)
idx = np.argmin(loss, axis=0)
return fpr[idx], tpr[idx], thresholds[idx]
def main():
parser = argparse.ArgumentParser(description='Train DSMIL on 20x patch features learned by SimCLR')
parser.add_argument('--num_classes', default=2, type=int, help='Number of output classes [2]')
parser.add_argument('--feats_size', default=512, type=int, help='Dimension of the feature size [512]')
parser.add_argument('--lr', default=0.0002, type=float, help='Initial learning rate [0.0002]')
parser.add_argument('--num_epochs', default=40, type=int, help='Number of total training epochs [40]')
parser.add_argument('--weight_decay', default=5e-3, type=float, help='Weight decay [5e-3]')
parser.add_argument('--dataset', default='TCGA-lung-default', type=str, help='Dataset folder name')
parser.add_argument('--split', default=0.2, type=float, help='Training/Validation split [0.2]')
parser.add_argument('--model', default='dsmil', type=str, help='MIL model [dsmil]')
args = parser.parse_args()
if args.model == 'dsmil':
import dsmil as mil
elif args.model == 'abmil':
import abmil as mil
i_classifier = mil.FCLayer(in_size=args.feats_size, out_size=args.num_classes).cuda()
b_classifier = mil.BClassifier(input_size=args.feats_size, output_class=args.num_classes).cuda()
milnet = mil.MILNet(i_classifier, b_classifier).cuda()
criterion = nn.BCEWithLogitsLoss()
optimizer = torch.optim.Adam(milnet.parameters(), lr=args.lr, betas=(0.5, 0.9), weight_decay=args.weight_decay)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, args.num_epochs, 0.000005)
if args.dataset == 'TCGA-lung-default':
bags_csv = 'datasets/tcga-dataset/TCGA.csv'
else:
bags_csv = os.path.join('datasets', args.dataset, args.dataset+'.csv')
bags_path = pd.read_csv(bags_csv)
train_path = bags_path.iloc[0:int(len(bags_path)*(1-args.split)), :]
test_path = bags_path.iloc[int(len(bags_path)*(1-args.split)):, :]
best_score = 0
save_path = os.path.join('weights', datetime.date.today().strftime("%m%d%Y"))
os.makedirs(save_path, exist_ok=True)
run = len(glob.glob(os.path.join(save_path, '*.pth')))
for epoch in range(1, args.num_epochs):
train_path = shuffle(train_path).reset_index(drop=True)
test_path = shuffle(test_path).reset_index(drop=True)
train_loss_bag = train(train_path, milnet, criterion, optimizer, args) # iterate all bags
test_loss_bag, avg_score, aucs, thresholds_optimal = test(test_path, milnet, criterion, optimizer, args)
if args.dataset=='TCGA-lung':
print('\r Epoch [%d/%d] train loss: %.4f test loss: %.4f, average score: %.4f, auc_LUAD: %.4f, auc_LUSC: %.4f' %
(epoch, args.num_epochs, train_loss_bag, test_loss_bag, avg_score, aucs[0], aucs[1]))
else:
print('\r Epoch [%d/%d] train loss: %.4f test loss: %.4f, average score: %.4f, AUC: ' %
(epoch, args.num_epochs, train_loss_bag, test_loss_bag, avg_score) + '|'.join('class-{}>>{}'.format(*k) for k in enumerate(aucs)))
scheduler.step()
current_score = (sum(aucs) + avg_score + 1 - test_loss_bag)/4
if current_score >= best_score:
best_score = current_score
save_name = os.path.join(save_path, str(run+1)+'.pth')
torch.save(milnet.state_dict(), save_name)
if args.dataset=='TCGA-lung':
print('Best model saved at: ' + save_name + ' Best thresholds: LUAD %.4f, LUSC %.4f' % (thresholds_optimal[0], thresholds_optimal[1]))
else:
print('Best model saved at: ' + save_name)
print('Best thresholds ===>>> '+ '|'.join('class-{}>>{}'.format(*k) for k in enumerate(thresholds_optimal)))
if __name__ == '__main__':
main()