Skip to content

Commit

Permalink
updated tiling to work with latest kornia version
Browse files Browse the repository at this point in the history
Former-commit-id: 2360cc0
  • Loading branch information
franioli committed Jan 24, 2024
1 parent e1b4c12 commit a9f56c4
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 38 deletions.
3 changes: 2 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[flake8]
ignore = E501
select = E9,F63,F7,F82
ignore = E501, E121, E123
exclude = .git,__pycache__,.ipynb_checkpoints
per-file-ignores =
src/deep_image_matching/thirdparty/RoMa/roma/losses/robust_loss.py:E9,F63,F7,F82
Expand Down
12 changes: 8 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
repos:
- repo: https://github.com/psf/black
rev: 23.12.1
hooks:
- id: black
# - repo: https://github.com/pycqa/flake8
# rev: 7.0.0 # Pick a git hash / tag to point to
# hooks:
# - id: flake8
- repo: https://github.com/kynan/nbstripout
rev: 0.6.1
hooks:
- id: nbstripout
files: ".ipynb"
- repo: https://github.com/psf/black
rev: 23.3.0
hooks:
- id: black
11 changes: 7 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

2. Initialize the pre-commit hooks

Please initialize the pre-commit hooks, which will automatically run the linters (check for code errors) and formatting (with blakc) before each commit.
Please initialize the pre-commit hooks, which will automatically run the flake8 linter (check for code errors) and formatting (with blakc) before each commit.

```bash
pre-commit install
Expand All @@ -24,18 +24,21 @@
git checkout -b <branch-name>
```

4. Make your changes
4. Make your changes, format your code, and run the tests

