This repository was archived by the owner on Jul 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 302
Add assert_is_*_link #285
Merged
Merged
Add assert_is_*_link #285
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
bce64c3
add assert_is_detection_link
Hakuyume 2c767d7
update docs
Hakuyume 4687d7d
add docs and tests
Hakuyume 4309d2e
use assert_is_detection_link
Hakuyume d629538
fix bug when bbox is empty
Hakuyume 36065ba
fix return type
Hakuyume b3f0bd2
add assert_is_semantic_segmentation_link
Hakuyume ea54d89
use assert_is_semantic_segmentation_link
Hakuyume 1af165c
fix return type
Hakuyume eaac7f5
autofunctions -> autofunction
Hakuyume File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
from chainercv.utils.testing.assertions import assert_is_bbox # NOQA | ||
from chainercv.utils.testing.assertions import assert_is_detection_dataset # NOQA | ||
from chainercv.utils.testing.assertions import assert_is_detection_link # NOQA | ||
from chainercv.utils.testing.assertions import assert_is_image # NOQA | ||
from chainercv.utils.testing.assertions import assert_is_semantic_segmentation_dataset # NOQA | ||
from chainercv.utils.testing.assertions import assert_is_semantic_segmentation_link # NOQA | ||
from chainercv.utils.testing.constant_stub_link import ConstantStubLink # NOQA | ||
from chainercv.utils.testing.generate_random_bbox import generate_random_bbox # NOQA |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
from chainercv.utils.testing.assertions.assert_is_bbox import assert_is_bbox # NOQA | ||
from chainercv.utils.testing.assertions.assert_is_detection_dataset import assert_is_detection_dataset # NOQA | ||
from chainercv.utils.testing.assertions.assert_is_detection_link import assert_is_detection_link # NOQA | ||
from chainercv.utils.testing.assertions.assert_is_image import assert_is_image # NOQA | ||
from chainercv.utils.testing.assertions.assert_is_semantic_segmentation_dataset import assert_is_semantic_segmentation_dataset # NOQA | ||
from chainercv.utils.testing.assertions.assert_is_semantic_segmentation_link import assert_is_semantic_segmentation_link # NOQA |
59 changes: 59 additions & 0 deletions
59
chainercv/utils/testing/assertions/assert_is_detection_link.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import numpy as np | ||
import six | ||
|
||
from chainercv.utils.testing.assertions.assert_is_bbox import assert_is_bbox | ||
|
||
|
||
def assert_is_detection_link(link, n_fg_class): | ||
"""Checks if a link satisfies detection link APIs. | ||
|
||
This function checks if a given link satisfies detection link APIs | ||
or not. | ||
If the link does not satifiy the APIs, this function raises an | ||
:class:`AssertionError`. | ||
|
||
Args: | ||
link: A link to be checked. | ||
n_fg_class (int): The number of foreground classes. | ||
|
||
""" | ||
|
||
imgs = [ | ||
np.random.randint(0, 256, size=(3, 480, 640)).astype(np.float32), | ||
np.random.randint(0, 256, size=(3, 480, 320)).astype(np.float32)] | ||
|
||
result = link.predict(imgs) | ||
assert len(result) == 3, \ | ||
'Link must return three elements: bboxes, labels and scores.' | ||
bboxes, labels, scores = result | ||
|
||
assert len(bboxes) == len(imgs), \ | ||
'The length of bboxes must be same as that of imgs.' | ||
assert len(labels) == len(imgs), \ | ||
'The length of labels must be same as that of imgs.' | ||
assert len(scores) == len(imgs), \ | ||
'The length of scores must be same as that of imgs.' | ||
|
||
for bbox, label, score in six.moves.zip(bboxes, labels, scores): | ||
assert_is_bbox(bbox) | ||
|
||
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.' | ||
if len(label) > 0: | ||
assert label.min() >= 0 and label.max() < n_fg_class, \ | ||
'The value of label must be in [0, n_fg_class - 1].' | ||
|
||
assert isinstance(score, np.ndarray), \ | ||
'score must be a numpy.ndarray.' | ||
assert score.dtype == np.float32, \ | ||
'The type of score must be numpy.float32.' | ||
assert score.shape[1:] == (), \ | ||
'The shape of score must be (*,).' | ||
assert len(score) == len(bbox), \ | ||
'The length of score must be same as that of bbox.' |
35 changes: 35 additions & 0 deletions
35
chainercv/utils/testing/assertions/assert_is_semantic_segmentation_link.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import numpy as np | ||
import six | ||
|
||
|
||
def assert_is_semantic_segmentation_link(link, n_class): | ||
"""Checks if a link satisfies semantic segmentation link APIs. | ||
|
||
This function checks if a given link satisfies semantic segmentation link | ||
APIs or not. | ||
If the link does not satifiy the APIs, this function raises an | ||
:class:`AssertionError`. | ||
|
||
Args: | ||
link: A link to be checked. | ||
n_class (int): The number of classes including background. | ||
|
||
""" | ||
|
||
imgs = [ | ||
np.random.randint(0, 256, size=(3, 480, 640)).astype(np.float32), | ||
np.random.randint(0, 256, size=(3, 480, 320)).astype(np.float32)] | ||
|
||
labels = link.predict(imgs) | ||
assert len(labels) == len(imgs), \ | ||
'The length of labels must be same as that of imgs.' | ||
|
||
for img, label in six.moves.zip(imgs, labels): | ||
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() >= 0 and label.max() < n_class, \ | ||
'The value of label must be in [0, n_class - 1].' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
tests/utils_tests/testing_tests/assertions_tests/test_assert_is_detection_link.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import numpy as np | ||
import unittest | ||
|
||
import chainer | ||
from chainer import testing | ||
|
||
from chainercv.utils import assert_is_detection_link | ||
from chainercv.utils import generate_random_bbox | ||
|
||
|
||
class DetectionLink(chainer.Link): | ||
|
||
def predict(self, imgs): | ||
bboxes = list() | ||
labels = list() | ||
scores = list() | ||
|
||
for img in imgs: | ||
n_bbox = np.random.randint(0, 10) | ||
bboxes.append(generate_random_bbox( | ||
n_bbox, img.shape[1:], 4, 12)) | ||
labels.append(np.random.randint( | ||
0, 20, size=n_bbox).astype(np.int32)) | ||
scores.append(np.random.uniform( | ||
0, 1, size=n_bbox).astype(np.float32)) | ||
|
||
return bboxes, labels, scores | ||
|
||
|
||
class InvalidPredictionSizeLink(DetectionLink): | ||
|
||
def predict(self, imgs): | ||
bboxes, labels, scores = super( | ||
InvalidPredictionSizeLink, self).predict(imgs) | ||
return bboxes[1:], labels[1:], scores[1:] | ||
|
||
|
||
class InvalidLabelSizeLink(DetectionLink): | ||
|
||
def predict(self, imgs): | ||
bboxes, labels, scores = super( | ||
InvalidLabelSizeLink, self).predict(imgs) | ||
return bboxes, [label[1:] for label in labels], scores | ||
|
||
|
||
class InvalidLabelValueLink(DetectionLink): | ||
|
||
def predict(self, imgs): | ||
bboxes, labels, scores = super( | ||
InvalidLabelValueLink, self).predict(imgs) | ||
return bboxes, [label + 1000 for label in labels], scores | ||
|
||
|
||
class InvalidScoreSizeLink(DetectionLink): | ||
|
||
def predict(self, imgs): | ||
bboxes, labels, scores = super( | ||
InvalidScoreSizeLink, self).predict(imgs) | ||
return bboxes, labels, [score[1:] for score in scores] | ||
|
||
|
||
@testing.parameterize( | ||
{'link': DetectionLink(), 'valid': True}, | ||
{'link': InvalidPredictionSizeLink(), 'valid': False}, | ||
{'link': InvalidLabelSizeLink(), 'valid': False}, | ||
{'link': InvalidLabelValueLink(), 'valid': False}, | ||
{'link': InvalidScoreSizeLink(), 'valid': False}, | ||
) | ||
class TestAssertIsDetectionLink(unittest.TestCase): | ||
|
||
def test_assert_is_detection_link(self): | ||
if self.valid: | ||
assert_is_detection_link(self.link, 20) | ||
else: | ||
with self.assertRaises(AssertionError): | ||
assert_is_detection_link(self.link, 20) | ||
|
||
|
||
testing.run_module(__name__, __file__) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
autofunctions
raises error.Can you use
autofunction
?