π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Fixed random_crop_size_generator across different devices (#793)
shijianjian6ae1ce4a1869
diff --git a/kornia/augmentation/random_generator/random_generator.py b/kornia/augmentation/random_generator/random_generator.pyindex 2abe88140..34555c4b5 100644--- a/kornia/augmentation/random_generator/random_generator.py+++ b/kornia/augmentation/random_generator/random_generator.py@@ -452,16 +452,14 @@ def random_crop_size_generator(Note:The generated random numbers are not reproducible across different devices and dtypes.- """-- # TODO: Add a doctest to this function. The following code failed on the cloud environment.- # Examples:- # >>> _ = torch.manual_seed(42)- # >>> random_crop_size_generator(3, (30, 30), scale=torch.tensor([.7, 1.3]), ratio=torch.tensor([.9, 1.]))- # {'size': tensor([[26, 27],- # [27, 28],- # [24, 26]])}+ Examples:+ >>> _ = torch.manual_seed(42)+ >>> random_crop_size_generator(3, (30, 30), scale=torch.tensor([.7, 1.3]), ratio=torch.tensor([.9, 1.]))+ {'size': tensor([[29, 29],+ [27, 28],+ [26, 29]])}+ """_common_param_check(batch_size, same_on_batch)_joint_range_check(scale, "scale")_joint_range_check(ratio, "ratio")@@ -486,10 +484,12 @@ def random_crop_size_generator(h = torch.sqrt(area / aspect_ratio).round().int()# Element-wise w, h conditioncond = ((0 < w) * (w < size[1]) * (0 < h) * (h < size[0])).int()- cond_bool = torch.sum(cond, dim=1) > 0- h_out = w[torch.arange(0, batch_size, device=device, dtype=torch.long), torch.argmax(cond, dim=1)]- w_out = h[torch.arange(0, batch_size, device=device, dtype=torch.long), torch.argmax(cond, dim=1)]+ # torch.argmax is not reproducible accross devices: https://github.com/pytorch/pytorch/issues/17738+ # Here, we will select the first occurance of the duplicated elements.+ cond_bool, argmax_dim1 = ((cond.cumsum(1) == 1) & cond.bool()).max(1)+ h_out = w[torch.arange(0, batch_size, device=device, dtype=torch.long), argmax_dim1]+ w_out = h[torch.arange(0, batch_size, device=device, dtype=torch.long), argmax_dim1]if not cond_bool.all():# Fallback to center cropdiff --git a/test/augmentation/test_random_generator.py b/test/augmentation/test_random_generator.pyindex 247cc8a07..d3133d0b4 100644--- a/test/augmentation/test_random_generator.py+++ b/test/augmentation/test_random_generator.py@@ -552,14 +552,14 @@ class TestRandomCropSizeGen(RandomGeneratorBaseTests):same_on_batch=False)expected = dict(size=torch.tensor([- [89, 87],+ [99, 94],[91, 95],- [81, 87],- [92, 98],- [89, 92],- [88, 90],- [87, 95],- [92, 87]], device=device, dtype=torch.long)+ [90, 96],+ [87, 86],+ [94, 98],+ [87, 81],+ [85, 93],+ [83, 90]], device=device, dtype=torch.long))assert res.keys() == expected.keys()assert_allclose(res['size'], expected['size'])@@ -585,14 +585,14 @@ class TestRandomCropSizeGen(RandomGeneratorBaseTests):same_on_batch=True)expected = dict(size=torch.tensor([- [89, 87],- [89, 87],- [89, 87],- [89, 87],- [89, 87],- [89, 87],- [89, 87],- [89, 87]], device=device, dtype=torch.long),+ [99, 95],+ [99, 95],+ [99, 95],+ [99, 95],+ [99, 95],+ [99, 95],+ [99, 95],+ [99, 95]], device=device, dtype=torch.long),)assert res.keys() == expected.keys()assert_allclose(res['size'], expected['size'])