<commit_before>import numpy as np
import unittest

import chainer
from chainer.backends import cuda
from chainer import testing
from chainer.testing import attr

from chainercv.links.model.faster_rcnn import RegionProposalNetwork


@testing.parameterize(
    {'train': True},
    {'train': False},
)
class TestRegionProposalNetwork(unittest.TestCase):

    def setUp(self):
        feat_stride = 4
        self.B = 2
        C = 16
        H = 8
        W = 12
        self.proposal_creator_params = {
            'n_train_post_nms': 10,
            'n_test_post_nms': 5}
        self.ratios = [0.25, 4]
        self.anchor_scales = [2, 4]
        self.link = RegionProposalNetwork(
            in_channels=C, mid_channels=24,
            ratios=self.ratios, anchor_scales=self.anchor_scales,
            feat_stride=feat_stride,
            proposal_creator_params=self.proposal_creator_params
        )
        self.x = np.random.uniform(size=(self.B, C, H, W)).astype(np.float32)
        self.img_size = (H * feat_stride, W * feat_stride)
        self.scales = np.ones((self.B, ), dtype=np.float32)

        chainer.config.train = self.train

    def _check_call(self, x, img_size, scales):
        _, _, H, W = x.shape
        rpn_locs, rpn_scores, rois, roi_indices, anchor = self.link(
            chainer.Variable(x), img_size, scales)
        self.assertIsInstance(rpn_locs, chainer.Variable)
        self.assertIsInstance(rpn_locs.array, type(x))
        self.assertIsInstance(rpn_scores, chainer.Variable)
        self.assertIsInstance(rpn_scores.array, type(x))

        A = len(self.ratios) * len(self.anchor_scales)
        self.assertEqual(rpn_locs.shape, (self.B, H * W * A, 4))
        self.assertEqual(rpn_scores.shape, (self.B, H * W * A, 2))

        if chainer.config.train:
            roi_size = self.proposal_creator_params[
                'n_train_post_nms']
        else:
            roi_size = self.proposal_creator_params[
                'n_test_post_nms']

        self.assertIsInstance(rois, type(x))
        self.assertIsInstance(roi_indices, type(x))<commit_msg>add test for rpn scales<commit_after>import numpy as np
import unittest

import chainer
from chainer.backends import cuda
from chainer import testing
from chainer.testing import attr

from chainercv.links.model.faster_rcnn import RegionProposalNetwork


@testing.parameterize(*(testing.product({
    'B': [1],
    'train': [True, False],
    'scales': [None, 1.0, 2.0, [1.0]],
}) + testing.product({
    'B': [2],
    'train': [True, False],
    'scales': [None, 1.0, 2.0, [1.0, 2.0]],
})))
class TestRegionProposalNetwork(unittest.TestCase):

    def setUp(self):
        feat_stride = 4
        C = 16
        H = 8
        W = 12
        self.proposal_creator_params = {
            'n_train_post_nms': 10,
            'n_test_post_nms': 5}
        self.ratios = [0.25, 4]
        self.anchor_scales = [2, 4]
        self.link = RegionProposalNetwork(
            in_channels=C, mid_channels=24,
            ratios=self.ratios, anchor_scales=self.anchor_scales,
            feat_stride=feat_stride,
            proposal_creator_params=self.proposal_creator_params
        )
        self.x = np.random.uniform(size=(self.B, C, H, W)).astype(np.float32)
        self.img_size = (H * feat_stride, W * feat_stride)

        chainer.config.train = self.train

    def _check_call(self, x, img_size, scales):
        _, _, H, W = x.shape
        rpn_locs, rpn_scores, rois, roi_indices, anchor = self.link(
            chainer.Variable(x), img_size, scales)
        self.assertIsInstance(rpn_locs, chainer.Variable)
        self.assertIsInstance(rpn_locs.array, type(x))
        self.assertIsInstance(rpn_scores, chainer.Variable)
        self.assertIsInstance(rpn_scores.array, type(x))

        A = len(self.ratios) * len(self.anchor_scales)
        self.assertEqual(rpn_locs.shape, (self.B, H * W * A, 4))
        self.assertEqual(rpn_scores.shape, (self.B, H * W * A, 2))

        if chainer.config.train:
            roi_size = self.proposal_creator_params[
                'n_train_post_nms']
        else:
            roi_size = self.proposal_creator_params[
                'n_test_post_nms']

        self.assertIsInstance(rois, type(x))
        self.assertIsInstance(roi_indices, type(x))
{'fraction_non_alphanumeric': 0.11515151515151516, 'fraction_numerical': 0.01421911421911422, 'mean_word_length': 2.531687242798354, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4558568', 'n_tokens_mistral': 1518, 'n_tokens_neox': 1449, 'n_words': 276}