Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix typing callable in load storage (#1768)

Edgar Riba5adfeecc9ec4
diff --git a/kornia/contrib/face_detection.py b/kornia/contrib/face_detection.py
index 79110116e..da5220c9e 100644
--- a/kornia/contrib/face_detection.py
+++ b/kornia/contrib/face_detection.py
@@ -281,8 +281,9 @@ class YuFaceDetectNet(nn.Module):
# use torch.hub to load pretrained model
if pretrained:
+ storage_fcn: Callable = lambda storage, loc: storage
pretrained_dict = torch.hub.load_state_dict_from_url(
- url, map_location=lambda storage, loc: storage
+ url, map_location=storage_fcn
)
self.load_state_dict(pretrained_dict, strict=True)
self.eval()
diff --git a/kornia/enhance/zca.py b/kornia/enhance/zca.py
index 1a425278a..446e9bdbc 100644
--- a/kornia/enhance/zca.py
+++ b/kornia/enhance/zca.py
@@ -95,7 +95,7 @@ class ZCAWhitening(nn.Module):
T, mean, T_inv = zca_mean(x, self.dim, self.unbiased, self.eps, self.compute_inv)
- self.mean_vector: torch.Tensor = mean
+ self.mean_vector = mean
self.transform_matrix: torch.Tensor = T
if T_inv is None:
self.transform_inv: Optional[torch.Tensor] = torch.empty([0])
diff --git a/kornia/feature/affine_shape.py b/kornia/feature/affine_shape.py
index e30454a66..75036d089 100644
--- a/kornia/feature/affine_shape.py
+++ b/kornia/feature/affine_shape.py
@@ -1,6 +1,6 @@
import math
import warnings
-from typing import Dict, Optional
+from typing import Callable, Dict, Optional
import torch
import torch.nn as nn
@@ -189,8 +189,9 @@ class LAFAffNetShapeEstimator(nn.Module):
self.patch_size = 32
# use torch.hub to load pretrained model
if pretrained:
+ storage_fcn: Callable = lambda storage, loc: storage
pretrained_dict = torch.hub.load_state_dict_from_url(
- urls['affnet'], map_location=lambda storage, loc: storage
+ urls['affnet'], map_location=storage_fcn
)
self.load_state_dict(pretrained_dict['state_dict'], strict=False)
self.preserve_orientation = preserve_orientation
diff --git a/kornia/feature/defmo.py b/kornia/feature/defmo.py
index d24125937..9cbbb6a7c 100644
--- a/kornia/feature/defmo.py
+++ b/kornia/feature/defmo.py
@@ -287,12 +287,13 @@ class DeFMO(nn.Module):
# use torch.hub to load pretrained model
if pretrained:
+ storage_fcn: Callable = lambda storage, loc: storage
pretrained_dict = torch.hub.load_state_dict_from_url(
- urls['defmo_encoder'], map_location=lambda storage, loc: storage
+ urls['defmo_encoder'], map_location=storage_fcn
)
self.encoder.load_state_dict(pretrained_dict, strict=True)
pretrained_dict_ren = torch.hub.load_state_dict_from_url(
- urls['defmo_rendering'], map_location=lambda storage, loc: storage
+ urls['defmo_rendering'], map_location=storage_fcn
)
self.rendering.load_state_dict(pretrained_dict_ren, strict=True)
self.eval()
diff --git a/kornia/feature/hardnet.py b/kornia/feature/hardnet.py
index 30b4f1567..ef767d7e9 100644
--- a/kornia/feature/hardnet.py
+++ b/kornia/feature/hardnet.py
@@ -1,4 +1,4 @@
-from typing import Dict
+from typing import Callable, Dict
import torch
import torch.nn as nn
@@ -65,8 +65,9 @@ class HardNet(nn.Module):
# use torch.hub to load pretrained model
if pretrained:
+ storage_fcn: Callable = lambda storage, loc: storage
pretrained_dict = torch.hub.load_state_dict_from_url(
- urls['liberty_aug'], map_location=lambda storage, loc: storage
+ urls['liberty_aug'], map_location=storage_fcn
)
self.load_state_dict(pretrained_dict['state_dict'], strict=True)
self.eval()
@@ -145,8 +146,9 @@ class HardNet8(nn.Module):
# use torch.hub to load pretrained model
if pretrained:
+ storage_fcn: Callable = lambda storage, loc: storage
pretrained_dict = torch.hub.load_state_dict_from_url(
- urls['hardnet8v2'], map_location=lambda storage, loc: storage
+ urls['hardnet8v2'], map_location=storage_fcn
)
self.load_state_dict(pretrained_dict, strict=True)
self.eval()
diff --git a/kornia/feature/hynet.py b/kornia/feature/hynet.py
index 7e0ef30fb..4a0b2cea9 100644
--- a/kornia/feature/hynet.py
+++ b/kornia/feature/hynet.py
@@ -1,4 +1,4 @@
-from typing import Dict
+from typing import Callable, Dict
import torch
import torch.nn as nn
@@ -205,8 +205,9 @@ class HyNet(nn.Module):
self.desc_norm = nn.LocalResponseNorm(2 * self.dim_desc, 2.0 * self.dim_desc, 0.5, 0.0)
# use torch.hub to load pretrained model
if pretrained:
+ storage_fcn: Callable = lambda storage, loc: storage
pretrained_dict = torch.hub.load_state_dict_from_url(
- urls['liberty'], map_location=lambda storage, loc: storage
+ urls['liberty'], map_location=storage_fcn
)
self.load_state_dict(pretrained_dict, strict=True)
self.eval()
diff --git a/kornia/feature/keynet.py b/kornia/feature/keynet.py
index d2767c23e..cdc82dc30 100644
--- a/kornia/feature/keynet.py
+++ b/kornia/feature/keynet.py
@@ -1,5 +1,5 @@
import math
-from typing import Dict, List, Optional, Tuple
+from typing import Callable, Dict, List, Optional, Tuple
import torch
import torch.nn as nn
@@ -148,8 +148,9 @@ class KeyNet(nn.Module):
nn.ReLU(inplace=True))
# use torch.hub to load pretrained model
if pretrained:
+ storage_fcn: Callable = lambda storage, loc: storage
pretrained_dict = torch.hub.load_state_dict_from_url(
- urls['keynet'], map_location=lambda storage, loc: storage
+ urls['keynet'], map_location=storage_fcn
)
self.load_state_dict(pretrained_dict['state_dict'], strict=True)
self.eval()
diff --git a/kornia/feature/loftr/loftr.py b/kornia/feature/loftr/loftr.py
index 6c190b8a8..4f4200cbe 100644
--- a/kornia/feature/loftr/loftr.py
+++ b/kornia/feature/loftr/loftr.py
@@ -1,4 +1,4 @@
-from typing import Dict, Optional
+from typing import Callable, Dict, Optional
import torch
import torch.nn as nn
@@ -95,8 +95,10 @@ class LoFTR(nn.Module):
if pretrained is not None:
if pretrained not in urls.keys():
raise ValueError(f"pretrained should be None or one of {urls.keys()}")
+ storage_fcn: Callable = lambda storage, loc: storage
pretrained_dict = torch.hub.load_state_dict_from_url(
- urls[pretrained], map_location=lambda storage, loc: storage)
+ urls[pretrained], map_location=storage_fcn
+ )
self.load_state_dict(pretrained_dict['state_dict'])
self.eval()
diff --git a/kornia/feature/mkd.py b/kornia/feature/mkd.py
index a921b5bd3..dad1ad605 100644
--- a/kornia/feature/mkd.py
+++ b/kornia/feature/mkd.py
@@ -1,4 +1,4 @@
-from typing import Dict, List, Tuple, Union
+from typing import Callable, Dict, List, Tuple, Union
import torch
import torch.nn as nn
@@ -579,8 +579,9 @@ class MKDDescriptor(nn.Module):
# Load supervised(lw)/unsupervised(pca) model trained on training_set.
if self.whitening is not None:
+ storage_fcn: Callable = lambda storage, loc: storage
whitening_models = torch.hub.load_state_dict_from_url(
- urls[self.kernel_type], map_location=lambda storage, loc: storage
+ urls[self.kernel_type], map_location=storage_fcn
)
whitening_model = whitening_models[training_set]
self.whitening_layer = Whitening(
@@ -639,7 +640,10 @@ class MKDDescriptor(nn.Module):
def load_whitening_model(kernel_type: str, training_set: str) -> Dict:
- whitening_models = torch.hub.load_state_dict_from_url(urls[kernel_type], map_location=lambda storage, loc: storage)
+ storage_fcn: Callable = lambda storage, loc: storage
+ whitening_models = torch.hub.load_state_dict_from_url(
+ urls[kernel_type], map_location=storage_fcn
+ )
whitening_model = whitening_models[training_set]
return whitening_model
diff --git a/kornia/feature/orientation.py b/kornia/feature/orientation.py
index 670d2f7ce..b694aceb5 100644
--- a/kornia/feature/orientation.py
+++ b/kornia/feature/orientation.py
@@ -1,5 +1,5 @@
import math
-from typing import Dict, Optional
+from typing import Callable, Dict, Optional
import torch
import torch.nn as nn
@@ -165,8 +165,9 @@ class OriNet(nn.Module):
self.eps = eps
# use torch.hub to load pretrained model
if pretrained:
+ storage_fcn: Callable = lambda storage, loc: storage
pretrained_dict = torch.hub.load_state_dict_from_url(
- urls['orinet'], map_location=lambda storage, loc: storage
+ urls['orinet'], map_location=storage_fcn
)
self.load_state_dict(pretrained_dict['state_dict'], strict=False)
self.eval()
diff --git a/kornia/feature/sosnet.py b/kornia/feature/sosnet.py
index a9674a802..6fa490310 100644
--- a/kornia/feature/sosnet.py
+++ b/kornia/feature/sosnet.py
@@ -1,4 +1,4 @@
-from typing import Dict
+from typing import Callable, Dict
import torch
import torch.nn as nn
@@ -59,7 +59,10 @@ class SOSNet(nn.Module):
self.desc_norm = nn.Sequential(nn.LocalResponseNorm(256, alpha=256.0, beta=0.5, k=0.0))
# load pretrained model
if pretrained:
- pretrained_dict = torch.hub.load_state_dict_from_url(urls['lib'], map_location=lambda storage, loc: storage)
+ storage_fcn: Callable = lambda storage, loc: storage
+ pretrained_dict = torch.hub.load_state_dict_from_url(
+ urls['lib'], map_location=storage_fcn
+ )
self.load_state_dict(pretrained_dict, strict=True)
self.eval()
return
diff --git a/kornia/feature/tfeat.py b/kornia/feature/tfeat.py
index c572e4b9b..8b5d70a7e 100644
--- a/kornia/feature/tfeat.py
+++ b/kornia/feature/tfeat.py
@@ -1,4 +1,4 @@
-from typing import Dict
+from typing import Callable, Dict
import torch
import torch.nn as nn
@@ -54,8 +54,9 @@ class TFeat(nn.Module):
self.descr = nn.Sequential(nn.Linear(64 * 8 * 8, 128), nn.Tanh())
# use torch.hub to load pretrained model
if pretrained:
+ storage_fcn: Callable = lambda storage, loc: storage
pretrained_dict = torch.hub.load_state_dict_from_url(
- urls['liberty'], map_location=lambda storage, loc: storage
+ urls['liberty'], map_location=storage_fcn
)
self.load_state_dict(pretrained_dict, strict=True)
self.eval()
diff --git a/kornia/utils/_compat.py b/kornia/utils/_compat.py
index 5d19b65b5..4c2b85896 100644
--- a/kornia/utils/_compat.py
+++ b/kornia/utils/_compat.py
@@ -16,19 +16,6 @@ def torch_version_geq(major, minor) -> bool:
return _version >= version.parse(f"{major}.{minor}")
-if version.parse(torch_version()) > version.parse("1.7.1"):
- # TODO: remove the type: ignore once Python 3.6 is deprecated.
- # It turns out that Pytorch has no attribute `torch.linalg` for
- # Python 3.6 / PyTorch 1.7.0, 1.7.1
- from torch.linalg import solve # type: ignore
-else:
- from torch import solve as _solve
-
- # NOTE: in previous versions `torch.solve` accepted arguments in another order.
- def solve(A: Tensor, B: Tensor) -> Tensor:
- return _solve(B, A).solution
-
-
if version.parse(torch_version()) > version.parse("1.7.1"):
# TODO: remove the type: ignore once Python 3.6 is deprecated.
# It turns out that Pytorch has no attribute `torch.linalg` for
diff --git a/kornia/utils/helpers.py b/kornia/utils/helpers.py
index 812a580a7..51d505365 100644
--- a/kornia/utils/helpers.py
+++ b/kornia/utils/helpers.py
@@ -5,7 +5,7 @@ from typing import Any, Callable, List, Optional, Tuple
import torch
-from kornia.utils._compat import solve, torch_version_geq
+from kornia.utils._compat import torch_version_geq
def get_cuda_device_if_available(index: int = 0) -> torch.device:
@@ -140,7 +140,7 @@ def _torch_solve_cast(input: torch.Tensor, A: torch.Tensor) -> Tuple[torch.Tenso
if dtype not in (torch.float32, torch.float64):
dtype = torch.float32
- out = solve(A.to(dtype), input.to(dtype))
+ out = torch.linalg.solve(A.to(dtype), input.to(dtype))
return (out.to(input.dtype), out)