Skip to content

Commit a9f56c4

Browse files
committed
updated tiling to work with latest kornia version
Former-commit-id: 2360cc0
1 parent e1b4c12 commit a9f56c4

File tree

6 files changed

+76
-38
lines changed

6 files changed

+76
-38
lines changed

.flake8

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[flake8]
2-
ignore = E501
2+
select = E9,F63,F7,F82
3+
ignore = E501, E121, E123
34
exclude = .git,__pycache__,.ipynb_checkpoints
45
per-file-ignores =
56
src/deep_image_matching/thirdparty/RoMa/roma/losses/robust_loss.py:E9,F63,F7,F82

.pre-commit-config.yaml

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
repos:
2+
- repo: https://github.com/psf/black
3+
rev: 23.12.1
4+
hooks:
5+
- id: black
6+
# - repo: https://github.com/pycqa/flake8
7+
# rev: 7.0.0 # Pick a git hash / tag to point to
8+
# hooks:
9+
# - id: flake8
210
- repo: https://github.com/kynan/nbstripout
311
rev: 0.6.1
412
hooks:
513
- id: nbstripout
614
files: ".ipynb"
7-
- repo: https://github.com/psf/black
8-
rev: 23.3.0
9-
hooks:
10-
- id: black

CONTRIBUTING.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
2. Initialize the pre-commit hooks
1212

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

1515
```bash
1616
pre-commit install
@@ -24,18 +24,21 @@
2424
git checkout -b <branch-name>
2525
```
2626

27-
4. Make your changes
27+
4. Make your changes, format your code, and run the tests
2828

29-
Make your changes.
29+
Make your changes (VSCode is strongly recommended as an editor)
3030
Please, format your code with a formatter (e.g. black).
3131
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)).
32-
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.
32+
The pre-commit hooks will automatically run the linter and formatter before each commit.
33+
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).
3334
Then, make sure that the tests are passing. You can manually run the tests with pytest:
3435

3536
```bash
3637
python -m pytest
3738
```
3839

40+
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.
41+
3942
5. Push your changes to the remote repository
4043

4144
Please push your changes to the remote repository, and open a pull request.

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ dependencies = [
3737
"exifread",
3838
"joblib",
3939
"pyyaml",
40+
"packaging",
4041
]
4142
requires-python = ">=3.8"
4243

src/deep_image_matching/utils/tiling.py

