Python chainer.functions.maximum() Examples

The following are 30 code examples of chainer.functions.maximum(). 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: 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 #2
Source File: caffefunction.py    From deel with MIT License 6 votes vote down vote up
def __call__(self, *xs):
		operation = self.operation

		if operation == 0:      # PROD
			return six.moves.reduce(lambda x, y: x * y, xs),

		elif operation == 1:    # SUM
			coeffs = self.coeffs
			if coeffs is not None:
				assert len(xs) == len(coeffs)
				xs = [x * coeff for x, coeff in zip(xs, coeffs)]
			return six.moves.reduce(lambda x, y: x + y, xs),

		elif operation == 2:    # MAX
			return six.moves.reduce(lambda x, y: functions.maximum(x, y), xs),

		else:
			raise ValueError('Invalid EltwiseParameter.EltwiseOp value.') 
Example #3
Source File: utils.py    From YOLOv2 with MIT License 6 votes vote down vote up
def reshape_to_yolo_size(img):
    input_height, input_width, _ = img.shape
    min_pixel = 320
    #max_pixel = 608
    max_pixel = 448

    min_edge = np.minimum(input_width, input_height)
    if min_edge < min_pixel:
        input_width *= min_pixel / min_edge
        input_height *= min_pixel / min_edge
    max_edge = np.maximum(input_width, input_height)
    if max_edge > max_pixel:
        input_width *= max_pixel / max_edge
        input_height *= max_pixel / max_edge

    input_width = int(input_width / 32 + round(input_width % 32 / 32)) * 32
    input_height = int(input_height / 32 + round(input_height % 32 / 32)) * 32
    img = cv2.resize(img, (input_width, input_height))

    return img 
Example #4
Source File: utils.py    From YOLOv2 with MIT License 6 votes vote down vote up
def random_hsv_image(bgr_image, delta_hue, delta_sat_scale, delta_val_scale):
    hsv_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2HSV).astype(np.float32)

    # hue
    hsv_image[:, :, 0] += int((np.random.rand() * delta_hue * 2 - delta_hue) * 255)

    # sat
    sat_scale = 1 + np.random.rand() * delta_sat_scale * 2 - delta_sat_scale
    hsv_image[:, :, 1] *= sat_scale

    # val
    val_scale = 1 + np.random.rand() * delta_val_scale * 2 - delta_val_scale
    hsv_image[:, :, 2] *= val_scale

    hsv_image[hsv_image < 0] = 0 
    hsv_image[hsv_image > 255] = 255 
    hsv_image = hsv_image.astype(np.uint8)
    bgr_image = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2BGR)
    return bgr_image

# non maximum suppression 
Example #5
Source File: utils.py    From kiss with GNU General Public License v3.0 6 votes vote down vote up
def calc_loss(self, grids, image_size, **kwargs):
        normalize = kwargs.get('normalize', True)
        corner_coordinates = self.get_corners(grids, image_size, scale_to_image_size=False)
        # determine whether a point is out of the image, image range is [-1, 1]
        # everything outside of this increases the loss!
        bbox = F.concat(corner_coordinates, axis=0)
        top_loss = bbox + 1.5
        bottom_loss = bbox - 1.5

        # do not penalize anything inside the image
        top_loss = F.absolute(F.minimum(top_loss, self.xp.zeros_like(top_loss.array)))
        top_loss = F.reshape(top_loss, (len(corner_coordinates), -1))
        bottom_loss = F.maximum(bottom_loss, self.xp.zeros_like(bottom_loss.array))
        bottom_loss = F.reshape(bottom_loss, (len(corner_coordinates), -1))

        loss = F.sum(F.concat([top_loss, bottom_loss], axis=0), axis=0)
        if normalize:
            loss = F.sum(loss)
        return loss 
Example #6
Source File: utils.py    From kiss with GNU General Public License v3.0 6 votes vote down vote up
def calc_loss(self, grids, image_size, **kwargs):
        """
            Calculate a loss based on the expected grid size. Penalize all predicted grids, where the area of the grid
            is smaller than the area of the crop area
        """
        top_left_x, top_right_x, _, _, top_left_y, _, bottom_left_y, _ = self.get_corners(grids, image_size)

        grid_widths = top_right_x - top_left_x
        grid_heights = bottom_left_y - top_left_y
        expected_width = self.xp.full_like(grid_widths.array, grids.shape[-1], dtype=grid_widths.dtype)
        expected_height = self.xp.full_like(grid_heights.array, grids.shape[2], dtype=grid_heights.dtype)

        width_loss = F.maximum(self.xp.zeros_like(grid_widths.array), expected_width - grid_widths)
        height_loss = F.maximum(self.xp.zeros_like(grid_heights.array), expected_height - grid_heights)

        return sum(width_loss) + sum(height_loss) 
