def test_forward_cpu_gpu_equal(self):
        # cpu
        x_cpu = chainer.Variable(self.x)
        rois_cpu = chainer.Variable(self.rois)
        roi_indices_cpu = chainer.Variable(self.roi_indices)
        y_cpu = functions.roi_average_align_2d(
            x_cpu,
            rois_cpu,
            roi_indices_cpu,
            outsize=self.outsize,
            spatial_scale=self.spatial_scale,
            sampling_ratio=self.sampling_ratio,
        )

        # gpu
        x_gpu = chainer.Variable(cuda.to_gpu(self.x))
        rois_gpu = chainer.Variable(cuda.to_gpu(self.rois))
        roi_indices_gpu = chainer.Variable(cuda.to_gpu(self.roi_indices))
        y_gpu = functions.roi_average_align_2d(
            x_gpu,
            rois_gpu,
            roi_indices_gpu,
            outsize=self.outsize,
            spatial_scale=self.spatial_scale,
            sampling_ratio=self.sampling_ratio,
        )
        testing.assert_allclose(y_cpu.data, cuda.to_cpu(y_gpu.data))
Beispiel #2
0
    def forward(self, hs, rois, roi_indices):
        pooled_hs = []
        for l, h in enumerate(hs):
            if len(rois[l]) == 0:
                continue

            pooled_hs.append(
                F.roi_average_align_2d(h, rois[l], roi_indices[l],
                                       self._roi_size, self._scales[l],
                                       self._roi_sample_ratio))

        if len(pooled_hs) == 0:
            out_size = self.segm_size
            segs = chainer.Variable(
                self.xp.empty((0, self._n_class, out_size, out_size),
                              dtype=np.float32))
            return segs

        h = F.concat(pooled_hs, axis=0)
        h = self.conv1(h)
        h = self.conv2(h)
        h = self.conv3(h)
        h = self.conv4(h)
        h = F.relu(self.conv5(h))
        return self.seg(h)
 def f(x, rois, roi_indices):
     return functions.roi_average_align_2d(
         x,
         rois,
         roi_indices,
         outsize=self.outsize,
         spatial_scale=self.spatial_scale,
         sampling_ratio=self.sampling_ratio)
Beispiel #4
0
 def f(x, rois):
     return functions.roi_average_align_2d(
         x,
         rois,
         outh=self.outh,
         outw=self.outw,
         spatial_scale=self.spatial_scale,
         sampling_ratio=self.sampling_ratio)
Beispiel #5
0
    def test_forward_cpu_gpu_equal(self):
        # cpu
        x_cpu = chainer.Variable(self.x)
        rois_cpu = chainer.Variable(self.rois)
        y_cpu = functions.roi_average_align_2d(
            x_cpu, rois_cpu, outh=self.outh, outw=self.outw,
            spatial_scale=self.spatial_scale,
            sampling_ratio=self.sampling_ratio,
        )

        # gpu
        x_gpu = chainer.Variable(cuda.to_gpu(self.x))
        rois_gpu = chainer.Variable(cuda.to_gpu(self.rois))
        y_gpu = functions.roi_average_align_2d(
            x_gpu, rois_gpu, outh=self.outh, outw=self.outw,
            spatial_scale=self.spatial_scale,
            sampling_ratio=self.sampling_ratio,
        )
        testing.assert_allclose(y_cpu.data, cuda.to_cpu(y_gpu.data))
Beispiel #6
0
    def check_forward(self, x_data, roi_data):
        x = chainer.Variable(x_data)
        rois = chainer.Variable(roi_data)
        y = functions.roi_average_align_2d(
            x, rois, outh=self.outh, outw=self.outw,
            spatial_scale=self.spatial_scale,
            sampling_ratio=self.sampling_ratio,
        )
        self.assertEqual(y.data.dtype, numpy.float32)
        y_data = cuda.to_cpu(y.data)

        self.assertEqual(self.gy.shape, y_data.shape)
    def check_forward(self, x_data, roi_data, roi_index_data):
        x = chainer.Variable(x_data)
        rois = chainer.Variable(roi_data)
        roi_indices = chainer.Variable(roi_index_data)
        y = functions.roi_average_align_2d(
            x, rois, roi_indices, outsize=self.outsize,
            spatial_scale=self.spatial_scale,
            sampling_ratio=self.sampling_ratio,
        )
        self.assertEqual(y.data.dtype, numpy.float32)
        y_data = cuda.to_cpu(y.data)

        self.assertEqual(self.gy.shape, y_data.shape)
Beispiel #8
0
    def forward(self, hs, rois, roi_indices):
        """Calculates RoIs.

        Args:
            hs (iterable of array): An iterable of feature maps.
            rois (list of arrays): A list of arrays of shape: math: `(R_l, 4)`,
                where: math: `R_l` is the number of RoIs in the: math: `l`- th
                feature map.
            roi_indices (list of arrays): A list of arrays of
                shape :math:`(R_l,)`.

        Returns:
            tuple of two arrays:
            :obj:`locs` and :obj:`confs`.

            * **locs**: An arrays whose shape is \
                :math:`(R, n\_class, 4)`, where :math:`R` is the total number \
                of RoIs in the batch.
            * **confs**: A list of array whose shape is :math:`(R, n\_class)`.
        """

        hs_ = []
        for l, h in enumerate(hs):
            if len(rois[l]) == 0:
                continue
            h = F.roi_average_align_2d(h, rois[l], roi_indices[l],
                                       self._roi_size, self._scales[l],
                                       self._roi_sample_ratio)
            hs_.append(h)
        hs = hs_

        if len(hs) == 0:
            locs = chainer.Variable(
                self.xp.empty((0, self._n_class, 4), dtype=np.float32))
            confs = chainer.Variable(
                self.xp.empty((0, self._n_class), dtype=np.float32))
            return locs, confs

        h = F.concat(hs, axis=0)
        h = F.reshape(h, (h.shape[0], -1))
        h = F.relu(self.fc1(h))
        h = F.relu(self.fc2(h))

        locs = self.loc(h)
        locs = F.reshape(locs, (locs.shape[0], -1, 4))
        confs = self.conf(h)
        return locs, confs
Beispiel #9
0
 def f(x, rois):
     return functions.roi_average_align_2d(
         x, rois, outh=self.outh, outw=self.outw,
         spatial_scale=self.spatial_scale,
         sampling_ratio=self.sampling_ratio)
 def f(x, rois, roi_indices):
     return functions.roi_average_align_2d(
         x, rois, roi_indices, outsize=self.outsize,
         spatial_scale=self.spatial_scale,
         sampling_ratio=self.sampling_ratio)