-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_pretrain.py
executable file
·222 lines (187 loc) · 6.83 KB
/
main_pretrain.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
import os
import types
from pprint import pprint
import torch
from pytorch_lightning import Trainer
from pytorch_lightning import seed_everything
from pytorch_lightning.callbacks import LearningRateMonitor
from pytorch_lightning.loggers import WandbLogger
from semissl.args.setup import parse_args_pretrain
from semissl.methods import METHODS
from semissl.semi import SEMISUPERVISED
try:
from semissl.methods.dali import PretrainABC
except ImportError:
_dali_avaliable = False
else:
_dali_avaliable = True
try:
from semissl.utils.auto_umap import AutoUMAP
except ImportError:
_umap_available = False
else:
_umap_available = True
from semissl.utils.checkpointer import Checkpointer
from semissl.utils.classification_dataloader import (
prepare_data as prepare_data_classification,
)
from semissl.utils.pretrain_dataloader import mask_dataset
from semissl.utils.pretrain_dataloader import prepare_dataloader
from semissl.utils.pretrain_dataloader import prepare_datasets
from semissl.utils.pretrain_dataloader import prepare_multicrop_transform
from semissl.utils.pretrain_dataloader import prepare_n_crop_transform
from semissl.utils.pretrain_dataloader import prepare_transform
def main():
seed_everything(5)
args = parse_args_pretrain()
# online eval dataset reloads when task dataset is over
args.multiple_trainloader_mode = "max_size_cycle"
# set online eval batch size and num workers
args.online_eval_batch_size = (
None # int(args.batch_size) if args.dataset == "cifar100" else None
)
# pretrain and online eval dataloaders
if not args.dali:
# asymmetric augmentations
if args.unique_augs > 1:
transform = [
prepare_transform(args.dataset, multicrop=args.multicrop, **kwargs)
for kwargs in args.transform_kwargs
]
else:
transform = prepare_transform(
args.dataset, multicrop=args.multicrop, **args.transform_kwargs
)
if args.debug_augmentations:
print("Transforms:")
pprint(transform)
if args.multicrop:
assert not args.unique_augs == 1
if args.dataset in ["cifar10", "cifar100"]:
size_crops = [32, 24]
elif args.dataset == "stl10":
size_crops = [96, 58]
# imagenet or custom dataset
else:
size_crops = [224, 96]
transform = prepare_multicrop_transform(
transform,
size_crops=size_crops,
num_crops=[args.num_crops, args.num_small_crops],
)
else:
if args.num_crops != 2:
assert args.method == "wmse"
online_eval_transform = (
transform[-1] if isinstance(transform, list) else transform
)
task_transform = prepare_n_crop_transform(
transform, num_crops=args.num_crops
)
task_dataset, online_eval_dataset = prepare_datasets(
args.dataset,
task_transform=task_transform,
online_eval_transform=online_eval_transform,
data_dir=args.data_dir,
train_dir=args.train_dir,
no_labels=args.no_labels,
)
task_dataset, label_task_dataset = mask_dataset(
task_dataset, args.dataset, args.semi_rate
)
label_loader = prepare_dataloader(
label_task_dataset,
batch_size=max(
args.batch_size // 4,
int(args.batch_size * len(label_task_dataset) / len(task_dataset)),
),
num_workers=args.num_workers,
)
task_loader = prepare_dataloader(
task_dataset,
batch_size=args.batch_size,
num_workers=args.num_workers,
)
train_loaders = {"ssl": task_loader, "semi": label_loader}
if args.online_eval_batch_size:
online_eval_loader = prepare_dataloader(
online_eval_dataset,
batch_size=args.online_eval_batch_size,
num_workers=args.num_workers,
)
train_loaders.update({"online_eval": online_eval_loader})
# normal dataloader for when it is available
if args.dataset == "custom" and (args.no_labels or args.val_dir is None):
val_loader = None
elif args.dataset in ["imagenet100", "imagenet"] and args.val_dir is None:
val_loader = None
else:
_, val_loader = prepare_data_classification(
args.dataset,
data_dir=args.data_dir,
train_dir=args.train_dir,
val_dir=args.val_dir,
batch_size=args.batch_size,
num_workers=args.num_workers,
)
# check method
assert args.method in METHODS, f"Choose from {METHODS.keys()}"
# build method
MethodClass = METHODS[args.method]
if args.dali:
assert (
_dali_avaliable
), "Dali is not currently avaiable, please install it first with [dali]."
MethodClass = types.new_class(
f"Dali{MethodClass.__name__}", (PretrainABC, MethodClass)
)
if args.semissl:
MethodClass = SEMISUPERVISED[args.semissl](MethodClass)
model: torch.nn.Module = MethodClass(
**args.__dict__, n_class=10 if args.dataset.lower() == "cifar10" else 100
)
callbacks = []
# wandb logging
wandb_logger = True
if args.wandb:
wandb_logger = WandbLogger(
name=f"{args.name}",
project=args.project,
entity=args.entity,
offline=args.offline,
reinit=True,
log_model=True,
)
if args.resume_from_checkpoint is None:
wandb_logger.watch(model, log="gradients", log_freq=100)
wandb_logger.log_hyperparams(args)
# lr logging
lr_monitor = LearningRateMonitor(logging_interval="epoch")
callbacks.append(lr_monitor)
if args.save_checkpoint:
# save checkpoint on last epoch only
ckpt = Checkpointer(
args,
logdir=args.checkpoint_dir,
frequency=args.checkpoint_frequency,
)
callbacks.append(ckpt)
if args.auto_umap:
assert (
_umap_available
), "UMAP is not currently avaiable, please install it first with [umap]."
auto_umap = AutoUMAP(
args,
logdir=os.path.join(args.auto_umap_dir, args.method),
frequency=args.auto_umap_frequency,
)
callbacks.append(auto_umap)
trainer: Trainer = Trainer.from_argparse_args(
args, logger=wandb_logger, callbacks=callbacks, log_every_n_steps=25
)
if args.dali:
trainer.fit(model, val_dataloaders=val_loader)
else:
trainer.fit(model, train_loaders, val_loader)
if __name__ == "__main__":
main()