-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
282 lines (237 loc) · 11.5 KB
/
train.py
File metadata and controls
282 lines (237 loc) · 11.5 KB
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
from __future__ import print_function
import argparse
import pdb
import os
import math
# internal imports
from utils.file_utils import save_pkl
from utils.utils import *
from utils.core_utils import train
from datasets.dataset_generic import Generic_MIL_Dataset
# pytorch imports
import torch
import pandas as pd
import numpy as np
def main(args):
# create results directory if necessary
if not os.path.isdir(args.results_dir):
os.mkdir(args.results_dir)
if args.k_start == -1:
start = 0
else:
start = args.k_start
if args.k_end == -1:
end = args.k
else:
end = args.k_end
all_test_auc = []
all_val_auc = []
all_test_acc = []
all_val_acc = []
all_test_loss= []
all_val_loss= []
folds = np.arange(start, end)
print('folds:',folds)
for i in folds:
seed_torch(args.seed+i)
# pdb.set_trace()
train_dataset, val_dataset, test_dataset = dataset.return_splits(from_id=False,
csv_path='{}/splits_{}.csv'.format(args.split_dir, i))
# train_loader = get_split_loader(train_dataset)
datasets = (train_dataset, val_dataset, test_dataset)
results, test_auc, val_auc, test_acc, val_acc, test_loss, val_loss = train(datasets, i, args)
all_test_auc.append(test_auc)
all_val_auc.append(val_auc)
all_test_acc.append(test_acc)
all_val_acc.append(val_acc)
all_test_loss.append(test_loss)
all_val_loss.append(val_loss)
#write results to pkl
filename = os.path.join(args.results_dir, 'split_{}_results.pkl'.format(i))
save_pkl(filename, results)
"Compute the average and std of the metrics"
test_auc_ave= np.mean(all_test_auc)
test_acc_ave= np.mean(all_test_acc)
test_loss_ave= np.mean(all_test_loss)
test_auc_std= np.std(all_test_auc)
test_acc_std= np.std(all_test_acc)
test_loss_std= np.std(all_test_loss)
val_auc_ave= np.mean(all_val_auc)
val_acc_ave= np.mean(all_val_acc)
val_loss_ave= np.mean(all_val_loss)
val_auc_std= np.std(all_val_auc)
val_acc_std= np.std(all_val_acc)
val_loss_std= np.std(all_val_loss)
final_df = pd.DataFrame({'folds': folds, 'test_auc': all_test_auc,
'val_auc': all_val_auc, 'test_acc': all_test_acc, 'val_acc' : all_val_acc, 'test_loss' : all_test_loss, 'val_loss' : all_val_loss})
if len(folds) != args.k:
save_name = 'summary_partial_{}_{}.csv'.format(start, end)
else:
save_name = 'summary.csv'
final_df.to_csv(os.path.join(args.results_dir, save_name))
print('\n Val:\n loss ± std: {0:.3f} ± {1:.3f}'.format(val_loss_ave, val_loss_std))
print('\n\n Test:\n auc ± std: {0:.3f} ± {1:.3f}, acc ± std: {2:.3f} ± {3:.3f}'.format(
test_auc_ave, test_auc_std, test_acc_ave, test_acc_std))
print('\n\n Misc:\n auc ± std (val): {0:.3f} ± {1:.3f}, acc ± std (val): {2:.3f} ± {3:.3f},'
'loss ± std (test): {4:.3f} ± {5:.3f} \n'.format(val_auc_ave, val_auc_std, val_acc_ave, val_acc_std, test_loss_ave, test_loss_std))
# Generic training settings
parser = argparse.ArgumentParser(description='Configurations for WSI Training')
parser.add_argument('--data_root_dir', type=str, default=None,
help='data directory')
parser.add_argument('--max_epochs', type=int, default=50,
help='maximum number of epochs to train (default: 50)')
parser.add_argument('--lr', type=float, default=1e-4,
help='learning rate (default: 0.0001)')
parser.add_argument('--label_frac', type=float, default=1.0,
help='fraction of training labels (default: 1.0)')
parser.add_argument('--reg', type=float, default=1e-2,
help='weight decay (default: 1e-5)')
parser.add_argument('--seed', type=int, default=1,
help='random seed for reproducible experiment (default: 1)')
parser.add_argument('--k', type=int, default=5, help='number of folds (default: 10)')
parser.add_argument('--k_start', type=int, default=-1, help='start fold (default: -1, last fold)')
parser.add_argument('--k_end', type=int, default=5, help='end fold (default: -1, first fold)')
parser.add_argument('--results_dir', default='./results', help='results directory (default: ./results)')
parser.add_argument('--split_dir', type=str, default=None,
help='manually specify the set of splits to use, '
+'instead of infering from the task and label_frac argument (default: None)')
parser.add_argument('--log_data', action='store_true', default=True, help='log data using tensorboard')
parser.add_argument('--testing', action='store_true', default=False, help='debugging tool')
parser.add_argument('--early_stopping', action='store_true', default=False, help='enable early stopping')
parser.add_argument('--opt', type=str, choices = ['adam', 'sgd'], default='adam')
parser.add_argument('--drop_out', action='store_true', default=False, help='enabel dropout (p=0.25)')
parser.add_argument('--bag_loss', type=str, choices=['svm', 'ce'], default='ce',
help='slide-level classification loss function (default: ce)')
parser.add_argument('--model_type', type=str, choices=['clam_sb', 'clam_sb_add', 'clam_mb', 'max_pool', 'mean_pool', 'max_pool_ins', 'mean_pool_ins', 'abmil', 'abmil_add', 'abmil_con', 'madmil', 'madmil_diff', 'madmil_class','dtfd', 'acmil', 'acmil_add'], default='clam_sb',
help='type of model (default: clam_sb, clam w/ single attention branch)')
parser.add_argument('--exp_code', type=str, help='experiment code for saving results')
parser.add_argument('--weighted_sample', action='store_true', default=False, help='enable weighted sampling')
parser.add_argument('--model_size', type=str, choices=['small', 'big', 'tiny'], default='small', help='size of model, does not affect mil')
parser.add_argument('--task', type=str, choices=['task_1_tumor_vs_normal', 'task_2_tumor_subtyping'])
### CLAM specific options
parser.add_argument('--no_inst_cluster', action='store_true', default=False,
help='disable instance-level clustering')
parser.add_argument('--inst_loss', type=str, choices=['svm', 'ce', None], default=None,
help='instance-level clustering loss function (default: None)')
parser.add_argument('--subtyping', action='store_true', default=False,
help='subtyping problem')
parser.add_argument('--bag_weight', type=float, default=1.0,
help='clam: weight coefficient for bag-level loss (default: 0.7)')
parser.add_argument('--B', type=int, default=8, help='numbr of positive/negative patches to sample for clam')
### MAD-MIL specific options
parser.add_argument('--n', type= int, default= 2, help='number of heads in the attention network')
parser.add_argument('--head_size', type= str, default= "small" , help='dimension of each head in the attention network')
### ACMIL specific options
parser.add_argument('--n_token', type= int, default= 1, help='number of tokens')
parser.add_argument('--n_masked_patch', type= int, default= 10 , help='number of masked patches')
### DTFD specific options
parser.add_argument('--drop_out2', type= float, default= 0.25, help='dropout value')
parser.add_argument('--mDim', type= int, default= 512, help='dimension of the compressed feature')
parser.add_argument('--distill', type=str, choices=['MaxMinS','MaxS', 'AFS'], default='AFS', help='distillation loss')
parser.add_argument('--in_chn', type= int, default= 1024, help='dimension of the input feature')
args = parser.parse_args()
device=torch.device("cuda" if torch.cuda.is_available() else "cpu")
args.drop_out=True
args.early_stopping =True
args.weighted_sample =True
args.inst_loss ="svm"
args.task = "task_1_tumor_vs_normal"
args.split_dir = "task_camelyon16/"
args.csv_path = './dataset_csv/camelyon16.csv'
args.data_root_dir = "/home/bme001/20215294/Data/CAM/Cam_ostu_20x/"
sub_feat_dir = 'feat'
args.use_drop_out = True
args.bag_weight = 0.7
args.seed = 2021
args.k = 5
args.k_end = 5
args.subtyping= False
def seed_torch(seed=7):
import random
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
if device.type == 'cuda':
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed) # if you are using multi-GPU.
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
settings = {'num_splits': args.k,
'k_start': args.k_start,
'k_end': args.k_end,
'task': args.task,
'max_epochs': args.max_epochs,
'results_dir': args.results_dir,
'lr': args.lr,
'experiment': args.exp_code,
'reg': args.reg,
'label_frac': args.label_frac,
'bag_loss': args.bag_loss,
'seed': args.seed,
'model_type': args.model_type,
'model_size': args.model_size,
"use_drop_out": args.drop_out,
'weighted_sample': args.weighted_sample,
'opt': args.opt,
'n': args.n,
'head_size': args.head_size,
'n_token': args.n_token,
'n_masked_patch': args.n_masked_patch,
'in_chn': args.in_chn,
'drop_out2': args.drop_out2,
'mDim': args.mDim,}
if args.model_type in ['clam_sb', 'clam_sb_add', 'clam_mb']:
settings.update({'bag_weight': args.bag_weight,
'inst_loss': args.inst_loss,
'B': args.B})
print('\nLoad Dataset')
if args.task == 'task_1_tumor_vs_normal':
args.n_classes=2
dataset = Generic_MIL_Dataset(
csv_path = args.csv_path,
data_dir = os.path.join(args.data_root_dir, sub_feat_dir),
shuffle = False,
seed = args.seed,
print_info = True,
label_dict={0: 0, 1: 1},
patient_strat=False,
ignore=[])
elif args.task == 'task_2_tumor_subtyping':
args.n_classes=2
dataset = Generic_MIL_Dataset(
csv_path = args.csv_path,
data_dir = os.path.join(args.data_root_dir, sub_feat_dir),
shuffle = False,
seed = args.seed,
print_info = True,
label_dict={0: 0, 1: 1},
patient_strat=False,
ignore=[])
if args.model_type in ['clam_sb', 'clam_sb_add', 'clam_mb']:
assert args.subtyping
else:
raise NotImplementedError
if not os.path.isdir(args.results_dir):
os.mkdir(args.results_dir)
args.results_dir = os.path.join(args.results_dir, str(args.exp_code) + '_s{}'.format(args.seed))
if not os.path.isdir(args.results_dir):
os.mkdir(args.results_dir)
if args.split_dir is None:
args.split_dir = os.path.join('splits', args.task+'_{}'.format(int(args.label_frac*100)))
else:
args.split_dir = os.path.join('splits', args.split_dir)
print('split_dir: ', args.split_dir)
assert os.path.isdir(args.split_dir)
settings.update({'split_dir': args.split_dir})
with open(args.results_dir + '/experiment_{}.txt'.format(args.exp_code), 'w') as f:
print(settings, file=f)
f.close()
print("################# Settings ###################")
for key, val in settings.items():
print("{}: {}".format(key, val))
if __name__ == "__main__":
# pdb.set_trace()
results = main(args)
print("finished!")
print("end script")