Skip to content

Commit b11a81b

Browse files
authored
Fix Gray Norm Issue (#328)
1 parent 92d4d38 commit b11a81b

File tree

4 files changed

+38
-10
lines changed

4 files changed

+38
-10
lines changed

luxonis_ml/data/__main__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,8 @@ def export(
472472
] = False,
473473
bucket_storage: BucketStorage = bucket_option,
474474
):
475+
"""Export a Luxonis dataset to disk in native format, with optional
476+
partitioning and zip compression for flexible storage."""
475477
save_dir = save_dir or dataset_name
476478
if delete_existing and Path(save_dir).exists():
477479
shutil.rmtree(save_dir)

luxonis_ml/data/augmentations/albumentations_engine.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,8 @@ def postprocess(
554554
"""
555555
out_labels = {}
556556
out_image = data.pop("image")
557+
if out_image.ndim == 2:
558+
out_image = np.expand_dims(out_image, axis=-1)
557559
image_height, image_width, _ = out_image.shape
558560

559561
bboxes_indices = {}

luxonis_ml/data/loaders/luxonis_loader.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,9 @@ def __getitem__(self, idx: int) -> LoaderOutput:
249249
if not self.exclude_empty_annotations:
250250
img, labels = self._add_empty_annotations(img, labels)
251251

252+
# Albumentations needs RGB
252253
if self.color_space == "BGR":
253254
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
254-
elif self.color_space == "GRAY":
255-
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)[..., np.newaxis]
256255

257256
return img, labels
258257

@@ -316,7 +315,12 @@ def _load_data(self, idx: int) -> tuple[np.ndarray, Labels]:
316315

317316
img_path = self.idx_to_img_path[idx]
318317

319-
img = cv2.cvtColor(cv2.imread(str(img_path)), cv2.COLOR_BGR2RGB)
318+
if self.color_space == "GRAY":
319+
img = cv2.cvtColor(cv2.imread(str(img_path)), cv2.COLOR_RGB2GRAY)[
320+
..., np.newaxis
321+
]
322+
else:
323+
img = cv2.cvtColor(cv2.imread(str(img_path)), cv2.COLOR_BGR2RGB)
320324

321325
labels_by_task: dict[str, list[Annotation]] = defaultdict(list)
322326
class_ids_by_task: dict[str, list[int]] = defaultdict(list)

tests/test_data/test_loader.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from pathlib import Path
55
from typing import Any
66

7-
import cv2
87
import numpy as np
98

109
from luxonis_ml.data import (
@@ -562,19 +561,40 @@ def round_nested_list(
562561

563562

564563
def test_colorspace(storage_url: str, tempdir: Path):
565-
loader = create_loader(storage_url, tempdir)
564+
norm_3d = [
565+
{
566+
"name": "Normalize",
567+
"params": {
568+
"mean": [0.5, 0.5, 0.5],
569+
"std": [0.5, 0.5, 0.5],
570+
"p": 1,
571+
},
572+
},
573+
]
574+
norm_1d = [
575+
{
576+
"name": "Normalize",
577+
"params": {
578+
"mean": [0.5],
579+
"std": [0.5],
580+
"p": 1,
581+
},
582+
},
583+
]
584+
loader = create_loader(storage_url, tempdir, augmentation_config=norm_3d)
566585
rgb_img, _ = next(iter(loader))
567586
assert len(rgb_img.shape) == 3
568587
assert rgb_img.shape[2] == 3
569-
loader = create_loader(storage_url, tempdir, color_space="BGR")
588+
loader = create_loader(
589+
storage_url, tempdir, color_space="BGR", augmentation_config=norm_3d
590+
)
570591
bgr_img, _ = next(iter(loader))
571592
assert len(bgr_img.shape) == 3
572593
assert bgr_img.shape[2] == 3
573594
assert np.array_equal(rgb_img, bgr_img[:, :, ::-1])
574-
loader = create_loader(storage_url, tempdir, color_space="GRAY")
595+
loader = create_loader(
596+
storage_url, tempdir, color_space="GRAY", augmentation_config=norm_1d
597+
)
575598
gray_img, _ = next(iter(loader))
576599
assert len(gray_img.shape) == 3
577600
assert gray_img.shape[2] == 1
578-
assert np.array_equal(
579-
cv2.cvtColor(rgb_img, cv2.COLOR_RGB2GRAY), gray_img[:, :, 0]
580-
)

0 commit comments

Comments
 (0)