MCPcopy Index your code
hub / github.com/TorchSSL/TorchSSL / train

Method train

models/uda/uda.py:78–232  ·  view source on GitHub ↗
(self, args, logger=None)

Source from the content-addressed store, hash-verified

76 self.scheduler = scheduler
77
78 def train(self, args, logger=None):
79
80 ngpus_per_node = torch.cuda.device_count()
81
82 # EMA Init
83 self.model.train()
84 self.ema = EMA(self.model, self.ema_m)
85 self.ema.register()
86 if args.resume == True:
87 self.ema.load(self.ema_model)
88
89 # for gpu profiling
90 start_batch = torch.cuda.Event(enable_timing=True)
91 end_batch = torch.cuda.Event(enable_timing=True)
92 start_run = torch.cuda.Event(enable_timing=True)
93 end_run = torch.cuda.Event(enable_timing=True)
94
95 start_batch.record()
96 best_eval_acc, best_it = 0.0, 0
97
98 scaler = GradScaler()
99 amp_cm = autocast if args.amp else contextlib.nullcontext
100
101 # eval for once to verify if the checkpoint is loaded correctly
102 if args.resume == True:
103 eval_dict = self.evaluate(args=args)
104 print(eval_dict)
105
106 selected_label = torch.ones((len(self.ulb_dset),), dtype=torch.long, ) * -1
107 selected_label = selected_label.cuda(args.gpu)
108 classwise_acc = torch.zeros((args.num_classes,)).cuda(args.gpu)
109
110 for (_, x_lb, y_lb), (x_ulb_idx, x_ulb_w, x_ulb_s) in zip(self.loader_dict['train_lb'],
111 self.loader_dict['train_ulb']):
112 # prevent the training iterations exceed args.num_train_iter
113 if self.it > args.num_train_iter:
114 break
115
116 end_batch.record()
117 torch.cuda.synchronize()
118 start_run.record()
119
120 num_lb = x_lb.shape[0]
121 num_ulb = x_ulb_w.shape[0]
122 assert num_ulb == x_ulb_s.shape[0]
123
124 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)
125 x_ulb_idx = x_ulb_idx.cuda(args.gpu)
126 y_lb = y_lb.cuda(args.gpu)
127
128 if args.use_flex:
129 pseudo_counter = Counter(selected_label.tolist())
130 if max(pseudo_counter.values()) < len(self.ulb_dset): # not all(5w) -1
131 for i in range(args.num_classes):
132 classwise_acc[i] = pseudo_counter[i] / max(pseudo_counter.values())
133
134 # inference and calculate sup/unsup losses
135 with amp_cm():

Callers 2

evaluateMethod · 0.45
save_modelMethod · 0.45

Calls 10

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

Tested by

no test coverage detected