[docs]defdrop_block2d(input:Tensor,p:float,block_size:int,inplace:bool=False,eps:float=1e-06,training:bool=True)->Tensor:""" Implements DropBlock2d from `"DropBlock: A regularization method for convolutional networks" <https://arxiv.org/abs/1810.12890>`. Args: input (Tensor[N, C, H, W]): The input tensor or 4-dimensions with the first one being its batch i.e. a batch with ``N`` rows. p (float): Probability of an element to be dropped. block_size (int): Size of the block to drop. inplace (bool): If set to ``True``, will do this operation in-place. Default: ``False``. eps (float): A value added to the denominator for numerical stability. Default: 1e-6. training (bool): apply dropblock if is ``True``. Default: ``True``. Returns: Tensor[N, C, H, W]: The randomly zeroed tensor after dropblock. """ifnottorch.jit.is_scripting()andnottorch.jit.is_tracing():_log_api_usage_once(drop_block2d)ifp<0.0orp>1.0:raiseValueError(f"drop probability has to be between 0 and 1, but got {p}.")ifinput.ndim!=4:raiseValueError(f"input should be 4 dimensional. Got {input.ndim} dimensions.")ifnottrainingorp==0.0:returninputN,C,H,W=input.size()block_size=min(block_size,W,H)# compute the gamma of Bernoulli distributiongamma=(p*H*W)/((block_size**2)*((H-block_size+1)*(W-block_size+1)))noise=torch.empty((N,C,H-block_size+1,W-block_size+1),dtype=input.dtype,device=input.device)noise.bernoulli_(gamma)noise=F.pad(noise,[block_size//2]*4,value=0)noise=F.max_pool2d(noise,stride=(1,1),kernel_size=(block_size,block_size),padding=block_size//2)noise=1-noisenormalize_scale=noise.numel()/(eps+noise.sum())ifinplace:input.mul_(noise).mul_(normalize_scale)else:input=input*noise*normalize_scalereturninput
[docs]defdrop_block3d(input:Tensor,p:float,block_size:int,inplace:bool=False,eps:float=1e-06,training:bool=True)->Tensor:""" Implements DropBlock3d from `"DropBlock: A regularization method for convolutional networks" <https://arxiv.org/abs/1810.12890>`. Args: input (Tensor[N, C, D, H, W]): The input tensor or 5-dimensions with the first one being its batch i.e. a batch with ``N`` rows. p (float): Probability of an element to be dropped. block_size (int): Size of the block to drop. inplace (bool): If set to ``True``, will do this operation in-place. Default: ``False``. eps (float): A value added to the denominator for numerical stability. Default: 1e-6. training (bool): apply dropblock if is ``True``. Default: ``True``. Returns: Tensor[N, C, D, H, W]: The randomly zeroed tensor after dropblock. """ifnottorch.jit.is_scripting()andnottorch.jit.is_tracing():_log_api_usage_once(drop_block3d)ifp<0.0orp>1.0:raiseValueError(f"drop probability has to be between 0 and 1, but got {p}.")ifinput.ndim!=5:raiseValueError(f"input should be 5 dimensional. Got {input.ndim} dimensions.")ifnottrainingorp==0.0:returninputN,C,D,H,W=input.size()block_size=min(block_size,D,H,W)# compute the gamma of Bernoulli distributiongamma=(p*D*H*W)/((block_size**3)*((D-block_size+1)*(H-block_size+1)*(W-block_size+1)))noise=torch.empty((N,C,D-block_size+1,H-block_size+1,W-block_size+1),dtype=input.dtype,device=input.device)noise.bernoulli_(gamma)noise=F.pad(noise,[block_size//2]*6,value=0)noise=F.max_pool3d(noise,stride=(1,1,1),kernel_size=(block_size,block_size,block_size),padding=block_size//2)noise=1-noisenormalize_scale=noise.numel()/(eps+noise.sum())ifinplace:input.mul_(noise).mul_(normalize_scale)else:input=input*noise*normalize_scalereturninput
torch.fx.wrap("drop_block2d")
[docs]classDropBlock2d(nn.Module):""" See :func:`drop_block2d`. """def__init__(self,p:float,block_size:int,inplace:bool=False,eps:float=1e-06)->None:super().__init__()self.p=pself.block_size=block_sizeself.inplace=inplaceself.eps=eps
[docs]defforward(self,input:Tensor)->Tensor:""" Args: input (Tensor): Input feature map on which some areas will be randomly dropped. Returns: Tensor: The tensor after DropBlock layer. """returndrop_block2d(input,self.p,self.block_size,self.inplace,self.eps,self.training)
[docs]classDropBlock3d(DropBlock2d):""" See :func:`drop_block3d`. """def__init__(self,p:float,block_size:int,inplace:bool=False,eps:float=1e-06)->None:super().__init__(p,block_size,inplace,eps)
[docs]defforward(self,input:Tensor)->Tensor:""" Args: input (Tensor): Input feature map on which some areas will be randomly dropped. Returns: Tensor: The tensor after DropBlock layer. """returndrop_block3d(input,self.p,self.block_size,self.inplace,self.eps,self.training)
Docs
Access comprehensive developer documentation for PyTorch
To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: Cookies Policy.