Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make drop_prob mutable in DropBlock ScriptModules #28

Open
wants to merge 1 commit into
base: torchscript
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions dropblock/dropblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ class DropBlock2D(Module):

"""

__constants__ = ['drop_prob', 'block_size', 'gamma']
__constants__ = ['block_size', 'gamma']

def __init__(self, drop_prob, block_size):
super(DropBlock2D, self).__init__()

self.drop_prob = drop_prob # type: float
self.register_buffer('drop_prob', torch.tensor(drop_prob, dtype=torch.float32))
self.block_size = block_size # type: int

# get gamma value
Expand All @@ -51,7 +51,7 @@ def forward(self, x):
assert x.dim() == 4, \
"Expected input with 4 dimensions (bsize, channels, height, width)"

if not self.training or self.drop_prob == 0.:
if not self.training or bool(self.drop_prob == torch.zeros(())):
out = x
else:
# sample mask
Expand Down Expand Up @@ -83,7 +83,7 @@ def _compute_block_mask(self, mask):
return block_mask

def _compute_gamma(self):
return self.drop_prob / (self.block_size ** 2)
return self.drop_prob.item() / (self.block_size ** 2)


class DropBlock3D(Module):
Expand All @@ -107,12 +107,12 @@ class DropBlock3D(Module):

"""

__constants__ = ['drop_prob', 'block_size', 'gamma']
__constants__ = ['block_size', 'gamma']

def __init__(self, drop_prob, block_size):
super(DropBlock3D, self).__init__()

self.drop_prob = drop_prob # type: float
self.register_buffer('drop_prob', torch.tensor(drop_prob, dtype=torch.float32))
self.block_size = block_size # type: int

# get gamma value
Expand All @@ -125,7 +125,7 @@ def forward(self, x):
assert x.dim() == 5, \
"Expected input with 5 dimensions (bsize, channels, depth, height, width)"

if not self.training or self.drop_prob == 0.:
if not self.training or bool(self.drop_prob == torch.zeros(())):
out = x
else:
# sample mask
Expand Down Expand Up @@ -157,4 +157,4 @@ def _compute_block_mask(self, mask):
return block_mask

def _compute_gamma(self):
return self.drop_prob / (self.block_size ** 3)
return self.drop_prob.item() / (self.block_size ** 3)