-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_covid.py
274 lines (240 loc) · 10.1 KB
/
main_covid.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
import argparse
import csv
import json
import os
import shutil
import copy
import torch
from termcolor import colored
from torch.utils.tensorboard import SummaryWriter
from lr_schedule import InvScheduler
from model.contrastive_loss import supervised_loss,InfoNCELoss,GaussianKernel
from model.key_memory import KeyMemory
from model.model import ImageClassifier
from pseudo_labeler import KMeansPseudoLabeler
from train_covid import Train
from utils import configure, get_dataset_name, moment_update, str2bool,SupConLoss
import utils
parser = argparse.ArgumentParser()
# dataset configurations
parser.add_argument('--config',
type=str,
default='config/config.yml',
help='Dataset configuration parameters')
# datapath
parser.add_argument('--src', default='./all_data_pneumonia/', type=str, help='data path of source data')
parser.add_argument('--label_file_source', default='./COVID-DA-master/data/pneumonia_task_for_python3.pkl', type=str,
help='the pkl file that records the source data list and labels')
parser.add_argument('--tgt', default='./all_data_covid/', type=str, help='data path of target data')
parser.add_argument('--label_file_target', default='./COVID-DA-master/data/COVID-19_task_for_python3.pkl', type=str,
help='the pkl file that records the target data list and labels')
# training configurations
parser.add_argument('--batch_size',
type=int,
default=32,
help='Batch size for both training and evaluation')
parser.add_argument('--eval_batch_size',
type=int,
default=32,
help='Batch size for both training and evaluation')
parser.add_argument('--pseudo_batch_size',
type=int,
default=4096,
help='Batch size for pseudo labeling')
parser.add_argument('--max_iterations',
type=int,
default=20000,
help='Maximum number of iterations')
parser.add_argument('--num_classes',
type=int,
default=2,
help='num of classes')
# logging configurations
parser.add_argument('--log_dir',
type=str,
default='logs',
help='Parent directory for log files')
parser.add_argument('--log_summary_interval',
type=int,
default=100,
help='Logging summaries frequency')
parser.add_argument('--log_image_interval',
type=int,
default=1000,
help='Logging images frequency')
parser.add_argument('--acc_file',
type=str,
default='hyper_search.csv', # 'result.txt'
help='File where accuracies are wrote')
# resource configurations
parser.add_argument('--num_workers',
type=int,
default=1,
help='Number of workers')
# InfoNCE loss configurations
parser.add_argument('--temperature',
type=float,
default=0.07,
help='Temperature parameter for InfoNCE loss')
# hyper-parameters
parser.add_argument('--cw',
type=float,
default=1,
help='Weight for NCE contrast loss')
parser.add_argument('--max_key_size',
type=int,
default=200,
help='Maximum number of key feature size computed in the model')
parser.add_argument('--min_conf_samples',
type=int,
default=1,
help='Minimum number of samples per confident target class')
parser.add_argument('--kcc',
type=int,
default=3,
help='the lcc')
# model configurations
parser.add_argument('--network',
type=str,
default='resnet18', # resnet101
help='Base network architecture')
parser.add_argument('--contrast_dim',
type=int,
default=128,
help='contrast layer dimension')
parser.add_argument('--alpha',
type=float,
default=0.9,
help='momentum coefficient for model ema')
# optimizer configurations
parser.add_argument('--optimizer',
type=str,
default='sgd',
help='Optimizer type')
parser.add_argument('--lr',
type=float,
default=0.001,
help='Initial learning rate')
parser.add_argument('--momentum',
type=float,
default=0.9,
help='Optimizer parameter, momentum')
parser.add_argument('--weight_decay',
type=float,
default=0.0005,
help='Optimizer parameter, weight decay')
parser.add_argument('--nesterov',
type=str2bool,
default=False, # True
help='Optimizer parameter, nesterov')
# learning rate scheduler configurations
parser.add_argument('--lr_scheduler',
type=str,
default='inv',
help='Learning rate scheduler type')
parser.add_argument('--gamma',
type=float,
default=0.001,
help='Inv learning rate scheduler parameter, gamma')
parser.add_argument('--decay_rate',
type=float,
default=0.75, #
help='Inv learning rate scheduler parameter, decay rate')
parser.add_argument('--non-linear', default=False, action='store_true',
help='whether not use the linear version')
parser.add_argument("--module", type=str, default='contrastive_loss', choices=['contrastive_loss', 'source_only'],
help="When module is 'contrastive_loss', it is our method.")
def main():
args = parser.parse_args()
print(args)
config = configure(args.config)
os.environ['CUDA_DEVICE_ORDER'] = 'PCI_BUS_ID'
# define model name
setup_list = ['covid19',
args.module,
args.network,
f"contrast_dim_{args.contrast_dim}",
f"maxiter_{args.max_iterations}",
f"batchsize_{args.batch_size}",
f"augment_{args.cw}",
f"kcc_{args.kcc}"
]
model_name = "_".join(setup_list)
print(colored(f"Model name: {model_name}", 'green'))
model_dir = os.path.join(args.log_dir, model_name)
if os.path.isdir(model_dir):
shutil.rmtree(model_dir)
os.mkdir(model_dir)
summary_writer = SummaryWriter(model_dir)
# save parsed arguments
with open(os.path.join(model_dir, 'parsed_args.txt'), 'w') as f:
json.dump(args.__dict__, f, indent=2)
criterion = SupConLoss(temperature=0.07)
backbone = utils.get_model(args.network)
pool_layer = None
model = ImageClassifier(backbone, args.num_classes, bottleneck_dim=args.contrast_dim,
pool_layer=pool_layer).cuda()
backbone_ema = utils.get_model(args.network)
model_ema = ImageClassifier(backbone_ema, args.num_classes, bottleneck_dim=args.contrast_dim,
pool_layer=pool_layer).cuda()
moment_update(model, model_ema, 0)
model = model.cuda()
model_ema = model_ema.cuda()
contrast_loss = InfoNCELoss(temperature=args.temperature).cuda()
max_key_size=args.max_key_size*args.num_classes
src_memory = KeyMemory(max_key_size, args.contrast_dim,args.num_classes).cuda()
tgt_memory = KeyMemory(max_key_size, args.contrast_dim,args.num_classes).cuda()
tgt_pseudo_labeler = KMeansPseudoLabeler(num_classes=args.num_classes,
batch_size=args.pseudo_batch_size)
parameters = model.get_parameter_list()
group_ratios = [parameter['lr'] for parameter in parameters]
optimizer = torch.optim.SGD(parameters,
lr=args.lr,
momentum=args.momentum,
weight_decay=args.weight_decay,
nesterov=args.nesterov)
assert args.lr_scheduler == 'inv'
lr_scheduler = InvScheduler(gamma=args.gamma,
decay_rate=args.decay_rate,
group_ratios=group_ratios,
init_lr=args.lr)
trainer = Train(model, model_ema, optimizer, lr_scheduler, model_dir,
summary_writer, args.src, args.tgt, args.label_file_source,args.label_file_target,contrast_loss,supervised_loss,src_memory, tgt_memory, tgt_pseudo_labeler,criterion,
cw=args.cw,
min_conf_samples=args.min_conf_samples,
num_classes=args.num_classes,
batch_size=args.batch_size,
eval_batch_size=args.eval_batch_size,
num_workers=args.num_workers,
max_iter=args.max_iterations,
iters_per_epoch=100,
log_summary_interval=args.log_summary_interval,
log_image_interval=args.log_image_interval,
acc_metric='total_mean',
alpha=args.alpha,module=args.module,kcc=args.kcc)
tgt_best_acc = trainer.train()
# write to text file
# with open(args.acc_file, 'a') as f:
# f.write(model_name + ' ' + str(tgt_best_acc) + '\n')
# f.close()
# write to xlsx file
write_list = [
args.src,
args.tgt,
args.network,
args.contrast_dim,
args.temperature,
args.alpha,
args.cw,
args.max_key_size,
args.module,
args.batch_size,
args.min_conf_samples,
args.gpu,
tgt_best_acc
]
with open(args.acc_file, 'a') as f:
csv_writer = csv.writer(f)
csv_writer.writerow(write_list)
if __name__ == '__main__':
main()