Example #7
Source File: utils.py    From kiss with GNU General Public License v3.0 6 votes vote down vote up
def calc_loss(self, grids, image_size, **kwargs):
        normalize = kwargs.get('normalize', True)
        top_left_x, top_right_x, _, _, top_left_y, _, bottom_left_y, _ = self.get_corners(grids, image_size)

        # penalize upside down images
        distance = top_left_y - bottom_left_y
        up_down_loss = F.maximum(distance, self.xp.zeros_like(distance.array))
        if normalize:
            up_down_loss = F.sum(up_down_loss)

        # penalize images that are vertically mirrored
        distance = top_left_x - top_right_x
        left_right_loss = F.maximum(distance, self.xp.zeros_like(distance.array))
        if normalize:
            left_right_loss = F.sum(left_right_loss)

        return up_down_loss + left_right_loss 
Example #8
Source File: loss_metrics.py    From see with GNU General Public License v3.0 6 votes vote down vote up
def calc_intersection(self, top_left_x_1, width_1, top_left_x_2, width_2, top_left_y_1, height_1, top_left_y_2, height_2):
        width_overlap = self.calc_overlap(
            top_left_x_1,
            width_1,
            top_left_x_2,
            width_2
        )

        height_overlap = self.calc_overlap(
            top_left_y_1,
            height_1,
            top_left_y_2,
            height_2
        )

        width_overlap = F.maximum(width_overlap, self.xp.zeros_like(width_overlap))
        height_overlap = F.maximum(height_overlap, self.xp.zeros_like(height_overlap))

        return width_overlap * height_overlap 
Example #9
Source File: caffe_function.py    From chainer with MIT License 6 votes vote down vote up
def __call__(self, *xs):
        operation = self.operation

        if operation == 0:      # PROD
            return six.moves.reduce(lambda x, y: x * y, xs),

        elif operation == 1:    # SUM
            coeffs = self.coeffs
            if coeffs is not None:
                assert len(xs) == len(coeffs)
                xs = [x * coeff for x, coeff in zip(xs, coeffs)]
            return six.moves.reduce(lambda x, y: x + y, xs),

        elif operation == 2:    # MAX
            return six.moves.reduce(lambda x, y: functions.maximum(x, y), xs),

        else:
            raise ValueError('Invalid EltwiseParameter.EltwiseOp value.') 
Example #10
Source File: test_maximum.py    From chainer with MIT License 5 votes vote down vote up
def forward(self, inputs, devices):
        x1, x2 = inputs
        return functions.maximum(x1, x2), 
Example #11
Source File: action_value.py    From chainerrl with MIT License 5 votes vote down vote up
def greedy_actions(self):
        with chainer.force_backprop_mode():
            a = self.mu
            if self.min_action is not None:
                a = F.maximum(
                    self.xp.broadcast_to(self.min_action, a.array.shape), a)
            if self.max_action is not None:
                a = F.minimum(
                    self.xp.broadcast_to(self.max_action, a.array.shape), a)
            return a 
Example #12
Source File: utils.py    From YOLOv2 with MIT License 5 votes vote down vote up
def multi_box_intersection(a, b):
    w = multi_overlap(a.x, a.w, b.x, b.w)
    h = multi_overlap(a.y, a.h, b.y, b.h)
    zeros = Variable(np.zeros(w.shape, dtype=w.data.dtype))
    zeros.to_gpu()

    w = F.maximum(w, zeros)
    h = F.maximum(h, zeros)

    area = w * h
    return area

# 2つのboxを受け取り、合計面積を返す。(union of 2 boxes) 
Example #13
Source File: utils.py    From YOLOv2 with MIT License 5 votes vote down vote up
def multi_overlap(x1, len1, x2, len2):
    len1_half = len1/2
    len2_half = len2/2

    left = F.maximum(x1 - len1_half, x2 - len2_half)
    right = F.minimum(x1 + len1_half, x2 + len2_half)

    return right - left

