Example #1
0
    def forward(self, depth, trans0, trans1, rotate):
        self.batchgrid3d = torch.zeros(torch.Size([depth.size(0)]) + self.grid3d.size())

        for i in range(depth.size(0)):
            self.batchgrid3d[i] = self.grid3d

        self.batchgrid3d = Variable(self.batchgrid3d)

        self.batchgrid = torch.zeros(torch.Size([depth.size(0)]) + self.grid.size())

        for i in range(depth.size(0)):
            self.batchgrid[i] = self.grid

        self.batchgrid = Variable(self.batchgrid)

        if depth.is_cuda:
            self.batchgrid = self.batchgrid.cuda()
            self.batchgrid3d = self.batchgrid3d.cuda()


        x_ = self.batchgrid3d[:,:,:,0:1] * depth + trans0.view(-1,1,1,1).repeat(1, self.height, self.width, 1)

        y_ = self.batchgrid3d[:,:,:,1:2] * depth + trans1.view(-1,1,1,1).repeat(1, self.height, self.width, 1)
        z = self.batchgrid3d[:,:,:,2:3] * depth
        #print(x.size(), y.size(), z.size())

        rotate_z = rotate.view(-1,1,1,1).repeat(1,self.height, self.width,1) * np.pi

        x = x_ * torch.cos(rotate_z) - y_ * torch.sin(rotate_z)
        y = x_ * torch.sin(rotate_z) + y_ * torch.cos(rotate_z)


        r = torch.sqrt(x**2 + y**2 + z**2) + 1e-5

        #print(r)
        theta = torch.acos(z/r)/(np.pi/2)  - 1
        #phi = torch.atan(y/x)

        if depth.is_cuda:
            phi = torch.atan(y/(x + 1e-5))  + np.pi * x.lt(0).type(torch.cuda.FloatTensor) * (y.ge(0).type(torch.cuda.FloatTensor) - y.lt(0).type(torch.cuda.FloatTensor))
        else:
            phi = torch.atan(y/(x + 1e-5))  + np.pi * x.lt(0).type(torch.FloatTensor) * (y.ge(0).type(torch.FloatTensor) - y.lt(0).type(torch.FloatTensor))


        phi = phi/np.pi

        output = torch.cat([theta,phi], 3)
        return output
Example #2
0
    def forward(self, input1):
        self.batchgrid3d = torch.zeros(torch.Size([input1.size(0)]) + self.grid3d.size())

        for i in range(input1.size(0)):
            self.batchgrid3d[i] = self.grid3d

        self.batchgrid3d = Variable(self.batchgrid3d)
        #print(self.batchgrid3d)

        x = torch.sum(torch.mul(self.batchgrid3d, input1[:,:,:,0:4]), 3)
        y = torch.sum(torch.mul(self.batchgrid3d, input1[:,:,:,4:8]), 3)
        z = torch.sum(torch.mul(self.batchgrid3d, input1[:,:,:,8:]), 3)
        #print(x)
        r = torch.sqrt(x**2 + y**2 + z**2) + 1e-5

        #print(r)
        theta = torch.acos(z/r)/(np.pi/2)  - 1
        #phi = torch.atan(y/x)
        phi = torch.atan(y/(x + 1e-5))  + np.pi * x.lt(0).type(torch.FloatTensor) * (y.ge(0).type(torch.FloatTensor) - y.lt(0).type(torch.FloatTensor))
        phi = phi/np.pi


        output = torch.cat([theta,phi], 3)

        return output
Example #3
0
    def forward(self, depth, trans0, trans1, rotate):
        self.batchgrid3d = torch.zeros(torch.Size([depth.size(0)]) + self.grid3d.size())

        for i in range(depth.size(0)):
            self.batchgrid3d[i] = self.grid3d

        self.batchgrid3d = Variable(self.batchgrid3d)

        self.batchgrid = torch.zeros(torch.Size([depth.size(0)]) + self.grid.size())

        for i in range(depth.size(0)):
            self.batchgrid[i] = self.grid

        self.batchgrid = Variable(self.batchgrid)

        x = self.batchgrid3d[:,:,:,0:1] * depth + trans0.view(-1,1,1,1).repeat(1, self.height, self.width, 1)

        y = self.batchgrid3d[:,:,:,1:2] * depth + trans1.view(-1,1,1,1).repeat(1, self.height, self.width, 1)
        z = self.batchgrid3d[:,:,:,2:3] * depth
        #print(x.size(), y.size(), z.size())
        r = torch.sqrt(x**2 + y**2 + z**2) + 1e-5

        #print(r)
        theta = torch.acos(z/r)/(np.pi/2)  - 1
        #phi = torch.atan(y/x)
        phi = torch.atan(y/(x + 1e-5))  + np.pi * x.lt(0).type(torch.FloatTensor) * (y.ge(0).type(torch.FloatTensor) - y.lt(0).type(torch.FloatTensor))
        phi = phi/np.pi

        #print(theta.size(), phi.size())


        input_u = rotate.view(-1,1,1,1).repeat(1,self.height, self.width,1)

        output = torch.cat([theta,phi], 3)
        #print(output.size())

        output1 = torch.atan(torch.tan(np.pi/2.0*(output[:,:,:,1:2] + self.batchgrid[:,:,:,2:] * input_u[:,:,:,:])))  /(np.pi/2)
        output2 = torch.cat([output[:,:,:,0:1], output1], 3)

        return output2
Example #4
0
    def set_stats_get_pairs(self, embeddings, labels, indices_tuple):
        a1, p, a2, _ = lmu.convert_to_pairs(indices_tuple, labels)
        if len(a1) == 0 or len(a2) == 0:
            return [None]*4
        anchors, positives = embeddings[a1], embeddings[p]
        keep_mask = (labels[a1].unsqueeze(1) != labels.unsqueeze(0)).float()

        centers = (anchors + positives) / 2
        ap_dist = torch.nn.functional.pairwise_distance(anchors, positives, 2)
        nc_dist = torch.norm(centers - embeddings.unsqueeze(1), p=2, dim=2).t()
        angles = torch.atan(ap_dist.unsqueeze(1) / (2*nc_dist))
        average_angle = torch.sum(angles*keep_mask) / torch.sum(keep_mask)
        self.average_angle = np.degrees(average_angle.item())
        return anchors, positives, keep_mask, a1
