Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

[Feat] update to pytorch 1.7 (#768)

Edgar Ribae41a3eb960db
diff --git a/.github/workflows/tests_cpu.yml b/.github/workflows/tests_cpu.yml
index 8b5f9ab4f..0297d35a5 100644
--- a/.github/workflows/tests_cpu.yml
+++ b/.github/workflows/tests_cpu.yml
@@ -16,7 +16,7 @@ jobs:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: [3.6, 3.7, 3.8]
- pytorch-version: [1.6.0]
+ pytorch-version: [1.7.0]
steps:
- uses: actions/checkout@v2
diff --git a/.github/workflows/tests_cpu_versions.yml b/.github/workflows/tests_cpu_versions.yml
index 0bdf03f8a..05b89c03a 100644
--- a/.github/workflows/tests_cpu_versions.yml
+++ b/.github/workflows/tests_cpu_versions.yml
@@ -17,14 +17,6 @@ jobs:
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: [3.6, 3.7, 3.8]
pytorch-version: [1.5.1, 1.6.0, 1.7.0]
- pytorch-channel: [pytorch]
- include:
- - os: ubuntu-latest
- pytorch-channel: pytorch-nightly
- - os: macos-latest
- pytorch-channel: pytorch-nightly
- - os: windows-latest
- pytorch-channel: pytorch-nightly
steps:
- uses: actions/checkout@v2
- name: Setup conda dependencies
@@ -36,7 +28,7 @@ jobs:
shell: bash -l {0}
run: |
conda install curl make -c conda-forge
- conda install pytorch=${{ matrix.pytorch-version }} cpuonly -c ${{ matrix.pytorch-channel }}
+ conda install pytorch=${{ matrix.pytorch-version }} cpuonly -c pytorch
pip install -r requirements-dev.txt
make install
- name: Run Tests
diff --git a/.github/workflows/tests_cuda.yml b/.github/workflows/tests_cuda.yml
index 70530abb6..7baf61754 100644
--- a/.github/workflows/tests_cuda.yml
+++ b/.github/workflows/tests_cuda.yml
@@ -6,7 +6,7 @@ on:
# - master
#pull_request:
schedule:
- - cron: "0 4 * * *"
+ - cron: "0 0 * * *"
jobs:
tests-cuda:
@@ -18,13 +18,14 @@ jobs:
fail-fast: false
matrix:
python-version: [3.7]
- pytorch-version: [1.6.0]
+ pytorch-version: [1.6.0, 1.7.0]
cuda-version: [9.2, 10.1, 10.2]
steps:
- uses: actions/checkout@v2
- name: Setup conda dependencies
uses: conda-incubator/setup-miniconda@v2
with:
+ miniconda-version: "latest"
auto-update-conda: true
python-version: ${{ matrix.python-version }}
- name: Install dependencies
diff --git a/.github/workflows/tests_nightly.yml b/.github/workflows/tests_nightly.yml
new file mode 100644
index 000000000..03989dabb
--- /dev/null
+++ b/.github/workflows/tests_nightly.yml
@@ -0,0 +1,35 @@
+name: test-nightly
+
+on:
+ #push:
+ # branches:
+ # - master
+ #pull_request:
+ schedule:
+ - cron: "0 4 * * *"
+
+jobs:
+ test-cpu-versions:
+ runs-on: ${{ matrix.os }}
+ strategy:
+ fail-fast: false
+ matrix:
+ os: [ubuntu-latest, macos-latest, windows-latest]
+ python-version: [3.6, 3.7, 3.8]
+ steps:
+ - uses: actions/checkout@v2
+ - name: Setup conda dependencies
+ uses: conda-incubator/setup-miniconda@v2
+ with:
+ auto-update-conda: true
+ python-version: ${{ matrix.python-version }}
+ - name: Install dependencies
+ shell: bash -l {0}
+ run: |
+ conda install curl make -c conda-forge
+ conda install pytorch cpuonly -c pytorch-nightly
+ pip install -r requirements-dev.txt
+ make install
+ - name: Run Tests
+ shell: bash -l {0}
+ run: pytest -v --device cpu --dtype float32,float64 ./test --flake8 --mypy
diff --git a/kornia/geometry/transform/crop/crop2d.py b/kornia/geometry/transform/crop/crop2d.py
index 8586b51c0..7a0c3728c 100644
--- a/kornia/geometry/transform/crop/crop2d.py
+++ b/kornia/geometry/transform/crop/crop2d.py
@@ -258,7 +258,8 @@ def infer_box_shape(boxes: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
return (height, width)
-def validate_bboxes(boxes: torch.Tensor) -> None:
+def validate_bboxes(boxes: torch.Tensor) -> bool:
"""Validate if a 2D bounding box usable or not.
This function checks if the boxes are rectangular or not.
@@ -270,12 +271,19 @@ def validate_bboxes(boxes: torch.Tensor) -> None:
order: top-left, top-right, bottom-right, bottom-left. The
coordinates must be in the x, y order.
"""
- assert torch.allclose((boxes[:, 1, 0] - boxes[:, 0, 0] + 1), (boxes[:, 2, 0] - boxes[:, 3, 0] + 1)), \
- "Boxes must have be rectangular, while get widths %s and %s" % \
- (str(boxes[:, 1, 0] - boxes[:, 0, 0] + 1), str(boxes[:, 2, 0] - boxes[:, 3, 0] + 1))
- assert torch.allclose((boxes[:, 2, 1] - boxes[:, 0, 1] + 1), (boxes[:, 3, 1] - boxes[:, 1, 1] + 1)), \
- "Boxes must have be rectangular, while get heights %s and %s" % \
- (str(boxes[:, 2, 1] - boxes[:, 0, 1] + 1), str(boxes[:, 3, 1] - boxes[:, 1, 1] + 1))
+ if not torch.allclose((boxes[:, 1, 0] - boxes[:, 0, 0] + 1),
+ (boxes[:, 2, 0] - boxes[:, 3, 0] + 1)):
+ raise ValueError("Boxes must have be rectangular, while get widths %s and %s" %
+ (str(boxes[:, 1, 0] - boxes[:, 0, 0] + 1),
+ str(boxes[:, 2, 0] - boxes[:, 3, 0] + 1)))
+
+ if not torch.allclose((boxes[:, 2, 1] - boxes[:, 0, 1] + 1),
+ (boxes[:, 3, 1] - boxes[:, 1, 1] + 1)):
+ raise ValueError("Boxes must have be rectangular, while get heights %s and %s" %
+ (str(boxes[:, 2, 1] - boxes[:, 0, 1] + 1),
+ str(boxes[:, 3, 1] - boxes[:, 1, 1] + 1)))
+
+ return True
def bbox_to_mask(boxes: torch.Tensor, width: int, height: int) -> torch.Tensor:
diff --git a/setup_dev_env.sh b/setup_dev_env.sh
index d3c700c6f..528d38ec7 100755
--- a/setup_dev_env.sh
+++ b/setup_dev_env.sh
@@ -38,6 +38,13 @@ fi
python_version=${PYTHON_VERSION:-"3.7"}
pytorch_version=${PYTORCH_VERSION:-"1.6.0"}
pytorch_mode=${PYTORCH_MODE:-""} # use `cpuonly` for CPU or leave it in blank for GPU
+cuda_version=${CUDA_VERSION:-""}
+
+cuda_toolkit=""
+if [ ! -z "$cuda_version" ]
+then
+ cuda_toolkit="cudatoolkit=$cuda_version"
+fi
# create an environment with the specific python version
$conda_bin config --append channels conda-forge
@@ -49,7 +56,7 @@ $conda_bin clean -ya
source $conda_bin_dir/activate $dev_env_dir/envs/venv
# install pytorch and torchvision
-conda install pytorch=$pytorch_version torchvision $pytorch_mode -c pytorch
+conda install pytorch=$pytorch_version torchvision $cuda_toolkit $pytorch_mode -c pytorch
# install testing dependencies
pip install -r requirements-dev.txt
diff --git a/test/geometry/test_homography.py b/test/geometry/test_homography.py
index 877bbcb09..7a85fa738 100644
--- a/test/geometry/test_homography.py
+++ b/test/geometry/test_homography.py
@@ -48,15 +48,15 @@ class TestFindHomographyDLT:
assert_allclose(H_noweights, H_withweights)
@pytest.mark.parametrize("batch_size", [1, 2, 5])
- def test_clean_points_and_gradcheck(self, batch_size, device):
+ def test_clean_points(self, batch_size, device, dtype):
# generate input data
- H = (torch.eye(3, device=device)[None].repeat(batch_size, 1, 1) +
- 0.3 * torch.rand(batch_size, 3, 3, device=device))
+ points_src = torch.rand(batch_size, 10, 2, device=device, dtype=dtype)
+ H = kornia.eye_like(3, points_src)
+ H = H * 0.3 * torch.rand_like(H)
H = H / H[:, 2:3, 2:3]
- points_src = torch.rand(batch_size, 10, 2).to(device)
points_dst = kornia.transform_points(H, points_src)
- weights = torch.ones(batch_size, 10, device=device)
+ weights = torch.ones(batch_size, 10, device=device, dtype=dtype)
# compute transform from source to target
dst_homo_src = find_homography_dlt(points_src, points_dst, weights)
@@ -64,18 +64,13 @@ class TestFindHomographyDLT:
assert_allclose(
kornia.transform_points(dst_homo_src, points_src), points_dst, rtol=1e-3, atol=1e-4)
- # compute gradient check
- points_src = utils.tensor_to_gradcheck_var(points_src) # to var
- points_dst = utils.tensor_to_gradcheck_var(points_dst) # to var
- weights = utils.tensor_to_gradcheck_var(weights) # to var
-
- assert gradcheck(
- find_homography_dlt, (
- points_src,
- points_dst,
- weights
- ), rtol=1e-1, atol=1e-5,
- raise_exception=True)
+ @pytest.mark.grad
+ def test_gradcheck(self, device, dtype):
+ points_src = torch.rand(1, 10, 2, device=device, dtype=torch.float64, requires_grad=True)
+ points_dst = torch.rand_like(points_src)
+ weights = torch.ones_like(points_src)[..., 0]
+ assert gradcheck(find_homography_dlt, (points_src, points_dst, weights),
+ rtol=1e-1, atol=1e-5, raise_exception=True)
class TestFindHomographyDLTIter:
@@ -97,17 +92,16 @@ class TestFindHomographyDLTIter:
H = find_homography_dlt_iterated(points1, points2, weights, 5)
assert H.shape == (B, 3, 3)
- @pytest.mark.parametrize("batch_size", [1, 2, 3])
- def test_clean_points_and_gradcheck(self, batch_size, device):
+ @pytest.mark.parametrize("batch_size", [1, 2])
+ def test_clean_points(self, batch_size, device, dtype):
# generate input data
- dtype = torch.float64
- H = (torch.eye(3, device=device)[None].repeat(batch_size, 1, 1) +
- 0.3 * torch.rand(batch_size, 3, 3, device=device))
+ points_src = torch.rand(batch_size, 10, 2, device=device, dtype=dtype)
+ H = kornia.eye_like(3, points_src)
+ H = H * 0.3 * torch.rand_like(H)
H = H / H[:, 2:3, 2:3]
- points_src = torch.rand(batch_size, 10, 2).to(device)
points_dst = kornia.transform_points(H, points_src)
- weights = torch.ones(batch_size, 10, device=device)
+ weights = torch.ones(batch_size, 10, device=device, dtype=dtype)
# compute transform from source to target
dst_homo_src = find_homography_dlt_iterated(points_src, points_dst, weights, 10)
@@ -115,49 +109,37 @@ class TestFindHomographyDLTIter:
assert_allclose(
kornia.transform_points(dst_homo_src, points_src), points_dst, rtol=1e-3, atol=1e-4)
- # compute gradient check
- points_src = utils.tensor_to_gradcheck_var(points_src) # to var
- points_dst = utils.tensor_to_gradcheck_var(points_dst) # to var
- weights = utils.tensor_to_gradcheck_var(weights) # to var
- assert gradcheck(
- kornia.find_homography_dlt_iterated, (
- points_src,
- points_dst,
- weights,
- ),
- rtol=1e-3, atol=1e-4,
- raise_exception=True)
-
+ # TODO(ducha-aiki): remove once #795 is fixed
+ @pytest.mark.xfail
+ @pytest.mark.grad
+ def test_gradcheck(self, device, dtype):
+ points_src = torch.rand(1, 10, 2, device=device, dtype=torch.float64, requires_grad=True)
+ points_dst = torch.rand_like(points_src)
+ weights = torch.ones_like(points_src)[..., 0]
+ assert gradcheck(find_homography_dlt_iterated, (points_src, points_dst, weights),
+ rtol=1e-3, atol=1e-4, raise_exception=True)
+
+ # TODO(ducha-aiki): remove once #795 is fixed
+ @pytest.mark.xfail
@pytest.mark.parametrize("batch_size", [1, 2])
- def test_dirty_points_and_gradcheck(self, batch_size, device):
+ def test_dirty_points_and_gradcheck(self, batch_size, device, dtype):
# generate input data
- dtype = torch.float64
- H = (torch.eye(3, device=device)[None].repeat(batch_size, 1, 1) +
- 0.3 * torch.rand(batch_size, 3, 3, device=device))
+ points_src = torch.rand(batch_size, 10, 2, device=device, dtype=dtype)
+ H = kornia.eye_like(3, points_src)
+ H = H * 0.3 * torch.rand_like(H)
H = H / H[:, 2:3, 2:3]
- points_src = 100. * torch.rand(batch_size, 20, 2).to(device)
+ points_src = 100. * torch.rand(batch_size, 20, 2, device=device, dtype=dtype)
points_dst = kornia.transform_points(H, points_src)
# making last point an outlier
points_dst[:, -1, :] += 20
- weights = torch.ones(batch_size, 20, device=device)
+ weights = torch.ones(batch_size, 20, device=device, dtype=dtype)
# compute transform from source to target
dst_homo_src = find_homography_dlt_iterated(points_src, points_dst, weights, 0.5, 10)
assert_allclose(
- kornia.transform_points(dst_homo_src, points_src[:, :-1]), points_dst[:, :-1], rtol=1e-3, atol=1e-3)
-
- # compute gradient check
- points_src = utils.tensor_to_gradcheck_var(points_src) # to var
- points_dst = utils.tensor_to_gradcheck_var(points_dst) # to var
- weights = utils.tensor_to_gradcheck_var(weights) # to var
- assert gradcheck(
- kornia.find_homography_dlt_iterated, (
- points_src,
- points_dst,
- weights,
- ),
- rtol=1e-3, atol=1e-4, raise_exception=True)
+ kornia.transform_points(dst_homo_src, points_src[:, :-1]),
+ points_dst[:, :-1], rtol=1e-3, atol=1e-3)
diff --git a/test/geometry/transform/test_imgwarp.py b/test/geometry/transform/test_imgwarp.py
index 46dade6ca..b07772987 100644
--- a/test/geometry/transform/test_imgwarp.py
+++ b/test/geometry/transform/test_imgwarp.py
@@ -40,15 +40,12 @@ def test_warp_perspective_rotation(batch_shape, device):
assert_allclose(mask_warped_inv * patch,
mask_warped_inv * patch_warped_inv)
- # evaluate function gradient
- patch = utils.tensor_to_gradcheck_var(patch) # to var
- M = utils.tensor_to_gradcheck_var(M, requires_grad=False) # to var
- assert gradcheck(
- kornia.warp_perspective, (patch, M, (
- height,
- width,
- )),
- raise_exception=True)
+
+def test_warp_perspective_gradcheck(device, dtype):
+ H, W = 5, 5
+ patch = torch.rand(1, 1, 5, 5, device=device, dtype=torch.float64, requires_grad=True)
+ M = kornia.eye_like(3, patch)
+ assert gradcheck(kornia.warp_perspective, (patch, M, (H, W),), raise_exception=True)
@pytest.mark.parametrize("batch_size", [1, 2, 5])