def bbox_iou2(box1, box2, x1y1x2y2=True):
    """
    Returns the IoU of two bounding boxes
    """
    if not x1y1x2y2:
        # Transform from center and width to exact coordinates
        b1_x1, b1_x2 = box1[:, 0] - box1[:, 2] / 2, box1[:, 0] + box1[:, 2] / 2
        b1_y1, b1_y2 = box1[:, 1] - box1[:, 3] / 2, box1[:, 1] + box1[:, 3] / 2
        b2_x1, b2_x2 = box2[:, 0] - box2[:, 2] / 2, box2[:, 0] + box2[:, 2] / 2
        b2_y1, b2_y2 = box2[:, 1] - box2[:, 3] / 2, box2[:, 1] + box2[:, 3] / 2
    else:
        # Get the coordinates of bounding boxes
        b1_x1, b1_y1, b1_x2, b1_y2 = box1[:, 0], box1[:, 1], box1[:,
                                                                  2], box1[:,
                                                                           3]
        b2_x1, b2_y1, b2_x2, b2_y2 = box2[:, 0], box2[:, 1], box2[:,
                                                                  2], box2[:,
                                                                           3]

    # get the corrdinates of the intersection rectangle
    inter_rect_x1 = np.max(b1_x1, b2_x1)
    inter_rect_y1 = np.max(b1_y1, b2_y1)
    inter_rect_x2 = np.min(b1_x2, b2_x2)
    inter_rect_y2 = np.min(b1_y2, b2_y2)
    # Intersection area
    inter_area = np.clamp(inter_rect_x2 - inter_rect_x1 + 1, min=0) * np.clamp(
        inter_rect_y2 - inter_rect_y1 + 1, min=0)
    # Union Area
    b1_area = (b1_x2 - b1_x1 + 1) * (b1_y2 - b1_y1 + 1)
    b2_area = (b2_x2 - b2_x1 + 1) * (b2_y2 - b2_y1 + 1)

    iou = inter_area / (b1_area + b2_area - inter_area + 1e-16)

    return iou
Ejemplo n.º 2
0
    def update(self, w, dw, config):
        if config['m'] is None: config['m'] = np.zeros_like(w)
        if config['v'] is None: config['v'] = np.zeros_like(w)
        if config['max_v'] is None: config['max_v'] = np.zeros_like(w)

        max_v = config['max_v']
        beta1, beta2 = config['beta1'], config['beta2']
        t, m, v = config['t'], config['m'], config['v']

        t += 1
        m = m * beta1 + (1 - beta1) * dw
        v = v * beta2 + (1 - beta2) * (dw**2)
        if config['amsbound']:
            max_v = np.max(max_v, v)
            denom = np.sqrt(max_v) + config['epsilon']
        else:
            denom = np.sqrt(v)

        bias_c1 = 1 - beta1**t
        bias_c2 = 1 - beta2**t

        step_size = config['learning_rate'] * np.sqrt(bias_c2) / bias_c1

        lb = config['final_lr'] * (1 - 1 / (config['gamma'] * t + 1))
        ub = config['final_lr'] * (1 + 1 / (config['gamma'] * t))
        step_size = np.full_like(denom, step_size)
        step_size = np.clamp(step_size / denom, lb, ub) * m
        w -= step_size

        config['m'] = m
        config['v'] = v
        config['t'] = t
        config['max_v'] = max_v

        return w, config
Ejemplo n.º 3
0
    def check(self, fore, back):
        id = self.slice[0]
        template = self.template[:fore.shape[0], :fore.shape[1], :]
        fore_warp = torch.round(template + fore).long()
        fore_warp[:, :, 0] = torch.clamp(fore_warp[:, :, 0],
                                         min=0,
                                         max=fore.shape[0] - 1)
        fore_warp[:, :, 1] = torch.clamp(fore_warp[:, :, 1],
                                         min=0,
                                         max=fore.shape[1] - 1)
        fore_result=torch.where(torch.norm(back[fore_warp[:,:,0],fore_warp[:,:,1]]+fore,2,dim=2)<=1, \
         self.one,self.zero)
        back_warp = torch.round(template + back).long()
        back_warp[:, :, 0] = torch.clamp(back_warp[:, :, 0],
                                         min=0,
                                         max=fore.shape[0] - 1)
        back_warp[:, :, 1] = torch.clamp(back_warp[:, :, 1],
                                         min=0,
                                         max=fore.shape[1] - 1)
        back_result=torch.where(torch.norm(fore[back_warp[:,:,0],back_warp[:,:,1]]+back,2,dim=2)<=1, \
         self.one,self.zero)

        fore *= fore_result.unsqueeze(-1)
        back *= back_result.unsqueeze(-1)
        fore_warp = torch.round(template + fore).long()
        fore_warp[:, :, 0] = torch.clamp(fore_warp[:, :, 0],
                                         min=0,
                                         max=fore.shape[0] - 1)
        fore_warp[:, :, 1] = torch.clamp(fore_warp[:, :, 1],
                                         min=0,
                                         max=fore.shape[1] - 1)
        fore_result=torch.where(torch.norm(back[fore_warp[:,:,0],fore_warp[:,:,1]]+fore,2,dim=2)<=1, \
         self.one,self.zero)
        return fore_result, fore_warp
Ejemplo n.º 4
0
    def forward(self, x, heatmap=None):
        """
        x: (batch, c, x_dim, y_dim)
        """
        # print(x.shape)
        coords = self.coords.numpy().repeat(x.shape[0], 1)
        coords = fluid.dygraph.to_variable(coords)

        if self.with_boundary and heatmap is not None:
            heatmap = heatmap.numpy()
            boundary_channel = np.clamp(heatmap[:, -1:, :, :], 0.0, 1.0)

            print('boundary_channel', boundary_channel)
            zero_tensor = fluid.layers.zeros_like(self.x_coords)

            zero_tensor = zero_tensor.numpy()
            self.x_coords = self.x_coords.numpy()
            self.y_coords = self.y_coords.numpy()
            print(self.x_coords)
            print(self.y_coords)
            # xx_boundary_channel = paddle_where(self.x_coords, heatmap, 0.05)
            # yy_boundary_channel = paddle_where(self.y_coords, heatmap, 0.05)
            xx_boundary_channel = np.where(boundary_channel > 0.05,
                                           self.x_coords, zero_tensor)
            yy_boundary_channel = np.where(boundary_channel > 0.05,
                                           self.y_coords, zero_tensor)
            xx_boundary_channel = fluid.dygraph.to_variable(
                xx_boundary_channel)
            yy_boundary_channel = fluid.dygraph.to_variable(
                yy_boundary_channel)
            # boundary_channel = fluid.layers.clip(heatmap[:, -1:, :, :], 0.0, 1.0)
            # xx_boundary_channel = paddle_where(self.x_coords, boundary_channel, 0.05)
            # yy_boundary_channel = paddle_where(self.y_coords, boundary_channel, 0.05)

            coords = fluid.layers.concat(
                [coords, xx_boundary_channel, yy_boundary_channel], axis=1)

        coords = fluid.layers.reshape(
            coords, [x.shape[0], 3, coords.shape[2], coords.shape[3]])

        x_and_coords = fluid.layers.concat([x, coords], axis=1)
        return x_and_coords