def training_step(self, batch, batch_nb): # REQUIRED labeled_batch, unlabeled_batch = batch[0], batch[1:] # labeled_batch, unlabeled_batch = batch labeled_x, labeled_y = labeled_batch labeled_y = smooth_label(labeled_y, self.hparams.n_classes, self.hparams.label_smoothing) unlabeled_xs = [b[0] for b in unlabeled_batch] # only get the images loss, l_l, l_u = self._loss( labeled_x, labeled_y, unlabeled_xs, batch_inference=self.hparams.batch_inference, ) loss = l_l + self.lambda_u * l_u # y = torch.argmax(mixed_target, dim=-1) # acc = self.accuracy(logits[:batch_size], y[:batch_size]) self.train_dict["loss"].append(loss.item()) # self.train_dict["acc"].append(acc.item()) self.train_dict["lab_loss"].append(l_l.item()) self.train_dict["unl_loss"].append(l_u.item()) # tensorboard_logs = {"train/loss": np.mean(self.train_dict["loss"]), # # "train/acc": np.mean(self.train_dict["acc"]), # "train/lab_loss": np.mean(self.train_dict["lab_loss"]), # "train/unl_loss": np.mean(self.train_dict["unl_loss"]), # "lambda_u": self.lambda_u} # progress_bar = {# "acc": np.mean(self.train_dict["acc"]), # "lab_loss": np.mean(self.train_dict["lab_loss"]), # "unl_loss": np.mean(self.train_dict["unl_loss"]), # "lambda_u": self.lambda_u} # return {"loss": loss, "log": tensorboard_logs, "progress_bar": progress_bar} self.log("train/loss", np.mean(self.train_dict["loss"]), prog_bar=False, logger=True) self.log( "train/lab_loss", np.mean(self.train_dict["lab_loss"]), prog_bar=True, logger=True, ) self.log( "train/unl_loss", np.mean(self.train_dict["unl_loss"]), prog_bar=True, logger=True, ) self.log("train/lambda_u", self.lambda_u, prog_bar=False, logger=True) return {"loss": loss}
def test_half_mixup(images_targets_num_classes_tuple): inputs, targets, num_classes = images_targets_num_classes_tuple logits = smooth_label(targets, num_classes) _, _, _, _, gt_lambda = half_mixup_data_for_testing(inputs, logits, 0.5) # test lambda is within the range assert torch.all((gt_lambda >= 0.5) * (gt_lambda <= 1))
def test_mixup(images_targets_num_classes_tuple): inputs, targets, num_classes = images_targets_num_classes_tuple logits = smooth_label(targets, num_classes) _, _, _, _, gt_lambda = mixup_data_for_testing(inputs, logits, 0.5) # the mixed data should be between it's two ingredients # exclude the data which shuffle to the same place, # this will cause mixed data equal to the origin ingredients, # but sometimes two same numbers will be considered different due to the limitation float number # test lambda is within the range assert torch.all((gt_lambda >= 0) * (gt_lambda <= 1))
def test_smooth_label(targets_smoothing_classes_tuple): targets, smoothing_factor, num_classes = targets_smoothing_classes_tuple ################################################################## # test for label smoothing batch_size = targets.shape[0] y_labels = smooth_label(targets, num_classes, smoothing_factor) # predicted classes pred = torch.argmax(y_labels, dim=-1) # the logits of maximum classes should be (1 - smoothing_factor) assert torch.all(y_labels[torch.arange(batch_size), targets] == 1 - smoothing_factor) # all the other logits should be the same assert torch.sum( y_labels != 1 - smoothing_factor) == batch_size * (num_classes - 1) # summation should be equal to 1 assert torch.all( torch.isclose(torch.sum(y_labels, -1), torch.ones(pred.shape)))
def training_step(self, batch, batch_nb): # REQUIRED x, y = batch # return smooth one-hot like label y_one_hot = smooth_label(y, self.hparams.n_classes, self.hparams.label_smoothing) # mixup mixed_x, mixed_y = mixup_data(x, y_one_hot, self.hparams.alpha) y_hat = self.classifier(mixed_x) loss = soft_cross_entropy(y_hat, mixed_y) y = torch.argmax(mixed_y, dim=-1) acc = self.accuracy(y_hat, y) num = len(y) self.train_dict["loss"].append(loss.item()) self.train_dict["acc"].append(acc.item()) # tensorboard_logs = {"train/loss": np.mean(self.train_dict["loss"]), # "train/acc": np.mean(self.train_dict["acc"])} # progress_bar = {"acc": np.mean(self.train_dict["acc"])} # return {"loss": loss, "train_acc": acc, # "train_num": num, "log": tensorboard_logs, # "progress_bar": progress_bar} self.log("train/loss", np.mean(self.train_dict["loss"]), prog_bar=False, logger=True) self.log("train/acc", np.mean(self.train_dict["acc"]), prog_bar=False, logger=True) return {"loss": loss, "train_acc": acc, "train_num": num}