def cartesian2elliptic(pt, S, L):
    # cartesian to elliptic coordinates
    # pt: (3,)
    # S: (3,)
    # L: (3,)
    f = torch.sqrt(torch.sum((L - S)**2)) / 2

    L = torch.sqrt(torch.sum((pt - S)**2)) + torch.sqrt(torch.sum((pt - L)**2))
    a = L / 2
    b = torch.sqrt(a**2 - f**2)
    if torch.isnan(b):
        return None
    else:
        elliptic_pt = torch.zeros(3)
        elliptic_pt[0] = L
        elliptic_pt[1] = torch.acos(pt[2] / b)
        phi_yplus = (torch.atan(pt[1] / (pt[0] + 1e-8))) * (pt[1] >= 0)
        phi_yplus = phi_yplus + (phi_yplus < 0).int() * (np.pi)
        phi_yminus = (torch.atan(pt[1] / (pt[0] + 1e-8))) * (pt[1] < 0)
        phi_yminus = phi_yminus + (phi_yminus > 0).int() * (-np.pi)
        elliptic_pt[2] = phi_yminus + phi_yplus

    return elliptic_pt
Example #6
0
def grasps2bboxes(labels):
    x, y, tan_theta, w, h = labels[:, 0], labels[:, 1], labels[:, 2], labels[:, 3], labels[:, 4] 
    theta = torch.atan(tan_theta)
    x0, y0 = x-w/2*torch.cos(theta) + h/2*torch.sin(theta), y - w/2*torch.sin(theta) - h/2*torch.cos(theta) # 数据集是按逆时针顺序给出4个顶点的,这里也跟随逆时针序
    x1, y1 = x-w/2*torch.cos(theta) - h/2*torch.sin(theta), y - w/2*torch.sin(theta) + h/2*torch.cos(theta) 
    x2, y2 = x+w/2*torch.cos(theta) - h/2*torch.sin(theta), y + w/2*torch.sin(theta) + h/2*torch.cos(theta)
    x3, y3 = x+w/2*torch.cos(theta) + h/2*torch.sin(theta), y + w/2*torch.sin(theta) - h/2*torch.cos(theta)
    batch_size = len(labels)
    point0 = torch.cat([x0.reshape(batch_size, 1), y0.reshape(batch_size, 1)], dim=1).cpu().numpy().astype(np.int32)
    point1 = torch.cat([x1.reshape(batch_size, 1), y1.reshape(batch_size, 1)], dim=1).cpu().numpy().astype(np.int32)
    point2 = torch.cat([x2.reshape(batch_size, 1), y2.reshape(batch_size, 1)], dim=1).cpu().numpy().astype(np.int32)
    point3 = torch.cat([x3.reshape(batch_size, 1), y3.reshape(batch_size, 1)], dim=1).cpu().numpy().astype(np.int32)

    return point0, point1, point2, point3
Example #7
0
def box_ciou(b1, b2):
    """
    输入为:
    ----------
    b1: tensor, shape=(batch, feat_w, feat_h, anchor_num, 4), xywh
    b2: tensor, shape=(batch, feat_w, feat_h, anchor_num, 4), xywh

    返回为:
    -------
    ciou: tensor, shape=(batch, feat_w, feat_h, anchor_num, 1)
    """
    # 求出预测框左上角右下角
    b1_xy = b1[..., :2]
    b1_wh = b1[..., 2:4]
    b1_wh_half = b1_wh / 2.
    b1_mins = b1_xy - b1_wh_half
    b1_maxes = b1_xy + b1_wh_half
    # 求出真实框左上角右下角
    b2_xy = b2[..., :2]
    b2_wh = b2[..., 2:4]
    b2_wh_half = b2_wh / 2.
    b2_mins = b2_xy - b2_wh_half
    b2_maxes = b2_xy + b2_wh_half

    # 求真实框和预测框所有的iou
    intersect_mins = torch.max(b1_mins, b2_mins)
    intersect_maxes = torch.min(b1_maxes, b2_maxes)
    intersect_wh = torch.max(intersect_maxes - intersect_mins, torch.zeros_like(intersect_maxes))
    intersect_area = intersect_wh[..., 0] * intersect_wh[..., 1]
    b1_area = b1_wh[..., 0] * b1_wh[..., 1]
    b2_area = b2_wh[..., 0] * b2_wh[..., 1]
    union_area = b1_area + b2_area - intersect_area
    iou = intersect_area / torch.clamp(union_area, min=1e-6)

    # 计算中心的差距
    center_distance = torch.sum(torch.pow((b1_xy - b2_xy), 2), axis=-1)

    # 找到包裹两个框的最小框的左上角和右下角
    enclose_mins = torch.min(b1_mins, b2_mins)
    enclose_maxes = torch.max(b1_maxes, b2_maxes)
    enclose_wh = torch.max(enclose_maxes - enclose_mins, torch.zeros_like(intersect_maxes))
    # 计算对角线距离
    enclose_diagonal = torch.sum(torch.pow(enclose_wh, 2), axis=-1)
    ciou = iou - 1.0 * (center_distance) / torch.clamp(enclose_diagonal, min=1e-6)

    v = (4 / (math.pi ** 2)) * torch.pow((torch.atan(b1_wh[..., 0] / torch.clamp(b1_wh[..., 1], min=1e-6)) - torch.atan(
        b2_wh[..., 0] / torch.clamp(b2_wh[..., 1], min=1e-6))), 2)
    alpha = v / torch.clamp((1.0 - iou + v), min=1e-6)
    ciou = ciou - alpha * v
    return ciou
