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

Commit

Permalink
Merge pull request #830 from 23pointsNorth/fpn_param
Browse files Browse the repository at this point in the history
FPN - Add min/max size params for pre-processing.
  • Loading branch information
Hakuyume authored Mar 12, 2019
2 parents 64ccdbb + 98e5058 commit 975964d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 25 deletions.
13 changes: 10 additions & 3 deletions chainercv/links/model/fpn/faster_rcnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class FasterRCNN(chainer.Chain):
head (Link): A link that has the same interface as
:class:`~chainercv.links.model.fpn.Head`.
Please refer to the documentation found there.
min_size (int): A preprocessing paramter for :meth:`prepare`. Please
refer to a docstring found for :meth:`prepare`.
max_size (int): A preprocessing paramter for :meth:`prepare`. Note
that the result of :meth:`prepare` can exceed this size due to
alignment with stride.
Parameters:
nms_thresh (float): The threshold value
Expand All @@ -40,17 +45,19 @@ class FasterRCNN(chainer.Chain):
"""

_min_size = 800
_max_size = 1333
_stride = 32

def __init__(self, extractor, rpn, head):
def __init__(self, extractor, rpn, head,
min_size=800, max_size=1333):
super(FasterRCNN, self).__init__()
with self.init_scope():
self.extractor = extractor
self.rpn = rpn
self.head = head

self._min_size = min_size
self._max_size = max_size

self.use_preset('visualize')

def use_preset(self, preset):
Expand Down
26 changes: 17 additions & 9 deletions chainercv/links/model/fpn/faster_rcnn_fpn_resnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class FasterRCNNFPNResNet(FasterRCNN):
A subclass of this class should have :obj:`_base` and :obj:`_models`.
"""

def __init__(self, n_fg_class=None, pretrained_model=None):
def __init__(self, n_fg_class=None, pretrained_model=None,
min_size=800, max_size=1333):
param, path = utils.prepare_pretrained_model(
{'n_fg_class': n_fg_class}, pretrained_model, self._models)

Expand All @@ -35,6 +36,7 @@ def __init__(self, n_fg_class=None, pretrained_model=None):
extractor=extractor,
rpn=RPN(extractor.scales),
head=Head(param['n_fg_class'] + 1, extractor.scales),
min_size=min_size, max_size=max_size
)

if path == 'imagenet':
Expand All @@ -56,10 +58,10 @@ class FasterRCNNFPNResNet50(FasterRCNNFPNResNet):
Feature Pyramid Networks for Object Detection. CVPR 2017
Args:
n_fg_class (int): The number of classes excluding the background.
pretrained_model (string): The weight file to be loaded.
This can take :obj:`'coco'`, `filepath` or :obj:`None`.
The default value is :obj:`None`.
n_fg_class (int): The number of classes excluding the background.
pretrained_model (string): The weight file to be loaded.
This can take :obj:`'coco'`, `filepath` or :obj:`None`.
The default value is :obj:`None`.
* :obj:`'coco'`: Load weights trained on train split of \
MS COCO 2017. \
Expand All @@ -74,6 +76,9 @@ class FasterRCNNFPNResNet50(FasterRCNNFPNResNet):
* `filepath`: A path of npz file. In this case, :obj:`n_fg_class` \
must be specified properly.
* :obj:`None`: Do not load weights.
min_size (int): A preprocessing paramter for :meth:`prepare`. Please \
refer to :meth:`prepare`.
max_size (int): A preprocessing paramter for :meth:`prepare`.
"""

Expand All @@ -99,10 +104,10 @@ class FasterRCNNFPNResNet101(FasterRCNNFPNResNet):
Feature Pyramid Networks for Object Detection. CVPR 2017
Args:
n_fg_class (int): The number of classes excluding the background.
pretrained_model (string): The weight file to be loaded.
This can take :obj:`'coco'`, `filepath` or :obj:`None`.
The default value is :obj:`None`.
n_fg_class (int): The number of classes excluding the background.
pretrained_model (string): The weight file to be loaded.
This can take :obj:`'coco'`, `filepath` or :obj:`None`.
The default value is :obj:`None`.
* :obj:`'coco'`: Load weights trained on train split of \
MS COCO 2017. \
Expand All @@ -117,6 +122,9 @@ class FasterRCNNFPNResNet101(FasterRCNNFPNResNet):
* `filepath`: A path of npz file. In this case, :obj:`n_fg_class` \
must be specified properly.
* :obj:`None`: Do not load weights.
min_size (int): A preprocessing paramter for :meth:`prepare`. Please \
refer to :meth:`prepare`.
max_size (int): A preprocessing paramter for :meth:`prepare`.
"""

Expand Down
44 changes: 31 additions & 13 deletions tests/links_tests/model_tests/fpn_tests/test_faster_rcnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,41 @@ def __call__(self, x):

class DummyFasterRCNN(FasterRCNN):

def __init__(self, n_fg_class):
def __init__(self, n_fg_class, min_size, max_size):
extractor = DummyExtractor()
super(DummyFasterRCNN, self).__init__(
extractor=extractor,
rpn=RPN(extractor.scales),
head=Head(n_fg_class + 1, extractor.scales),
min_size=min_size, max_size=max_size,
)


@testing.parameterize(
{'n_fg_class': 1},
{'n_fg_class': 5},
{'n_fg_class': 20},
)
@testing.parameterize(*testing.product_dict(
[
{'n_fg_class': 1},
{'n_fg_class': 5},
{'n_fg_class': 20},
],
[
{
'in_sizes': [(480, 640), (320, 320)],
'min_size': 800, 'max_size': 1333,
'expected_shape': (800, 1088),
},
{
'in_sizes': [(200, 50), (400, 100)],
'min_size': 200, 'max_size': 320,
'expected_shape': (320, 96),
},
],
))
class TestFasterRCNN(unittest.TestCase):

def setUp(self):
self.link = DummyFasterRCNN(n_fg_class=self.n_fg_class)
self.link = DummyFasterRCNN(n_fg_class=self.n_fg_class,
min_size=self.min_size,
max_size=self.max_size)

def test_use_preset(self):
self.link.nms_thresh = 0
Expand Down Expand Up @@ -118,12 +135,13 @@ def test_predict_gpu(self):
assert_is_detection_link(self.link, self.n_fg_class)

def test_prepare(self):
imgs = [
np.random.randint(0, 255, size=(3, 480, 640)).astype(np.float32),
np.random.randint(0, 255, size=(3, 320, 320)).astype(np.float32),
]
x, scales = self.link.prepare(imgs)
self.assertEqual(x.shape, (2, 3, 800, 1088))
imgs = [_random_array(np, (3, s[0], s[1])) for s in self.in_sizes]
out, scales = self.link.prepare(imgs)
self.assertIsInstance(out, np.ndarray)
full_expected_shape = (len(self.in_sizes), 3,
self.expected_shape[0],
self.expected_shape[1])
self.assertEqual(out.shape, full_expected_shape)


testing.run_module(__name__, __file__)

0 comments on commit 975964d

Please sign in to comment.