Python chainer.backends.cuda.get_array_module() Examples

The following are 30 code examples of chainer.backends.cuda.get_array_module(). 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.backends.cuda , or try the search function .
Example #1
Source File: model.py    From chainer-gqn with MIT License 6 votes vote down vote up
def generate_image(self, v, r):
        xp = cuda.get_array_module(v)

        batch_size = v.shape[0]
        h_t_gen, c_t_gen, u_t, _, _ = self.generate_initial_state(
            batch_size, xp)
        v = cf.reshape(v, v.shape[:2] + (1, 1))

        for t in range(self.num_layers):
            generation_core = self.get_generation_core(t)

            mean_z_p, ln_var_z_p = self.z_prior_distribution.compute_parameter(
                h_t_gen)
            z_t = cf.gaussian(mean_z_p, ln_var_z_p)

            h_next_gen, c_next_gen, u_next = generation_core(
                h_t_gen, c_t_gen, z_t, v, r, u_t)

            u_t = u_next
            h_t_gen = h_next_gen
            c_t_gen = c_next_gen

        mean_x = self.map_u_x(u_t)
        return mean_x.data 
Example #2
Source File: model.py    From chainer-gqn with MIT License 6 votes vote down vote up
def generate_image_from_zero_z(self, v, r):
        xp = cuda.get_array_module(v)

        batch_size = v.shape[0]
        h_t_gen, c_t_gen, u_t, _, _ = self.generate_initial_state(
            batch_size, xp)

        v = cf.reshape(v, v.shape[:2] + (1, 1))

        for t in range(self.num_layers):
            generation_core = self.get_generation_core(t)

            mean_z_p, _ = self.z_prior_distribution.compute_parameter(h_t_gen)
            z_t = xp.zeros_like(mean_z_p.data)

            h_next_gen, c_next_gen, u_next = generation_core(
                h_t_gen, c_t_gen, z_t, v, r, u_t)

            u_t = u_next
            h_t_gen = h_next_gen
            c_t_gen = c_next_gen

        mean_x = self.map_u_x(u_t)
        return mean_x.data 
Example #3
Source File: region_proposal_network.py    From chainercv with MIT License 6 votes vote down vote up
def _enumerate_shifted_anchor(anchor_base, feat_stride, height, width):
    # Enumerate all shifted anchors:
    #
    # add A anchors (1, A, 4) to
    # cell K shifts (K, 1, 4) to get
    # shift anchors (K, A, 4)
    # reshape to (K*A, 4) shifted anchors
    xp = cuda.get_array_module(anchor_base)
    shift_y = xp.arange(0, height * feat_stride, feat_stride)
    shift_x = xp.arange(0, width * feat_stride, feat_stride)
    shift_x, shift_y = xp.meshgrid(shift_x, shift_y)
    shift = xp.stack((shift_y.ravel(), shift_x.ravel(),
                      shift_y.ravel(), shift_x.ravel()), axis=1)

    A = anchor_base.shape[0]
    K = shift.shape[0]
    anchor = anchor_base.reshape((1, A, 4)) + \
        shift.reshape((1, K, 4)).transpose((1, 0, 2))
    anchor = anchor.reshape((K * A, 4)).astype(np.float32)
    return anchor 
Example #4
Source File: updater.py    From Guided-Attention-Inference-Network with MIT License 6 votes vote down vote up
def update_core(self):

		image, labels = self.converter(self.get_iterator('main').next())
		assert image.shape[0] == 1, "Batchsize of only 1 is allowed for now"
		image = Variable(image)

		if self.device >= 0:
			image.to_gpu(self.device)
		cl_output = self._optimizers['main'].target.classify(image)
		xp = get_array_module(cl_output.data)

		target = xp.asarray([[0]*(self.no_of_classes)]*cl_output.shape[0])
		for i in range(labels.shape[0]):
			gt_labels = np.unique(labels[i]).astype(np.int32)[2:] - 1 # Not considering -1 & 0
			target[i][gt_labels] = 1
		loss = F.sigmoid_cross_entropy(cl_output, target, normalize=True)
		report({'Loss':loss}, self.get_optimizer('main').target)
		self._optimizers['main'].target.cleargrads()
		loss.backward()
		self._optimizers['main'].update() 