Example #8
0
    def forward(self, input, target):
        # F.cross_entropy(input, target, weight=self.weight, ignore_index=self.ignore_index, reduction=self.reduction)

        err = cross_entropy(input,
                            target,
                            weight=self.weight,
                            ignore_index=self.ignore_index,
                            reduction=self.reduction)

        err = 10 * torch.atan(err) + err

        # return cross_entropy(input, target, weight=self.weight, ignore_index=self.ignore_index, reduction=self.reduction)

        return err
def moveIBLwithParamsTorch(posCenters, depths, radius, translation, rotationAzimuth):
    # Translation should be specified as a (x, y, z) translation vector
    # Rotation should be specified as an angle in radians around the origin

    if configInfos.coordinateSystem == "xyz" and posCenters.shape[-1] == 3:
        # We have to convert everything in spherical coordinates first
        posCenters, _ = xyz2sphericalTorch(posCenters)
    else:
        posCenters = posCenters.clone()   # Ensure we do not modify something important

    # First, we apply the rotation (with spherical coordinates)
    posCenters = posCenters.clone()      # Ensure we do not modify something important
    posCenters[:, 1].sub_(rotationAzimuth)

    # Then, we convert to xyz (easier to translate than with spherical coordinates)
    xyz = spherical2xyzTorch(posCenters[:, 0], posCenters[:, 1], norm=depths.clone())

    # Apply the translation
    # Careful : we translate the _lights_ so we should go in the opposite
    # direction than the direction to the crop
    xyz[:, 2].add_(translation)

    # We convert the coordinates back into spherical
    newPosCenters, newDepths = xyz2sphericalTorch(xyz)

    # We adjust the angular size of each light
    # We assume that the area lights normal are always towards the virtual
    # center of projection, even if we moved in the scene
    # (as if the lights were actually spheres with the same angular area
    # from any reference point in the panorama)
    
    # To do that, we first convert the angular area from the original point
    # to an actual area using the distance
    # IMPORTANT : this assumes `radius` to be in radians, which IS NOT HOW
    # THEY ARE SAVED IN THE PARAMETERS FILE
    radiusMeters = 2 * depths * torch.tan(radius/2)
    
    # From there, we do the inverse computation, but now with the depth
    # at the new virtual viewpoint
    # TODO : there is one *2 that could be removed from both formulaes
    # currently keeping it just to be more clear
    newRadius = 2 * torch.atan(radiusMeters / 2 / newDepths)

    newPosCenters[:, 1].add_(2*np.pi)
    newPosCenters[:, 1] = torch.fmod(newPosCenters[:, 1], 2*np.pi)

    if torch.any(torch.isnan(newPosCenters)):
        breakpoint()

    return newPosCenters, newDepths, newRadius
Example #10
0
def aten_atan(inputs, attributes, scope):
    inp = inputs[0]
    ctx = current_context()
    net = ctx.network
    if ctx.is_tensorrt and has_trt_tensor(inputs):
        layer = net.add_unary(inp, trt.UnaryOperation.ATAN)
        output = layer.get_output(0)
        output.name = scope
        layer.name = scope
        return [output]
    elif ctx.is_tvm and has_tvm_tensor(inputs):
        raise NotImplementedError

    return [torch.atan(inp)]
Example #11
0
def compute_c_iou(bboxes1, bboxes2):
    "bboxes1 of shape [N, 4] and bboxes2 of shape [N, 4]"
    # assert bboxes1.size(0) == bboxes2.size(0)
    w1 = bboxes1[:, 2] - bboxes1[:, 0]
    h1 = bboxes1[:, 3] - bboxes1[:, 1]
    w2 = bboxes2[:, 2] - bboxes2[:, 0]
    h2 = bboxes2[:, 3] - bboxes2[:, 1]
    area1 = w1 * h1
    area2 = w2 * h2
    min_x2 = torch.min(bboxes1[:, 2], bboxes2[:, 2])
    max_x1 = torch.max(bboxes1[:, 0], bboxes2[:, 0])
    min_y2 = torch.min(bboxes1[:, 3], bboxes2[:, 3])
    max_y1 = torch.max(bboxes1[:, 1], bboxes2[:, 1])

    inter = torch.clamp(min_x2 - max_x1, min=0) * torch.clamp(min_y2 - max_y1,
                                                              min=0)
    union = area1 + area2 - inter

    center_x1 = (bboxes1[:, 2] + bboxes1[:, 0]) / 2
    center_y1 = (bboxes1[:, 3] + bboxes1[:, 1]) / 2
    center_x2 = (bboxes2[:, 2] + bboxes2[:, 0]) / 2
    center_y2 = (bboxes2[:, 3] + bboxes2[:, 1]) / 2
    # squared euclidian distance between the target and predicted bboxes
    d_2 = (center_x1 - center_x2)**2 + (center_y1 - center_y2)**2
    # squared length of the diagonal of the minimum bbox that encloses both bboxes
    c_2 = (torch.max(bboxes1[:, 2], bboxes2[:, 2]) -
           torch.min(bboxes1[:, 0], bboxes2[:, 0]))**2 + (
               torch.max(bboxes1[:, 3], bboxes2[:, 3]) -
               torch.min(bboxes1[:, 1], bboxes2[:, 1]))**2
    iou = inter / union
    v = 4 / np.pi**2 * (torch.atan(w1 / h1) - torch.atan(w2 / h2))**2
    with torch.no_grad():
        S = 1 - iou
        alpha = v / (S + v + eps)
    c_iou = iou - (d_2 / c_2 + alpha * v)
    c_iou = torch.clamp(c_iou, min=-1.0, max=1.0)
    return c_iou
    def forward(self, input1, input2):
        self.batchgrid3d = torch.zeros(torch.Size([input1.size(0)]) + self.grid3d.size())

        for i in range(input1.size(0)):
            self.batchgrid3d[i] = self.grid3d

        self.batchgrid3d = Variable(self.batchgrid3d)

        self.batchgrid = torch.zeros(torch.Size([input1.size(0)]) + self.grid.size())

        for i in range(input1.size(0)):
            self.batchgrid[i] = self.grid

        self.batchgrid = Variable(self.batchgrid)

        #print(self.batchgrid3d)

        x = torch.sum(torch.mul(self.batchgrid3d, input1[:,:,:,0:4]), 3)
        y = torch.sum(torch.mul(self.batchgrid3d, input1[:,:,:,4:8]), 3)
        z = torch.sum(torch.mul(self.batchgrid3d, input1[:,:,:,8:]), 3)
        #print(x)
        r = torch.sqrt(x**2 + y**2 + z**2) + 1e-5

        #print(r)
        theta = torch.acos(z/r)/(np.pi/2)  - 1
        #phi = torch.atan(y/x)
        phi = torch.atan(y/(x + 1e-5))  + np.pi * x.lt(0).type(torch.FloatTensor) * (y.ge(0).type(torch.FloatTensor) - y.lt(0).type(torch.FloatTensor))
        phi = phi/np.pi

        input_u = input2.view(-1,1,1,1).repeat(1,self.height, self.width,1)

        output = torch.cat([theta,phi], 3)

        output1 = torch.atan(torch.tan(np.pi/2.0*(output[:,:,:,1:2] + self.batchgrid[:,:,:,2:] * input_u[:,:,:,:])))  /(np.pi/2)
        output2 = torch.cat([output[:,:,:,0:1], output1], 3)

        return output2
