def forward(self, x): # shape: (bsize, channels, height, width) assert x.dim() == 4, \ "Expected input with 4 dimensions (bsize, channels, height, width)" if not self.training or self.drop_prob == 0.: return x else: # sample from a mask mask_reduction = self.block_size // 2 mask_height = x.shape[-2] - mask_reduction mask_width = x.shape[-1] - mask_reduction mask_sizes = [mask_height, mask_width] if any([x <= 0 for x in mask_sizes]): raise ValueError( 'Input of shape {} is too small for block_size {}'.format( tuple(x.shape), self.block_size)) # get gamma value gamma = self._compute_gamma(x, mask_sizes) # sample mask mask = Bernoulli(gamma).sample((x.shape[0], *mask_sizes)) # place mask on input device mask = mask.to(x.device) # mask.cuda() # compute block mask block_mask = self._compute_block_mask(mask) channel_mask = self._compute_channel_mask(x, block_mask) # apply block mask out = x * channel_mask return out
def forward(self, x): # shape: (bsize, channels, height, width) assert x.dim() == 4, \ "Expected input with 4 dimensions (bsize, channels, height, width)" if not self.training or self.drop_prob == 0.: return x else: # get gamma value gamma = self._compute_gamma(feat_size=x.shape[1]) # sample from a mask mask_reduction = self.block_size // 2 mask_height = x.shape[2] - mask_reduction mask_width = x.shape[3] - mask_reduction mask = Bernoulli(gamma).sample( (x.shape[0], mask_height, mask_width)) # place mask on input device mask = mask.to(x.device) # compute block mask block_mask = self._compute_block_mask(mask) # apply block mask out = x * block_mask[:, None, :, :] # scale output out = out * block_mask.numel() / block_mask.sum() return out
def forward(ctx, x, p, is_training): """ The forward function. Zero out entries of x with probability p. Parameters ---------- ctx: torch.autograd.function the context object in which to store stuff. x: torch.FloatTensor the input tensor. p: float the probability of zeroing out elements. is_training: bool whether the model is in training mode. Returns ------- o: torch.FloatTensor the ReLU activated tensor. """ if is_training: mask = Bernoulli(1 - p).sample(x.shape) mask = mask * (1 / (1 - p)) else: mask = Bernoulli(1).sample(x.shape) if x.is_cuda: mask = mask.to('cuda') ctx.save_for_backward(mask) o = x * mask return o
def forward(self, x): # shape: (bsize, channels, height, width) assert x.dim() == 4, \ "Expected input with 4 dimensions (bsize, channels, height, width)" if not self.training or self.drop_prob == 0.: return x else: # sample from a mask mask_reduction = self.block_size // 2 mask_height = x.shape[-2] - mask_reduction mask_width = x.shape[-1] - mask_reduction mask_sizes = [mask_height, mask_width] if any([x <= 0 for x in mask_sizes]): raise ValueError( 'Input of shape {} is too small for block_size {}'.format( tuple(x.shape), self.block_size)) # get gamma value gamma = self._compute_gamma(x, mask_sizes) if self.att: x_norm = self.normalize(x) x_mask = (x_norm > 0.8).float() gamma = 1 - (1 - gamma)**(x.shape[-1] * x.shape[-2] * x_mask.sum() / x_mask.numel()) gamma = torch.min(gamma.float(), torch.tensor(0.25)) mask = Bernoulli(1 - gamma).sample( (x.shape[0], x.shape[1])).byte().to(x.device) x_mask[mask, :, :] = 0 block_mask = 1 - x_mask out = x * block_mask else: # sample mask mask = Bernoulli(gamma).sample( (x.shape[0], *mask_sizes)).float() # place mask on input device mask = mask.to(x.device) # compute block mask block_mask = self._compute_block_mask(mask) # apply block mask out = x * block_mask[:, None, :, :] # scale output out = out * block_mask.numel() / block_mask.sum() return out
def forward(self, x): # shape: (bsize, channels, height, width) assert x.dim() == 4, \ "Expected input with 4 dimensions (bsize, channels, height, width)" if not self.training or self.drop_prob[0] == 0.: return x else: # sample from a mask mask_reduction = self.block_size // 2 mask_height = x.shape[-2] - mask_reduction mask_width = x.shape[-1] - mask_reduction mask_sizes = [mask_height, mask_width] if any([size <= 0 for size in mask_sizes]): raise ValueError( 'Input of shape {} is too small for block_size {}'.format( tuple(x.shape), self.block_size)) # get gamma value gamma = self._compute_gamma(x, mask_sizes) # sample mask # modified by Riheng 2018/12/06 #mask = Bernoulli(gamma).sample((x.shape[0], *mask_sizes)) # *mask_sizes for python3 mask = Bernoulli(gamma).sample( (x.shape[0], mask_height, mask_width)) # can not run in torch0.3.1 # place mask on input device mask = mask.to(x.device) # compute block mask block_mask = self._compute_block_mask(mask) # apply block mask out = x * block_mask[:, None, :, :] # scale output out = out * block_mask.numel() / block_mask.sum() return out
def forward(self, x): # shape: (bsize, channels, depth, height, width) assert x.dim() == 5, \ "Expected input with 5 dimensions (bsize, channels, depth, height, width)" if not self.training or self.drop_prob == 0.: return x else: mask_reduction = self.block_size // 2 mask_depth = x.shape[-3] - mask_reduction mask_height = x.shape[-2] - mask_reduction mask_width = x.shape[-1] - mask_reduction mask_sizes = [mask_depth, mask_height, mask_width] if any([x <= 0 for x in mask_sizes]): raise ValueError( 'Input of shape {} is too small for block_size {}'.format( tuple(x.shape), self.block_size)) # get gamma value gamma = self._compute_gamma(x, mask_sizes) # sample mask mask = Bernoulli(gamma).sample((x.shape[0], *mask_sizes)) # place mask on input device mask = mask.to(x.device) # compute block mask block_mask = self._compute_block_mask(mask) # apply block mask out = x * block_mask[:, None, :, :, :] # scale output out = out * block_mask.numel() / block_mask.sum() return out
def collate_ae(self, data, train_p=0.75): fname, batch = data batch = torch.stack(batch) bs = batch.size(0) sl = batch.size(2) ts = batch[:, 0] ys = batch[:, 1].unsqueeze(-1) p = torch.FloatTensor([train_p]) mask_train = Bernoulli(p).sample(torch.Size([bs, sl])) y_train = ys * mask_train y_pred = ys batch_dict = { 'observed_data': y_train.to(device), 'observed_tp': ts[0].view(-1).to(device), 'data_to_predict': y_pred.to(device), 'tp_to_predict': ts[0].view(-1).to(device), 'observed_mask': mask_train.to(device), 'mask_predicted_data': None, 'labels': None, 'mode': 'interp' } return batch_dict