# 2つのboxを受け取り、被ってる面積を返す(intersection of 2 boxes) 
Example #14
Source File: Maximum.py    From chainer-compiler with MIT License 5 votes vote down vote up
def forward(self, v1,v2):
        return F.maximum(v1, v2) 
Example #15
Source File: utils.py    From kiss with GNU General Public License v3.0 5 votes vote down vote up
def calc_loss(self, grids, image_size, **kwargs):
        normalize = kwargs.get('normalize', True)
        width, height = self.get_bbox_side_lengths(grids, image_size)

        # penalize aspect ratios that are higher than wide, and penalize aspect ratios that are tooo wide
        aspect_ratio = height / F.maximum(width, self.xp.ones_like(width))
        # do not give an incentive to bboxes with a width that is 2x the height of the box
        aspect_loss = F.maximum(aspect_ratio - 0.5, self.xp.zeros_like(aspect_ratio))

        return F.mean(aspect_loss) 
Example #16
Source File: utils.py    From kiss with GNU General Public License v3.0 5 votes vote down vote up
def calc_loss(self, grids, image_size, **kwargs):
        top_left_x, top_right_x, _, _, top_left_y, _, bottom_left_y, _ = self.get_corners(grids, image_size)

        grid_widths = top_right_x - top_left_x
        grid_heights = bottom_left_y - top_left_y
        expected_width = self.xp.full_like(grid_widths, image_size.width, dtype=grid_widths.dtype)
        expected_height = self.xp.full_like(grid_heights, image_size.height, dtype=grid_heights.dtype)

        width_loss = F.maximum(self.xp.zeros_like(grid_widths), grid_widths - expected_width)
        height_loss = F.maximum(self.xp.zeros_like(grid_heights), grid_heights - expected_height)

        return sum(width_loss) + sum(height_loss) 
Example #17
Source File: Maximum.py    From chainer-compiler with MIT License 5 votes vote down vote up
def forward(self, v1,v2):
        return np.maximum(v1, v2)

# ====================================== 
Example #18
Source File: test_maximum.py    From chainer with MIT License 5 votes vote down vote up
def test_maximum_inconsistent_shapes(self):
        x1_data = numpy.random.uniform(-1, 1, (3, 2)).astype(self.dtype)
        x2_data = numpy.random.uniform(-1, 1, (2, 3)).astype(self.dtype)
        x1 = chainer.Variable(x1_data)
        x2 = chainer.Variable(x2_data)
        with self.assertRaises(type_check.InvalidType):
            functions.maximum(x1, x2) 
Example #19
Source File: utils.py    From kiss with GNU General Public License v3.0 5 votes vote down vote up
def intersection(self, bbox1, bbox2):
        width_overlap = self.overlap(bbox1[:, 0], bbox1[:, 2] - bbox1[:, 0], bbox2[:, 0], bbox2[:, 2] - bbox2[:, 0])
        height_overlap = self.overlap(bbox1[:, 1], bbox1[:, 3] - bbox1[:, 1], bbox2[:, 1], bbox2[:, 3] - bbox2[:, 1])

        overlaps = width_overlap * height_overlap
        # overlaps = F.maximum(overlaps, self.xp.zeros_like(overlaps))
        return overlaps 
Example #20
Source File: utils.py    From kiss with GNU General Public License v3.0 5 votes vote down vote up
def overlap(self, x1, w1, x2, w2):
        return F.maximum(self.xp.zeros_like(x1), F.minimum(x1 + w1, x2 + w2) - F.maximum(x1, x2)) 
Example #21
Source File: utils.py    From kiss with GNU General Public License v3.0 5 votes vote down vote up
def intersection(self, bbox1, bbox2):
        width_overlap = self.overlap(bbox1[:, 0], bbox1[:, 2] - bbox1[:, 0], bbox2[:, 0], bbox2[:, 2] - bbox2[:, 0])
        height_overlap = self.overlap(bbox1[:, 1], bbox1[:, 3] - bbox1[:, 1], bbox2[:, 1], bbox2[:, 3] - bbox2[:, 1])

        overlaps = width_overlap * height_overlap
        overlaps = self.xp.maximum(overlaps, self.xp.zeros_like(overlaps))
        return overlaps 