Make your changes.
Make your changes (VSCode is strongly recommended as an editor)
Please, format your code with a formatter (e.g. black).
If you are using VSCode as your editor, you can install the Python extension, and set the formatter to black ([https://code.visualstudio.com/docs/python/formatting](https://code.visualstudio.com/docs/python/formatting)).
The pre-commit hooks will automatically run the linters and formatters before each commit, but better to already have the code well formatted before committing.
The pre-commit hooks will automatically run the linter and formatter before each commit.
If some code needs to be formatted, the pre-commit hooks stop the commit and format the code. You can then commit again (so better to already have the code well formatted before committing to avoid re-doing the commit).
Then, make sure that the tests are passing. You can manually run the tests with pytest:

```bash
python -m pytest
```

If you are using VSCode as your editor, you can also install the [Python extension](https://code.visualstudio.com/docs/python/testing), and run the tests from the editor.

5. Push your changes to the remote repository

Please push your changes to the remote repository, and open a pull request.
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dependencies = [
"exifread",
"joblib",
"pyyaml",
"packaging",
]
requires-python = ">=3.8"

Expand Down
34 changes: 17 additions & 17 deletions src/deep_image_matching/utils/tiling.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ def compute_tiles_by_size(

H, W = input.shape[2:]

# Compute padding to make the image divisible by the window size
# This returns a tuple of 4 int (top, bottom, left, right)
# Compute padding to make the image divisible by the window size.
# This returns a tuple of 2 int (vertical, horizontal)
# Note: from version 0.7.1 compute_padding() returns a tuple of 2 int and not 4 ints (top, bottom, left, right) anymore.
padding = K.contrib.compute_padding((H, W), window_size)

stride = [w - o for w, o in zip(window_size, overlap)]
patches = K.contrib.extract_tensor_patches(
input, window_size, stride=stride, padding=padding
Expand All @@ -122,15 +122,25 @@ def compute_tiles_by_size(
# Remove batch dimension
patches = patches.squeeze(0)

# Compute number of rows and columns (fallback if padding is not a tuple of 2 int)
if len(padding) == 2:
n_rows = (H + padding[0] + padding[1] - window_size[0]) // stride[0] + 1
n_cols = (W + padding[0] + padding[1] - window_size[1]) // stride[1] + 1
elif len(padding) == 4:
n_rows = (H + padding[0] + padding[1] - window_size[0]) // stride[0] + 1
n_cols = (W + padding[2] + padding[3] - window_size[1]) // stride[1] + 1

# compute x,y coordinates of the top-left corner of each tile in the original image (before padding)
origins = {}
n_rows = (H + padding[0] + padding[1] - window_size[0]) // stride[0] + 1
n_cols = (W + padding[2] + padding[3] - window_size[1]) // stride[1] + 1
for row in range(n_rows):
for col in range(n_cols):
tile_idx = np.ravel_multi_index((row, col), (n_rows, n_cols), order="C")
x = -padding[2] + col * stride[1]
y = -padding[0] + row * stride[0]
if len(padding) == 2:
x = -padding[1] + col * stride[1]
y = -padding[0] + row * stride[0]
elif len(padding) == 4:
x = -padding[2] + col * stride[1]
y = -padding[0] + row * stride[0]
origins[tile_idx] = (x, y)

# Convert patches to numpy array (H, W, C)
Expand Down Expand Up @@ -206,14 +216,4 @@ def compute_tiles_auto(self, input: Union[np.ndarray, torch.Tensor]):
)
print(origins)

img_path = Path("data/belv_lingua_easy/DJI_20220728115852_0003.JPG")
img = cv2.imread(str(img_path))

tile_size = (2048, 2730)
overlap = 50
tiler = Tiler()
tiles, origins, padding = tiler.compute_tiles_by_size(
input=img, window_size=tile_size, overlap=overlap
)

print("done")
53 changes: 41 additions & 12 deletions tests/test_tiling.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import kornia
import numpy as np
import pytest
import torch

from deep_image_matching.utils.tiling import Tiler


Expand All @@ -10,6 +10,14 @@ def tiler():
return Tiler()


def recent_konria_version(base_version: str = "0.7.1"):
try:
from packaging import version
except ImportError:
return False
return version.parse(kornia.__version__) >= version.parse(base_version)


def test_compute_tiles_by_size_no_overlap_no_padding(tiler):
# Create a numpy array with shape (100, 100, 3)
input_shape = (100, 100, 3)
Expand All @@ -25,7 +33,10 @@ def test_compute_tiles_by_size_no_overlap_no_padding(tiler):
assert isinstance(tiles, dict)
assert isinstance(origins, dict)
assert isinstance(padding, tuple)
assert len(padding) == 4
if recent_konria_version():
assert len(padding) == 2
else:
assert len(padding) == 4

# Assert the number of tiles and origins
assert len(tiles) == 4
Expand All @@ -36,7 +47,10 @@ def test_compute_tiles_by_size_no_overlap_no_padding(tiler):
assert tile.shape == (window_size, window_size, 3)

# Assert the padding values
assert padding == (0, 0, 0, 0)
if recent_konria_version():
assert padding == (0, 0)
else:
assert padding == (0, 0, 0, 0)


def test_compute_tiles_by_size_no_overlap_padding(tiler):
Expand All @@ -54,8 +68,10 @@ def test_compute_tiles_by_size_no_overlap_padding(tiler):
assert isinstance(tiles, dict)
assert isinstance(origins, dict)
assert isinstance(padding, tuple)
assert len(padding) == 4

if recent_konria_version():
assert len(padding) == 2
else:
assert len(padding) == 4
# Assert the number of tiles and origins
assert len(tiles) == 9
assert len(origins) == 9
Expand All @@ -65,7 +81,10 @@ def test_compute_tiles_by_size_no_overlap_padding(tiler):
assert tile.shape == (window_size, window_size, 3)

# Assert the padding values
assert padding == (10, 10, 10, 10)
if recent_konria_version():
assert padding == (10, 10)
else:
assert padding == (10, 10, 10, 10)


def test_compute_tiles_by_size_overlap_no_padding(tiler):
Expand All @@ -83,8 +102,10 @@ def test_compute_tiles_by_size_overlap_no_padding(tiler):
assert isinstance(tiles, dict)
assert isinstance(origins, dict)
assert isinstance(padding, tuple)
assert len(padding) == 4

if recent_konria_version():
assert len(padding) == 2
else:
assert len(padding) == 4
# Assert the number of tiles and origins
assert len(tiles) == 4
assert len(origins) == 4
Expand All @@ -94,7 +115,10 @@ def test_compute_tiles_by_size_overlap_no_padding(tiler):
assert tile.shape == (window_size, window_size, 3)

# Assert the padding values
assert padding == (0, 0, 0, 0)
if recent_konria_version():
assert padding == (0, 0)
else:
assert padding == (0, 0, 0, 0)


def test_compute_tiles_by_size_with_torch_tensor(tiler):
Expand All @@ -113,8 +137,10 @@ def test_compute_tiles_by_size_with_torch_tensor(tiler):
assert isinstance(tiles, dict)
assert isinstance(origins, dict)
assert isinstance(padding, tuple)
assert len(padding) == 4

if recent_konria_version():
assert len(padding) == 2
else:
assert len(padding) == 4
# Assert the number of tiles and origins
assert len(tiles) == 4
assert len(origins) == 4
Expand All @@ -124,7 +150,10 @@ def test_compute_tiles_by_size_with_torch_tensor(tiler):
assert tile.shape == (window_size[0], window_size[1], channels)

# Assert the padding values
assert padding == (0, 0, 0, 0)
if recent_konria_version():
assert padding == (0, 0)
else:
assert padding == (0, 0, 0, 0)


def test_compute_tiles_by_size_with_invalid_input(tiler):
Expand Down

0 comments on commit a9f56c4

Please sign in to comment.