-
Notifications
You must be signed in to change notification settings - Fork 34
/
fixmatch.py
276 lines (209 loc) · 10.7 KB
/
fixmatch.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
275
276
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.models as models
from torch.cuda.amp import autocast, GradScaler
import os
import contextlib
from train_utils import AverageMeter
from .fixmatch_utils import consistency_loss, Get_Scalar
from train_utils import ce_loss
class FixMatch:
def __init__(self, net_builder, num_classes, ema_m, T, p_cutoff, lambda_u,\
hard_label=True, t_fn=None, p_fn=None, it=0, num_eval_iter=1000, tb_log=None, logger=None):
"""
class Fixmatch contains setter of data_loader, optimizer, and model update methods.
Args:
net_builder: backbone network class (see net_builder in utils.py)
num_classes: # of label classes
ema_m: momentum of exponential moving average for eval_model
T: Temperature scaling parameter for output sharpening (only when hard_label = False)
p_cutoff: confidence cutoff parameters for loss masking
lambda_u: ratio of unsupervised loss to supervised loss
hard_label: If True, consistency regularization use a hard pseudo label.
it: initial iteration count
num_eval_iter: freqeuncy of iteration (after 500,000 iters)
tb_log: tensorboard writer (see train_utils.py)
logger: logger (see utils.py)
"""
super(FixMatch, self).__init__()
# momentum update param
self.loader = {}
self.num_classes = num_classes
self.ema_m = ema_m
# create the encoders
# network is builded only by num_classes,
# other configs are covered in main.py
self.train_model = net_builder(num_classes=num_classes)
self.eval_model = net_builder(num_classes=num_classes)
self.num_eval_iter = num_eval_iter
self.t_fn = Get_Scalar(T) #temperature params function
self.p_fn = Get_Scalar(p_cutoff) #confidence cutoff function
self.lambda_u = lambda_u
self.tb_log = tb_log
self.use_hard_label = hard_label
self.optimizer = None
self.scheduler = None
self.it = 0
self.logger = logger
self.print_fn = print if logger is None else logger.info
for param_q, param_k in zip(self.train_model.parameters(), self.eval_model.parameters()):
param_k.data.copy_(param_q.detach().data) # initialize
param_k.requires_grad = False # not update by gradient for eval_net
self.eval_model.eval()
@torch.no_grad()
def _eval_model_update(self):
"""
Momentum update of evaluation model (exponential moving average)
"""
for param_train, param_eval in zip(self.train_model.module.parameters(), self.eval_model.parameters()):
param_eval.copy_(param_eval * self.ema_m + param_train.detach() * (1-self.ema_m))
for buffer_train, buffer_eval in zip(self.train_model.buffers(), self.eval_model.buffers()):
buffer_eval.copy_(buffer_train)
def set_data_loader(self, loader_dict):
self.loader_dict = loader_dict
self.print_fn(f'[!] data loader keys: {self.loader_dict.keys()}')
def set_optimizer(self, optimizer, scheduler=None):
self.optimizer = optimizer
self.scheduler = scheduler
def train(self, args, logger=None):
"""
Train function of FixMatch.
From data_loader, it inference training data, computes losses, and update the networks.
"""
ngpus_per_node = torch.cuda.device_count()
#lb: labeled, ulb: unlabeled
self.train_model.train()
# for gpu profiling
start_batch = torch.cuda.Event(enable_timing=True)
end_batch = torch.cuda.Event(enable_timing=True)
start_run = torch.cuda.Event(enable_timing=True)
end_run = torch.cuda.Event(enable_timing=True)
start_batch.record()
best_eval_acc, best_it = 0.0, 0
scaler = GradScaler()
amp_cm = autocast if args.amp else contextlib.nullcontext
for (x_lb, y_lb), (x_ulb_w, x_ulb_s, _) in zip(self.loader_dict['train_lb'], self.loader_dict['train_ulb']):
# prevent the training iterations exceed args.num_train_iter
if self.it > args.num_train_iter:
break
end_batch.record()
torch.cuda.synchronize()
start_run.record()
num_lb = x_lb.shape[0]
num_ulb = x_ulb_w.shape[0]
assert num_ulb == x_ulb_s.shape[0]
x_lb, x_ulb_w, x_ulb_s = x_lb.cuda(args.gpu), x_ulb_w.cuda(args.gpu), x_ulb_s.cuda(args.gpu)
y_lb = y_lb.cuda(args.gpu)
inputs = torch.cat((x_lb, x_ulb_w, x_ulb_s))
# inference and calculate sup/unsup losses
with amp_cm():
logits = self.train_model(inputs)
logits_x_lb = logits[:num_lb]
logits_x_ulb_w, logits_x_ulb_s = logits[num_lb:].chunk(2)
del logits
# hyper-params for update
T = self.t_fn(self.it)
p_cutoff = self.p_fn(self.it)
sup_loss = ce_loss(logits_x_lb, y_lb, reduction='mean')
unsup_loss, mask = consistency_loss(logits_x_ulb_w,
logits_x_ulb_s,
'ce', T, p_cutoff,
use_hard_labels=args.hard_label)
total_loss = sup_loss + self.lambda_u * unsup_loss
# parameter updates
if args.amp:
scaler.scale(total_loss).backward()
scaler.step(self.optimizer)
scaler.update()
else:
total_loss.backward()
self.optimizer.step()
self.scheduler.step()
self.train_model.zero_grad()
with torch.no_grad():
self._eval_model_update()
end_run.record()
torch.cuda.synchronize()
#tensorboard_dict update
tb_dict = {}
tb_dict['train/sup_loss'] = sup_loss.detach()
tb_dict['train/unsup_loss'] = unsup_loss.detach()
tb_dict['train/total_loss'] = total_loss.detach()
tb_dict['train/mask_ratio'] = 1.0 - mask.detach()
tb_dict['lr'] = self.optimizer.param_groups[0]['lr']
tb_dict['train/prefecth_time'] = start_batch.elapsed_time(end_batch)/1000.
tb_dict['train/run_time'] = start_run.elapsed_time(end_run)/1000.
if self.it % self.num_eval_iter == 0:
eval_dict = self.evaluate(args=args)
tb_dict.update(eval_dict)
save_path = os.path.join(args.save_dir, args.save_name)
if tb_dict['eval/top-1-acc'] > best_eval_acc:
best_eval_acc = tb_dict['eval/top-1-acc']
best_it = self.it
self.print_fn(f"{self.it} iteration, USE_EMA: {hasattr(self, 'eval_model')}, {tb_dict}, BEST_EVAL_ACC: {best_eval_acc}, at {best_it} iters")
if not args.multiprocessing_distributed or \
(args.multiprocessing_distributed and args.rank % ngpus_per_node == 0):
if self.it == best_it:
self.save_model('model_best.pth', save_path)
if not self.tb_log is None:
self.tb_log.update(tb_dict, self.it)
self.it +=1
del tb_dict
start_batch.record()
if self.it > 2**19:
self.num_eval_iter = 1000
eval_dict = self.evaluate(args=args)
eval_dict.update({'eval/best_acc': best_eval_acc, 'eval/best_it': best_it})
return eval_dict
@torch.no_grad()
def evaluate(self, eval_loader=None, args=None):
use_ema = hasattr(self, 'eval_model')
eval_model = self.eval_model if use_ema else self.train_model
eval_model.eval()
if eval_loader is None:
eval_loader = self.loader_dict['eval']
total_loss = 0.0
total_acc = 0.0
total_num = 0.0
for x, y in eval_loader:
x, y = x.cuda(args.gpu), y.cuda(args.gpu)
num_batch = x.shape[0]
total_num += num_batch
logits = eval_model(x)
loss = F.cross_entropy(logits, y, reduction='mean')
acc = torch.sum(torch.max(logits, dim=-1)[1] == y)
total_loss += loss.detach()*num_batch
total_acc += acc.detach()
if not use_ema:
eval_model.train()
return {'eval/loss': total_loss/total_num, 'eval/top-1-acc': total_acc/total_num}
def save_model(self, save_name, save_path):
save_filename = os.path.join(save_path, save_name)
train_model = self.train_model.module if hasattr(self.train_model, 'module') else self.train_model
eval_model = self.eval_model.module if hasattr(self.eval_model, 'module') else self.eval_model
torch.save({'train_model': train_model.state_dict(),
'eval_model': eval_model.state_dict(),
'optimizer': self.optimizer.state_dict(),
'scheduler': self.scheduler.state_dict(),
'it': self.it}, save_filename)
self.print_fn(f"model saved: {save_filename}")
def load_model(self, load_path):
checkpoint = torch.load(load_path)
train_model = self.train_model.module if hasattr(self.train_model, 'module') else self.train_model
eval_model = self.eval_model.module if hasattr(self.eval_model, 'module') else self.eval_model
for key in checkpoint.keys():
if hasattr(self, key) and getattr(self, key) is not None:
if 'train_model' in key:
train_model.load_state_dict(checkpoint[key])
elif 'eval_model' in key:
eval_model.load_state_dict(checkpoint[key])
elif key == 'it':
self.it = checkpoint[key]
else:
getattr(self, key).load_state_dict(checkpoint[key])
self.print_fn(f"Check Point Loading: {key} is LOADED")
else:
self.print_fn(f"Check Point Loading: {key} is **NOT** LOADED")
if __name__ == "__main__":
pass