Delete main/src/utils/upsample.py with huggingface_hub
Browse files- main/src/utils/upsample.py +0 -105
main/src/utils/upsample.py
DELETED
|
@@ -1,105 +0,0 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
import torch.nn as nn
|
| 3 |
-
from typing_extensions import Literal
|
| 4 |
-
|
| 5 |
-
from basic_typing import ShapeNCX, TensorNCX, TorchTensorNCX, ShapeX
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
def _upsample_int_1d(tensor: TorchTensorNCX, size: ShapeNCX) -> TorchTensorNCX:
|
| 9 |
-
# this is just a workaround! TODO assess the speed impact!
|
| 10 |
-
# see https://discuss.pytorch.org/t/what-is-the-good-way-to-interpolate-int-tensor/29490
|
| 11 |
-
assert len(size) + 2 == len(tensor.shape), 'shape must be only the resampled components, ' \
|
| 12 |
-
'WITHOUT the batch and filter channels'
|
| 13 |
-
assert len(tensor.shape) == 3
|
| 14 |
-
|
| 15 |
-
dx = torch.linspace(0 + 0.5, tensor.shape[2] - 1 + 0.5, steps=size[0], dtype=torch.float32).long()
|
| 16 |
-
|
| 17 |
-
tensor_interp = tensor[:, :, dx]
|
| 18 |
-
return tensor_interp
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
def _upsample_int_2d(tensor: TorchTensorNCX, size: ShapeNCX) -> TorchTensorNCX:
|
| 22 |
-
# this is just a workaround! TODO assess the speed impact!
|
| 23 |
-
# see https://discuss.pytorch.org/t/what-is-the-good-way-to-interpolate-int-tensor/29490
|
| 24 |
-
assert len(size) + 2 == len(tensor.shape), 'shape must be only the resampled components, ' \
|
| 25 |
-
'WITHOUT the batch and filter channels'
|
| 26 |
-
assert len(tensor.shape) == 4
|
| 27 |
-
|
| 28 |
-
dx = torch.linspace(0 + 0.5, tensor.shape[2] - 1 + 0.5, steps=size[0], dtype=torch.float32).long()
|
| 29 |
-
dy = torch.linspace(0 + 0.5, tensor.shape[3] - 1 + 0.5, steps=size[1], dtype=torch.float32).long()
|
| 30 |
-
|
| 31 |
-
tensor_interp = tensor[:, :, dx[:, None], dy]
|
| 32 |
-
return tensor_interp
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
def _upsample_int_3d(tensor: TorchTensorNCX, size: ShapeNCX) -> TorchTensorNCX:
|
| 36 |
-
# this is just a workaround! TODO assess the speed impact!
|
| 37 |
-
# see https://discuss.pytorch.org/t/what-is-the-good-way-to-interpolate-int-tensor/29490
|
| 38 |
-
assert len(size) + 2 == len(tensor.shape), 'shape must be only the resampled components, ' \
|
| 39 |
-
'WITHOUT the batch and filter channels'
|
| 40 |
-
assert len(tensor.shape) == 5
|
| 41 |
-
|
| 42 |
-
dx = torch.linspace(0 + 0.5, tensor.shape[2] - 1 + 0.5, steps=size[0], dtype=torch.float32).long()
|
| 43 |
-
dy = torch.linspace(0 + 0.5, tensor.shape[3] - 1 + 0.5, steps=size[1], dtype=torch.float32).long()
|
| 44 |
-
dz = torch.linspace(0 + 0.5, tensor.shape[4] - 1 + 0.5, steps=size[2], dtype=torch.float32).long()
|
| 45 |
-
|
| 46 |
-
tensor_interp = tensor[:, :, dx[:, None, None], dy[:, None], dz]
|
| 47 |
-
return tensor_interp
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
def upsample(tensor: TensorNCX, size: ShapeX, mode: Literal['linear', 'nearest'] = 'linear') -> TensorNCX:
|
| 51 |
-
"""
|
| 52 |
-
Upsample a 1D, 2D, 3D tensor
|
| 53 |
-
|
| 54 |
-
This is a wrapper around `torch.nn.Upsample` to make it more practical. Support integer based tensors.
|
| 55 |
-
|
| 56 |
-
Note:
|
| 57 |
-
PyTorch as of version 1.3 doesn't support non-floating point upsampling
|
| 58 |
-
(see https://github.com/pytorch/pytorch/issues/13218 and https://github.com/pytorch/pytorch/issues/5580).
|
| 59 |
-
Instead use a workaround (TODO assess the speed impact!).
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
Args:
|
| 63 |
-
tensor: 1D (shape = b x c x n), 2D (shape = b x c x h x w) or 3D (shape = b x c x d x h x w)
|
| 64 |
-
size: if 1D, shape = n, if 2D shape = h x w, if 3D shape = d x h x w
|
| 65 |
-
mode: `linear` or `nearest`
|
| 66 |
-
|
| 67 |
-
Returns:
|
| 68 |
-
an up-sampled tensor with same batch size and filter size as the input
|
| 69 |
-
"""
|
| 70 |
-
|
| 71 |
-
assert len(size) + 2 == len(tensor.shape), 'shape must be only the resampled components, ' \
|
| 72 |
-
'WITHOUT the batch and filter channels'
|
| 73 |
-
assert len(tensor.shape) >= 3, 'only 1D, 2D, 3D tensors are currently handled!'
|
| 74 |
-
assert len(tensor.shape) <= 5, 'only 1D, 2D, 3D tensors are currently handled!'
|
| 75 |
-
|
| 76 |
-
size = tuple(size)
|
| 77 |
-
if not torch.is_floating_point(tensor):
|
| 78 |
-
# Workaround for non floating point tensors. Ignore `mode`
|
| 79 |
-
if len(tensor.shape) == 3:
|
| 80 |
-
return _upsample_int_1d(tensor, size)
|
| 81 |
-
elif len(tensor.shape) == 4:
|
| 82 |
-
return _upsample_int_2d(tensor, size)
|
| 83 |
-
elif len(tensor.shape) == 5:
|
| 84 |
-
return _upsample_int_3d(tensor, size)
|
| 85 |
-
else:
|
| 86 |
-
raise NotImplementedError('dimension not implemented!')
|
| 87 |
-
|
| 88 |
-
if mode == 'linear':
|
| 89 |
-
align_corners = True
|
| 90 |
-
if len(tensor.shape) == 4:
|
| 91 |
-
# 2D case
|
| 92 |
-
return nn.Upsample(mode='bilinear', size=size, align_corners=align_corners).forward(tensor)
|
| 93 |
-
elif len(tensor.shape) == 5:
|
| 94 |
-
# 3D case
|
| 95 |
-
return nn.Upsample(mode='trilinear', size=size, align_corners=align_corners).forward(tensor)
|
| 96 |
-
elif len(tensor.shape) == 3:
|
| 97 |
-
# 1D case
|
| 98 |
-
return nn.Upsample(mode='linear', size=size, align_corners=align_corners).forward(tensor)
|
| 99 |
-
else:
|
| 100 |
-
assert 0, 'impossible or bug!'
|
| 101 |
-
|
| 102 |
-
elif mode == 'nearest':
|
| 103 |
-
return nn.Upsample(mode='nearest', size=size).forward(tensor)
|
| 104 |
-
else:
|
| 105 |
-
assert 0, 'upsample mode ({}) is not handled'.format(mode)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|