Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

implement cuda test for integration

edgarribaac267d2807f1
diff --git a/test/integration/test_focal.py b/test/integration/test_focal.py
index 790b5724c..897ff7e42 100644
--- a/test/integration/test_focal.py
+++ b/test/integration/test_focal.py
@@ -5,7 +5,9 @@ import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
-import kornia as kornia
+from test.common import device
+
+import kornia
logger = logging.getLogger(__name__)
@@ -23,7 +25,7 @@ class TestIntegrationFocalLoss:
def generate_sample(self, base_target, std_val=0.1):
target = base_target.float() / base_target.max()
- noise = std_val * torch.rand(1, 1, 6, 5)
+ noise = std_val * torch.rand(1, 1, 6, 5).to(base_target.device)
return target + noise
@staticmethod
@@ -31,17 +33,17 @@ class TestIntegrationFocalLoss:
if isinstance(m, nn.Conv2d):
torch.nn.init.xavier_uniform_(m.weight)
- def test_conv2d_relu(self):
+ def test_conv2d_relu(self, device):
# we generate base sample
- target = torch.LongTensor(1, 6, 5).fill_(0)
+ target = torch.LongTensor(1, 6, 5).fill_(0).to(device)
for i in range(1, self.num_classes):
target[..., i:-i, i:-i] = i
m = nn.Sequential(
nn.Conv2d(1, self.num_classes, kernel_size=3, padding=1),
nn.ReLU(True),
- )
+ ).to(device)
m.apply(self.init_weights)
optimizer = optim.Adam(m.parameters(), lr=self.lr)
@@ -52,16 +54,16 @@ class TestIntegrationFocalLoss:
# criterion = nn.CrossEntropyLoss()
for iter_id in range(self.num_iterations):
- sample = self.generate_sample(target)
+ sample = self.generate_sample(target).to(device)
output = m(sample)
- loss = criterion(output, target)
+ loss = criterion(output, target.to(device))
logger.debug("Loss: {}".format(loss.item()))
optimizer.zero_grad()
loss.backward()
optimizer.step()
- sample = self.generate_sample(target)
+ sample = self.generate_sample(target).to(device)
output_argmax = torch.argmax(m(sample), dim=1)
logger.debug("Output argmax: \n{}".format(output_argmax))
diff --git a/test/integration/test_soft_argmax2d.py b/test/integration/test_soft_argmax2d.py
index 36462915a..5ef4998a2 100644
--- a/test/integration/test_soft_argmax2d.py
+++ b/test/integration/test_soft_argmax2d.py
@@ -1,11 +1,14 @@
import logging
import pytest
+from test.common import device
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
-import kornia as kornia
+from torch.testing import assert_allclose
+
+import kornia
logger = logging.getLogger(__name__)
@@ -26,12 +29,12 @@ class TestIntegrationSoftArgmax2d:
noise = std_val * torch.rand_like(base_target)
return base_target + noise
- def test_regression_2d(self):
+ def test_regression_2d(self, device):
# create the parameters to estimate: the heatmap
- params = nn.Parameter(torch.rand(1, 1, self.height, self.width))
+ params = nn.Parameter(torch.rand(1, 1, self.height, self.width).to(device))
# generate base sample
- target = torch.zeros(1, 1, 2)
+ target = torch.zeros(1, 1, 2).to(device)
target[..., 0] = self.width / 2
target[..., 1] = self.height / 2
@@ -50,7 +53,7 @@ class TestIntegrationSoftArgmax2d:
for iter_id in range(self.num_iterations):
x = params
- sample = self.generate_sample(target)
+ sample = self.generate_sample(target).to(device)
pred = soft_argmax2d(temperature * x)
loss = criterion(pred, sample)
logger.debug("Loss: {0:.3f} Pred: {1}".format(loss.item(), pred))
@@ -59,5 +62,5 @@ class TestIntegrationSoftArgmax2d:
loss.backward()
optimizer.step()
- assert pytest.approx(pred[..., 0].item(), target[..., 0].item())
- assert pytest.approx(pred[..., 1].item(), target[..., 1].item())
+ assert_allclose(pred[..., 0], target[..., 0], rtol=1e-2, atol=1e-2)
+ assert_allclose(pred[..., 1], target[..., 1], rtol=1e-2, atol=1e-2)