Example #5
Source File: r2_score.py    From chainer-chemistry with MIT License 6 votes vote down vote up
def forward(self, inputs):
        xp = cuda.get_array_module(*inputs)
        pred, true = inputs
        diff = pred - true
        dev = true - xp.mean(true, axis=0)
        if self.ignore_nan:
            diff[xp.isnan(diff)] = 0.
            dev[xp.isnan(dev)] = 0.
        SS_res = xp.asarray(
            xp.sum(diff ** 2, axis=0))
        SS_tot = xp.asarray(
            xp.sum(dev ** 2, axis=0))
        SS_tot_iszero = SS_tot == 0
        SS_tot[SS_tot_iszero] = 1  # Assign dummy value to avoid zero-division
        ret = xp.where(
            SS_tot_iszero, 0.0, 1 - SS_res / SS_tot).astype(pred.dtype)
        if self.multioutput == 'uniform_average':
            return xp.asarray(ret.mean()),
        elif self.multioutput == 'raw_values':
            return ret, 
Example #6
Source File: r2_score_evaluator.py    From chainer-chemistry with MIT License 6 votes vote down vote up
def r2_score(self, pred, true, sample_weight=None,
                 multioutput='uniform_average', ignore_nan=False):

        if self.sample_weight is not None:
            raise NotImplementedError()
        if self.multioutput not in ['uniform_average', 'raw_values']:
            raise ValueError('invalid multioutput argument')

        xp = cuda.get_array_module(pred)
        diff = pred - true
        dev = true - xp.mean(true, axis=0)
        if self.ignore_nan:
            diff[xp.isnan(diff)] = 0.
            dev[xp.isnan(dev)] = 0.
        SS_res = xp.asarray(xp.sum(diff ** 2, axis=0))
        SS_tot = xp.asarray(xp.sum(dev ** 2, axis=0))
        SS_tot_iszero = SS_tot == 0
        SS_tot[SS_tot_iszero] = 1  # Assign dummy value to avoid zero-division
        ret = xp.where(
            SS_tot_iszero, 0.0, 1 - SS_res / SS_tot).astype(pred.dtype)
        if self.multioutput == 'uniform_average':
            return xp.asarray(ret.mean())
        elif self.multioutput == 'raw_values':
            return ret 
Example #7
Source File: text_recognition_evaluator.py    From kiss with GNU General Public License v3.0 6 votes vote down vote up
def __call__(self, **kwargs):
        image = kwargs.pop('image', None)
        words = kwargs.pop('words', None)
        return_predictions = kwargs.pop('return_predictions', False)

        with chainer.using_device(self.device):
            rois, bboxes = self.localizer.predict(image)[:2]
            predicted_words = self.recognizer.predict(rois).array
            self.xp = cuda.get_array_module(bboxes)
            batch_size, num_bboxes, num_channels, height, width = rois.shape
            rois = self.xp.reshape(rois.array, (-1, num_channels, height, width))
            bboxes = self.xp.reshape(bboxes.array, (-1, 2, height, width))

            self.calc_word_accuracy(predicted_words, words)

        if return_predictions:
            return rois, bboxes, predicted_words 
Example #8
Source File: adaptive_softmax.py    From models with MIT License 6 votes vote down vote up
def backward_log_softmax(self, x, y, gy):
        if cuda.cudnn_enabled:
            cudnn = cuda.cudnn
            libcudnn = cuda.cuda.cudnn
            _algorithm = libcudnn.CUDNN_SOFTMAX_LOG
            _mode = libcudnn.CUDNN_SOFTMAX_MODE_CHANNEL

        xp = cuda.get_array_module(x)
        if xp is not numpy and chainer.should_use_cudnn('>=auto', 3000):
            oz_dtype = 'd' if x.dtype == 'd' else 'f'
            one = numpy.array(1, dtype=oz_dtype).ctypes
            zero = numpy.array(0, dtype=oz_dtype).ctypes
            handle = cudnn.get_handle()
            gx = xp.empty(x.shape, dtype=x.dtype)
            gx_cube = gx.reshape(gx.shape[:2] + (-1, 1))
            desc = cudnn.create_tensor_descriptor(gx_cube)
            libcudnn.softmaxBackward(
                handle, _algorithm, _mode, one.data, desc.value,
                y.data.ptr, desc.value, gy.data.ptr, zero.data,
                desc.value, gx.data.ptr)
        else:
            gx = gy - xp.exp(y) * gy.sum(axis=1, keepdims=True)

        return gx 
