Skip to content

Commit 768fc3d

Browse files
authored
Merge pull request #120 from worldcoin/dev
Dev
2 parents f51786e + c9d5d6e commit 768fc3d

8 files changed

Lines changed: 57 additions & 28 deletions

File tree

src/iris/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "1.8.1"
1+
__version__ = "1.8.2"

src/iris/nodes/matcher/hamming_distance_matcher.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class HammingDistanceMatcher(Matcher):
1818
4) If parameters norm_mean and weights are both defined, calculate weighted normalized Hamming distance (WNHD) based on IB_Counts, MB_Counts, norm_mean and weights.
1919
5) Otherwise, calculate Hamming distance (HD) based on IB_Counts and MB_Counts.
2020
6) If parameter rotation_shift is > 0, repeat the above steps for additional rotations of the iriscode.
21-
7) Return the minimium distance from above calculations.
21+
7) Return the minimum distance from above calculations.
2222
"""
2323

2424
class Parameters(Matcher.Parameters):
@@ -29,6 +29,7 @@ class Parameters(Matcher.Parameters):
2929
norm_mean: confloat(ge=0, le=1, strict=True)
3030
norm_gradient: float
3131
separate_half_matching: bool
32+
weights_path: Optional[str]
3233
weights: Optional[List[np.ndarray]]
3334

3435
__parameters_type__ = Parameters
@@ -40,24 +41,29 @@ def __init__(
4041
norm_mean: confloat(ge=0, le=1, strict=True) = 0.45,
4142
norm_gradient: float = 0.00005,
4243
separate_half_matching: bool = True,
44+
weights_path: Optional[str] = None,
4345
weights: Optional[List[np.ndarray]] = None,
4446
) -> None:
4547
"""Assign parameters.
4648
4749
Args:
4850
rotation_shift (Optional[conint(ge=0, strict=True)], optional): Rotation shifts allowed in matching (in columns). Defaults to 15.
4951
normalise (bool, optional): Flag to normalize HD. Defaults to True.
50-
norm_mean (Optional[confloat(ge=0, le = 1, strict=True)], optional): Nonmatch distance used for normalized HD. Optional paremeter for normalized HD. Defaults to 0.45.
52+
norm_mean (Optional[confloat(ge=0, le = 1, strict=True)], optional): Nonmatch distance used for normalized HD. Optional parameter for normalized HD. Defaults to 0.45.
5153
norm_gradient: float, optional): Gradient for linear approximation of normalization term. Defaults to 0.00005.
5254
separate_half_matching (bool, optional): Separate the upper and lower halves for matching. Defaults to True.
53-
weights (Optional[List[np.ndarray]], optional): list of weights table. Optional paremeter for weighted HD. Defaults to None.
55+
weights_path (Optional[str], optional): Path to the weights table. Optional parameter for weighted HD. Defaults to None.
56+
weights (Optional[List[np.ndarray]], optional): list of weights table. Optional parameter for weighted HD. Defaults to None.
5457
"""
58+
if weights_path is not None:
59+
weights = self.load_weights(weights_path)
5560
super().__init__(
5661
rotation_shift=rotation_shift,
5762
normalise=normalise,
5863
norm_mean=norm_mean,
5964
norm_gradient=norm_gradient,
6065
separate_half_matching=separate_half_matching,
66+
weights_path=weights_path,
6167
weights=weights,
6268
)
6369

src/iris/nodes/matcher/hamming_distance_matcher_interface.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import abc
22
from typing import Any, List
3+
import numpy as np
34

45
from pydantic import conint
56

@@ -24,6 +25,28 @@ def __init__(self, **kwargs) -> None:
2425
rotation_shift (int = 15): rotation allowed in matching, converted to columns. Defaults to 15.
2526
"""
2627
self.params = self.__parameters_type__(**kwargs)
28+
29+
def load_weights(self, weights_path: str) -> List[np.array]:
30+
"""Load weights from a file.
31+
32+
Args:
33+
weights_path (str): Path to the weights file.
34+
35+
Returns:
36+
List[Any]: Loaded weights.
37+
"""
38+
with open(weights_path, 'rb') as f:
39+
try:
40+
weights = np.load(f, allow_pickle=True)
41+
if isinstance(weights, np.ndarray):
42+
return [weights]
43+
elif isinstance(weights, list):
44+
return weights
45+
else:
46+
raise ValueError("Weights file does not contain a valid format.")
47+
except Exception as e:
48+
print(f"Error loading weights: {e}")
49+
return []
2750

2851
@abc.abstractmethod
2952
def run(self, template_probe: IrisTemplate, template_gallery: IrisTemplate) -> float:

