def interpolate(input, size=None, scale_factor=None, mode="nearest", align_corners=None): # type: (Tensor, Optional[List[int]], Optional[float], str, Optional[bool]) -> Tensor """ Equivalent to nn.functional.interpolate, but with support for empty batch sizes. This will eventually be supported natively by PyTorch, and this class can go away. """ if input.numel() > 0: return torch.nn.functional.interpolate( input, size, scale_factor, mode, align_corners ) output_shape = _output_size(2, input, size, scale_factor) output_shape = list(input.shape[:-2]) + list(output_shape) return _new_empty_tensor(input, output_shape)
def forward(self, x): if x.numel() > 0: return self.super_forward(x) # get output shape output_shape = [(i - 1) * d - 2 * p + (di * (k - 1) + 1) + op for i, p, di, k, d, op in zip( x.shape[-2:], list(self.padding), list(self.dilation), list(self.kernel_size), list(self.stride), list(self.output_padding), )] output_shape = [x.shape[0], self.bias.shape[0]] + output_shape return _new_empty_tensor(x, output_shape)
def interpolate( input, size=None, scale_factor=None, mode="nearest", align_corners=None ): """ Equivalent to nn.functional.interpolate, but with support for empty batch sizes. This will eventually be supported natively by PyTorch, and this class can go away. """ if float(torchvision.__version__[:3]) < 0.7: if input.numel() > 0: return torch.nn.functional.interpolate( input, size, scale_factor, mode, align_corners ) output_shape = _output_size(2, input, size, scale_factor) output_shape = list(input.shape[:-2]) + list(output_shape) return _new_empty_tensor(input, output_shape) else: return torchvision.ops.misc.interpolate( input, size, scale_factor, mode, align_corners )
def interpolate( input: "Tensor", size: "Optional[List[int]]" = None, scale_factor: "Optional[float]" = None, mode: str = "nearest", align_corners: "Optional[bool]" = None, ) -> "Tensor": """ Equivalent to nn.functional.interpolate, but with support for empty batch sizes. This will eventually be supported natively by PyTorch, and this class can go away. """ # if float(torchvision.__version__[:3]) < 0.7: if LooseVersion(torchvision.__version__) < LooseVersion("0.7.0"): if input.numel() > 0: return torch.nn.functional.interpolate(input, size, scale_factor, mode, align_corners) output_shape = _output_size(2, input, size, scale_factor) output_shape = list(input.shape[:-2]) + list(output_shape) return _new_empty_tensor(input, output_shape) else: return torchvision.ops.misc.interpolate(input, size, scale_factor, mode, align_corners)
def forward(self, x): if x.numel() > 0: return super(BatchNorm2d, self).forward(x) # get output shape output_shape = x.shape return _new_empty_tensor(x, output_shape)