Example #13
0
def bbox_ciou(box1, box2, eps: float = 1e-9):
    # Returns the IoU of box1 to box2. box1 is 4, box2 is nx4
    box2 = box2.T

    # Get the coordinates of bounding boxes
    # transform from xywh to xyxy
    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

    # Intersection area
    inter = (torch.min(b1_x2, b2_x2) - torch.max(b1_x1, b2_x1)).clamp(0) * \
            (torch.min(b1_y2, b2_y2) - torch.max(b1_y1, b2_y1)).clamp(0)

    # Union Area
    w1, h1 = b1_x2 - b1_x1, b1_y2 - b1_y1 + eps
    w2, h2 = b2_x2 - b2_x1, b2_y2 - b2_y1 + eps
    union = w1 * h1 + w2 * h2 - inter + eps

    iou = inter / union

    cw = torch.max(b1_x2, b2_x2) - torch.min(
        b1_x1, b2_x1)  # convex (smallest enclosing box) width
    ch = torch.max(b1_y2, b2_y2) - torch.min(b1_y1, b2_y1)  # convex height

    c2 = cw**2 + ch**2 + eps  # convex diagonal squared
    # center distance squared
    rho2 = ((b2_x1 + b2_x2 - b1_x1 - b1_x2)**2 +
            (b2_y1 + b2_y2 - b1_y1 - b1_y2)**2) / 4

    v = (4 / math.pi**2) * torch.pow(
        torch.atan(w2 / h2) - torch.atan(w1 / h1), 2)
    with torch.no_grad():
        alpha = v / ((1 + eps) - iou + v)

    return iou - (rho2 / c2 + v * alpha)  # CIoU
Example #14
0
    def forward(self,
                lstm_output,
                various_observations,
                opponent_observations=None):
        # check and improve it
        player_scalar_out = self.pre_forward(various_observations)
        del various_observations

        # AlphaStar: The baseline extracts those same observations from `opponent_observations`.
        if opponent_observations is not None:
            opponenet_scalar_out = self.pre_forward(opponent_observations)
            del opponent_observations
        else:
            opponenet_scalar_out = torch.zeros_like(player_scalar_out)

        # AlphaStar: These features are all concatenated together to yield `action_type_input`
        action_type_input = torch.cat(
            [lstm_output, player_scalar_out, opponenet_scalar_out], dim=1)
        del lstm_output, player_scalar_out, opponenet_scalar_out
        print("action_type_input.shape:",
              action_type_input.shape) if debug else None

        # AlphaStar: passed through a linear of size 256
        x = self.embed_fc(action_type_input)
        del action_type_input
        print("x.shape:", x.shape) if debug else None

        # AlphaStar: then passed through 16 ResBlocks with 256 hidden units and layer normalization,
        # Note: tje layer normalization is already inside each resblock
        x = x.unsqueeze(-1)
        for resblock in self.resblock_stack:
            x = resblock(x)
        x = x.squeeze(-1)
        print("x.shape:", x.shape) if debug else None

        # AlphaStar: passed through a ReLU
        x = F.relu(x)

        # AlphaStar: then passed through a linear with 1 hidden unit.
        baseline = self.out_fc(x)
        print("baseline:", baseline) if debug else None

        # AlphaStar: This baseline value is transformed by ((2.0 / PI) * atan((PI / 2.0) * baseline)) and is used as the baseline value
        out = (2.0 / np.pi) * torch.atan((np.pi / 2.0) * baseline)
        print("out:", out) if debug else None

        del x, baseline

        return out
        def get_alpha(rot):
            """Generate alpha value from predicted CLSREG array

            Args:
                rot (torch.Tensor): rotation CLSREG array: (B, num_classes, 8) 
                [bin1_cls[0], bin1_cls[1], bin1_sin, bin1_cos,
                 bin2_cls[0], bin2_cls[1], bin2_sin, bin2_cos]

            Returns:
                torch.Tensor: (B, num_classes, 1)
            """
            alpha1 = torch.atan(
                rot[:, :, 2:3] / rot[:, :, 3:4]) + (-0.5 * np.pi)
            alpha2 = torch.atan(
                rot[:, :, 6:7] / rot[:, :, 7:8]) + (0.5 * np.pi)
            # Model is not decisive at index overlap region
            # Could be unstable for the alpha estimation
            # idx1 = (rot[:, :, 1:2] > rot[:, :, 0:1]).float()
            # idx2 = (rot[:, :, 5:6] > rot[:, :, 4:5]).float()
            # alpha = (alpha1 * idx1 + alpha2 * idx2)
            # alpha /= (idx1 + idx2 + ((idx1 + idx2) == 0))
            idx = (rot[:, :, 1:2] > rot[:, :, 5:6]).float()
            alpha = alpha1 * idx + alpha2 * (1 - idx)
            return alpha
