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

Method train

models/fixmatch/fixmatch.py:85–230  ·  view source on GitHub ↗
(self, args, logger=None)

Source from the content-addressed store, hash-verified

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

Callers 2

evaluateMethod · 0.45
save_modelMethod · 0.45

Calls 9

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
updateMethod · 0.45

Tested by

no test coverage detected