|
| 1 | +import numpy as np |
| 2 | +import unittest |
| 3 | + |
| 4 | +from chainer import testing |
| 5 | + |
| 6 | +from chainercv.utils import generate_random_bbox |
| 7 | +from chainercv.visualizations import vis_instance_segmentation |
| 8 | + |
| 9 | +try: |
| 10 | + import matplotlib # NOQA |
| 11 | + optional_modules = True |
| 12 | +except ImportError: |
| 13 | + optional_modules = False |
| 14 | + |
| 15 | + |
| 16 | +@testing.parameterize( |
| 17 | + { |
| 18 | + 'n_bbox': 3, 'label': (0, 1, 2), 'score': (0, 0.5, 1), |
| 19 | + 'label_names': ('c0', 'c1', 'c2')}, |
| 20 | + { |
| 21 | + 'n_bbox': 3, 'label': (0, 1, 2), 'score': None, |
| 22 | + 'label_names': ('c0', 'c1', 'c2')}, |
| 23 | + { |
| 24 | + 'n_bbox': 3, 'label': (0, 1, 2), 'score': (0, 0.5, 1), |
| 25 | + 'label_names': None}, |
| 26 | + { |
| 27 | + 'n_bbox': 3, 'label': None, 'score': (0, 0.5, 1), |
| 28 | + 'label_names': ('c0', 'c1', 'c2')}, |
| 29 | + { |
| 30 | + 'n_bbox': 3, 'label': None, 'score': (0, 0.5, 1), |
| 31 | + 'label_names': None}, |
| 32 | + { |
| 33 | + 'n_bbox': 3, 'label': None, 'score': None, |
| 34 | + 'label_names': None}, |
| 35 | + { |
| 36 | + 'n_bbox': 3, 'label': (0, 1, 1), 'score': (0, 0.5, 1), |
| 37 | + 'label_names': ('c0', 'c1', 'c2')}, |
| 38 | + { |
| 39 | + 'n_bbox': 0, 'label': (), 'score': (), |
| 40 | + 'label_names': ('c0', 'c1', 'c2')}, |
| 41 | +) |
| 42 | +class TestVisInstanceSegmentation(unittest.TestCase): |
| 43 | + |
| 44 | + def setUp(self): |
| 45 | + self.img = np.random.randint(0, 255, size=(3, 32, 48)) |
| 46 | + self.bbox = generate_random_bbox( |
| 47 | + self.n_bbox, (48, 32), 8, 16) |
| 48 | + self.mask = np.random.randint( |
| 49 | + 0, 1, size=(self.n_bbox, 32, 48), dtype=bool) |
| 50 | + if self.label is not None: |
| 51 | + self.label = np.array(self.label, dtype=np.int32) |
| 52 | + if self.score is not None: |
| 53 | + self.score = np.array(self.score) |
| 54 | + |
| 55 | + def test_vis_instance_segmentation(self): |
| 56 | + if not optional_modules: |
| 57 | + return |
| 58 | + |
| 59 | + ax = vis_instance_segmentation( |
| 60 | + self.img, self.bbox, self.mask, self.label, self.score, |
| 61 | + label_names=self.label_names) |
| 62 | + |
| 63 | + self.assertIsInstance(ax, matplotlib.axes.Axes) |
| 64 | + |
| 65 | + |
| 66 | +@testing.parameterize( |
| 67 | + { |
| 68 | + 'n_bbox': 3, 'label': (0, 1), 'score': (0, 0.5, 1), |
| 69 | + 'label_names': ('c0', 'c1', 'c2')}, |
| 70 | + { |
| 71 | + 'n_bbox': 3, 'label': (0, 1, 2, 1), 'score': (0, 0.5, 1), |
| 72 | + 'label_names': ('c0', 'c1', 'c2')}, |
| 73 | +
|
| 74 | + { |
| 75 | + 'n_bbox': 3, 'label': (0, 1, 2), 'score': (0, 0.5), |
| 76 | + 'label_names': ('c0', 'c1', 'c2')}, |
| 77 | + { |
| 78 | + 'n_bbox': 3, 'label': (0, 1, 2), 'score': (0, 0.5, 1, 0.75), |
| 79 | + 'label_names': ('c0', 'c1', 'c2')}, |
| 80 | +
|
| 81 | + { |
| 82 | + 'n_bbox': 3, 'label': (0, 1, 3), 'score': (0, 0.5, 1), |
| 83 | + 'label_names': ('c0', 'c1', 'c2')}, |
| 84 | + { |
| 85 | + 'n_bbox': 3, 'label': (-1, 1, 2), 'score': (0, 0.5, 1), |
| 86 | + 'label_names': ('c0', 'c1', 'c2')}, |
| 87 | +
|
| 88 | +) |
| 89 | +class TestVisInstanceSegmentationInvalidInputs(unittest.TestCase): |
| 90 | + |
| 91 | + def setUp(self): |
| 92 | + self.img = np.random.randint(0, 255, size=(3, 32, 48)) |
| 93 | + self.bbox = np.random.uniform(size=(self.n_bbox, 4)) |
| 94 | + self.mask = np.random.randint( |
| 95 | + 0, 1, size=(self.n_bbox, 32, 48), dtype=bool) |
| 96 | + if self.label is not None: |
| 97 | + self.label = np.array(self.label, dtype=int) |
| 98 | + if self.score is not None: |
| 99 | + self.score = np.array(self.score) |
| 100 | + |
| 101 | + def test_vis_instance_segmentation_invalid_inputs(self): |
| 102 | + if not optional_modules: |
| 103 | + return |
| 104 | + |
| 105 | + with self.assertRaises(ValueError): |
| 106 | + vis_instance_segmentation( |
| 107 | + self.img, self.bbox, self.mask, self.label, self.score, |
| 108 | + label_names=self.label_names) |
| 109 | + |
| 110 | + |
| 111 | +testing.run_module(__name__, __file__) |
0 commit comments