本文整理汇总了Python中torch.atan2方法的典型用法代码示例。如果您正苦于以下问题:Python torch.atan2方法的具体用法?Python torch.atan2怎么用?Python torch.atan2使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torch
的用法示例。
在下文中一共展示了torch.atan2方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __call__
# 需要导入模块: import torch [as 别名]
# 或者: from torch import atan2 [as 别名]
def __call__(self, data):
(row, col), pos, pseudo = data.edge_index, data.pos, data.edge_attr
assert pos.dim() == 2 and pos.size(1) == 2
cart = pos[col] - pos[row]
rho = torch.norm(cart, p=2, dim=-1).view(-1, 1)
theta = torch.atan2(cart[..., 1], cart[..., 0]).view(-1, 1)
theta = theta + (theta < 0).type_as(theta) * (2 * PI)
if self.norm:
rho = rho / (rho.max() if self.max is None else self.max)
theta = theta / (2 * PI)
polar = torch.cat([rho, theta], dim=-1)
if pseudo is not None and self.cat:
pseudo = pseudo.view(-1, 1) if pseudo.dim() == 1 else pseudo
data.edge_attr = torch.cat([pseudo, polar.type_as(pos)], dim=-1)
else:
data.edge_attr = polar
return data
示例2: delta2box_rotated
# 需要导入模块: import torch [as 别名]
# 或者: from torch import atan2 [as 别名]
def delta2box_rotated(deltas, anchors, size, stride):
'Convert deltas from anchors to boxes'
anchors_wh = anchors[:, 2:4] - anchors[:, :2] + 1
ctr = anchors[:, :2] + 0.5 * anchors_wh
pred_ctr = deltas[:, :2] * anchors_wh + ctr
pred_wh = torch.exp(deltas[:, 2:4]) * anchors_wh
pred_sin = deltas[:, 4]
pred_cos = deltas[:, 5]
m = torch.zeros([2], device=deltas.device, dtype=deltas.dtype)
M = (torch.tensor([size], device=deltas.device, dtype=deltas.dtype) * stride - 1)
clamp = lambda t: torch.max(m, torch.min(t, M))
return torch.cat([
clamp(pred_ctr - 0.5 * pred_wh),
clamp(pred_ctr + 0.5 * pred_wh - 1),
torch.atan2(pred_sin, pred_cos)[:, None]
], 1)
示例3: complex_sign
# 需要导入模块: import torch [as 别名]
# 或者: from torch import atan2 [as 别名]
def complex_sign(t, dim=0):
"""Complex sign function value, complex dimension is dim.
Args:
t (tensor): A tensor where dimension dim is the complex dimension.
dim (int, default=0): An integer indicating the complex dimension.
Returns:
tensor: The complex sign of t.
"""
assert t.shape[dim] == 2
signt = torch.atan2(t.select(dim, 1), t.select(dim, 0))
signt = imag_exp(signt, dim=dim)
return signt
示例4: rotmat2quat_torch
# 需要导入模块: import torch [as 别名]
# 或者: from torch import atan2 [as 别名]
def rotmat2quat_torch(R):
"""
Converts a rotation matrix to quaternion
batch pytorch version ported from the corresponding numpy method above
:param R: N * 3 * 3
:return: N * 4
"""
rotdiff = R - R.transpose(1, 2)
r = torch.zeros_like(rotdiff[:, 0])
r[:, 0] = -rotdiff[:, 1, 2]
r[:, 1] = rotdiff[:, 0, 2]
r[:, 2] = -rotdiff[:, 0, 1]
r_norm = torch.norm(r, dim=1)
sintheta = r_norm / 2
r0 = torch.div(r, r_norm.unsqueeze(1).repeat(1, 3) + 0.00000001)
t1 = R[:, 0, 0]
t2 = R[:, 1, 1]
t3 = R[:, 2, 2]
costheta = (t1 + t2 + t3 - 1) / 2
theta = torch.atan2(sintheta, costheta)
q = Variable(torch.zeros(R.shape[0], 4)).float().cuda()
q[:, 0] = torch.cos(theta / 2)
q[:, 1:] = torch.mul(r0, torch.sin(theta / 2).unsqueeze(1).repeat(1, 3))
return q
示例5: post_process_output
# 需要导入模块: import torch [as 别名]
# 或者: from torch import atan2 [as 别名]
def post_process_output(q_img, cos_img, sin_img, width_img):
"""
Post-process the raw output of the GG-CNN, convert to numpy arrays, apply filtering.
:param q_img: Q output of GG-CNN (as torch Tensors)
:param cos_img: cos output of GG-CNN
:param sin_img: sin output of GG-CNN
:param width_img: Width output of GG-CNN
:return: Filtered Q output, Filtered Angle output, Filtered Width output
"""
q_img = q_img.cpu().numpy().squeeze()
ang_img = (torch.atan2(sin_img, cos_img) / 2.0).cpu().numpy().squeeze()
width_img = width_img.cpu().numpy().squeeze() * 150.0
q_img = gaussian(q_img, 2.0, preserve_range=True)
ang_img = gaussian(ang_img, 2.0, preserve_range=True)
width_img = gaussian(width_img, 1.0, preserve_range=True)
return q_img, ang_img, width_img
示例6: forward
# 需要导入模块: import torch [as 别名]
# 或者: from torch import atan2 [as 别名]
def forward(self, b):
'''
Input:
b: bounding box [batch, num_obj, 4] (x1,y1,x2,y2)
Output:
pseudo_coord [batch, num_obj, num_obj, 2] (rho, theta)
'''
batch_size, num_obj, _ = b.shape
centers = (b[:,:,2:] + b[:,:,:2]) * 0.5
relative_coord = centers.view(batch_size, num_obj, 1, 2) - \
centers.view(batch_size, 1, num_obj, 2) # broadcast: [batch, num_obj, num_obj, 2]
rho = torch.sqrt(relative_coord[:,:,:,0]**2 + relative_coord[:,:,:,1]**2)
theta = torch.atan2(relative_coord[:,:,:,0], relative_coord[:,:,:,1])
new_coord = torch.cat((rho.unsqueeze(-1), theta.unsqueeze(-1)), dim=-1)
return new_coord
示例7: forward
# 需要导入模块: import torch [as 别名]
# 或者: from torch import atan2 [as 别名]
def forward(self, x, return_rot_matrix = False):
gx = self.gx(F.pad(x, (1,1,0, 0), 'replicate'))
gy = self.gy(F.pad(x, (0,0, 1,1), 'replicate'))
mag = torch.sqrt(gx * gx + gy * gy + 1e-10)
if x.is_cuda:
self.gk = self.gk.cuda()
mag = mag * self.gk.unsqueeze(0).unsqueeze(0).expand_as(mag)
ori = torch.atan2(gy,gx)
o_big = float(self.num_ang_bins) *(ori + 1.0 * math.pi )/ (2.0 * math.pi)
bo0_big = torch.floor(o_big)
wo1_big = o_big - bo0_big
bo0_big = bo0_big % self.num_ang_bins
bo1_big = (bo0_big + 1) % self.num_ang_bins
wo0_big = (1.0 - wo1_big) * mag
wo1_big = wo1_big * mag
ang_bins = []
for i in range(0, self.num_ang_bins):
ang_bins.append(F.adaptive_avg_pool2d((bo0_big == i).float() * wo0_big, (1,1)))
ang_bins = torch.cat(ang_bins,1).view(-1,1,self.num_ang_bins)
ang_bins = self.angular_smooth(ang_bins)
values, indices = ang_bins.view(-1,self.num_ang_bins).max(1)
angle = -((2. * float(np.pi) * indices.float() / float(self.num_ang_bins)) - float(math.pi))
if return_rot_matrix:
return self.get_rotation_matrix(angle)
return angle
示例8: forward
# 需要导入模块: import torch [as 别名]
# 或者: from torch import atan2 [as 别名]
def forward(self, input_data):
num_batches, _, num_samples = input_data.size()
self.num_samples = num_samples
forward_transform = F.conv1d(input_data,
self.forward_basis,
stride=self.hop_length,
padding=self.filter_length)
cutoff = int((self.filter_length / 2) + 1)
real_part = forward_transform[:, :cutoff, :]
imag_part = forward_transform[:, cutoff:, :]
magnitude = torch.sqrt(real_part**2 + imag_part**2)
phase = torch.autograd.Variable(torch.atan2(imag_part.data, real_part.data))
return magnitude, phase
示例9: __call__
# 需要导入模块: import torch [as 别名]
# 或者: from torch import atan2 [as 别名]
def __call__(self, wav):
with torch.no_grad():
# STFT
data = torch.stft(wav, n_fft=self.nfft, hop_length=self.window_shift,
win_length=self.window_size, window=self.window)
data /= self.window.pow(2).sum().sqrt_()
#mag = data.pow(2).sum(-1).log1p_()
#ang = torch.atan2(data[:, :, 1], data[:, :, 0])
## {mag, phase} x n_freq_bin x n_frame
#data = torch.cat([mag.unsqueeze_(0), ang.unsqueeze_(0)], dim=0)
## FxTx2 -> 2xFxT
data = data.transpose(1, 2).transpose(0, 1)
return data
# transformer: frame splitter
示例10: to_rpy
# 需要导入模块: import torch [as 别名]
# 或者: from torch import atan2 [as 别名]
def to_rpy(Rot):
"""Convert a rotation matrix to RPY Euler angles."""
pitch = torch.atan2(-Rot[2, 0], torch.sqrt(Rot[0, 0]**2 + Rot[1, 0]**2))
if isclose(pitch, np.pi / 2.):
yaw = pitch.new_zeros(1)
roll = torch.atan2(Rot[0, 1], Rot[1, 1])
elif isclose(pitch, -np.pi / 2.):
yaw = pitch.new_zeros(1)
roll = -torch.atan2(Rot[0, 1], Rot[1, 1])
else:
sec_pitch = 1. / pitch.cos()
yaw = torch.atan2(Rot[1, 0] * sec_pitch, Rot[0, 0] * sec_pitch)
roll = torch.atan2(Rot[2, 1] * sec_pitch, Rot[2, 2] * sec_pitch)
return roll, pitch, yaw
示例11: compute_euler_angles_from_rotation_matrices
# 需要导入模块: import torch [as 别名]
# 或者: from torch import atan2 [as 别名]
def compute_euler_angles_from_rotation_matrices(rotation_matrices):
batch=rotation_matrices.shape[0]
R=rotation_matrices
sy = torch.sqrt(R[:,0,0]*R[:,0,0]+R[:,1,0]*R[:,1,0])
singular= sy<1e-6
singular=singular.float()
x=torch.atan2(R[:,2,1], R[:,2,2])
y=torch.atan2(-R[:,2,0], sy)
z=torch.atan2(R[:,1,0],R[:,0,0])
xs=torch.atan2(-R[:,1,2], R[:,1,1])
ys=torch.atan2(-R[:,2,0], sy)
zs=R[:,1,0]*0
out_euler=torch.autograd.Variable(torch.zeros(batch,3).cuda())
out_euler[:,0]=x*(1-singular)+xs*singular
out_euler[:,1]=y*(1-singular)+ys*singular
out_euler[:,2]=z*(1-singular)+zs*singular
return out_euler
#input batch*4
#output batch*4
示例12: angle
# 需要导入模块: import torch [as 别名]
# 或者: from torch import atan2 [as 别名]
def angle(
complex_tensor: Tensor
) -> Tensor:
r"""Compute the angle of complex tensor input.
Args:
complex_tensor (Tensor): Tensor shape of `(..., complex=2)`
Return:
Tensor: Angle of a complex tensor. Shape of `(..., )`
"""
return torch.atan2(complex_tensor[..., 1], complex_tensor[..., 0])
示例13: __call__
# 需要导入模块: import torch [as 别名]
# 或者: from torch import atan2 [as 别名]
def __call__(self, data):
(row, col), pos, pseudo = data.edge_index, data.pos, data.edge_attr
assert pos.dim() == 2 and pos.size(1) == 3
cart = pos[col] - pos[row]
rho = torch.norm(cart, p=2, dim=-1).view(-1, 1)
theta = torch.atan2(cart[..., 1], cart[..., 0]).view(-1, 1)
theta = theta + (theta < 0).type_as(theta) * (2 * PI)
phi = torch.acos(cart[..., 2] / rho.view(-1)).view(-1, 1)
if self.norm:
rho = rho / (rho.max() if self.max is None else self.max)
theta = theta / (2 * PI)
phi = phi / PI
spher = torch.cat([rho, theta, phi], dim=-1)
if pseudo is not None and self.cat:
pseudo = pseudo.view(-1, 1) if pseudo.dim() == 1 else pseudo
data.edge_attr = torch.cat([pseudo, spher.type_as(pos)], dim=-1)
else:
data.edge_attr = spher
return data
示例14: forward
# 需要导入模块: import torch [as 别名]
# 或者: from torch import atan2 [as 别名]
def forward(self, z, pos, batch=None):
""""""
edge_index = radius_graph(pos, r=self.cutoff, batch=batch)
i, j, idx_i, idx_j, idx_k, idx_kj, idx_ji = self.triplets(
edge_index, num_nodes=z.size(0))
# Calculate distances.
dist = (pos[i] - pos[j]).pow(2).sum(dim=-1).sqrt()
# Calculate angles.
pos_i = pos[idx_i]
pos_ji, pos_ki = pos[idx_j] - pos_i, pos[idx_k] - pos_i
a = (pos_ji * pos_ki).sum(dim=-1)
b = torch.cross(pos_ji, pos_ki).norm(dim=-1)
angle = torch.atan2(b, a)
rbf = self.rbf(dist)
sbf = self.sbf(dist, angle, idx_kj)
# Embedding block.
x = self.emb(z, rbf, i, j)
P = self.output_blocks[0](x, rbf, i, num_nodes=pos.size(0))
# Interaction blocks.
for interaction_block, output_block in zip(self.interaction_blocks,
self.output_blocks[1:]):
x = interaction_block(x, rbf, sbf, idx_kj, idx_ji)
P += output_block(x, rbf, i)
return P.sum(dim=0) if batch is None else scatter(P, batch, dim=0)
示例15: get_angle
# 需要导入模块: import torch [as 别名]
# 或者: from torch import atan2 [as 别名]
def get_angle(v1: Tensor, v2: Tensor) -> Tensor:
return torch.atan2(
torch.cross(v1, v2, dim=1).norm(p=2, dim=1), (v1 * v2).sum(dim=1))