Skip to content
This repository has been archived by the owner on Jul 2, 2021. It is now read-only.

Add tests for VOCDetectionDataset #177

Merged
merged 10 commits into from
Jun 14, 2017
6 changes: 4 additions & 2 deletions chainercv/datasets/voc/voc_detection_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class VOCDetectionDataset(chainer.dataset.DatasetMixin):

The array :obj:`difficult` is a one dimensional boolean array of shape
:math:`(R,)`. :math:`R` is the number of bounding boxes in the image.
If :obj:`use_difficult` is :obj:`False`, this array is
a boolean array with all :obj:`False`.

The type of the image, the bounding boxes and the labels are as follows.

Expand Down Expand Up @@ -76,7 +78,6 @@ def __init__(self, data_dir='auto', split='train', year='2012',
'for 2012 dataset. For 2007 dataset, you can pick \'test\''
' in addition to the above mentioned splits.'
)

id_list_file = os.path.join(
data_dir, 'ImageSets/Main/{0}.txt'.format(split))

Expand Down Expand Up @@ -111,10 +112,10 @@ def get_example(self, i):
for obj in anno.findall('object'):
# when in not using difficult split, and the object is
# difficult, skipt it.
difficult.append(int(obj.find('difficult').text))
if not self.use_difficult and int(obj.find('difficult').text) == 1:
continue

difficult.append(int(obj.find('difficult').text))
bndbox_anno = obj.find('bndbox')
# subtract 1 to make pixel indexes 0-based
bbox.append([
Expand All @@ -124,6 +125,7 @@ def get_example(self, i):
label.append(voc_utils.voc_detection_label_names.index(name))
bbox = np.stack(bbox).astype(np.float32)
label = np.stack(label).astype(np.int32)
# When `use_difficult==False`, all elements in `difficult` are False.
difficult = np.array(difficult, dtype=np.bool)

# Load a image
Expand Down
79 changes: 79 additions & 0 deletions tests/datasets_tests/test_voc/test_voc_detection_dataset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import unittest

import numpy as np

from chainer import testing
from chainer.testing import attr
from chainer.testing import condition

from chainercv.datasets import voc_detection_label_names
from chainercv.datasets import VOCDetectionDataset


def _create_paramters():
split_years = testing.product({
'split': ['train', 'trainval', 'val'],
'year': ['2007', '2012']})
split_years += [{'split': 'test', 'year': '2007'}]
params = testing.product_dict(
split_years,
[{'use_difficult': True, 'return_difficult': True},
{'use_difficult': True, 'return_difficult': False},
{'use_difficult': False, 'return_difficult': True},
{'use_difficult': False, 'return_difficult': False}])
return params


@testing.parameterize(*_create_paramters())
class TestVOCDetectionDataset(unittest.TestCase):

def setUp(self):
self.dataset = VOCDetectionDataset(
split=self.split,
year=self.year,
use_difficult=self.use_difficult,
return_difficult=self.return_difficult)
self.n_out = 4 if self.return_difficult else 3

@attr.slow
@condition.repeat(10)
def test_get_example(self):
i = np.random.randint(0, len(self.dataset))
out = self.dataset[i]

self.assertEqual(len(out), self.n_out)

img, bbox, label = out[:3]
C, H, W = img.shape

self.assertIsInstance(img, np.ndarray)
self.assertEqual(img.dtype, np.float32)
self.assertEqual(C, 3)
self.assertGreaterEqual(np.min(img), 0)
self.assertLessEqual(np.max(img), 255)

self.assertIsInstance(bbox, np.ndarray)
self.assertEqual(bbox.dtype, np.float32)
self.assertEqual(bbox.ndim, 2)
self.assertEqual(bbox.shape[1], 4)
np.testing.assert_array_less(bbox[:, 0], bbox[:, 2])
np.testing.assert_array_less(bbox[:, 1], bbox[:, 3])

self.assertIsInstance(label, np.ndarray)
self.assertEqual(label.dtype, np.int32)
self.assertEqual(label.shape, (bbox.shape[0],))
self.assertGreaterEqual(np.min(label), 0)
self.assertLessEqual(
np.max(label), len(voc_detection_label_names) - 1)

if self.n_out == 4:
difficult = out[3]
self.assertIsInstance(difficult, np.ndarray)
self.assertEqual(difficult.dtype, np.bool)
self.assertEqual(difficult.shape, (bbox.shape[0],))

if not self.use_difficult and self.return_difficult:
np.testing.assert_equal(difficult, 0)


testing.run_module(__name__, __file__)