Example #16
0
    def make_z(self, skew_idx_x, skew_idx_y):
        skew = torch.tensor(
            (1 / self.n_skews * skew_idx_x, 1 / self.n_skews * skew_idx_y))
        # apply skew function
        xc = torch.tensor((self.center[1], self.center[0])).float()
        x, y = torch.meshgrid(
            [torch.arange(self.shape[1]),
             torch.arange(self.shape[0])])
        pos = torch.empty(x.shape + (2, ))
        pos[:, :, 0] = x
        pos[:, :, 1] = y
        z = (1 / math.pi) * torch.atan((pos - xc) @ skew) + \
            (1 / 2)

        return z
Example #17
0
def get_angles_smass_in_rag(edges, segmentation):
    """
        CAUTION: this is specificly targeted for the leptin dataset
    """
    sigm_flatness = torch.numel(segmentation) / 500
    sigm_shift = 0.8
    c_norm = math.sqrt(segmentation.shape[-1]**2 + segmentation.shape[-2]**2) / 8
    nodes = torch.unique(segmentation)
    random_rotation = (torch.rand((1,)).item() * 2) - 1

    meshgrid = torch.stack(torch.meshgrid(torch.arange(segmentation.shape[0], device=segmentation.device),
                                          torch.arange(segmentation.shape[1], device=segmentation.device)))
    one_hot = segmentation[None] == nodes[:, None, None]
    sup_sizes = one_hot.flatten(1).sum(-1)
    cart_cms = (one_hot[None] * meshgrid[:, None]).flatten(2).sum(2) / sup_sizes[None]

    a = (cart_cms[0].float() - 390)  # This is not the image center because the Leptin data is not exactly concentric to it
    b = (cart_cms[1].float() - 340)
    c = torch.sqrt(a**2 + b**2)
    normed_c = torch.sigmoid(c / c_norm - 1) * 2 - 1
    ang = torch.atan(a/(b + 1e-10)).abs()
    ang[(b < 0) & (a >= 0)] = np.pi - ang[(b < 0) & (a >= 0)]
    ang[(b < 0) & (a < 0)] = np.pi + ang[(b < 0) & (a < 0)]
    ang[(b >= 0) & (a < 0)] = 2 * np.pi - ang[(b >= 0) & (a < 0)]
    ang /= np.pi
    ang -= 1
    # ang += random_rotation
    # ang[a > 1] = (ang[a > 1] - 1) - 1
    # ang[ang < -1] = 1 + (ang[ang < -1] + 1)
    cms = torch.stack([ang, normed_c, c], 1)

    vec = cart_cms[:, edges[0]] - cart_cms[:, edges[1]]
    angles = torch.atan(vec[0] / (vec[1] + np.finfo(float).eps))
    angles = 2 * angles / np.pi
    sup_sizes = torch.sigmoid(sup_sizes / sigm_flatness - sigm_shift) * 2 - 1
    return angles, torch.cat([sup_sizes[:, None], cms[:, :-1]], 1), cms[:, -1], cart_cms, sup_sizes
Example #18
0
def cartesian2polar(xyz):
    #  B, N, K, C or B N C
    len4 = False
    B, N, C = xyz.shape[0], xyz.shape[1], xyz.shape[-1]
    if len(xyz.shape) == 4:
        xyz = xyz.reshape(B, -1, C)
        len4 = True
    ro = xyz.norm(dim=2)
    theta = torch.atan(xyz[:, :, 1] / (xyz[:, :, 0] + 1e-9))
    phi = torch.acos(xyz[:, :, 2] / (ro + 1e-9))
    sphere = torch.stack([ro, theta, phi]).permute(1, 2, 0)

    if len4:
        sphere = sphere.reshape(B, N, -1, C)
    return sphere
Example #19
0
    def forward(self, p1s: MaskedTensor, p2s: MaskedTensor) -> torch.Tensor:
        """
        Angle of the X/Y axis between two points
        :param p1s: MaskedTensor (Points, Batch, Len, Dims)
        :param p2s: MaskedTensor (Points, Batch, Len, Dims)
        :return: torch.Tensor (Points, Batch, Len)
        """
        dims = p1s.shape[-1]

        d = p2s - p1s  # (Points, Batch, Len, Dims)
        xs, ys = d.split([1] * dims, dim=3)[:2]  # (Points, Batch, Len, 1)
        slopes = ys.div(xs, in_place=True).zero_filled().squeeze(axis=3)
        slopes[slopes != slopes] = 0  # Fix NaN, TODO think of faster way

        return torch.atan(slopes)
 def set_stats_get_triplets(self, embeddings, labels, indices_tuple):
     anchor_idx, positive_idx, negative_idx = lmu.convert_to_triplets(
         indices_tuple, labels, self.triplets_per_anchor)
     self.num_anchors = len(anchor_idx)
     if self.num_anchors == 0:
         return [None] * 4
     anchors, positives, negatives = embeddings[anchor_idx], embeddings[
         positive_idx], embeddings[negative_idx]
     centers = (anchors + positives) / 2
     ap_dist = torch.nn.functional.pairwise_distance(anchors, positives, 2)
     nc_dist = torch.nn.functional.pairwise_distance(negatives, centers, 2)
     self.average_angle = np.degrees(
         torch.mean(torch.atan(ap_dist / (2 * nc_dist))).item())
     alpha = self.maybe_mask_param(self.alpha, labels[anchor_idx])
     return anchors, positives, negatives, alpha