src/iris/nodes/matcher/utils.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ def normalized_HD(irisbitcount: int, maskbitcount: int, norm_mean: float, norm_g
2525

2626

2727
def get_bitcounts(template_probe: IrisTemplate, template_gallery: IrisTemplate, shift: int) -> np.ndarray:
28-
"""Get bitcounts in iris and mask codes.
28+
"""Get bit counts in iris and mask codes.
2929
3030
Args:
3131
template_probe (IrisTemplate): Iris template from probe.
3232
template_gallery (IrisTemplate): Iris template from gallery.
3333
shift (int): Rotation shift (in columns)
3434
3535
Returns:
36-
np.ndarray: Bitcounts in iris and mask codes.
36+
np.ndarray: Bit counts in iris and mask codes.
3737
"""
3838
irisbits = [
3939
np.roll(probe_code, shift, axis=1) != gallery_code
@@ -52,13 +52,13 @@ def count_nonmatchbits(
5252
half_width: Optional[List[int]] = None,
5353
weights: Optional[List[np.ndarray]] = None,
5454
) -> Union[Tuple[int, int], Tuple[List[int], List[int]]]:
55-
"""Count nonmatch bits for Hammming distance.
55+
"""Count nonmatch bits for Hamming distance.
5656
5757
Args:
5858
irisbits (np.ndarray): Nonmatch irisbits.
5959
maskbits (np.ndarray): Common maskbits.
60-
half_width (Optional[np.ndarray] = None): List of half of code width. Optional paremeter for scoring the upper and lower halves separately. Defaults to None.
61-
weights (Optional[np.ndarray] = None): List of weights table. Optional paremeter for weighted HD. Defaults to None.
60+
half_width (Optional[np.ndarray] = None): List of half of code width. Optional parameter for scoring the upper and lower halves separately. Defaults to None.
61+
weights (Optional[np.ndarray] = None): List of weights table. Optional parameter for weighted HD. Defaults to None.
6262
6363
Returns:
6464
Tuple[int, int]: Total nonmatch iriscode bit count and common maskcode bit count, could be a list for top and bottom iris separately.
@@ -72,10 +72,10 @@ def count_nonmatchbits(
7272

7373
if half_width:
7474
totalirisbitcount = np.sum(
75-
[[np.sum(x[hw:, ...]), np.sum(x[:hw, ...])] for x, hw in zip(irisbitcount, half_width)], axis=0
75+
[[np.sum(x[hw:, ...])*2, np.sum(x[:hw, ...])*2] for x, hw in zip(irisbitcount, half_width)], axis=0
7676
)
7777
totalmaskbitcount = np.sum(
78-
[[np.sum(y[hw:, ...]), np.sum(y[:hw, ...])] for y, hw in zip(maskbitcount, half_width)], axis=0
78+
[[np.sum(y[hw:, ...])*2, np.sum(y[:hw, ...])*2] for y, hw in zip(maskbitcount, half_width)], axis=0
7979
)
8080
else:
8181
totalirisbitcount = np.sum(irisbitcount)
@@ -103,7 +103,7 @@ def simple_hamming_distance(
103103
norm_gradient (float): Gradient for linear approximation of normalization term. Defaults to 0.00005.
104104
105105
Returns:
106-
Tuple[float, int]: Miminum Hamming distance and corresonding rotation shift.
106+
Tuple[float, int]: Minimum Hamming distance and corresponding rotation shift.
107107
"""
108108
for probe_code, gallery_code in zip(template_probe.iris_codes, template_gallery.iris_codes):
109109
if probe_code.shape != gallery_code.shape:
@@ -134,7 +134,7 @@ def simple_hamming_distance(
134134
def hamming_distance(
135135
template_probe: IrisTemplate,
136136
template_gallery: IrisTemplate,
137-
rotation_shift: int,
137+
rotation_shift: int = 15,
138138
normalise: bool = False,
139139
norm_mean: float = 0.45,
140140
norm_gradient: float = 0.00005,
@@ -146,18 +146,18 @@ def hamming_distance(
146146
Args:
147147
template_probe (IrisTemplate): Iris template from probe.
148148
template_gallery (IrisTemplate): Iris template from gallery.
149-
rotation_shift (int): Rotation allowed in matching, converted to columns.
149+
rotation_shift (int): Rotation allowed in matching, converted to columns. Defaults to 15.
150150
normalise (bool, optional): Flag to normalize HD. Defaults to False.
151151
norm_mean (float, optional): Nonmatch mean distance for normalized HD. Defaults to 0.45.
152152
norm_gradient (float): Gradient for linear approximation of normalization term. Defaults to 0.00005.
153153
separate_half_matching (bool, optional): Separate the upper and lower halves for matching. Defaults to False.
154-
weights (Optional[List[np.ndarray]], optional): List of weights table. Optional paremeter for weighted HD. Defaults to None.
154+
weights (Optional[List[np.ndarray]], optional): List of weights table. Optional parameter for weighted HD. Defaults to None.
155155
156156
Raises:
157157
MatcherError: If probe and gallery iris codes are of different sizes or number of columns of iris codes is not even or If weights (when defined) and iris codes are of different sizes.
158158
159159
Returns:
160-
Tuple[float, int]: Miminum Hamming distance and corresonding rotation shift.
160+
Tuple[float, int]: Minimum Hamming distance and corresponding rotation shift.
161161
"""
162162
half_codewidth = []
163163

src/iris/pipelines/confs/multiframe_aggregation_pipeline.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
metadata:
22
pipeline_name: iris_pipeline
3-
iris_version: 1.8.1
3+
iris_version: 1.8.2
44

55
pipeline:
66
- name: segmentation
@@ -305,7 +305,7 @@ pipeline:
305305

306306
templates_aggregation:
307307
metadata:
308-
iris_version: 1.8.1
308+
iris_version: 1.8.2
309309
pipeline_name: templates_aggregation
310310

311311
pipeline:

src/iris/pipelines/confs/pipeline.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
metadata:
22
pipeline_name: iris_pipeline
3-
iris_version: 1.8.1
3+
iris_version: 1.8.2
44

55
pipeline:
66
- name: segmentation

tests/e2e_tests/nodes/matcher/test_e2e_hamming_distance_matcher.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ def load_mock_pickle(name: str) -> Any:
2222
[
2323
pytest.param(10, False, 0.45, 0.00005, True, None, 0.0),
2424
pytest.param(15, False, 0.45, 0.00005, False, None, 0.0),
25-
pytest.param(10, True, 0.45, 0.00005, True, None, 0.0347),
25+
pytest.param(10, True, 0.45, 0.00005, True, None, 0.0026),
2626
pytest.param(15, True, 0.45, 0.00005, False, None, 0),
2727
pytest.param(10, False, 0.45, 0.00005, True, [np.ones([16, 256, 2]), np.ones([16, 256, 2])], 0.0),
2828
pytest.param(15, False, 0.45, 0.00005, False, [np.ones([16, 256, 2]), np.ones([16, 256, 2])], 0.0),
29-
pytest.param(10, True, 0.45, 0.00005, True, [np.ones([16, 256, 2]), np.ones([16, 256, 2])], 0.0347),
29+
pytest.param(10, True, 0.45, 0.00005, True, [np.ones([16, 256, 2]), np.ones([16, 256, 2])], 0.0026),
3030
pytest.param(15, True, 0.45, 0.00005, False, [np.ones([16, 256, 2]), np.ones([16, 256, 2])], 0.0),
3131
pytest.param(10, True, 0.45, 0.001, True, [np.ones([16, 256, 2]), np.ones([16, 256, 2])], 0.0),
3232
pytest.param(15, True, 0.45, 0.00008, False, [np.ones([16, 256, 2]), np.ones([16, 256, 2])], 0.0),

tests/unit_tests/nodes/matcher/test_matcher_utils.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@
136136
0.45,
137137
0.00005,
138138
True,
139-
(0.34997, -1),
139+
(0.34994000000000003, -1),
140140
),
141141
(
142142
IrisTemplate(
@@ -154,7 +154,7 @@
154154
0.45,
155155
0.000046,
156156
True,
157-
(0.7251518000000001, 0),
157+
(0.7253036, 0),
158158
),
159159
(
160160
IrisTemplate(
@@ -208,7 +208,7 @@
208208
0.45,
209209
0.00005,
210210
True,
211-
(0.6250645, -1),
211+
(0.625129, -1),
212212
),
213213
(
214214
IrisTemplate(
@@ -226,7 +226,7 @@
226226
0.45,
227227
0.00005,
228228
True,
229-
(0.5041829166666667, 0),
229+
(0.5041991666666666, 0),
230230
),
231231
(
232232
IrisTemplate(
@@ -395,7 +395,7 @@ def test_hamming_distance(
395395
0.00005,
396396
True,
397397
[np.array([[3, 1], [1, 2]]), np.array([[3, 1], [1, 2]])],
398-
(0.22486408163265306, 0),
398+
(0.22472816326530615, 0),
399399
),
400400
(
401401
IrisTemplate(
@@ -471,7 +471,7 @@ def test_hamming_distance(
471471
0.00005,
472472
True,
473473
[np.array([[3, 1], [1, 2]]), np.array([[3, 1], [1, 2]])],
474-
(0.7251328571428572, 0),
474+
(0.7252657142857142, 0),
475475
),
476476
(
477477
IrisTemplate(
@@ -502,7 +502,7 @@ def test_hamming_distance(
502502
0.00005,
503503
True,
504504
[np.array([[3, 1], [1, 2]]), np.array([[3, 1, 4, 2], [1, 2, 5, 4]])],
505-
(0.7251492394655704, 0),
505+
(0.7252984789311407, 0),
506506
),
507507
],
508508
ids=[

0 commit comments

Comments
 (0)