Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Datumaro visualizer's import errors after introducing lazy import #1220

Merged
merged 5 commits into from
Dec 13, 2023
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
(<https://github.com/openvinotoolkit/datumaro/pull/1210>)
- Fix label compare of distance method
(<https://github.com/openvinotoolkit/datumaro/pull/1205>)
- Fix Datumaro visualizer's import errors after introducing lazy import
(<https://github.com/openvinotoolkit/datumaro/pull/1220>)
- Fix broken link to supported formats in readme
(<https://github.com/openvinotoolkit/datumaro/pull/1221>)

Expand Down
5 changes: 2 additions & 3 deletions src/datumaro/cli/util/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,11 @@
from datumaro.util.import_util import lazy_import

if TYPE_CHECKING:
import matplotlib.pyplot as plt

with warnings.catch_warnings():
warnings.simplefilter("ignore")
import tensorboardX as tb
else:
tb = lazy_import("tensorboardX")
plt = lazy_import("matplotlib.pyplot")


class DistanceCompareVisualizer:
Expand Down Expand Up @@ -292,6 +289,8 @@ def save_as_tensorboard(self, img, name):
self._file_writer.add_image(name, img)

def save_conf_matrix(self, conf_matrix, filename):
import matplotlib.pyplot as plt

def _get_class_map(label_categories):
classes = None
if label_categories is not None:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright (C) 2023 Intel Corporation
#
# SPDX-License-Identifier: MIT
from __future__ import annotations

from collections import defaultdict
from dataclasses import dataclass, field
Expand All @@ -13,8 +14,6 @@
if TYPE_CHECKING:
from matplotlib.figure import Figure
from pandas import DataFrame, Series
else:
DataFrame, Series, Figure = None, None, None


__all__ = ["LossDynamicsAnalyzer", "NoisyLabelCandidate"]
Expand Down
6 changes: 2 additions & 4 deletions src/datumaro/components/visualizer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Copyright (C) 2023 Intel Corporation
#
# SPDX-License-Identifier: MIT
from __future__ import annotations

import logging as log
import math
import random
Expand Down Expand Up @@ -35,10 +37,6 @@
from matplotlib.axes import Axes
from matplotlib.figure import Figure
from matplotlib.text import Text
else:
Figure = None
Axes = None
Text = None

CAPTION_BBOX_PAD = 0.2
DEFAULT_COLOR_CYCLES: List[str] = [
Expand Down
45 changes: 44 additions & 1 deletion tests/unit/test_compare.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
# Copyright (C) 2020-2023 Intel Corporation
#
# SPDX-License-Identifier: MIT
import os
import platform
import unittest
from unittest import TestCase, skipIf
from unittest.mock import call, mock_open, patch
from unittest.mock import MagicMock, call, mock_open, patch

import numpy as np
import pytest

from datumaro.cli.util.compare import DistanceCompareVisualizer
from datumaro.components.annotation import Bbox, Caption, Label, Mask, Points
from datumaro.components.comparator import DistanceComparator, EqualityComparator, TableComparator
from datumaro.components.dataset_base import DEFAULT_SUBSET_NAME, DatasetItem
Expand Down Expand Up @@ -462,3 +468,40 @@ def test_save_compare_report(
call(f"Low-level Comparison:\n{mock_low_level_table}\n\n"),
]
)


class DistanceCompareVisualizerTest:
@pytest.fixture
def fxt_dataset_pair(self):
"""Two datasets pair which has the same (id, subset) dataset item to be compared."""
class_count = 3
dataset_1 = Dataset.from_iterable(
[DatasetItem(id=1, annotations=[Label(label=idx) for idx in range(class_count)])],
categories=[f"label_{idx}" for idx in range(class_count)],
)
dataset_2 = Dataset.from_iterable(
[DatasetItem(id=1, annotations=[Label(label=idx) for idx in range(class_count)])],
categories=[f"label_{idx}" for idx in range(class_count)],
)
return dataset_1, dataset_2

@pytest.mark.parametrize("output_format", ["simple", "tensorboard"])
def test_save(self, fxt_dataset_pair, test_dir, output_format):
mock_dist_comparator = MagicMock(spec=DistanceComparator)

# matches, a_unmatched, b_unmatched = label_diff
mock_dist_comparator.match_labels.return_value = ([0], [1], [2])
mock_dist_comparator.match_boxes.return_value = ([], [], [], [])
mock_dist_comparator.match_polygons.return_value = ([], [], [], [])
mock_dist_comparator.match_masks.return_value = ([], [], [], [])

with DistanceCompareVisualizer(
save_dir=test_dir,
comparator=mock_dist_comparator,
output_format=output_format,
) as visualizer:
first_dataset, second_dataset = fxt_dataset_pair
visualizer.save(first_dataset, second_dataset)

# Assert non-empty after save()
assert os.listdir(test_dir)
Loading