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