-
-
Save FrancescoSaverioZuppichini/336662c9b4d6a0fb4dbbf1b460ea975d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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