Example #22
Source File: utils.py    From kiss with GNU General Public License v3.0 5 votes vote down vote up
def overlap(self, x1, w1, x2, w2):
        return self.xp.maximum(self.xp.zeros_like(x1), self.xp.minimum(x1 + w1, x2 + w2) - self.xp.maximum(x1, x2)) 
Example #23
Source File: distribution.py    From chainerrl with MIT License 5 votes vote down vote up
def clip_actions(actions, min_action, max_action):
    min_actions = F.broadcast_to(min_action, actions.shape)
    max_actions = F.broadcast_to(max_action, actions.shape)
    return F.maximum(F.minimum(actions, max_actions), min_actions) 
Example #24
Source File: loss_metrics.py    From see with GNU General Public License v3.0 5 votes vote down vote up
def calc_aspect_ratio_loss(self, width, height, label_lengths=None):
        # penalize aspect ratios that are higher than wide, and penalize aspect ratios that are tooo wide
        aspect_ratio = height / F.maximum(width, self.xp.ones_like(width))
        # do not give an incentive to bboxes with a width that is 2x the height of the box
        aspect_loss = F.maximum(aspect_ratio - 0.5, self.xp.zeros_like(aspect_ratio))

        # penalize very long bboxes (based on the underlying word), by assuming that a single letter
        # has a max width of its height, if the width of the bbox is too large it will be penalized
        if label_lengths is not None:
            max_width = label_lengths * height
            width_ratio = width - max_width
            width_threshold = F.maximum(width_ratio, self.xp.zeros_like(width_ratio))
            aspect_loss = aspect_ratio + width_threshold

        return sum(aspect_loss) / len(aspect_loss) 
Example #25
Source File: loss_metrics.py    From see with GNU General Public License v3.0 5 votes vote down vote up
def calc_iou_loss(self, grids1, grids2):
        top_left_x_1, top_right_x_1, _, top_left_y_1, _, bottom_left_y_1 = self.get_corners(grids1)
        top_left_x_2, top_right_x_2, _, top_left_y_2, _, bottom_left_y_2 = self.get_corners(grids2)

        width_1 = top_right_x_1 - top_left_x_1
        width_2 = top_right_x_2 - top_left_x_2
        height_1 = bottom_left_y_1 - top_left_y_1
        height_2 = bottom_left_y_2 - top_left_y_2
        intersection = self.calc_intersection(top_left_x_1, width_1, top_left_x_2, width_2, top_left_y_1, height_1, top_left_y_2, height_2)
        union = width_1 * height_1 + width_2 * height_2 - intersection
        iou = intersection / F.maximum(union, self.xp.ones_like(union))

        return sum(iou) / len(iou) 
Example #26
Source File: test_maximum.py    From chainer with MIT License 5 votes vote down vote up
def forward_expected(self, inputs):
        x1, x2 = inputs
        expected = numpy.maximum(x1, x2)
        expected = numpy.asarray(expected)
        return expected, 
Example #27
Source File: loss_metrics.py    From see with GNU General Public License v3.0 5 votes vote down vote up
def calc_overlap(self, left_1, width_1, left_2, width_2):
        radius_1 = width_1 / 2
        center_1 = left_1 + radius_1
        radius_2 = width_2 / 2
        center_2 = left_2 + radius_2

        center_distance = center_2 - center_1
        center_distance = F.maximum(center_distance, center_distance * -1)
        min_distance_for_no_overlap = radius_1 + radius_2
        return min_distance_for_no_overlap - center_distance 
Example #28
Source File: loss_metrics.py    From see with GNU General Public License v3.0 5 votes vote down vote up
def calc_direction_loss(self, grids):
        top_left_x, top_right_x, _, top_left_y, _, bottom_left_y = self.get_corners(grids)

        # penalize upside down images
        distance = top_left_y - bottom_left_y
        loss_values = F.maximum(distance, self.xp.zeros_like(distance))
        up_down_loss = F.average(loss_values)

        # penalize images that are vertically mirrored
        distance = top_left_x - top_right_x
        loss_values = F.maximum(distance, self.xp.zeros_like(distance))
        left_right_loss = F.average(loss_values)

        return up_down_loss + left_right_loss 
