Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions kornia/geometry/transform/affwarp.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,16 @@ def _compute_scaling_matrix(scale: Tensor, center: Tensor) -> Tensor:

def _compute_shear_matrix(shear: Tensor) -> Tensor:
"""Compute affine matrix for shearing."""
# Create a 3x3 identity like matrix using the optimized eye_like function
matrix: Tensor = eye_like(3, shear, shared_memory=False)

# Split the shear tensor into two separate tensors for x and y shearing
shx, shy = torch.chunk(shear, chunks=2, dim=-1)
matrix[..., 0, 1:2] += shx
matrix[..., 1, 0:1] += shy

# Efficiently adding shearing values to the identity matrix to form the resulting shear matrix
matrix[..., 0, 1] = shx.squeeze(-1)
matrix[..., 1, 0] = shy.squeeze(-1)

return matrix


Expand Down
7 changes: 6 additions & 1 deletion kornia/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,14 @@ def eye_like(n: int, input: Tensor, shared_memory: bool = False) -> Tensor:
if len(input.shape) < 1:
raise AssertionError(input.shape)

# Create identity matrix of size n
identity = eye(n, device=input.device).type(input.dtype)

return identity[None].expand(input.shape[0], n, n) if shared_memory else identity[None].repeat(input.shape[0], 1, 1)
# Use expand if shared_memory is True; otherwise use repeat
if shared_memory:
return identity[None].expand(input.shape[0], n, n)
else:
return identity.repeat([input.shape[0], 1, 1])


def vec_like(n: int, tensor: Tensor, shared_memory: bool = False) -> Tensor:
Expand Down
8 changes: 8 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,11 @@ ignore_errors = true

[tool.pydocstyle]
match = '.*\.py'

[tool.codeflash]
# All paths are relative to this pyproject.toml's directory.
module-root = "kornia"
tests-root = "tests"
test-framework = "pytest"
ignore-paths = []
formatter-cmds = ["ruff check --exit-zero --fix $file", "ruff format $file"]