+17-17
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ def compute_tiles_by_size(
110110

111111
H, W = input.shape[2:]
112112

113-
# Compute padding to make the image divisible by the window size
114-
# This returns a tuple of 4 int (top, bottom, left, right)
113+
# Compute padding to make the image divisible by the window size.
114+
# This returns a tuple of 2 int (vertical, horizontal)
115+
# Note: from version 0.7.1 compute_padding() returns a tuple of 2 int and not 4 ints (top, bottom, left, right) anymore.
115116
padding = K.contrib.compute_padding((H, W), window_size)
116-
117117
stride = [w - o for w, o in zip(window_size, overlap)]
118118
patches = K.contrib.extract_tensor_patches(
119119
input, window_size, stride=stride, padding=padding
@@ -122,15 +122,25 @@ def compute_tiles_by_size(
122122
# Remove batch dimension
123123
patches = patches.squeeze(0)
124124

125+
# Compute number of rows and columns (fallback if padding is not a tuple of 2 int)
126+
if len(padding) == 2:
127+
n_rows = (H + padding[0] + padding[1] - window_size[0]) // stride[0] + 1
128+
n_cols = (W + padding[0] + padding[1] - window_size[1]) // stride[1] + 1
129+
elif len(padding) == 4:
130+
n_rows = (H + padding[0] + padding[1] - window_size[0]) // stride[0] + 1
131+
n_cols = (W + padding[2] + padding[3] - window_size[1]) // stride[1] + 1
132+
125133
# compute x,y coordinates of the top-left corner of each tile in the original image (before padding)
126134
origins = {}
127-
n_rows = (H + padding[0] + padding[1] - window_size[0]) // stride[0] + 1
128-
n_cols = (W + padding[2] + padding[3] - window_size[1]) // stride[1] + 1
129135
for row in range(n_rows):
130136
for col in range(n_cols):
131137
tile_idx = np.ravel_multi_index((row, col), (n_rows, n_cols), order="C")
132-
x = -padding[2] + col * stride[1]
133-
y = -padding[0] + row * stride[0]
138+
if len(padding) == 2:
139+
x = -padding[1] + col * stride[1]
140+
y = -padding[0] + row * stride[0]
141+
elif len(padding) == 4:
142+
x = -padding[2] + col * stride[1]
143+
y = -padding[0] + row * stride[0]
134144
origins[tile_idx] = (x, y)
135145

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

209-
img_path = Path("data/belv_lingua_easy/DJI_20220728115852_0003.JPG")
210-
img = cv2.imread(str(img_path))
211-
212-
tile_size = (2048, 2730)
213-
overlap = 50
214-
tiler = Tiler()
215-
tiles, origins, padding = tiler.compute_tiles_by_size(
216-
input=img, window_size=tile_size, overlap=overlap
217-
)
218-
219219
print("done")

tests/test_tiling.py

+41-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import kornia
12
import numpy as np
23
import pytest
34
import torch
4-
55
from deep_image_matching.utils.tiling import Tiler
66

77

@@ -10,6 +10,14 @@ def tiler():
1010
return Tiler()
1111

1212

13+
def recent_konria_version(base_version: str = "0.7.1"):
14+
try:
15+
from packaging import version
16+
except ImportError:
17+
return False
18+
return version.parse(kornia.__version__) >= version.parse(base_version)
19+
20+
1321
def test_compute_tiles_by_size_no_overlap_no_padding(tiler):
1422
# Create a numpy array with shape (100, 100, 3)
1523
input_shape = (100, 100, 3)
@@ -25,7 +33,10 @@ def test_compute_tiles_by_size_no_overlap_no_padding(tiler):
2533
assert isinstance(tiles, dict)
2634
assert isinstance(origins, dict)
2735
assert isinstance(padding, tuple)
28-
assert len(padding) == 4
36+
if recent_konria_version():
37+
assert len(padding) == 2
38+
else:
39+
assert len(padding) == 4
2940

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

3849
# Assert the padding values
39-
assert padding == (0, 0, 0, 0)
50+
if recent_konria_version():
51+
assert padding == (0, 0)
52+
else:
53+
assert padding == (0, 0, 0, 0)
4054

4155

4256
def test_compute_tiles_by_size_no_overlap_padding(tiler):
@@ -54,8 +68,10 @@ def test_compute_tiles_by_size_no_overlap_padding(tiler):
5468
assert isinstance(tiles, dict)
5569
assert isinstance(origins, dict)
5670
assert isinstance(padding, tuple)
57-
assert len(padding) == 4
58-
71+
if recent_konria_version():
72+
assert len(padding) == 2
73+
else:
74+
assert len(padding) == 4
5975
# Assert the number of tiles and origins
6076
assert len(tiles) == 9
6177
assert len(origins) == 9
@@ -65,7 +81,10 @@ def test_compute_tiles_by_size_no_overlap_padding(tiler):
6581
assert tile.shape == (window_size, window_size, 3)
6682

6783
# Assert the padding values
68-
assert padding == (10, 10, 10, 10)
84+
if recent_konria_version():
85+
assert padding == (10, 10)
86+
else:
87+
assert padding == (10, 10, 10, 10)
6988

7089

7190
def test_compute_tiles_by_size_overlap_no_padding(tiler):
@@ -83,8 +102,10 @@ def test_compute_tiles_by_size_overlap_no_padding(tiler):
83102
assert isinstance(tiles, dict)
84103
assert isinstance(origins, dict)
85104
assert isinstance(padding, tuple)
86-
assert len(padding) == 4
87-
105+
if recent_konria_version():
106+
assert len(padding) == 2
107+
else:
108+
assert len(padding) == 4
88109
# Assert the number of tiles and origins
89110
assert len(tiles) == 4
90111
assert len(origins) == 4
@@ -94,7 +115,10 @@ def test_compute_tiles_by_size_overlap_no_padding(tiler):
94115
assert tile.shape == (window_size, window_size, 3)
95116

96117
# Assert the padding values
97-
assert padding == (0, 0, 0, 0)
118+
if recent_konria_version():
119+
assert padding == (0, 0)
120+
else:
121+
assert padding == (0, 0, 0, 0)
98122

99123

100124
def test_compute_tiles_by_size_with_torch_tensor(tiler):
@@ -113,8 +137,10 @@ def test_compute_tiles_by_size_with_torch_tensor(tiler):
113137
assert isinstance(tiles, dict)
114138
assert isinstance(origins, dict)
115139
assert isinstance(padding, tuple)
116-
assert len(padding) == 4
117-
140+
if recent_konria_version():
141+
assert len(padding) == 2
142+
else:
143+
assert len(padding) == 4
118144
# Assert the number of tiles and origins
119145
assert len(tiles) == 4
120146
assert len(origins) == 4
@@ -124,7 +150,10 @@ def test_compute_tiles_by_size_with_torch_tensor(tiler):
124150
assert tile.shape == (window_size[0], window_size[1], channels)
125151

126152
# Assert the padding values
127-
assert padding == (0, 0, 0, 0)
153+
if recent_konria_version():
154+
assert padding == (0, 0)
155+
else:
156+
assert padding == (0, 0, 0, 0)
128157

129158

130159
def test_compute_tiles_by_size_with_invalid_input(tiler):

0 commit comments

Comments
 (0)