Example #21
0
def get_normal_anlges(image, eps=EPS):
    """Calculate the normal direction of edges.

    Ref: https://github.com/nv-tlabs/STEAL/blob/master/utils/edges_nms.m
    """
    first_grads = spatial_gradient(gaussian_blur2d(image, (5, 5), (2, 2)))
    second_grad_x = spatial_gradient(first_grads[:, :, 0, :, :].squeeze_(2))
    second_grad_y = spatial_gradient(first_grads[:, :, 1, :, :].squeeze_(2))

    grad_xx = second_grad_x[:, :, 0, :, :].squeeze_()
    grad_xy = second_grad_y[:, :, 0, :, :].squeeze_()
    grad_yy = second_grad_y[:, :, 1, :, :].squeeze_()
    angle = torch.atan(grad_yy * torch.sign(-(grad_xy + eps)) /
                       (grad_xx + eps))
    return angle
Example #22
0
def orientation_decode(ori, locations, flip_mask=None):
    '''
    :param pred_ori: location orientations (N, 2)
    :param locations:  object locations (N, 3)
    :return: global orientations
    '''
    offsets = torch.atan(locations[:, 0] / locations[:, 2] + 1e-7)
    alpha = torch.atan(ori[:, 0] / (ori[:, 1] + 1e-7))

    cos_pos_idx = (ori[:, 1] >= 0).nonzero()
    cos_neg_idx = (ori[:, 1] < 0).nonzero()

    alpha[cos_pos_idx] = alpha[cos_pos_idx] - PI / 2
    alpha[cos_neg_idx] = alpha[cos_neg_idx] + PI / 2

    rotys = alpha + offsets

    roty_large_idx = (rotys > PI).nonzero()
    roty_small_idx = (rotys < -PI).nonzero()

    if len(roty_large_idx) != 0:
        rotys[roty_large_idx] = rotys[roty_large_idx] - 2 * PI
    if len(roty_small_idx) != 0:
        rotys[roty_small_idx] = rotys[roty_small_idx] + 2 * PI

    fm = flip_mask.view(-1, 1).squeeze(1)
    rotys_flip = fm.float() * rotys

    rotys_flip_pos_idx = rotys_flip > 0
    rotys_flip_neg_idx = rotys_flip < 0
    rotys_flip[rotys_flip_pos_idx] -= PI
    rotys_flip[rotys_flip_neg_idx] += PI

    rotys_all = fm.float() * rotys_flip + (1 - fm.float()) * rotys

    return rotys_all.unsqueeze(1), alpha.unsqueeze(1)
def get_feature(n, points, adj):
    """
    :param n: points # of graph
    :param points: torch tensor, (n, 2)
    :param adj: torch tensor, (n, n)
    :return: edge feat, angle feat
    """
    points_1 = points.reshape(n, 1, 2).repeat(1, n, 1)
    points_2 = points.reshape(1, n, 2).repeat(n, 1, 1)
    edge_feat = torch.sqrt(torch.sum((points_1 - points_2) ** 2, dim=2))
    edge_feat = edge_feat / torch.max(edge_feat)
    angle_feat = torch.atan((points_1[:, :, 1] - points_2[:, :, 1]) / (points_1[:, :, 0] - points_2[:, :, 0] + 1e-8))
    angle_feat = 2 * angle_feat / math.pi

    return edge_feat, angle_feat
Example #24
0
    def forward(self, x):
        out = self.conv1(x)
        out = self.conv2(out)
        out = self.conv3(out)

        #flatten layer
        out_size = list(out.size())
        # print(out_size)
        # out_size[0] -->batch_size or test size as desired
        out = out.view(out_size[0], -1)
        # print(out.size())
        out = self.fc1(out)
        out = self.fc2(out)
        out = torch.mul(torch.atan(out), 2)
        return out
Example #25
0
def two_points_angle(point1, point2):
    '''
    point1
    point2 : center
    range : (-pi ~ pi)
                          |   
   (pi-atan(y/x))    -+   |  ++ (atan(y/x)) 
                    ______|______
                          |
                     --   |  +-
   (-pi + atan(y/x)       |    (atan(y/x)) 
'''
    point1_x, point1_y = point1
    point2_x, point2_y = point2
    x_positive_ids = point1_x >= point2_x
    y_positive_ids = point1_y > point2_y
    x_negative_ids = point1_x <= point2_x
    y_negative_ids = point1_y < point2_y
    first_quadrant_ids = (x_positive_ids & y_positive_ids).type(
        torch.cuda.FloatTensor)
    second_quadrant_ids = (x_negative_ids & y_positive_ids).type(
        torch.cuda.FloatTensor)
    third_quadrant_ids = (x_negative_ids & y_negative_ids).type(
        torch.cuda.FloatTensor)
    fourth_quadrant_ids = (x_positive_ids & y_negative_ids).type(
        torch.cuda.FloatTensor)
    first_quadrant_angle = first_quadrant_ids * (torch.atan(
        (point1_y - point2_y) / (point1_x - point2_x)))
    second_quadrant_angle = second_quadrant_ids * (np.pi + torch.atan(
        (point1_y - point2_y) / (point1_x - point2_x)))
    third_quadrant_angle = third_quadrant_ids * (-np.pi + torch.atan(
        (point1_y - point2_y) / (point1_x - point2_x)))
    fourth_quadrant_angle = fourth_quadrant_ids * (torch.atan(
        (point1_y - point2_y) / (point1_x - point2_x)))
    angle = first_quadrant_angle + second_quadrant_angle + third_quadrant_angle + fourth_quadrant_angle
    return angle
