def update_soda_ccm(self, x, size, L=None, step=None): assert x.size(-1) == size aug_x = x.clone() x = augmentations.random_crop(x) aug_x = augmentations.random_crop(aug_x) x_pos = augmentations.random_overlay(aug_x) self.ccm_optimizer.zero_grad() # identical encoders z_1 = self.ccm_head(x) z_2 = self.ccm_head(x_pos) # empirical cross-correlation matrix c = self.bn(z_1).T @ self.bn(z_2) # cross-correlation matrix c.div_(z_1.size(0)) # DxD on_diag = torch.diagonal(c).add_(-1).pow_(2).sum().mul(1 / 32) off_diag = off_diagonal(c).pow_(2).sum().mul(1 / 32) ccm_loss = 0.01 * (on_diag + 3.9e-3 * off_diag) ccm_loss.backward() self.ccm_optimizer.step() if L is not None: L.log('train/ccm_loss', ccm_loss, step)
def sample(self, n=None): idxs = self._get_idxs(n) obs = torch.as_tensor(self.obs[idxs]).cuda().float() actions = torch.as_tensor(self.actions[idxs]).cuda() rewards = torch.as_tensor(self.rewards[idxs]).cuda() next_obs = torch.as_tensor(self.next_obs[idxs]).cuda().float() not_dones = torch.as_tensor(self.not_dones[idxs]).cuda() obs = augmentations.random_crop(obs) next_obs = augmentations.random_crop(next_obs) return obs, actions, rewards, next_obs, not_dones
def update_soda(self, replay_buffer, L=None, step=None): x = replay_buffer.sample_soda(self.soda_batch_size) assert x.size(-1) == 100 aug_x = x.clone() x = augmentations.random_crop(x) aug_x = augmentations.random_crop(aug_x) # print(x.shape, aug_x.shape) aug_x = augmentations.random_overlay(aug_x, self.args) # print(x.shape, aug_x.shape) soda_loss = self.compute_soda_loss(aug_x, x) self.soda_optimizer.zero_grad() soda_loss.backward() self.soda_optimizer.step() if L is not None: L.log('train/aux_loss', soda_loss, step) utils.soft_update_params(self.predictor, self.predictor_target, self.soda_tau)
def update_soda_curl(self, x, size, L=None, step=None): assert x.size(-1) == size aug_x = x.clone() x = augmentations.random_crop(x) aug_x = augmentations.random_crop(aug_x) x_pos = augmentations.random_overlay(aug_x) z_a = self.curl_head.encoder(x) with torch.no_grad(): z_pos = self.critic_target.encoder(x_pos) logits = self.curl_head.compute_logits(z_a, z_pos) labels = torch.arange(logits.shape[0]).long().cuda() curl_loss = F.cross_entropy(logits, labels) self.curl_optimizer.zero_grad() curl_loss.backward() self.curl_optimizer.step() if L is not None: L.log('train/aux_loss', curl_loss, step)
gt_path = img_path.replace("img", "annot") image, gt = get_image_gt(img_path) rand0 = np.random.rand() if rand0 > 0.3: image, gt = random_rotation(image, gt, normal=True) rand1 = np.random.rand() if rand1 > 0.3: image, gt = elastic_deformation(image, gt, 5000, 100) rand2 = np.random.rand() if rand2 > 0.5: rand = np.random.rand() crop_size = rand + 1 image, gt = random_crop(image, gt, int(image.shape[0]/crop_size), int(image.shape[1]/crop_size)) if rand0 <= 0.3 and rand1 <= 0.3 and rand2 <= 0.5: image, gt = elastic_deformation(image, gt, 5000, 80) new_img_path = img_path.replace(".png", "_{num:04d}.png".format(num=n)) new_gt_path = gt_path.replace(".png", "_{num:04d}.png".format(num=n)) write_image(image, new_img_path) write_image(gt, new_gt_path) n += 1 # # elastic deformation # train_paths = get_file_list(data_path) # for i in tqdm(range(2000, 5000)): # img_path = train_paths[i % len(train_paths)]