Example #9
Source File: image_masking.py    From kiss with GNU General Public License v3.0 5 votes vote down vote up
def add_inhibition_of_return_mask(self, images, bboxes):
        xp = cuda.get_array_module(bboxes.data)
        corners = self.extract_corners(bboxes.data, xp)
        corners = self.scale_bboxes(corners, Size._make(images.shape[-2:]))

        batch_size, num_channels, height, width = images.shape
        masked_images = xp.reshape(images, (batch_size * num_channels, height, width))
        if not self.mask_full_box:
            corners = self.create_inhibition_of_return_box(corners, xp)
        corners = self.tile_box(corners, num_channels, xp)
        masked_images = self.mask_array(masked_images, corners, xp)
        return xp.reshape(masked_images, (batch_size, num_channels, height, width)) 
Example #10
Source File: light_head_rcnn_train_chain.py    From chainercv with MIT License 5 votes vote down vote up
def _ohem_loss(
        roi_cls_locs, roi_scores, gt_roi_locs, gt_roi_labels,
        n_ohem_sample, roi_sigma=1.0
):
    xp = cuda.get_array_module(roi_cls_locs)
    n_sample = roi_cls_locs.shape[0]
    roi_cls_locs = roi_cls_locs.reshape((n_sample, -1, 4))
    roi_locs = roi_cls_locs[xp.arange(n_sample), gt_roi_labels]
    roi_loc_loss = _fast_rcnn_loc_loss(
        roi_locs, gt_roi_locs, gt_roi_labels, roi_sigma, reduce='no')
    roi_cls_loss = F.softmax_cross_entropy(
        roi_scores, gt_roi_labels, reduce='no')
    assert roi_loc_loss.shape == roi_cls_loss.shape

    n_ohem_sample = min(n_ohem_sample, n_sample)
    # sort in CPU because of GPU memory
    roi_cls_loc_loss = cuda.to_cpu(roi_loc_loss.array + roi_cls_loss.array)
    indices = roi_cls_loc_loss.argsort(axis=0)[::-1]
    # filter nan
    indices = np.array(
        [i for i in indices if not np.isnan(roi_cls_loc_loss[i])],
        dtype=np.int32)
    indices = indices[:n_ohem_sample]
    if cuda.get_array_module(roi_loc_loss.array) != np:
        indices = cuda.to_gpu(indices)
    if len(indices) > 0:
        roi_loc_loss = F.sum(roi_loc_loss[indices]) / n_ohem_sample
        roi_cls_loss = F.sum(roi_cls_loss[indices]) / len(indices)
    else:
        roi_loc_loss = chainer.Variable(xp.array(0.0, dtype=xp.float32))
        roi_cls_loss = chainer.Variable(xp.array(0.0, dtype=xp.float32))
        roi_loc_loss.zerograd()
        roi_cls_loss.zerograd()

    return roi_loc_loss, roi_cls_loss 
Example #11
Source File: anchor_target_creator.py    From chainercv with MIT License 5 votes vote down vote up
def _get_inside_index(anchor, H, W):
    # Calc indicies of anchors which are located completely inside of the image
    # whose size is speficied.
    xp = cuda.get_array_module(anchor)

    index_inside = xp.where(
        (anchor[:, 0] >= 0) &
        (anchor[:, 1] >= 0) &
        (anchor[:, 2] <= H) &
        (anchor[:, 3] <= W)
    )[0]
    return index_inside 
