-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDropoutblock.py
36 lines (32 loc) · 1.18 KB
/
Dropoutblock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import torch
import torch.nn.functional as F
from torch import nn, Tensor
class DropBlock_search(nn.Module):
def __init__(self, block_size: int = 3, p: float = 0.5):
super(DropBlock_search, self).__init__()
self.block_size = block_size
self.p = p
def calculate_gamma(self, x: Tensor) -> float:
"""gamma
Args:
x (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 not self.training or self.p == 0.:
return x
else:
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