Skip to content

Instantly share code, notes, and snippets.

@FrancescoSaverioZuppichini
Created September 4, 2021 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FrancescoSaverioZuppichini/336662c9b4d6a0fb4dbbf1b460ea975d to your computer and use it in GitHub Desktop.
Save FrancescoSaverioZuppichini/336662c9b4d6a0fb4dbbf1b460ea975d to your computer and use it in GitHub Desktop.
import torch.nn.functional as F
class DropBlock(nn.Module):
def __init__(self, block_size: int, p: float = 0.5):
super().__init__()
self.block_size = block_size
self.p = p
def calculate_gamma(self, x: Tensor) -> float:
"""Compute gamma, eq (1) in the paper
Args:
x (Tensor): Input tensor
Returns:
Tensor: gamma
"""
invalid = (1 - self.p) / (self.block_size ** 2)
valid = (x.shape[-1] ** 2) / ((x.shape[-1] - self.block_size + 1) ** 2)
return invalid * valid
def forward(self, x: Tensor) -> Tensor:
if self.training:
gamma = self.calculate_gamma(x)
mask = torch.bernoulli(torch.ones_like(x) * gamma)
mask_block = 1 - F.max_pool2d(
mask,
kernel_size=(self.block_size, self.block_size),
stride=(1, 1),
padding=(self.block_size // 2, self.block_size // 2),
)
x = mask_block * x * (mask_block.numel() / mask_block.sum())
return x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment