-
Notifications
You must be signed in to change notification settings - Fork 1
/
train-2net.py
188 lines (163 loc) · 7.04 KB
/
train-2net.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
import os
import pytorch_lightning as pl
import torch
import yaml
import argparse
from pytorch_lightning.loggers import WandbLogger
from models.agreement_score import ClassificationAgreementScore
from models.tasks import CIFARClassificationTask, CIFAREmbeddingClassificationTask
from models.taskness_score import TwoSupervisedClassifiers
from models.supervised import TwoSupervisedModels
from datautils import MyCIFAR10DataModule
from tiny_imagenet import TinyImageNetDataModule
import utils
config_parser = argparse.ArgumentParser(description='Training Config', add_help=False)
config_parser.add_argument('-c', '--config', default='', type=str, metavar='FILE',
help='YAML config file specifying default arguments')
parser = argparse.ArgumentParser()
parser.add_argument('--name', type=str, default='tmp')
parser.add_argument('--notes', type=str, default='')
parser.add_argument('--group', type=str, default='ats')
parser.add_argument('--tags', type=str, nargs='*', default=[])
parser.add_argument('--tmp', dest='tmp', action='store_true')
parser.set_defaults(tmp=False)
parser.add_argument('--task', type=str, default='classification')
parser.add_argument('--task_net', type=str, default='')
parser.add_argument('--task_arch', type=str, default='resnet18')
parser.add_argument('--task_type', type=str, default='real')
parser.add_argument('--task_idx', type=int, default=0)
parser.add_argument('--task_mix', type=str, default='')
parser.add_argument('--task_ckpts', type=str, nargs='*', default=[])
parser.add_argument('--task_h_dim', type=int, default=512)
parser.add_argument('--task_out_type', type=str, default='class')
parser.add_argument('--emb_lin_task', type=str, default='learned')
parser.add_argument('--n_classes', type=int, default=2)
parser.add_argument('--seed', type=int, default=0)
parser.add_argument('--noise', type=float, default=None)
parser.add_argument('--dataset', type=str, default='cifar10')
parser.add_argument('--entity', type=str, default='task-discovery')
parser.add_argument('--project', type=str, default='task-discovery-repo')
# Trainer
parser.add_argument('--nologger', action='store_true', default=False)
parser.add_argument('--save_ckpt', action='store_true', default=False)
parser.add_argument('--save_dir', type=str, default=None)
parser = TwoSupervisedModels.add_model_specific_args(parser)
parser = pl.Trainer.add_argparse_args(parser)
parser = MyCIFAR10DataModule.add_argparse_args(parser)
parser.set_defaults(random_labelling=False)
parser.set_defaults(val_split=0.1)
parser.set_defaults(automatic_optimization=True)
parser.set_defaults(shuffle=True)
# first, load config if any
args_config, remaining = config_parser.parse_known_args()
if args_config.config:
with open(args_config.config, 'r') as f:
cfg = yaml.safe_load(f)
parser.set_defaults(**cfg)
# The main arg parser parses the rest of the args, the usual
# defaults will have been overridden if config file specified.
args = parser.parse_args(remaining)
utils.set_seeds(args.seed)
# Define datamodule
tmp_data_module = MyCIFAR10DataModule(
data_dir=os.environ.get('DATA_ROOT', os.getcwd()),
**{k: v for k, v in vars(args).items() if k not in ['data_dir', 'val_split', 'drop_last']},
drop_last=False,
)
tmp_data_module.setup()
if args.dataset == 'cifar10':
data_module = MyCIFAR10DataModule(
data_dir=os.environ.get('DATA_ROOT', os.getcwd()),
**{k: v for k, v in vars(args).items() if k not in ['data_dir']}
)
elif args.dataset == 'tiny_imagenet':
data_module = TinyImageNetDataModule(
data_dir=os.environ.get('DATA_ROOT', os.getcwd()),
**{k: v for k, v in vars(args).items() if k not in ['data_dir', 'val_split']}
)
elif args.dataset == 'tiny_imagenet_64':
data_module = TinyImageNetDataModule(
data_dir=os.environ.get('DATA_ROOT', os.getcwd()),
image_size=64,
**{k: v for k, v in vars(args).items() if k not in ['data_dir', 'val_split']}
)
# Defining a task
if args.task == 'classification':
if args.task_type == 'emb':
task_fn = lambda: CIFAREmbeddingClassificationTask(
h_dim=args.task_h_dim,
in_dim=data_module.dims,
out_type=args.task_out_type,
arch=args.task_arch,
n_classes=args.n_classes,
)
else:
assert args.task_out_type == 'class'
task_fn = lambda: CIFARClassificationTask(
task_type=args.task_type,
task_idx=args.task_idx,
dataset=args.dataset,
n_classes=args.n_classes,
)
agreement_score = ClassificationAgreementScore()
task = task_fn()
if len(args.task_ckpts) == 1 and args.task_ckpts[0] != '':
if args.task_type == 'emb':
from models.as_uniformity import ASUniformityTraining
model = ASUniformityTraining.load_from_checkpoint(args.task_ckpts[0], dataset=args.dataset, arch=args.task_arch)
if args.emb_lin_task == 'learned':
model.set_task(idx=args.task_idx)
elif args.emb_lin_task == 'random':
model.set_random_task(seed=args.task_idx)
else:
raise ValueError(f'{args.emb_lin_task=}')
task.encoder.backbone.load_state_dict(model.encoder.backbone.state_dict())
task.encoder.projector.load_state_dict(model.encoder.projector.state_dict())
if args.n_classes != model.hparams.get('n_classes', 2):
print(f'===> !!!! Warning !!! {args.n_classes} != {model.hparams.get("n_classes", 2)}')
ws = utils.random_k_way_linear_task(args.n_classes, model.hparams.h_dim, args.task_idx)
task.encoder.classifier.weight.copy_(model.hparams.task_temp * torch.FloatTensor(ws.T))
else:
task.encoder.classifier.load_state_dict(model.encoder.classifier.state_dict())
else:
task.load_state_dict(torch.load(args.task_ckpts[0]))
elif len(args.task_ckpts) > 1:
raise RuntimeError
for p in task.parameters():
p.requires_grad = False
task.eval()
# Two models module
task_discovery_model = TwoSupervisedModels(
**{k: v for k, v in vars(args).items() if k not in ['task']},
agreement_score=agreement_score,
task=task,
in_dim=data_module.dims[0],
)
name = ('tmp-' if args.tmp else '') + args.name.format(**vars(args))
if not args.nologger:
logger = WandbLogger(
name=name,
project=args.project,
entity=args.entity,
save_dir=args.save_dir if not args.tmp else '/tmp/exps/',
tags=['ats'] + args.tags,
group=args.group.format(**vars(args)),
notes=args.notes
)
else:
logger = None
trainer = pl.Trainer(
gpus=torch.cuda.device_count(),
logger=logger,
log_every_n_steps=args.log_every_n_steps,
max_epochs=args.max_epochs,
max_steps=args.max_steps,
val_check_interval=args.val_check_interval,
check_val_every_n_epoch=args.check_val_every_n_epoch,
limit_val_batches=args.limit_val_batches,
deterministic=args.deterministic,
checkpoint_callback=args.save_ckpt,
default_root_dir=args.save_dir,
)
trainer.fit(task_discovery_model, datamodule=data_module)
trainer.test(task_discovery_model, datamodule=data_module)