Python chainer.functions.get_item() Examples

The following are 20 code examples of chainer.functions.get_item(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module chainer.functions , or try the search function .
Example #1
Source File: utils.py    From kiss with GNU General Public License v3.0 6 votes vote down vote up
def get_corners(self, grids, image_size, scale_to_image_size=True):
        _, _, height, width = grids.shape
        if scale_to_image_size:
            grids = (grids + 1) / 2
            x_points = grids[:, 0, ...] * image_size.width
            y_points = grids[:, 1, ...] * image_size.height
        else:
            x_points = grids[:, 0, ...]
            y_points = grids[:, 1, ...]

        top_left_x = F.get_item(x_points, [..., 0, 0])
        top_left_y = F.get_item(y_points, [..., 0, 0])
        top_right_x = F.get_item(x_points, [..., 0, width - 1])
        top_right_y = F.get_item(y_points, [..., 0, width - 1])
        bottom_left_x = F.get_item(x_points, [..., height - 1, 0])
        bottom_left_y = F.get_item(y_points, [..., height - 1, 0])
        bottom_right_x = F.get_item(x_points, [..., height - 1, width - 1])
        bottom_right_y = F.get_item(y_points, [..., height - 1, width - 1])
        return top_left_x, top_right_x, bottom_left_x, bottom_right_x, top_left_y, top_right_y, bottom_left_y, bottom_right_y 
Example #2
Source File: utils.py    From kiss with GNU General Public License v3.0 6 votes vote down vote up
def calc_bboxes(self, predicted_bboxes, image_size, out_size):
        predicted_bboxes = (predicted_bboxes + 1) / 2
        x_points = predicted_bboxes[:, 0, ...] * image_size.width
        y_points = predicted_bboxes[:, 1, ...] * image_size.height
        top_left_x = F.get_item(x_points, [..., 0, 0])
        top_left_y = F.get_item(y_points, [..., 0, 0])
        bottom_right_x = F.get_item(x_points, [..., out_size.height - 1, out_size.width - 1])
        bottom_right_y = F.get_item(y_points, [..., out_size.height - 1, out_size.width - 1])

        bboxes = F.stack(
            [
                F.minimum(top_left_x, bottom_right_x),
                F.minimum(top_left_y, bottom_right_y),
                F.maximum(top_left_x, bottom_right_x),
                F.maximum(top_left_y, bottom_right_y),
            ],
            axis=1
        )
        return bboxes 
Example #3
Source File: match_bbox.py    From kiss with GNU General Public License v3.0 6 votes vote down vote up
def get_aabb_corners(grids, image_size):
    _, _, height, width = grids.shape
    grids = (grids + 1) / 2
    x_points = grids[:, 0, ...] * image_size.width
    y_points = grids[:, 1, ...] * image_size.height
    x_points = F.clip(x_points, 0., float(image_size.width))
    y_points = F.clip(y_points, 0., float(image_size.height))
    top_left_x = F.get_item(x_points, [..., 0, 0])
    top_left_y = F.get_item(y_points, [..., 0, 0])
    top_right_x = F.get_item(x_points, [..., 0, width - 1])
    top_right_y = F.get_item(y_points, [..., 0, width - 1])
    bottom_right_x = F.get_item(x_points, [..., height - 1, width - 1])
    bottom_right_y = F.get_item(y_points, [..., height - 1, width - 1])
    bottom_left_x = F.get_item(x_points, [..., height - 1, 0])
    bottom_left_y = F.get_item(y_points, [..., height - 1, 0])

    top_left_x_aabb = F.minimum(top_left_x, bottom_left_x)
    top_left_y_aabb = F.minimum(top_left_y, top_right_y)
    bottom_right_x_aabb = F.maximum(top_right_x, bottom_right_x)
    bottom_right_y_aabb = F.maximum(bottom_left_y, bottom_right_y)

    return top_left_y_aabb, top_left_x_aabb, bottom_right_y_aabb, bottom_right_x_aabb 
Example #4
Source File: loss_metrics.py    From see with GNU General Public License v3.0 5 votes vote down vote up
def get_corners(self, grids):
        _, _, height, width = grids.shape
        grids = (grids + 1) / 2
        x_points = grids[:, 0, ...] * self.image_size.width
        y_points = grids[:, 1, ...] * self.image_size.height
        top_left_x = F.get_item(x_points, [..., 0, 0])
        top_left_y = F.get_item(y_points, [..., 0, 0])
        top_right_x = F.get_item(x_points, [..., 0, width - 1])
        top_right_y = F.get_item(y_points, [..., 0, width - 1])
        bottom_left_x = F.get_item(x_points, [..., height - 1, 0])
        bottom_left_y = F.get_item(y_points, [..., height - 1, 0])
        return top_left_x, top_right_x, bottom_left_x, top_left_y, top_right_y, bottom_left_y 
Example #5
Source File: match_bbox.py    From kiss with GNU General Public License v3.0 5 votes vote down vote up
def get_bbox_corners(grids, image_size):
        _, _, height, width = grids.shape
        grids = (grids + 1) / 2
        x_points = grids[:, 0, ...] * image_size.width
        y_points = grids[:, 1, ...] * image_size.height
        x_points = F.clip(x_points, 0., float(image_size.width))
        y_points = F.clip(y_points, 0., float(image_size.height))
        top_left_x = F.get_item(x_points, [..., 0, 0])
        top_left_y = F.get_item(y_points, [..., 0, 0])
        bottom_right_x = F.get_item(x_points, [..., height - 1, width - 1])
        bottom_right_y = F.get_item(y_points, [..., height - 1, width - 1])
        return top_left_x, top_left_y, bottom_right_x, bottom_right_y 
Example #6
Source File: nets_utils.py    From espnet with Apache License 2.0 5 votes vote down vote up
def _subsamplex(x, n):
    x = [F.get_item(xx, (slice(None, None, n), slice(None))) for xx in x]
    ilens = [xx.shape[0] for xx in x]
    return x, ilens 
Example #7
Source File: test_arrays.py    From onnx-chainer with MIT License 5 votes vote down vote up
def test_get_item_error(slices):
    model = chainer.Sequential(
        lambda x: F.get_item(x, slices=slices))
    x = input_generator.increasing(2, 3, 4)

    with pytest.raises(ValueError):
        export(model, x) 
Example #8
Source File: test_arrays.py    From onnx-chainer with MIT License 5 votes vote down vote up
def test_output(self, name, slices):
        skip_opsets = None
        if name.startswith('gathernd'):
            skip_opsets = tuple(range(7, 11))
        name = 'get_item_' + name

        model = chainer.Sequential(
            lambda x: F.get_item(x, slices=slices))
        x = input_generator.increasing(2, 3, 4)

        self.expect(
            model, x, name=name, expected_num_initializers=0,
            skip_opset_version=skip_opsets) 
Example #9
Source File: mdn.py    From models with MIT License 5 votes vote down vote up
def sample(self, x):
        pi, mu, log_var = self.get_gaussian_params(x)
        n_batch = pi.shape[0]

        # Choose one of Gaussian means and vars n_batch times
        ps = chainer.backends.cuda.to_cpu(pi.array)
        idx = [np.random.choice(self.gaussian_mixtures, p=p) for p in ps]
        mu = F.get_item(mu, [range(n_batch), idx])
        log_var = F.get_item(log_var, [range(n_batch), idx])

        # Sampling
        z = F.gaussian(mu, log_var)

        return z 
Example #10
Source File: models.py    From EEND with MIT License 5 votes vote down vote up
def forward(self, xs, activation=None):
        ilens = [x.shape[0] for x in xs]
        # xs: (B, T, F)
        xs = F.pad_sequence(xs, padding=-1)
        pad_shape = xs.shape
        # emb: (B*T, E)
        emb = self.enc(xs)
        # ys: (B*T, C)
        ys = self.linear(emb)
        if activation:
            ys = activation(ys)
        # ys: [(T, C), ...]
        ys = F.separate(ys.reshape(pad_shape[0], pad_shape[1], -1), axis=0)
        ys = [F.get_item(y, slice(0, ilen)) for y, ilen in zip(ys, ilens)]
        return ys 
Example #11
Source File: test_arrays.py    From chainer with MIT License 5 votes vote down vote up
def test_get_item_unsupported_advanced_index(self, slices):
        model = chainer.Sequential(
            lambda x: F.get_item(x, slices=slices))
        x = input_generator.increasing(2, 3, 4)

        with pytest.raises(ValueError):
            export(model, x) 
Example #12
Source File: test_arrays.py    From chainer with MIT License 5 votes vote down vote up
def test_get_item_unsupported(self, slices):
        model = chainer.Sequential(
            lambda x: F.get_item(x, slices=slices))
        x = input_generator.increasing(2, 3, 4)

        with pytest.raises(ValueError):
            export(model, x, opset_version=7) 
Example #13
Source File: test_arrays.py    From chainer with MIT License 5 votes vote down vote up
def test_get_item_slice_step(self, name, slices):
        skip_opsets = tuple(range(7, 11))
        name = 'get_item_' + name

        model = chainer.Sequential(
            lambda x: F.get_item(x, slices=slices))
        x = input_generator.increasing(2, 3, 4)

        self.expect(
            model, x, name=name, expected_num_initializers=0,
            skip_opset_version=skip_opsets) 
Example #14
Source File: test_get_item.py    From chainer with MIT License 5 votes vote down vote up
def test_multiple_ellipsis(self):
        with self.assertRaises(ValueError):
            functions.get_item(self.x_data, (Ellipsis, Ellipsis)) 
Example #15
Source File: test_get_item.py    From chainer with MIT License 5 votes vote down vote up
def check_backward(self, x_data, y_grad):
        slices = []
        for i, s in enumerate(self.slices):
            if isinstance(s, numpy.ndarray):
                s = chainer.backends.cuda.cupy.array(s)
            if isinstance(s, list):
                s = chainer.backends.cuda.cupy.array(s, dtype=numpy.int32)
            slices.append(s)
        slices = tuple(slices)

        def f(x):
            return functions.get_item(x, slices)

        gradient_check.check_backward(
            f, (x_data,), y_grad, dtype='d') 
Example #16
Source File: test_get_item.py    From chainer with MIT License 5 votes vote down vote up
def check_forward(self, x_data):
        slices = []
        for i, s in enumerate(self.slices):
            if isinstance(s, numpy.ndarray):
                s = chainer.backends.cuda.cupy.array(s)
            if isinstance(s, list):
                s = chainer.backends.cuda.cupy.array(s, dtype=numpy.int32)
            slices.append(s)
        slices = tuple(slices)
        x = chainer.Variable(x_data)
        y = functions.get_item(x, slices)
        self.assertEqual(y.data.dtype, numpy.float32)
        numpy.testing.assert_equal(cuda.to_cpu(x_data)[self.slices],
                                   cuda.to_cpu(y.data)) 
Example #17
Source File: test_get_item.py    From chainer with MIT License 5 votes vote down vote up
def forward(self, inputs, device):
        x, = inputs
        slices = self._convert_slices(self.slices, device)
        y = functions.get_item(x, slices)
        return y, 
Example #18
Source File: test_get_item.py    From chainer with MIT License 5 votes vote down vote up
def forward(self, inputs, device):
        x, = inputs
        y = functions.get_item(x, self.slices)
        return y, 
Example #19
Source File: utils.py    From kiss with GNU General Public License v3.0 4 votes vote down vote up
def calc_loss(self, image_size, predicted_grids, gt_bbox_points, objectness_scores, normalize=True):
        predicted_bbox_points = self.get_corners(predicted_grids, image_size, scale_to_image_size=False)

        # 1. transform box coordinates to aabb coordinates for determination of iou
        predicted_bbox_points = predicted_bbox_points[0], predicted_bbox_points[4], predicted_bbox_points[3], predicted_bbox_points[7]
        predicted_bbox_points = F.stack(predicted_bbox_points, axis=1)

        # 2. find best prediction area for each gt bbox
        gt_bboxes_to_use_for_loss = []
        positive_anchor_indices = self.xp.empty((0,), dtype=self.xp.int32)
        not_contributing_anchors = self.xp.empty((0,), dtype=self.xp.int32)
        for index, gt_bbox in enumerate(gt_bbox_points):
            # determine which bboxes are positive boxes as they have high iou with gt and also which bboxes are negative
            # this is also used to train objectness classification
            gt_bbox = self.xp.tile(gt_bbox[None, ...], (len(predicted_bbox_points), 1))

            ious = bbox_iou(gt_bbox, predicted_bbox_points.data)
            positive_boxes = self.xp.where((ious[0] >= 0.7))
            not_contributing_boxes = self.xp.where(self.xp.logical_and(0.3 < ious[0], ious[0] < 0.7))
            if len(positive_boxes[0]) == 0:
                best_iou_index = ious[0, :].argmax()
                positive_anchor_indices = self.xp.concatenate((positive_anchor_indices, best_iou_index[None, ...]), axis=0)
                gt_bboxes_to_use_for_loss.append(gt_bbox[0])
            else:
                positive_anchor_indices = self.xp.concatenate((positive_anchor_indices, positive_boxes[0]), axis=0)
                gt_bboxes_to_use_for_loss.extend(gt_bbox[:len(positive_boxes[0])])
            not_contributing_anchors = self.xp.concatenate((not_contributing_anchors, not_contributing_boxes[0]), axis=0)

        if len(gt_bboxes_to_use_for_loss) == 0:
            return Variable(self.xp.array(0, dtype=predicted_grids.dtype))

        gt_bboxes_to_use_for_loss = F.stack(gt_bboxes_to_use_for_loss)

        # filter predicted bboxes and only keep bboxes from those regions that actually contain a bbox
        predicted_bbox_points = F.get_item(predicted_bbox_points, positive_anchor_indices)

        # 3. calculate L1 loss for bbox regression
        loss = F.huber_loss(
            predicted_bbox_points,
            gt_bboxes_to_use_for_loss,
            1
        )

        # 4. calculate objectness loss
        objectness_labels = self.xp.zeros(len(objectness_scores), dtype=self.xp.int32)
        objectness_labels[not_contributing_anchors] = -1
        objectness_labels[positive_anchor_indices] = 1

        objectness_loss = F.softmax_cross_entropy(
            objectness_scores,
            objectness_labels,
            ignore_label=-1,
        )

        return F.mean(loss), objectness_loss 
Example #20
Source File: fsns.py    From see with GNU General Public License v3.0 4 votes vote down vote up
def __call__(self, images):
        self.lstm.reset_state()
        self.transform_2.reset_state()

        h = self.bn0(self.conv0(images))
        h = F.average_pooling_2d(F.relu(h), 2, stride=2)

        h = self.rs1(h)
        h = F.max_pooling_2d(h, 2, stride=2)

        h = self.rs2(h)
        h = F.max_pooling_2d(h, 2, stride=2)

        h = self.rs3(h)
        self.vis_anchor = h
        h = F.average_pooling_2d(h, 5, stride=2)

        localizations = []

        with cuda.get_device_from_array(h.data):
            homogenuous_addon = self.xp.zeros((len(h), 1, 3), dtype=h.data.dtype)
            homogenuous_addon[:, 0, 2] = 1

        for _ in range(self.num_timesteps):
            lstm_prediction = F.relu(self.lstm(h))
            translation_transform = F.reshape(self.rotation_transform(lstm_prediction), (-1, 2, 3))
            translation_transform = disable_shearing(translation_transform)
            translation_transform = F.concat((translation_transform, homogenuous_addon), axis=1)

            rotation_transform = F.reshape(self.rotation_transform(lstm_prediction), (-1, 2, 3))
            rotation_transform = disable_translation(rotation_transform)
            rotation_transform = F.concat((rotation_transform, homogenuous_addon), axis=1)

            # first rotate, then translate
            transform = F.batch_matmul(rotation_transform, translation_transform)
            # homogenuous_multiplier = F.get_item(transform, (..., 2, 2))
            #
            # # bring matrices from homogenous coordinates to normal coordinates
            transform = transform[:, :2, :]
            # transform = transform / homogenuous_multiplier
            localizations.append(rotation_dropout(transform, ratio=self.dropout_factor))

        return F.concat(localizations, axis=0)