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

Commit

Permalink
repeat -> n_example
Browse files Browse the repository at this point in the history
  • Loading branch information
Hakuyume committed Jun 14, 2017
1 parent a308a83 commit b09454a
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 51 deletions.
63 changes: 35 additions & 28 deletions chainercv/utils/testing/assertions/assert_is_detection_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from chainercv.utils.testing.assertions.assert_is_image import assert_is_image


def assert_is_detection_dataset(dataset, n_fg_class, repeat=10):
def assert_is_detection_dataset(dataset, n_fg_class, n_example=None):
"""Checks if a dataset satisfies detection dataset APIs.
This function checks if a given dataset satisfies detection dataset APIs
Expand All @@ -16,34 +16,41 @@ def assert_is_detection_dataset(dataset, n_fg_class, repeat=10):
Args:
dataset: A dataset to be checked.
n_fg_class (int): The number of foreground classes.
repeat (int): The number of trials. This function picks
an example randomly and checks it. This argmuments determines,
how many times this function picks and checks.
The default value is :obj:`10`.
n_example (int): The number of examples to be checked.
If this argument is specified, this function picks
examples ramdomly and checks them. Otherwise,
this function checks all examples.
"""

assert len(dataset) > 0, 'The length of dataset must be greater than zero.'

for _ in six.moves.range(repeat):
i = np.random.randint(0, len(dataset))
sample = dataset[i]

assert len(sample) >= 3, \
'Each example must have at least three elements:' \
'img, bbox and label.'

img, bbox, label = sample[:3]

assert_is_image(img, color=True)
assert_is_bbox(bbox, size=img.shape[1:])

assert isinstance(label, np.ndarray), \
'label must be a numpy.ndarray.'
assert label.dtype == np.int32, \
'The type of label must be numpy.int32.'
assert label.shape[1:] == (), \
'The shape of label must be (*,).'
assert len(label) == len(bbox), \
'The length of label must be same as that of bbox.'
assert label.min() >= 0 and label.max() < n_fg_class, \
'The value of label must be in [0, n_fg_class - 1].'
if n_example:
for _ in six.moves.range(n_example):
i = np.random.randint(0, len(dataset))
_check_example(dataset[i], n_fg_class)
else:
for i in six.moves.range(len(dataset)):
_check_example(dataset[i], n_fg_class)


def _check_example(example, n_fg_class):
assert len(example) >= 3, \
'Each example must have at least three elements:' \
'img, bbox and label.'

img, bbox, label = example[:3]

assert_is_image(img, color=True)
assert_is_bbox(bbox, size=img.shape[1:])

assert isinstance(label, np.ndarray), \
'label must be a numpy.ndarray.'
assert label.dtype == np.int32, \
'The type of label must be numpy.int32.'
assert label.shape[1:] == (), \
'The shape of label must be (*,).'
assert len(label) == len(bbox), \
'The length of label must be same as that of bbox.'
assert label.min() >= 0 and label.max() < n_fg_class, \
'The value of label must be in [0, n_fg_class - 1].'
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from chainercv.utils.testing.assertions.assert_is_image import assert_is_image


def assert_is_semantic_segmentation_dataset(dataset, n_class, repeat=10):
def assert_is_semantic_segmentation_dataset(dataset, n_class, n_example=None):
"""Checks if a dataset satisfies semantic segmentation dataset APIs.
This function checks if a given dataset satisfies semantic segmentation
Expand All @@ -15,31 +15,38 @@ def assert_is_semantic_segmentation_dataset(dataset, n_class, repeat=10):
Args:
dataset: A dataset to be checked.
n_class (int): The number of classes including background.
repeat (int): The number of trials. This function picks
an example randomly and checks it. This argmuments determines,
how many times this function picks and checks.
The default value is :obj:`10`.
n_example (int): The number of examples to be checked.
If this argument is specified, this function picks
examples ramdomly and checks them. Otherwise,
this function checks all examples.
"""

assert len(dataset) > 0, 'The length of dataset must be greater than zero.'

for _ in six.moves.range(repeat):
i = np.random.randint(0, len(dataset))
sample = dataset[i]
if n_example:
for _ in six.moves.range(n_example):
i = np.random.randint(0, len(dataset))
_check_example(dataset[i], n_class)
else:
for i in six.moves.range(len(dataset)):
_check_example(dataset[i], n_class)


assert len(sample) >= 2, \
'Each example must have at least two elements:' \
'img and label.'
def _check_example(example, n_class):
assert len(example) >= 2, \
'Each example must have at least two elements:' \
'img and label.'

img, label = sample[:2]
img, label = example[:2]

assert_is_image(img, color=True)
assert_is_image(img, color=True)

assert isinstance(label, np.ndarray), \
'label must be a numpy.ndarray.'
assert label.dtype == np.int32, \
'The type of label must be numpy.int32.'
assert label.shape == img.shape[1:], \
'The shape of label must be (H, W).'
assert label.min() >= -1 and label.max() < n_class, \
'The value of label must be in [-1, n_class - 1].'
assert isinstance(label, np.ndarray), \
'label must be a numpy.ndarray.'
assert label.dtype == np.int32, \
'The type of label must be numpy.int32.'
assert label.shape == img.shape[1:], \
'The shape of label must be (H, W).'
assert label.min() >= -1 and label.max() < n_class, \
'The value of label must be in [-1, n_class - 1].'
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __init__(self, *options):
self.options = options

def __len__(self):
return 100
return 10

def get_example(self, i):
img = np.random.randint(0, 256, size=(3, 48, 64))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def __init__(self, *options):
self.options = options

def __len__(self):
return 100
return 10

def get_example(self, i):
img = np.random.randint(0, 256, size=(3, 48, 64))
Expand Down

0 comments on commit b09454a

Please sign in to comment.