Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

refactor(feature, models): resolve Ruff SIM108 (#5610)

Alex76ea95c80488
diff --git a/kornia/feature/dedode/detector.py b/kornia/feature/dedode/detector.py
index 516e7673c..e4cae3558 100644
--- a/kornia/feature/dedode/detector.py
+++ b/kornia/feature/dedode/detector.py
@@ -52,10 +52,8 @@ class DeDoDeDetector(nn.Module):
scales = ["8", "4", "2", "1"]
for idx, (feature_map, scale) in enumerate(zip(reversed(features), scales)):
delta_logits, context = self.decoder(feature_map, context=context, scale=scale)
- if logits is None:
- logits = delta_logits
- else:
- logits = logits + delta_logits.float() # ensure float (need bf16 doesn't have f.interpolate)
+ # ensure float (need bf16 doesn't have f.interpolate)
+ logits = delta_logits if logits is None else logits + delta_logits.float()
if idx < len(scales) - 1:
size = sizes[-(idx + 2)]
logits = F.interpolate(logits, size=size, mode="bicubic", align_corners=False)
diff --git a/kornia/feature/integrated.py b/kornia/feature/integrated.py
index bd4edc384..f8f7bf0d8 100644
--- a/kornia/feature/integrated.py
+++ b/kornia/feature/integrated.py
@@ -777,14 +777,8 @@ class LightGlueMatcher(GeometryAwareDescriptorMatcher):
if len(desc2.shape) == 2:
desc2 = desc2.unsqueeze(0)
dev = lafs1.device
- if hw1 is None:
- hw1_ = keypoints1.max(dim=1)[0].squeeze().flip(0)
- else:
- hw1_ = torch.tensor(hw1, device=dev)
- if hw2 is None:
- hw2_ = keypoints2.max(dim=1)[0].squeeze().flip(0)
- else:
- hw2_ = torch.tensor(hw2, device=dev)
+ hw1_ = keypoints1.max(dim=1)[0].squeeze().flip(0) if hw1 is None else torch.tensor(hw1, device=dev)
+ hw2_ = keypoints2.max(dim=1)[0].squeeze().flip(0) if hw2 is None else torch.tensor(hw2, device=dev)
scale0, ori0 = _lightglue_keypoint_scale_ori(lafs1, self.feature_name)
scale1, ori1 = _lightglue_keypoint_scale_ori(lafs2, self.feature_name)
input_dict = {
diff --git a/kornia/feature/laf.py b/kornia/feature/laf.py
index 9be9e0bd2..21d17ada8 100644
--- a/kornia/feature/laf.py
+++ b/kornia/feature/laf.py
@@ -678,10 +678,7 @@ def extract_patches_simple(
laf = laf.to(device=img.device)
grid_dtype = _promoted_grid_dtype(img.dtype, laf.dtype)
laf = laf.to(grid_dtype) if laf.dtype != grid_dtype else laf
- if normalize_lafs_before_extraction:
- nlaf = normalize_laf(laf, img)
- else:
- nlaf = laf
+ nlaf = normalize_laf(laf, img) if normalize_lafs_before_extraction else laf
_, ch, h, w = img.size()
B, N, _, _ = laf.size()
if B == 0 or N == 0:
@@ -732,10 +729,7 @@ def extract_patches_from_pyramid(
laf = laf.to(device=img.device)
grid_dtype = _promoted_grid_dtype(img.dtype, laf.dtype)
laf = laf.to(grid_dtype) if laf.dtype != grid_dtype else laf
- if normalize_lafs_before_extraction:
- nlaf = normalize_laf(laf, img)
- else:
- nlaf = laf
+ nlaf = normalize_laf(laf, img) if normalize_lafs_before_extraction else laf
B, N, _, _ = laf.size()
_, ch, h, w = img.size()
if B == 0 or N == 0:
diff --git a/kornia/feature/sold2/backbones.py b/kornia/feature/sold2/backbones.py
index 52d6ad054..29de1ada7 100644
--- a/kornia/feature/sold2/backbones.py
+++ b/kornia/feature/sold2/backbones.py
@@ -200,10 +200,7 @@ class Hourglass(nn.Module):
low1 = F.max_pool2d(x, 2, stride=2)
low1 = self.hg[n - 1][1](low1) # type: ignore[index]
- if n > 1:
- low2 = self._hour_glass_forward(n - 1, low1)
- else:
- low2 = self.hg[n - 1][3](low1) # type: ignore[index]
+ low2 = self._hour_glass_forward(n - 1, low1) if n > 1 else self.hg[n - 1][3](low1) # type: ignore[index]
low3 = self.hg[n - 1][2](low2) # type: ignore[index]
up2 = F.interpolate(low3, size=up1.shape[2:])
return up1 + up2
diff --git a/kornia/models/paligemma/modeling_paligemma.py b/kornia/models/paligemma/modeling_paligemma.py
index 2ff02eed4..ee10393b7 100644
--- a/kornia/models/paligemma/modeling_paligemma.py
+++ b/kornia/models/paligemma/modeling_paligemma.py
@@ -323,10 +323,7 @@ class PaliGemma(nn.Module):
"""
vision_outputs = self.vision_tower(pixel_values)
- if isinstance(vision_outputs, (tuple, list)):
- image_features = vision_outputs[1]
- else:
- image_features = vision_outputs
+ image_features = vision_outputs[1] if isinstance(vision_outputs, (tuple, list)) else vision_outputs
if image_features.dim() != 3:
image_features = image_features.unsqueeze(1)
diff --git a/kornia/models/rt_detr/architecture/hybrid_encoder.py b/kornia/models/rt_detr/architecture/hybrid_encoder.py
index 19d346309..63686d983 100644
--- a/kornia/models/rt_detr/architecture/hybrid_encoder.py
+++ b/kornia/models/rt_detr/architecture/hybrid_encoder.py
@@ -58,11 +58,7 @@ class RepVggBlock(nn.Module):
Feature map after either the fused convolution or the summed
``3x3`` and ``1x1`` branches followed by SiLU activation.
"""
- if self.conv is not None:
- out = self.act(self.conv(x))
- else:
- out = self.act(self.conv1(x) + self.conv2(x))
- return out
+ return self.act(self.conv(x) if self.conv is not None else self.conv1(x) + self.conv2(x))
@torch.no_grad()
def optimize_for_deployment(self) -> None:
diff --git a/kornia/models/sam/architecture/mask_decoder.py b/kornia/models/sam/architecture/mask_decoder.py
index f271933a0..da3979e3f 100644
--- a/kornia/models/sam/architecture/mask_decoder.py
+++ b/kornia/models/sam/architecture/mask_decoder.py
@@ -106,10 +106,7 @@ class MaskDecoder(nn.Module):
)
# Select the correct mask or masks for output
- if multimask_output:
- mask_slice = slice(1, None)
- else:
- mask_slice = slice(0, 1)
+ mask_slice = slice(1, None) if multimask_output else slice(0, 1)
masks = masks[:, mask_slice, :, :]
iou_pred = iou_pred[:, mask_slice]
diff --git a/kornia/models/segmentation/segmentation_models.py b/kornia/models/segmentation/segmentation_models.py
index 90736c9c9..1f1b1cd80 100644
--- a/kornia/models/segmentation/segmentation_models.py
+++ b/kornia/models/segmentation/segmentation_models.py
@@ -143,15 +143,9 @@ class SegmentationModelsBuilder:
raise ValueError(f"Unsupported input range: {input_range}")
# Handle mean and std normalization
- if preproc_params["mean"] is not None:
- mean = torch.tensor([preproc_params["mean"]])
- else:
- mean = torch.tensor(0.0)
+ mean = torch.tensor([preproc_params["mean"]]) if preproc_params["mean"] is not None else torch.tensor(0.0)
- if preproc_params["std"] is not None:
- std = torch.tensor([preproc_params["std"]])
- else:
- std = torch.tensor(1.0)
+ std = torch.tensor([preproc_params["std"]]) if preproc_params["std"] is not None else torch.tensor(1.0)
proc_sequence.append(kornia.enhance.Normalize(mean=mean, std=std))
return kornia.augmentation.container.ImageSequential(*proc_sequence)
diff --git a/kornia/models/structures.py b/kornia/models/structures.py
index 4a5b0d94e..0c2d2744b 100644
--- a/kornia/models/structures.py
+++ b/kornia/models/structures.py
@@ -52,10 +52,7 @@ class SegmentationResults:
based on the original resolution logits.
Otherwise, this will use the low resolution logits (self.logits).
"""
- if self._original_res_logits is not None:
- x = self._original_res_logits
- else:
- x = self.logits
+ x = self._original_res_logits if self._original_res_logits is not None else self.logits
return x > self.mask_threshold