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
5 changes: 4 additions & 1 deletion fastembed/image/transform/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,10 @@ def _get_rescale(transforms: list[Transform], config: dict[str, Any]) -> None:

@staticmethod
def _get_normalize(transforms: list[Transform], config: dict[str, Any]) -> None:
if config.get("do_normalize", False):
do_normalize = config.get("do_normalize")
if do_normalize is False:
return
if do_normalize:
transforms.append(Normalize(mean=config["image_mean"], std=config["image_std"]))
elif "mean" in config and "std" in config:
transforms.append(Normalize(mean=config["mean"], std=config["std"]))
Expand Down
17 changes: 17 additions & 0 deletions tests/test_image_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,23 @@ def test_normalize_chw_is_channel_wise(
assert np.allclose(result[c], (image[c] - means[c]) / stds[c], atol=1e-6)


@pytest.mark.parametrize(
("options", "expected"),
[
({"mean": 0.5, "std": 0.25}, [-1.0, 0.0, 1.0]),
({"do_normalize": False, "mean": 0.5, "std": 0.25}, [0.25, 0.5, 0.75]),
({"do_normalize": True, "image_mean": 0.5, "image_std": 0.25}, [-1.0, 0.0, 1.0]),
],
)
def test_normalization_respects_explicit_disable(options: dict, expected: list[float]) -> None:
processor = Compose.from_config({"rescale_factor": 1 / 256, **options})
image = Image.new("RGB", (2, 2), (64, 128, 192))

result = np.asarray(processor([image])[0])

np.testing.assert_array_equal(result[:, 0, 0], expected)


@pytest.mark.parametrize("batch_size", [2, 3])
def test_normalize_batched_matches_per_image(batch_size: int) -> None:
"""A batch must give exactly what the (C, H, W) path gives image by image.
Expand Down