MCPcopy Create free account
hub / github.com/TorchSSL/TorchSSL / train

Method train

models/freematch_entropy/freematch.py:177–317  ·  view source on GitHub ↗
(self, args, logger=None)

Source from the content-addressed store, hash-verified

175 self.label_hist = label_hist / label_hist.sum()
176
177 def train(self, args, logger=None):
178
179 ngpus_per_node = torch.cuda.device_count()
180
181 # EMA Init
182 self.model.train()
183 self.ema = EMA(self.model, self.ema_m)
184 self.ema.register()
185 if args.resume == True:
186 self.ema.load(self.ema_model)
187
188 # for gpu profiling
189 start_batch = torch.cuda.Event(enable_timing=True)
190 end_batch = torch.cuda.Event(enable_timing=True)
191 start_run = torch.cuda.Event(enable_timing=True)
192 end_run = torch.cuda.Event(enable_timing=True)
193
194 start_batch.record()
195 best_eval_acc, best_it = 0.0, 0
196
197 scaler = GradScaler()
198 amp_cm = autocast if args.amp else contextlib.nullcontext
199
200 # eval for once to verify if the checkpoint is loaded correctly
201 if args.resume == True:
202 eval_dict = self.evaluate(args=args)
203 print(eval_dict)
204
205 p_model = (torch.ones(args.num_classes) / args.num_classes).cuda()
206 label_hist = (torch.ones(args.num_classes) / args.num_classes).cuda()
207 time_p = p_model.mean()
208
209 for (_, x_lb, y_lb), (x_ulb_idx, x_ulb_w, x_ulb_s) in zip(self.loader_dict['train_lb'],
210 self.loader_dict['train_ulb']):
211
212 # prevent the training iterations exceed args.num_train_iter
213 if self.it > args.num_train_iter:
214 break
215
216 end_batch.record()
217 torch.cuda.synchronize()
218 start_run.record()
219
220 num_lb = x_lb.shape[0]
221 num_ulb = x_ulb_w.shape[0]
222 assert num_ulb == x_ulb_s.shape[0]
223
224 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)
225 y_lb = y_lb.cuda(args.gpu)
226
227 inputs = torch.cat((x_lb, x_ulb_w, x_ulb_s))
228
229 # inference and calculate sup/unsup losses
230 with amp_cm():
231 logits = self.model(inputs)
232 logits_x_lb = logits[:num_lb]
233 logits_x_ulb_w, logits_x_ulb_s = logits[num_lb:].chunk(2)
234 sup_loss = ce_loss(logits_x_lb, y_lb, reduction='mean')

Callers 3

warmupMethod · 0.45
evaluateMethod · 0.45
save_modelMethod · 0.45

Calls 11

evaluateMethod · 0.95
save_modelMethod · 0.95
EMAClass · 0.90
ce_lossFunction · 0.90
registerMethod · 0.80
loadMethod · 0.80
stepMethod · 0.80
consistency_lossFunction · 0.70
entropy_lossFunction · 0.70
updateMethod · 0.45

Tested by

no test coverage detected