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

Method train

models/softmatch/softmatch.py:84–236  ·  view source on GitHub ↗
(self, args, logger=None)

Source from the content-addressed store, hash-verified

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

Callers 2

evaluateMethod · 0.45
save_modelMethod · 0.45

Calls 12

evaluateMethod · 0.95
update_prob_tMethod · 0.95
calculate_maskMethod · 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