Example #12
Source File: mask_head.py    From chainercv with MIT License 5 votes vote down vote up
def decode(self, segms, bboxes, labels, sizes):
        """Decodes back to masks.

        Args:
            segms (iterable of arrays): An iterable of arrays of
                shape :math:`(R_n, n\_class, M, M)`.
            bboxes (iterable of arrays): An iterable of arrays of
                shape :math:`(R_n, 4)`.
            labels (iterable of arrays): An iterable of arrays of
                shape :math:`(R_n,)`.
            sizes (list of tuples of two ints): A list of
                :math:`(H_n, W_n)`, where :math:`H_n` and :math:`W_n`
                are height and width of the :math:`n`-th image.

        Returns:
            list of arrays:
            This list contains instance segmentation for each image
            in the batch.
            More precisely, this is a list of boolean arrays of shape
            :math:`(R'_n, H_n, W_n)`, where :math:`R'_n` is the number of
            bounding boxes in the :math:`n`-th image.
        """

        xp = chainer.backends.cuda.get_array_module(*segms)
        if xp != np:
            raise ValueError(
                'MaskHead.decode only supports numpy inputs for now.')
        masks = []
        for bbox, segm, label, size in zip(
                bboxes, segms, labels, sizes):
            if len(segm) > 0:
                masks.append(
                    segm_to_mask(segm[np.arange(len(label)), label + 1],
                                 bbox, size))
            else:
                masks.append(np.zeros((0,) + size, dtype=np.bool))
        return masks 
Example #13
Source File: mask_head.py    From chainercv with MIT License 5 votes vote down vote up
def mask_head_loss_post(segms, mask_roi_indices, gt_segms, gt_mask_labels,
                        batchsize):
    """Loss function for Mask Head (post).

     Args:
         segms (array): An array whose shape is :math:`(R, n\_class, M, M)`,
             where :math:`R` is the total number of RoIs in the given batch.
         mask_roi_indices (array): A list of arrays returned by
             :func:`mask_head_loss_pre`.
         gt_segms (list of arrays): A list of arrays returned by
             :func:`mask_head_loss_pre`.
         gt_mask_labels (list of arrays): A list of arrays returned by
             :func:`mask_head_loss_pre`.
         batchsize (int): The size of batch.

     Returns:
        chainer.Variable:
        Mask loss.
    """
    xp = cuda.get_array_module(segms.array)

    mask_roi_indices = xp.hstack(mask_roi_indices).astype(np.int32)
    gt_segms = xp.vstack(gt_segms)
    gt_mask_labels = xp.hstack(gt_mask_labels).astype(np.int32)

    mask_loss = F.sigmoid_cross_entropy(
        segms[np.arange(len(gt_mask_labels)), gt_mask_labels],
        gt_segms.astype(np.int32))
    return mask_loss 
Example #14
Source File: misc.py    From chainercv with MIT License 5 votes vote down vote up
def choice(x, size):
    xp = cuda.get_array_module(x)
    y = np.random.choice(cuda.to_cpu(x), size, replace=False)
    if xp is np:
        return y
    else:
        return cuda.to_gpu(y) 
Example #15
Source File: bbox_head.py    From chainercv with MIT License 5 votes vote down vote up
def _suppress(raw_bbox, raw_score, nms_thresh, score_thresh):
    xp = cuda.get_array_module(raw_bbox, raw_score)

    bbox = []
    label = []
    score = []
    for l in range(raw_score.shape[1] - 1):
        bbox_l = raw_bbox[:, l + 1]
        score_l = raw_score[:, l + 1]

        mask = score_l >= score_thresh
        bbox_l = bbox_l[mask]
        score_l = score_l[mask]

        order = argsort(-score_l)
        bbox_l = bbox_l[order]
        score_l = score_l[order]
        indices = utils.non_maximum_suppression(bbox_l, nms_thresh)
        bbox_l = bbox_l[indices]
        score_l = score_l[indices]

        bbox.append(bbox_l)
        label.append(xp.array((l,) * len(bbox_l)))
        score.append(score_l)

    bbox = xp.vstack(bbox).astype(np.float32)
    label = xp.hstack(label).astype(np.int32)
    score = xp.hstack(score).astype(np.float32)
    return bbox, label, score 
Example #16
Source File: test_proposal_target_creator.py    From chainercv with MIT License 5 votes vote down vote up
def check_proposal_target_creator(
            self, roi, mask, label, proposal_target_creator):
        xp = cuda.get_array_module(roi)
        bbox = mask_to_bbox(mask)
        sample_roi, gt_roi_mask, gt_roi_label, gt_roi_loc =\
            proposal_target_creator(
                roi, mask, label, bbox, mask_size=self.mask_size)

        # Test types
        self.assertIsInstance(sample_roi, xp.ndarray)
        self.assertIsInstance(gt_roi_loc, xp.ndarray)
        self.assertIsInstance(gt_roi_mask, xp.ndarray)
        self.assertIsInstance(gt_roi_label, xp.ndarray)

        sample_roi = cuda.to_cpu(sample_roi)
        gt_roi_loc = cuda.to_cpu(gt_roi_loc)
        gt_roi_mask = cuda.to_cpu(gt_roi_mask)
        gt_roi_label = cuda.to_cpu(gt_roi_label)

        # Test shapes
        self.assertEqual(sample_roi.shape, (self.n_sample, 4))
        self.assertEqual(gt_roi_loc.shape, (self.n_sample, 4))
        self.assertEqual(
            gt_roi_mask.shape, (self.n_sample, self.mask_size, self.mask_size))
        self.assertEqual(gt_roi_label.shape, (self.n_sample,))

        # Test foreground and background labels
        np.testing.assert_equal(np.sum(gt_roi_label >= 0), self.n_sample)
        n_pos = np.sum(gt_roi_label >= 1)
        n_neg = np.sum(gt_roi_label == 0)
        self.assertLessEqual(n_pos, self.n_sample * self.pos_ratio)
        self.assertLessEqual(n_neg, self.n_sample - n_pos) 
Example #17
Source File: test_anchor_target_creator.py    From chainercv with MIT License 5 votes vote down vote up
def check_anchor_target_creator(
            self, anchor_target_layer,
            bbox, anchor, img_size):
        xp = cuda.get_array_module(bbox)

        loc, label = self.anchor_target_layer(
            bbox, anchor, img_size)

        # Test types
        self.assertIsInstance(loc, xp.ndarray)
        self.assertIsInstance(label, xp.ndarray)

        # Test shapes
        self.assertEqual(loc.shape, (self.n_anchor, 4))
        self.assertEqual(label.shape, (self.n_anchor,))

        # Test dtype
        self.assertEqual(loc.dtype, np.float32)
        self.assertEqual(label.dtype, np.int32)

        # Test ratio of foreground and background labels
        np.testing.assert_equal(
            cuda.to_cpu(utils.force_array(xp.sum(label >= 0))),
            self.n_sample)
        n_pos = cuda.to_cpu(utils.force_array(xp.sum(label == 1)))
        n_neg = cuda.to_cpu(utils.force_array(xp.sum(label == 0)))
        self.assertLessEqual(
            n_pos, self.n_sample * self.pos_ratio)
        self.assertLessEqual(n_neg, self.n_sample - n_pos) 
Example #18
Source File: test_ps_roi_max_pooling.py    From chainercv with MIT License 5 votes vote down vote up
def check_backward(self, x_data, roi_data, roi_index_data, y_grad_data):
        def f(x, rois, roi_indices):
            y = functions.ps_roi_max_pooling_2d(
                x, rois, roi_indices, self.outsize,
                self.spatial_scale, self.group_size)
            xp = cuda.get_array_module(y)
            y = F.where(
                xp.isinf(y.array), xp.zeros(y.shape, dtype=y.dtype), y)
            return y
        gradient_check.check_backward(
            f, (x_data, roi_data, roi_index_data), y_grad_data,
            no_grads=[False, True, True], **self.check_backward_options) 
Example #19
Source File: test_ps_roi_max_align.py    From chainercv with MIT License 5 votes vote down vote up
def check_backward(self, x_data, roi_data, roi_index_data, y_grad_data):
        def f(x, rois, roi_indices):
            y = functions.ps_roi_max_align_2d(
                x, rois, roi_indices, self.outsize,
                self.spatial_scale, self.group_size,
                sampling_ratio=self.sampling_ratio)
            xp = cuda.get_array_module(y)
            y = F.where(
                xp.isinf(y.array), xp.zeros(y.shape, dtype=y.dtype), y)
            return y

        gradient_check.check_backward(
            f, (x_data, roi_data, roi_index_data), y_grad_data,
            no_grads=[False, True, True], **self.check_backward_options) 
Example #20
Source File: mask_to_bbox.py    From chainercv with MIT License 5 votes vote down vote up
def mask_to_bbox(mask):
    """Compute the bounding boxes around the masked regions.

    This function accepts both :obj:`numpy.ndarray` and :obj:`cupy.ndarray` as
    inputs.

    Args:
        mask (array): An array whose shape is :math:`(R, H, W)`.
            :math:`R` is the number of masks.
            The dtype should be :obj:`numpy.bool`.

    Returns:
        array:
        The bounding boxes around the masked regions.
        This is an array whose shape is :math:`(R, 4)`.
        :math:`R` is the number of bounding boxes.
        The dtype should be :obj:`numpy.float32`.

    """
    R, H, W = mask.shape
    xp = cuda.get_array_module(mask)

    instance_index, ys, xs = xp.nonzero(mask)
    bbox = xp.zeros((R, 4), dtype=np.float32)
    for i in range(R):
        ys_i = ys[instance_index == i]
        xs_i = xs[instance_index == i]
        if len(ys_i) == 0:
            continue
        y_min = ys_i.min()
        x_min = xs_i.min()
        y_max = ys_i.max() + 1
        x_max = xs_i.max() + 1
        bbox[i] = xp.array([y_min, x_min, y_max, x_max], dtype=np.float32)
    return bbox 
Example #21
Source File: rotation_detection_evaluator.py    From kiss with GNU General Public License v3.0 5 votes vote down vote up
def __call__(self, *inputs):
        images, labels = inputs[:2]
        with cuda.Device(self.device):
            rois, bboxes = self.link.predict(images)[:2]

            self.xp = cuda.get_array_module(bboxes)
            bboxes = bboxes.data
            labels = self.ndarray_to_list(labels)

            batch_size, num_predicted_masks, pred_masks = self.bboxes_to_masks(bboxes, images)
            pred_masks = self.ndarray_to_list(pred_masks)

            if self.assessor is not None:
                pred_scores = self.ndarray_to_list(
                    self.assessor.extract_iou_prediction(self.assessor(rois)).data.reshape(batch_size, num_predicted_masks)
                )
                pred_masks, pred_scores = self.perform_nms(batch_size, bboxes, num_predicted_masks, pred_masks, pred_scores)
            else:
                pred_scores = self.ndarray_to_list(numpy.ones((batch_size, num_predicted_masks)))

            ious = self.xp.concatenate(self.calculate_iou(pred_masks, labels))
            mean_iou = float(self.xp.sum(ious) / len(ious))
            reporter.report({'mean_iou': mean_iou})

            result = self.calculate_map(pred_masks, pred_scores, labels)
            reporter.report({'map': result['map']}) 
Example #22
Source File: mean_absolute_error.py    From chainer-chemistry with MIT License 5 votes vote down vote up
def backward(self, indexes, gy):
        x0, x1 = self.get_retained_inputs()
        xp = cuda.get_array_module(x0)
        diff = x0 - x1
        if self.ignore_nan:
            diff = chainer.functions.where(xp.isnan(diff.array),
                                           xp.zeros_like(diff.array), diff)
        gy0 = chainer.functions.broadcast_to(gy[0], diff.shape)
        gx0 = gy0 * chainer.functions.sign(diff) * 1. / diff.size
        return gx0, -gx0 
Example #23
Source File: updater.py    From Guided-Attention-Inference-Network with MIT License 5 votes vote down vote up
def update_core(self):
		image, labels = self.converter(self.get_iterator('main').next())
		image = Variable(image)

		assert image.shape[0] == 1, "Batchsize of only 1 is allowed for now"

		if self.device >= 0:
			image.to_gpu(self.device)

		xp = get_array_module(image.data)
		to_substract = np.array((-1, 0))
		noise_classes = np.unique(labels[0]).astype(np.int32)
		target = xp.asarray([[0] * (self.no_of_classes)])
		gt_labels = np.setdiff1d(noise_classes, to_substract) - 1  # np.unique(labels[0]).astype(np.int32)[2:] - 1
		target[0][gt_labels] = 1
		
		gcam, cl_scores, class_id = self._optimizers['main'].target.stream_cl(image, gt_labels)

		mask = self._optimizers['main'].target.get_mask(gcam)
		masked_image = self._optimizers['main'].target.mask_image(image, mask)
		masked_output = self._optimizers['main'].target.stream_am(masked_image)
		masked_output = F.sigmoid(masked_output)

		cl_loss = F.sigmoid_cross_entropy(cl_scores, target, normalize=True)
		am_loss = masked_output[0][class_id][0]

		labels = Variable(labels)
		if self.device >= 0:
			labels.to_gpu(self.device)
		segment_loss = self._optimizers['main'].target(image, labels)
		total_loss = self.lambd1 * cl_loss + self.lambd2 * am_loss + self.lambd3*segment_loss
		report({'AM_Loss': am_loss}, self.get_optimizer('main').target)
		report({'CL_Loss': cl_loss}, self.get_optimizer('main').target)
		report({'SG_Loss': segment_loss}, self.get_optimizer('main').target)
		report({'TotalLoss': total_loss}, self.get_optimizer('main').target)
		self._optimizers['main'].target.cleargrads()
		total_loss.backward()
		self._optimizers['main'].update() 
Example #24
Source File: utils.py    From Guided-Attention-Inference-Network with MIT License 5 votes vote down vote up
def VGGprepare_am_input(var):
	xp = get_array_module(var)

	# var = F.resize_images(var, size)
	var = F.transpose(var, (0, 2, 3, 1)) # [[W, H, C]]
	var = F.flip(var, 3)
	var -= xp.array([[103.939, 116.779, 123.68]], dtype=xp.float32)
	var = F.transpose(var, (0, 3, 1, 2))
	return var 
Example #25
Source File: _utility.py    From pytorch-sso with MIT License 5 votes vote down vote up
def _check_array(array, name):
    xp = cuda.get_array_module(array)
    with cuda.get_device_from_array(array):
        if not array.dtype == xp.float32:
            warnings.warn('non FP32 dtype detected in {}'.format(name))
            array = array.astype(xp.float32)
        if not (array.flags.c_contiguous or array.flags.f_contiguous):
            warnings.warn('non contiguous array detected in {}'.format(name))
            array = xp.ascontiguousarray(array)
    return array 
Example #26
Source File: cls_metrics.py    From imgclsmob with MIT License 5 votes vote down vote up
def update(self, labels, preds):
        """
        Updates the internal evaluation result.

        Parameters
        ----------
        labels : xp.array
            The labels of the data.
        preds : xp.array
            Predicted values.
        """
        xp = cuda.get_array_module(preds)
        if len(preds.shape) == 1:
            num_samples = 1
            argsorted_pred = xp.argsort(preds)[-self.top_k:]
            num_correct = int(xp.any(argsorted_pred.T == labels, axis=0))
        else:
            assert (len(labels) == len(preds))
            num_samples = preds.shape[0]
            argsorted_pred = xp.argsort(preds)[:, -self.top_k:]
            num_correct = xp.any(argsorted_pred.T == labels, axis=0).sum()
        assert (num_correct <= num_samples)
        self.sum_metric += num_correct
        self.global_sum_metric += num_correct
        self.num_inst += num_samples
        self.global_num_inst += num_samples 
