-
Notifications
You must be signed in to change notification settings - Fork 5
/
train_sra.py
182 lines (161 loc) · 7.35 KB
/
train_sra.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
import argparse
import numpy as np
from model.utils import get_logger
from model.transform import get_supervised_train_augmentation, get_supervised_val_augmentation, TwoCropsTransform
from model.sra import SRA
from model.sra_trainer import SRATrainer
from dataset.builder import build_dataset
from torch.utils.data import WeightedRandomSampler
from torch.utils.data import DataLoader, ConcatDataset
def main(args):
logger = get_logger('{}.log'.format(args.exp_name))
logger.debug(args)
# Define data augmentation
transform_train = get_supervised_train_augmentation(heavy=True)
transform_val = get_supervised_val_augmentation()
logger.debug("Transform train:\n{}".format(transform_train))
logger.debug("Transform validation:\n{}".format(transform_val))
dataset_paths = args.root.split(':')
use_lind = bool(args.lind == 'yes')
use_lcrd = bool(args.lcrd == 'yes')
use_e2h = bool(args.e2h == 'yes')
use_moco = bool(args.moco == 'yes')
use_me = bool(args.me == 'yes')
mi = bool(args.mi == 'yes')
ms = bool(args.ms == 'yes')
dataset_train = []
dataset_val = []
for i, p in enumerate(dataset_paths):
# Create dataset
logger.debug("Load dataset {} from: {}".format(i, p))
dataset_train_, dataset_val_, _ = build_dataset(
path=p,
transform_train=TwoCropsTransform(transform_train),
transform_val=TwoCropsTransform(transform_val),
)
# Remap dataset labels to current dataset index
new_classes = {c: ('D{}'.format(i), i) for c in dataset_train_.classes}
dataset_train_.remap_classes(new_classes)
dataset_val_.remap_classes(new_classes)
# Limit size of dataset to the expected number of samples
dataset_train.append(dataset_train_)
dataset_val.append(dataset_val_)
# Create weighted sampler (sample the same amount of example from both sets)
ratio_samples = [int(r) for r in args.ratio_samples.split(':')]
weights = np.concatenate([(r/len(d))*np.ones(len(d)) for r, d in zip(ratio_samples, dataset_train)])
weights *= len(weights)
sampler = WeightedRandomSampler(weights, args.num_samples, replacement=True)
loader_train = DataLoader(
dataset=ConcatDataset(dataset_train), batch_size=args.bs, num_workers=args.j, sampler=sampler, drop_last=True)
loader_val = DataLoader(
dataset=ConcatDataset(dataset_val), batch_size=args.bs, num_workers=args.j, shuffle=True, drop_last=True)
# Define model and train it
model = SRA(
dim=args.moco_dim,
K=args.moco_k,
m=args.moco_m,
T=args.moco_t,
n_dataset=len(dataset_train),
device=args.device,
mean_entropy=use_me,
mi=mi,
ms=ms,
)
trainer = SRATrainer(
model=model,
train_loader=loader_train,
val_loader=loader_val,
use_lind=use_lind,
use_lcrd=use_lcrd,
use_e2h=use_e2h,
use_moco=use_moco,
opt_lr=args.lr,
opt_momentum=args.momentum,
opt_weight_decay=args.wd,
t_max=args.epochs,
sh=args.sh,
sw=args.sw,
checkpoint_epochs=args.checkpoint_epochs,
device=args.device,
prefix=args.exp_name,
logger=logger,
)
trainer.train(n_epochs=args.epochs)
if __name__ == '__main__':
"""
Train SRA model
"""
parser = argparse.ArgumentParser(
description='Train model on histological data')
parser.add_argument('--root', type=str,
default='',
help='Path to source dataset')
parser.add_argument('--exp_name', type=str,
default='sra',
help='Name of the experiment that will appear in logging.')
parser.add_argument('--num_samples', type=int, default=100000,
help='Number of samples to draw from dataset per epoch')
parser.add_argument('--ratio_samples', type=str,
default="1:1:2",
help='Samples ratio')
# --------- Trainer settings
parser.add_argument('--device', default="cuda", type=str,
help='Either cpu or cuda.')
parser.add_argument('--epochs', default=200, type=int,
help='number of total epochs to run')
parser.add_argument('--lr', default=0.03, type=float,
help='initial learning rate', dest='lr')
parser.add_argument('--momentum', default=0.9, type=float,
help='momentum of SGD solver')
parser.add_argument('--wd', default=1e-4, type=float,
help='weight decay (default: 1e-4)')
parser.add_argument('--j', default=4, type=int,
help='number of data loading workers (default: 4)')
parser.add_argument('--bs', default=128, type=int,
help='mini-batch size (default: 128)')
parser.add_argument('--sh', default=0.15, type=float, metavar='N',
help='Simple to hard height for step function update')
parser.add_argument('--sw', default=0.25, type=float, metavar='N',
help='Simple to hard width for step function update')
parser.add_argument('--checkpoint_epochs', default=50, type=int,
help='manual epoch number (useful on restarts)')
parser.add_argument('--moco', type=str,
default='no',
choices=['yes', 'no'],
help='Use moco loss')
parser.add_argument('--lind', type=str,
default='yes',
choices=['yes', 'no'],
help='Use in-domain learning procedure')
parser.add_argument('--lcrd', type=str,
default='yes',
choices=['yes', 'no'],
help='Use cross-domain learning procedure')
parser.add_argument('--e2h', type=str,
default='yes',
choices=['yes', 'no'],
help='Use easy-to-hard learning procedure')
parser.add_argument('--me', type=str,
default='yes',
choices=['yes', 'no'],
help='Compute mean entropy.')
parser.add_argument('--mi', type=str,
default='no',
choices=['yes', 'no'],
help='If true, we optimize the in-domain for each domain. If false optimize all domain as one.')
parser.add_argument('--ms', type=str,
default='no',
choices=['yes', 'no'],
help='If true, we optimize the cross-domain for each domain. If false optimize all domain as '
'one.')
# --------- MoCo settings
parser.add_argument('--moco-dim', default=128, type=int,
help='feature dimension (default: 128)')
parser.add_argument('--moco-k', default=65536, type=int,
help='queue size; number of negative keys (default: 65536)')
parser.add_argument('--moco-m', default=0.999, type=float,
help='moco momentum of updating key encoder (default: 0.999)')
parser.add_argument('--moco-t', default=0.2, type=float,
help='softmax temperature (default: 0.2)')
args = parser.parse_args()
main(args)