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

Method train

models/flexmatch/flexmatch.py:82–249  ·  view source on GitHub ↗
(self, args, logger=None)

Source from the content-addressed store, hash-verified

80 self.scheduler = scheduler
81
82 def train(self, args, logger=None):
83
84 ngpus_per_node = torch.cuda.device_count()
85
86 # EMA Init
87 self.model.train()
88 self.ema = EMA(self.model, self.ema_m)
89 self.ema.register()
90 if args.resume == True:
91 self.ema.load(self.ema_model)
92
93 # p(y) based on the labeled examples seen during training
94 dist_file_name = r"./data_statistics/" + args.dataset + '_' + str(args.num_labels) + '.json'
95 if args.dataset.upper() == 'IMAGENET':
96 p_target = None
97 else:
98 with open(dist_file_name, 'r') as f:
99 p_target = json.loads(f.read())
100 p_target = torch.tensor(p_target['distribution'])
101 p_target = p_target.cuda(args.gpu)
102 # print('p_target:', p_target)
103
104 p_model = None
105
106 # for gpu profiling
107 start_batch = torch.cuda.Event(enable_timing=True)
108 end_batch = torch.cuda.Event(enable_timing=True)
109 start_run = torch.cuda.Event(enable_timing=True)
110 end_run = torch.cuda.Event(enable_timing=True)
111
112 start_batch.record()
113 best_eval_acc, best_it = 0.0, 0
114
115 scaler = GradScaler()
116 amp_cm = autocast if args.amp else contextlib.nullcontext
117
118 # eval for once to verify if the checkpoint is loaded correctly
119 if args.resume == True:
120 eval_dict = self.evaluate(args=args)
121 print(eval_dict)
122
123 selected_label = torch.ones((len(self.ulb_dset),), dtype=torch.long, ) * -1
124 selected_label = selected_label.cuda(args.gpu)
125
126 classwise_acc = torch.zeros((args.num_classes,)).cuda(args.gpu)
127
128 for (_, x_lb, y_lb), (x_ulb_idx, x_ulb_w, x_ulb_s) in zip(self.loader_dict['train_lb'],
129 self.loader_dict['train_ulb']):
130 # prevent the training iterations exceed args.num_train_iter
131 if self.it > args.num_train_iter:
132 break
133
134 end_batch.record()
135 torch.cuda.synchronize()
136 start_run.record()
137
138 num_lb = x_lb.shape[0]
139 num_ulb = x_ulb_w.shape[0]

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