Example #27
Source File: model.py    From chainer-gqn with MIT License 5 votes vote down vote up
def sample_z_and_x_params_from_posterior(self, x, v, r):
        batch_size = x.shape[0]
        xp = cuda.get_array_module(x)

        h_t_gen, c_t_gen, u_t, h_t_enc, c_t_enc = self.generate_initial_state(
            batch_size, xp)
        v = cf.reshape(v, v.shape + (1, 1))

        z_t_params_array = []

        for t in range(self.num_layers):
            inference_core = self.get_inference_core(t)
            generation_core = self.get_generation_core(t)

            h_next_enc, c_next_enc = inference_core(h_t_gen, h_t_enc, c_t_enc,
                                                    x, v, r, u_t)

            mean_z_q, ln_var_z_q = self.z_posterior_distribution.compute_parameter(
                h_t_enc)
            z_t = cf.gaussian(mean_z_q, ln_var_z_q)

            mean_z_p, ln_var_z_p = self.z_prior_distribution.compute_parameter(
                h_t_gen)

            h_next_gen, c_next_gen, u_next = generation_core(
                h_t_gen, c_t_gen, z_t, v, r, u_t)

            z_t_params_array.append((mean_z_q, ln_var_z_q, mean_z_p,
                                     ln_var_z_p))

            u_t = u_next
            h_t_gen = h_next_gen
            c_t_gen = c_next_gen
            h_t_enc = h_next_enc
            c_t_enc = c_next_enc

        mean_x = self.map_u_x(u_t)
        return z_t_params_array, mean_x 
Example #28
Source File: preprocessing.py    From chainer-gqn with MIT License 5 votes vote down vote up
def preprocess_images(images, add_noise=False):
    xp = cuda.get_array_module(images)
    if add_noise:
        images += xp.random.uniform(0, 1, size=images.shape).astype(xp.float32)
    images = images / 256 - 0.5
    return images 
Example #29
Source File: arctanh.py    From chainerrl with MIT License 5 votes vote down vote up
def forward(self, inputs):
        self.retain_inputs((0,))
        x, = inputs
        xp = cuda.get_array_module(x)
        y = xp.arctanh(x)
        return utils.force_array(y, dtype=x.dtype), 
Example #30
Source File: bbox_iou.py    From chainer-compiler with MIT License 5 votes vote down vote up
def bbox_iou(bbox_a, bbox_b):
    """Calculate the Intersection of Unions (IoUs) between bounding boxes.

    IoU is calculated as a ratio of area of the intersection
    and area of the union.

    This function accepts both :obj:`numpy.ndarray` and :obj:`cupy.ndarray` as
    inputs. Please note that both :obj:`bbox_a` and :obj:`bbox_b` need to be
    same type.
    The output is same type as the type of the inputs.

    Args:
        bbox_a (array): An array whose shape is :math:`(N, 4)`.
            :math:`N` is the number of bounding boxes.
            The dtype should be :obj:`numpy.float32`.
        bbox_b (array): An array similar to :obj:`bbox_a`,
            whose shape is :math:`(K, 4)`.
            The dtype should be :obj:`numpy.float32`.

    Returns:
        array:
        An array whose shape is :math:`(N, K)`. \
        An element at index :math:`(n, k)` contains IoUs between \
        :math:`n` th bounding box in :obj:`bbox_a` and :math:`k` th bounding \
        box in :obj:`bbox_b`.

    """
    if bbox_a.shape[1] != 4 or bbox_b.shape[1] != 4:
        raise IndexError
    xp = cuda.get_array_module(bbox_a)

    # top left
    tl = xp.maximum(bbox_a[:, None, :2], bbox_b[:, :2])
    # bottom right
    br = xp.minimum(bbox_a[:, None, 2:], bbox_b[:, 2:])

    area_i = xp.prod(br - tl, axis=2) * (tl < br).all(axis=2)
    area_a = xp.prod(bbox_a[:, 2:] - bbox_a[:, :2], axis=1)
    area_b = xp.prod(bbox_b[:, 2:] - bbox_b[:, :2], axis=1)
    return area_i / (area_a[:, None] + area_b - area_i)