This repository has been archived by the owner on Jul 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 304
/
test_faster_rcnn.py
191 lines (155 loc) · 6.28 KB
/
test_faster_rcnn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
from __future__ import division
import mock
import numpy as np
import unittest
import chainer
from chainer import backends
from chainer import testing
from chainer.testing import attr
from chainercv.links.model.fpn import BboxHead
from chainercv.links.model.fpn import FasterRCNN
from chainercv.links.model.fpn import MaskHead
from chainercv.links.model.fpn import RPN
from chainercv.utils import assert_is_bbox
from chainercv.utils import assert_is_detection_link
from chainercv.utils import assert_is_instance_segmentation_link
def _random_array(xp, shape):
return xp.array(
np.random.uniform(-1, 1, size=shape), dtype=np.float32)
class DummyExtractor(chainer.Link):
scales = (1 / 2, 1 / 4, 1 / 8)
mean = _random_array(np, (3, 1, 1))
def forward(self, x):
n, _, h, w = x.shape
return [chainer.Variable(_random_array(
self.xp, (n, 16, int(h * scale), int(w * scale))))
for scale in self.scales]
class DummyFasterRCNN(FasterRCNN):
def __init__(self, n_fg_class, return_values, min_size, max_size):
extractor = DummyExtractor()
super(DummyFasterRCNN, self).__init__(
extractor=extractor,
rpn=RPN(extractor.scales),
bbox_head=BboxHead(n_fg_class + 1, extractor.scales),
mask_head=MaskHead(n_fg_class + 1, extractor.scales),
return_values=return_values,
min_size=min_size, max_size=max_size,
)
def dummy_roi_average_align_2d(
x, rois, roi_indices, outsize, spatial_scale, sampling_ratio=None):
if not isinstance(outsize, chainer.utils.collections_abc.Iterable):
outsize = outsize, outsize
xp = backends.cuda.get_array_module(x.array)
y = _random_array(xp, (len(rois), x.shape[1], outsize[0], outsize[1]))
return chainer.Variable(y)
@testing.parameterize(*testing.product_dict(
[
{'return_values': 'detection'},
{'return_values': 'instance_segmentation'},
{'return_values': 'rpn'}
],
[
{'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):
if self.return_values == 'detection':
return_values = ['bboxes', 'labels', 'scores']
elif self.return_values == 'instance_segmentation':
return_values = ['masks', 'labels', 'scores']
elif self.return_values == 'rpn':
return_values = ['rois']
self.link = DummyFasterRCNN(n_fg_class=self.n_fg_class,
return_values=return_values,
min_size=self.min_size,
max_size=self.max_size)
def test_use_preset(self):
self.link.nms_thresh = 0
self.link.score_thresh = 0
self.link.use_preset('visualize')
self.assertEqual(self.link.nms_thresh, 0.5)
self.assertEqual(self.link.score_thresh, 0.7)
self.link.nms_thresh = 0
self.link.score_thresh = 0
self.link.use_preset('evaluate')
self.assertEqual(self.link.nms_thresh, 0.5)
self.assertEqual(self.link.score_thresh, 0.05)
with self.assertRaises(ValueError):
self.link.use_preset('unknown')
def _check_call(self):
x = _random_array(self.link.xp, (2, 3, 32, 32))
with chainer.using_config('train', False):
hs, rois, roi_indices = self.link(x)
self.assertEqual(len(hs), len(self.link.extractor.scales))
for l in range(len(self.link.extractor.scales)):
self.assertIsInstance(hs[l], chainer.Variable)
self.assertIsInstance(hs[l].data, self.link.xp.ndarray)
self.assertIsInstance(rois, self.link.xp.ndarray)
self.assertEqual(rois.shape[1:], (4,))
self.assertIsInstance(roi_indices, self.link.xp.ndarray)
self.assertEqual(roi_indices.shape[1:], ())
self.assertEqual(rois.shape[0], roi_indices.shape[0])
@attr.slow
def test_call_cpu(self):
with mock.patch('chainer.functions.roi_average_align_2d',
dummy_roi_average_align_2d):
self._check_call()
@attr.gpu
def test_call_gpu(self):
self.link.to_gpu()
self._check_call()
def test_call_train_mode(self):
x = _random_array(self.link.xp, (2, 3, 32, 32))
with self.assertRaises(AssertionError):
with chainer.using_config('train', True):
self.link(x)
def _check_predict(self):
if self.return_values == 'detection':
assert_is_detection_link(self.link, self.n_fg_class)
elif self.return_values == 'instance_segmentation':
assert_is_instance_segmentation_link(self.link, self.n_fg_class)
elif self.return_values == 'rpn':
imgs = [
np.random.randint(
0, 256, size=(3, 480, 320)).astype(np.float32),
np.random.randint(
0, 256, size=(3, 480, 320)).astype(np.float32)]
result = self.link.predict(imgs)
assert len(result) == 1
assert len(result[0]) == len(imgs)
for i in range(len(result[0])):
roi = result[0][i]
assert_is_bbox(roi)
@attr.slow
def test_predict_cpu(self):
with mock.patch('chainer.functions.roi_average_align_2d',
dummy_roi_average_align_2d):
self._check_predict()
@attr.gpu
def test_predict_gpu(self):
self.link.to_gpu()
self._check_predict()
def test_prepare(self):
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__)