-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
165 lines (150 loc) · 7.01 KB
/
main.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
#!coding:utf-8
import os
import torch
from torch import optim
from torch.optim import lr_scheduler
from torch.utils.data.sampler import BatchSampler, SubsetRandomSampler
from utils import datasets
from utils.ramps import exp_warmup
from utils.config import parse_commandline_args
from utils.data_utils import DataSetWarpper
from utils.data_utils import TwoStreamBatchSampler
from utils.data_utils import TransformTwice as twice
from architectures.arch import arch
from trainer import *
build_model = {
'mtv1': MeanTeacherv1.Trainer,
'mtv2': MeanTeacherv2.Trainer,
'piv1': PIv1.Trainer,
'piv2': PIv2.Trainer,
'vatv1': VATv1.Trainer,
'vatv2': VATv2.Trainer,
'epslab2013v1': ePseudoLabel2013v1.Trainer,
'epslab2013v2': ePseudoLabel2013v2.Trainer,
'ipslab2013v1': iPseudoLabel2013v1.Trainer,
'ipslab2013v2': iPseudoLabel2013v2.Trainer,
'etempensv1': eTempensv1.Trainer,
'etempensv2': eTempensv2.Trainer,
'itempensv1': iTempensv1.Trainer,
'itempensv2': iTempensv2.Trainer,
'ictv1': ICTv1.Trainer,
'ictv2': ICTv2.Trainer,
'mixmatch': MixMatch.Trainer,
'ifixmatch': iFixMatch.Trainer,
'efixmatch': eFixMatch.Trainer,
'emixpslabv1': eMixPseudoLabelv1.Trainer,
'emixpslabv2': eMixPseudoLabelv2.Trainer,
}
def create_loaders_v1(trainset, evalset, label_idxs, unlab_idxs,
num_classes,
config):
if config.data_twice: trainset.transform = twice(trainset.transform)
if config.data_idxs: trainset = DataSetWarpper(trainset, num_classes)
## two-stream batch loader
batch_size = config.sup_batch_size + config.usp_batch_size
batch_sampler = TwoStreamBatchSampler(
unlab_idxs, label_idxs, batch_size, config.sup_batch_size)
train_loader = torch.utils.data.DataLoader(trainset,
batch_sampler=batch_sampler,
num_workers=config.workers,
pin_memory=True)
## test batch loader
eval_loader = torch.utils.data.DataLoader(evalset,
batch_size=batch_size,
shuffle=False,
num_workers=2*config.workers,
pin_memory=True,
drop_last=False)
return train_loader, eval_loader
def create_loaders_v2(trainset, evalset, label_idxs, unlab_idxs,
num_classes,
config):
if config.data_twice: trainset.transform = twice(trainset.transform)
if config.data_idxs: trainset = DataSetWarpper(trainset, num_classes)
## supervised batch loader
label_sampler = SubsetRandomSampler(label_idxs)
label_batch_sampler = BatchSampler(label_sampler, config.sup_batch_size,
drop_last=True)
label_loader = torch.utils.data.DataLoader(trainset,
batch_sampler=label_batch_sampler,
num_workers=config.workers,
pin_memory=True)
## unsupervised batch loader
if not config.label_exclude: unlab_idxs += label_idxs
unlab_sampler = SubsetRandomSampler(unlab_idxs)
unlab_batch_sampler = BatchSampler(unlab_sampler, config.usp_batch_size,
drop_last=True)
unlab_loader = torch.utils.data.DataLoader(trainset,
batch_sampler=unlab_batch_sampler,
num_workers=config.workers,
pin_memory=True)
## test batch loader
eval_loader = torch.utils.data.DataLoader(evalset,
batch_size=config.sup_batch_size,
shuffle=False,
num_workers=2*config.workers,
pin_memory=True,
drop_last=False)
return label_loader, unlab_loader, eval_loader
def create_optim(params, config):
if config.optim == 'sgd':
optimizer = optim.SGD(params, config.lr,
momentum=config.momentum,
weight_decay=config.weight_decay,
nesterov=config.nesterov)
elif config.optim == 'adam':
optimizer = optim.Adam(params, config.lr)
return optimizer
def create_lr_scheduler(optimizer, config):
if config.lr_scheduler == 'cos':
scheduler = lr_scheduler.CosineAnnealingLR(optimizer,
T_max=config.epochs,
eta_min=config.min_lr)
elif config.lr_scheduler == 'multistep':
if config.steps is None: return None
if isinstance(config.steps, int): config.steps = [config.steps]
scheduler = lr_scheduler.MultiStepLR(optimizer,
milestones=config.steps,
gamma=config.gamma)
elif config.lr_scheduler == 'exp-warmup':
lr_lambda = exp_warmup(config.rampup_length,
config.rampdown_length,
config.epochs)
scheduler = lr_scheduler.LambdaLR(optimizer, lr_lambda=lr_lambda)
elif config.lr_scheduler == 'none':
scheduler = None
else:
raise ValueError("No such scheduler: {}".format(config.lr_scheduler))
return scheduler
def run(config):
print(config)
print("pytorch version : {}".format(torch.__version__))
## create save directory
if config.save_freq!=0 and not os.path.exists(config.save_dir):
os.makedirs(config.save_dir)
## prepare data
dconfig = datasets.load[config.dataset](config.num_labels)
if config.model[-1]=='1':
loaders = create_loaders_v1(**dconfig, config=config)
elif config.model[-1]=='2' or config.model[-5:]=='match':
loaders = create_loaders_v2(**dconfig, config=config)
else:
raise ValueError('No such model: {}'.format(config.model))
device = 'cuda' if torch.cuda.is_available() else 'cpu'
## prepare architecture
net = arch[config.arch](dconfig['num_classes'], config.drop_ratio)
net = net.to(device)
optimizer = create_optim(net.parameters(), config)
scheduler = create_lr_scheduler(optimizer, config)
## run the model
MTbased = set(['mt', 'ict'])
if config.model[:-2] in MTbased or config.model[-5:]=='match':
net2 = arch[config.arch](dconfig['num_classes'], config.drop_ratio)
net2 = net2.to(device)
trainer = build_model[config.model](net, net2, optimizer, device, config)
else:
trainer = build_model[config.model](net, optimizer, device, config)
trainer.loop(config.epochs, *loaders, scheduler=scheduler)
if __name__ == '__main__':
config = parse_commandline_args()
run(config)