Example #26
0
def box_ciou(box1, box2):  # box format: (cx, cy, w, h)
    cx1, cy1, w1, h1 = box1.T
    cx2, cy2, w2, h2 = box2.T

    b1_x1, b1_x2 = cx1 - w1 / 2, cx1 + w1 / 2
    b1_y1, b1_y2 = cy1 - h1 / 2, cy1 + h1 / 2
    b2_x1, b2_x2 = cx2 - w2 / 2, cx2 + w2 / 2
    b2_y1, b2_y2 = cy2 - h2 / 2, cy2 + h2 / 2

    ws = torch.min(b1_x2, b2_x2) - torch.max(b1_x1, b2_x1)
    hs = torch.min(b1_y2, b2_y2) - torch.max(b1_y1, b2_y1)
    inter = ws.clamp(min=0) * hs.clamp(min=0)
    union = w1 * h1 + w2 * h2 - inter
    iou = inter / union

    v = (2 / math.pi * (torch.atan(w2 / h2) - torch.atan(w1 / h1)))**2
    with torch.no_grad():
        alpha = v / (1 - iou + v)

    rho2 = (cx1 - cx2)**2 + (cy1 - cy2)**2
    cw = torch.max(b1_x2, b2_x2) - torch.min(b1_x1, b2_x1)
    ch = torch.max(b1_y2, b2_y2) - torch.min(b1_y1, b2_y1)
    c2 = cw**2 + ch**2
    return iou - (rho2 / c2 + v * alpha)
Example #27
0
    def step(self, closure=None):
        """Performs a single optimization step.

        Arguments:
            closure (callable, optional): A closure that reevaluates the model
                and returns the loss.
        """
        loss = None
        if closure is not None:
            loss = closure()

        for group in self.param_groups:
            weight_decay = group['weight_decay']
            momentum = group['momentum']
            dampening = group['dampening']
            nesterov = group['nesterov']

            for p in group['params']:
                if p.grad is None:
                    continue
                d_p = p.grad.data
                if cfg.SOLVER.TAN:
                    d_p = cfg.SOLVER.A * torch.atan(d_p * cfg.SOLVER.B)
                # d_p = 0.3 * torch.atan(d_p*4.5)
                # d_p = 0.05 * torch.atan(d_p*1.5)
                # d_p = 0.7 * torch.atan(d_p*0.7)
                # d_p = 2 * (1 / (1 + torch.exp(-1 * d_p)) - 0.5)
                # 1.5 * (1 / (1 + np.exp(-1 * x)) - 0.5) + 0.1 * np.sign(x)
                if weight_decay != 0:
                    d_p.add_(weight_decay, p.data)
                if momentum != 0:
                    param_state = self.state[p]
                    if 'momentum_buffer' not in param_state:

                        buf = param_state['momentum_buffer'] = torch.clone(
                            d_p).detach()
                    else:
                        buf = param_state['momentum_buffer']
                        buf.mul_(momentum).add_(1 - dampening, d_p)
                    if nesterov:
                        d_p = d_p.add(momentum, buf)

                    else:
                        d_p = buf

                p.data.add_(-group['lr'], d_p)

        return loss
Example #28
0
    def get_regions_masks(self):
        regions_masks = []
        # create sectors
        R = self.img_size[0] // 2
        section_regions = [(ring_idx / self.n_sections * R,
                            (ring_idx + 1) / self.n_sections * R)
                           for ring_idx in range(self.n_sections)]

        # concatenate first and last regions
        orientation_regions = [
            (-np.pi / 2, -np.pi / 2 + 1 / self.n_orientations * np.pi / 2)
        ]
        offset = -np.pi / 2 + 1 / self.n_orientations * np.pi / 2
        orientation_regions += [
            (offset + wedge_idx / self.n_orientations * np.pi,
             offset + (wedge_idx + 1) / self.n_orientations * np.pi)
            for wedge_idx in range(self.n_orientations - 1)
        ]

        grid_x, grid_y = torch.meshgrid(torch.range(0, R - 1, 1),
                                        torch.range(-R, R - 1, 1))
        grid_r = (grid_x**2 + grid_y**2).sqrt()
        grid_theta = torch.atan(grid_y / grid_x)

        # fill feature vector
        for section_region in section_regions:
            r1 = section_region[0]
            r2 = section_region[1]

            # edge region
            theta1 = orientation_regions[0][0]
            theta2 = orientation_regions[0][1]

            region_mask = (grid_r >= r1) & (grid_r < r2) & (
                ((grid_theta >= theta1) & (grid_theta < theta2)) |
                ((grid_theta >= -offset) & (grid_theta <= np.pi / 2)))
            regions_masks.append(region_mask)

            # inner region
            for orientation_region in orientation_regions[1:]:
                theta1 = orientation_region[0]
                theta2 = orientation_region[1]

                region_mask = (grid_r >= r1) & (grid_r < r2) & (
                    grid_theta >= theta1) & (grid_theta < theta2)
                regions_masks.append(region_mask)

        return regions_masks
Example #29
0
def unitvec2azimuthelevation(
        x: Union[np.ndarray, torch.Tensor]) -> Union[np.ndarray, torch.Tensor]:
    """
    Convert 3-D unit vectors to (azimuth, elevation) values
    :param x: shape [BATCH_SIZE, TIME, CLASS, 3]
    :return: shape [BATCH_SIZE, TIME, CLASS * 2]
    """
    assert len(x.shape) == 4 and x.shape[-1] == 3
    if type(x) == np.ndarray:
        azim = np.arctan2(x[:, :, :, 1], x[:, :, :, 0])
        elev = np.arctan(x[:, :, :, 2])
        return np.concatenate((azim, elev), -1)
    elif type(x) == torch.Tensor:
        azim = torch.atan2(x[:, :, :, 1], x[:, :, :, 0])
        elev = torch.atan(x[:, :, :, 2])
        return torch.cat((azim, elev), -1)
Example #30
0
 def draw_flow(self, alpha=1):
     assert self.field is not None, "please create a field"
     field = self.field.detach()
     pi = 3.141592653
     deltay = field[:, 1:]
     deltax = field[:, :1]
     h = (torch.atan(deltay / (deltax + 1e-7)) / (2 * pi) + 0.25 +
          (deltax >= 0) * 0.5).cpu()
     s = alpha * ((deltax**2 + deltay**2)**0.5 / 2).cpu()
     v = torch.ones(h.shape)
     color_field = torch.cat([h, s, v], dim=1).permute([0, 2, 3, 1])
     l = []
     for f in color_field.numpy():
         l.append(skimage.color.hsv2rgb(f))
     color_field = torch.tensor(l).permute([0, 3, 1, 2])
     return color_field
Example #31
0
 def get_obs(self, x):
     theta = torch.atan(
         torch.abs(x[:, 1]) / (torch.abs(x[:, 0]) + self.tol))
     theta = torch.where((x[:, 0] >= 0) & (x[:, 1] < 0), -theta, theta)
     theta = torch.where((x[:, 0] < 0) & (x[:, 1] >= 0), np.pi - theta,
                         theta)
     theta = torch.where((x[:, 0] < 0) & (x[:, 1] < 0), theta - np.pi,
                         theta)
     new_theta_dot = x[:, 2] + (3 * self.g /
                                (2 * self.l) * torch.sin(theta) + 3 /
                                (self.m * self.l**2) * x[:, 3]) * self.dt
     new_theta = theta + new_theta_dot * self.dt
     new_theta_dot = torch.clamp(new_theta_dot, min=-8, max=8)
     out_obs = torch.stack(
         (torch.cos(new_theta), torch.sin(new_theta), new_theta_dot), dim=1)
     return out_obs, theta
Example #32
0
def normals_to_angles(normals):
    '''
    Given a batch of normals at each time step, converts to a 2D angle representation.
    normals : B x T x N x 3
    '''
    x2y2 = torch.norm(normals[:, :, :, :2], dim=3)
    theta = torch.atan(x2y2 / normals[:, :, :, 2]).unsqueeze(
        3)  # norm(x, y) / z
    theta[
        theta <
        0] += np.pi  # make angles from 0 to pi rather than discontinously from pi/2 to -pi/2
    phi = torch.atan2(normals[:, :, :, 1], normals[:, :, :,
                                                   0]).unsqueeze(3)  # y / x
    phi[phi < 0] += 2.0 * np.pi  # make angles from 0 to 2pi rather -pi to pi
    angles = torch.cat([theta, phi], dim=3)
    return angles
Example #33
0
    def evaluate_function(
            j
    ):  # Evaluate function over Rs[j] x Rs[j+1] fibers, each of size I[j]
        Xs = []
        for k, t in enumerate(tensors):
            if tensors[k].cores[j].dim() == 3:  # TT core
                V = torch.einsum('ai,ibj,jc->abc',
                                 (t_linterfaces[k][j], tensors[k].cores[j],
                                  t_rinterfaces[k][j]))
            else:  # CP factor
                V = torch.einsum('ai,bi,ic->abc',
                                 (t_linterfaces[k][j], tensors[k].cores[j],
                                  t_rinterfaces[k][j]))
            Xs.append(V.flatten())

        eval_start = time.time()
        evaluation = f(*Xs)
        info['eval_time'] += time.time() - eval_start
        if _minimize:
            evaluation = np.pi / 2 - torch.atan(
                evaluation - info['min']
            )  # Function used by I. Oseledets for TT minimization in ttpy
            evaluation_argmax = torch.argmax(evaluation)
            eval_min = torch.tan(np.pi / 2 -
                                 evaluation[evaluation_argmax]) + info['min']
            if info['min'] == 0 or eval_min < info['min']:
                coords = np.unravel_index(evaluation_argmax,
                                          [Rs[j], Is[j], Rs[j + 1]])
                info['min'] = eval_min
                info['argmin'] = tuple(lsets[j][coords[0]][1:]) + tuple(
                    [coords[1]]) + tuple(rsets[j][coords[2]][:-1])

        # Check for nan/inf values
        if evaluation.dim() == 2:
            evaluation = evaluation[:, 0]
        invalid = (torch.isnan(evaluation) | torch.isinf(evaluation)).nonzero()
        if len(invalid) > 0:
            invalid = invalid[0].item()
            raise ValueError(
                'Invalid return value for function {}: f({}) = {}'.format(
                    function,
                    ', '.join('{:g}'.format(x[invalid].numpy()) for x in Xs),
                    f(*[x[invalid:invalid + 1][:, None] for x in Xs]).item()))

        V = torch.reshape(evaluation, [Rs[j], Is[j], Rs[j + 1]])
        info['nsamples'] += V.numel()
        return V
Example #34
0
    def forward(self, input):
        self.batchgrid = torch.zeros(torch.Size([input.size(0)]) + self.grid.size() )
        #print(self.batchgrid.size())
        for i in range(input.size(0)):
            self.batchgrid[i,:,:,:] = self.grid
        self.batchgrid = Variable(self.batchgrid)

        #print(self.batchgrid.size())

        input_u = input.view(-1,1,1,1).repeat(1,self.height, self.width,1)
        #print(input_u.requires_grad, self.batchgrid)

        output0 = self.batchgrid[:,:,:,0:1]
        output1 = torch.atan(torch.tan(np.pi/2.0*(self.batchgrid[:,:,:,1:2] + self.batchgrid[:,:,:,2:] * input_u[:,:,:,:])))  /(np.pi/2)
        #print(output0.size(), output1.size())

        output = torch.cat([output0, output1], 3)
        return output
Example #35
0
def getPhase(x):
    return torch.atan(x[...,1]/x[...,0])
Example #36
0
 def cdf(self, value):
     self._validate_log_prob_arg(value)
     return torch.atan((value - self.loc) / self.scale) / math.pi + 0.5