Example #29
Source File: feature_propagation_block.py    From chainer-pointnet with MIT License 4 votes vote down vote up
def __call__(self, distances, points1, points2):
        """

        Args:
            distances (numpy.ndarray or cupy.ndarray):
                3-dim array (bs, num_point2, num_point1)
            points1 (Variable): 3-dim (batch_size, num_point1, ch1)
            points2 (Variable): 3-dim (batch_size, num_point2, ch2)
                points2 is deeper, rich feature. num_point1 > num_point2

        Returns (Variable): 3-dim (batch_size, num_point1, ch1+ch2)

        """
        # batch_size, num_point1, ch1 = points1.shape
        # batch_size2, num_point2, ch2 = points2.shape
        batch_size, num_point2, num_point1 = distances.shape
        # assert batch_size == batch_size2
        if distances is None:
            print('[WARNING] distances is None')
            # calculate distances by feature vector (not coord vector)
            distances = self.xp(self.metric(points1, points2))
            # Better in this form
            # distances = self.xp(self.metric(coord1, coord2))

        # --- weight calculation ---
        # k-nearest neighbor with k=self.num_fp_point
        # sorted_indices (bs, num_fp_point, num_point1)
        sorted_indices = self.xp.argsort(
            distances, axis=1)[:, :self.num_fp_point, :]
        # sorted_dists (bs, num_fp_point, num_point1)
        sorted_dists = distances[
            self.xp.arange(batch_size)[:, None, None],
            sorted_indices,
            self.xp.arange(num_point1)[None, None, :]]

        eps_array = self.xp.ones(
            sorted_dists.shape, dtype=sorted_dists.dtype) * self.eps
        sorted_dists = functions.maximum(sorted_dists, eps_array)
        inv_dist = 1.0 / sorted_dists
        norm = functions.sum(inv_dist, axis=1, keepdims=True)
        norm = functions.broadcast_to(norm, sorted_dists.shape)
        # weight (bs, num_fp_point, num_point1)
        weight = inv_dist / norm
        # --- weight calculation end ---
        # point2_selected (bs, num_fp_point, num_point1, ch2)
        points2_selected = points2[
            self.xp.arange(batch_size)[:, None, None],
            sorted_indices, :]
        # print('debug', weight.shape, points2_selected.shape)
        weight = functions.broadcast_to(
            weight[:, :, :, None], points2_selected.shape)
        # interpolated_points (bs, num_point1, ch2)
        interpolated_points = functions.sum(
            weight * points2_selected, axis=1)
        if points1 is None:
            return interpolated_points
        else:
            return functions.concat([interpolated_points, points1], axis=2) 
Example #30
Source File: double_pal.py    From chainerrl with MIT License 4 votes vote down vote up
def _compute_y_and_t(self, exp_batch):

        batch_state = exp_batch['state']
        batch_size = len(exp_batch['reward'])

        if self.recurrent:
            qout, _ = self.model.n_step_forward(
                batch_state, exp_batch['recurrent_state'],
                output_mode='concat')
        else:
            qout = self.model(batch_state)

        batch_actions = exp_batch['action']
        batch_q = qout.evaluate_actions(batch_actions)

        # Compute target values

        with chainer.no_backprop_mode():
            batch_next_state = exp_batch['next_state']
            if self.recurrent:
                next_qout, _ = self.model.n_step_forward(
                    batch_next_state, exp_batch['next_recurrent_state'],
                    output_mode='concat')
                target_qout, _ = self.target_model.n_step_forward(
                    batch_state, exp_batch['recurrent_state'],
                    output_mode='concat')
                target_next_qout, _ = self.target_model.n_step_forward(
                    batch_next_state, exp_batch['next_recurrent_state'],
                    output_mode='concat')
            else:
                next_qout = self.model(batch_next_state)
                target_qout = self.target_model(batch_state)
                target_next_qout = self.target_model(batch_next_state)

            next_q_max = F.reshape(target_next_qout.evaluate_actions(
                next_qout.greedy_actions), (batch_size,))

            batch_rewards = exp_batch['reward']
            batch_terminal = exp_batch['is_state_terminal']

            # T Q: Bellman operator
            t_q = batch_rewards + exp_batch['discount'] * \
                (1.0 - batch_terminal) * next_q_max

            # T_PAL Q: persistent advantage learning operator
            cur_advantage = F.reshape(
                target_qout.compute_advantage(batch_actions), (batch_size,))
            next_advantage = F.reshape(
                target_next_qout.compute_advantage(batch_actions),
                (batch_size,))
            tpal_q = t_q + self.alpha * \
                F.maximum(cur_advantage, next_advantage)

        return batch_q, tpal_q