Spaces:
Running on Zero
Running on Zero
Claude Sonnet 4.5 Claude Fable 5 commited on
fix: stub files contained HF redirect pages instead of Python
Browse filescurl without -L saved the 307 redirect body. Re-downloaded with
redirect-following and validated all 19 files with ast.parse.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
- flash_attn_stub/flash_attn/__init__.py +191 -1
- kaolin_stub/kaolin/__init__.py +2 -1
- kaolin_stub/kaolin/ops/__init__.py +2 -1
- kaolin_stub/kaolin/ops/mesh.py +1 -1
- kaolin_stub/kaolin/render/__init__.py +2 -1
- kaolin_stub/kaolin/render/camera.py +22 -1
- kaolin_stub/kaolin/utils/__init__.py +2 -1
- kaolin_stub/kaolin/utils/testing.py +4 -1
- kaolin_stub/kaolin/visualize/__init__.py +4 -1
- pytorch3d_stub/pytorch3d/__init__.py +21 -1
- pytorch3d_stub/pytorch3d/renderer/__init__.py +126 -1
- pytorch3d_stub/pytorch3d/renderer/camera_utils.py +11 -1
- pytorch3d_stub/pytorch3d/renderer/cameras.py +3 -1
- pytorch3d_stub/pytorch3d/renderer/mesh/__init__.py +1 -1
- pytorch3d_stub/pytorch3d/renderer/mesh/textures.py +1 -1
- pytorch3d_stub/pytorch3d/structures/__init__.py +48 -1
- pytorch3d_stub/pytorch3d/transforms/__init__.py +244 -1
- pytorch3d_stub/pytorch3d/vis/__init__.py +17 -1
- pytorch3d_stub/pytorch3d/vis/plotly_vis.py +42 -1
flash_attn_stub/flash_attn/__init__.py
CHANGED
|
@@ -1 +1,191 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""flash_attn stub – implements flash attention API using torch SDPA.
|
| 2 |
+
|
| 3 |
+
This replaces the real flash_attn package on systems where it cannot be compiled
|
| 4 |
+
(e.g. ZeroGPU with PyTorch 2.10+cu128 and no matching wheel).
|
| 5 |
+
All functions accept the same signatures as flash_attn 2.x and delegate to
|
| 6 |
+
torch.nn.functional.scaled_dot_product_attention.
|
| 7 |
+
"""
|
| 8 |
+
import torch
|
| 9 |
+
import torch.nn.functional as F
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def _sdpa(q, k, v, causal=False, softmax_scale=None):
|
| 13 |
+
"""Apply SDPA. q/k/v are (B, H, L, D)."""
|
| 14 |
+
return F.scaled_dot_product_attention(
|
| 15 |
+
q, k, v,
|
| 16 |
+
is_causal=causal,
|
| 17 |
+
scale=softmax_scale,
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
# ---------- non-varlen ----------
|
| 22 |
+
|
| 23 |
+
def flash_attn_func(q, k, v, dropout_p=0.0, softmax_scale=None, causal=False,
|
| 24 |
+
window_size=(-1, -1), softcap=0.0, alibi_slopes=None,
|
| 25 |
+
deterministic=False, return_attn_probs=False):
|
| 26 |
+
"""q/k/v: (B, L, H, D) -> out: (B, L, H, D)"""
|
| 27 |
+
# Permute to (B, H, L, D) for SDPA
|
| 28 |
+
q2 = q.transpose(1, 2)
|
| 29 |
+
k2 = k.transpose(1, 2)
|
| 30 |
+
v2 = v.transpose(1, 2)
|
| 31 |
+
out = _sdpa(q2, k2, v2, causal=causal, softmax_scale=softmax_scale)
|
| 32 |
+
out = out.transpose(1, 2) # back to (B, L, H, D)
|
| 33 |
+
return out
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def flash_attn_qkvpacked_func(qkv, dropout_p=0.0, softmax_scale=None, causal=False,
|
| 37 |
+
window_size=(-1, -1), softcap=0.0, alibi_slopes=None,
|
| 38 |
+
deterministic=False, return_attn_probs=False):
|
| 39 |
+
"""qkv: (B, L, 3, H, D) -> out: (B, L, H, D)"""
|
| 40 |
+
q, k, v = qkv.unbind(dim=2)
|
| 41 |
+
return flash_attn_func(q, k, v, dropout_p=dropout_p, softmax_scale=softmax_scale,
|
| 42 |
+
causal=causal)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def flash_attn_kvpacked_func(q, kv, dropout_p=0.0, softmax_scale=None, causal=False,
|
| 46 |
+
window_size=(-1, -1), softcap=0.0, alibi_slopes=None,
|
| 47 |
+
deterministic=False, return_attn_probs=False):
|
| 48 |
+
"""q: (B, Lq, H, D), kv: (B, Lk, 2, H, D) -> out: (B, Lq, H, D)"""
|
| 49 |
+
k, v = kv.unbind(dim=2)
|
| 50 |
+
return flash_attn_func(q, k, v, dropout_p=dropout_p, softmax_scale=softmax_scale,
|
| 51 |
+
causal=causal)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
# ---------- varlen ----------
|
| 55 |
+
|
| 56 |
+
def _varlen_sdpa(q, k, v, cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k,
|
| 57 |
+
causal=False, softmax_scale=None):
|
| 58 |
+
"""
|
| 59 |
+
q: (total_q, H, D), k: (total_k, H, D), v: (total_k, H, D)
|
| 60 |
+
cu_seqlens_q/k: (batch+1,) int32
|
| 61 |
+
Returns: (total_q, H, D)
|
| 62 |
+
"""
|
| 63 |
+
batch = cu_seqlens_q.shape[0] - 1
|
| 64 |
+
H = q.shape[1]
|
| 65 |
+
D = q.shape[2]
|
| 66 |
+
|
| 67 |
+
# Fast path: all seqlens are equal (common case)
|
| 68 |
+
cu_q = cu_seqlens_q.tolist()
|
| 69 |
+
cu_k = cu_seqlens_k.tolist()
|
| 70 |
+
|
| 71 |
+
all_equal = True
|
| 72 |
+
sq0 = cu_q[1] - cu_q[0]
|
| 73 |
+
sk0 = cu_k[1] - cu_k[0]
|
| 74 |
+
for i in range(1, batch):
|
| 75 |
+
if cu_q[i + 1] - cu_q[i] != sq0 or cu_k[i + 1] - cu_k[i] != sk0:
|
| 76 |
+
all_equal = False
|
| 77 |
+
break
|
| 78 |
+
|
| 79 |
+
if all_equal and sq0 == max_seqlen_q and sk0 == max_seqlen_k:
|
| 80 |
+
# Reshape directly – no padding needed
|
| 81 |
+
q2 = q.reshape(batch, sq0, H, D).transpose(1, 2) # (B, H, Lq, D)
|
| 82 |
+
k2 = k.reshape(batch, sk0, H, D).transpose(1, 2)
|
| 83 |
+
v2 = v.reshape(batch, sk0, H, D).transpose(1, 2)
|
| 84 |
+
out = _sdpa(q2, k2, v2, causal=causal, softmax_scale=softmax_scale)
|
| 85 |
+
return out.transpose(1, 2).reshape(-1, H, D)
|
| 86 |
+
|
| 87 |
+
# Slow path: unequal lengths – pad, compute, then gather
|
| 88 |
+
q_padded = q.new_zeros(batch, max_seqlen_q, H, D)
|
| 89 |
+
k_padded = k.new_zeros(batch, max_seqlen_k, H, D)
|
| 90 |
+
v_padded = v.new_zeros(batch, max_seqlen_k, H, D)
|
| 91 |
+
|
| 92 |
+
for i in range(batch):
|
| 93 |
+
sq = cu_q[i + 1] - cu_q[i]
|
| 94 |
+
sk = cu_k[i + 1] - cu_k[i]
|
| 95 |
+
q_padded[i, :sq] = q[cu_q[i]:cu_q[i + 1]]
|
| 96 |
+
k_padded[i, :sk] = k[cu_k[i]:cu_k[i + 1]]
|
| 97 |
+
v_padded[i, :sk] = v[cu_k[i]:cu_k[i + 1]]
|
| 98 |
+
|
| 99 |
+
# Create attention mask for padding
|
| 100 |
+
q_mask = torch.arange(max_seqlen_q, device=q.device).unsqueeze(0) # (1, Lq)
|
| 101 |
+
k_mask = torch.arange(max_seqlen_k, device=k.device).unsqueeze(0) # (1, Lk)
|
| 102 |
+
q_lens = torch.tensor([cu_q[i + 1] - cu_q[i] for i in range(batch)],
|
| 103 |
+
device=q.device).unsqueeze(1) # (B, 1)
|
| 104 |
+
k_lens = torch.tensor([cu_k[i + 1] - cu_k[i] for i in range(batch)],
|
| 105 |
+
device=k.device).unsqueeze(1) # (B, 1)
|
| 106 |
+
# (B, 1, 1, Lk) – True where valid
|
| 107 |
+
attn_mask = (k_mask < k_lens).unsqueeze(1).unsqueeze(2)
|
| 108 |
+
# Also mask out query positions that are padding (their output is ignored anyway)
|
| 109 |
+
# Use float mask: -inf for invalid positions
|
| 110 |
+
attn_bias = torch.zeros(batch, 1, max_seqlen_q, max_seqlen_k,
|
| 111 |
+
device=q.device, dtype=q.dtype)
|
| 112 |
+
attn_bias.masked_fill_(~attn_mask, float('-inf'))
|
| 113 |
+
|
| 114 |
+
if causal:
|
| 115 |
+
causal_mask = torch.triu(
|
| 116 |
+
torch.ones(max_seqlen_q, max_seqlen_k, device=q.device, dtype=torch.bool),
|
| 117 |
+
diagonal=1
|
| 118 |
+
)
|
| 119 |
+
attn_bias.masked_fill_(causal_mask.unsqueeze(0).unsqueeze(0), float('-inf'))
|
| 120 |
+
|
| 121 |
+
q2 = q_padded.transpose(1, 2) # (B, H, Lq, D)
|
| 122 |
+
k2 = k_padded.transpose(1, 2)
|
| 123 |
+
v2 = v_padded.transpose(1, 2)
|
| 124 |
+
|
| 125 |
+
out = F.scaled_dot_product_attention(q2, k2, v2, attn_mask=attn_bias,
|
| 126 |
+
scale=softmax_scale)
|
| 127 |
+
out = out.transpose(1, 2) # (B, Lq, H, D)
|
| 128 |
+
|
| 129 |
+
# Gather results back to packed format
|
| 130 |
+
parts = []
|
| 131 |
+
for i in range(batch):
|
| 132 |
+
sq = cu_q[i + 1] - cu_q[i]
|
| 133 |
+
parts.append(out[i, :sq]) # (sq, H, D)
|
| 134 |
+
return torch.cat(parts, dim=0)
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
def flash_attn_varlen_func(q, k, v, cu_seqlens_q, cu_seqlens_k,
|
| 138 |
+
max_seqlen_q, max_seqlen_k,
|
| 139 |
+
dropout_p=0.0, softmax_scale=None, causal=False,
|
| 140 |
+
window_size=(-1, -1), softcap=0.0, alibi_slopes=None,
|
| 141 |
+
deterministic=False, return_attn_probs=False,
|
| 142 |
+
block_table=None):
|
| 143 |
+
"""q/k/v: (total, H, D) -> out: (total_q, H, D)"""
|
| 144 |
+
return _varlen_sdpa(q, k, v, cu_seqlens_q, cu_seqlens_k,
|
| 145 |
+
max_seqlen_q, max_seqlen_k,
|
| 146 |
+
causal=causal, softmax_scale=softmax_scale)
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
def flash_attn_varlen_qkvpacked_func(qkv, cu_seqlens, max_seqlen,
|
| 150 |
+
dropout_p=0.0, softmax_scale=None, causal=False,
|
| 151 |
+
window_size=(-1, -1), softcap=0.0,
|
| 152 |
+
alibi_slopes=None, deterministic=False,
|
| 153 |
+
return_attn_probs=False):
|
| 154 |
+
"""qkv: (total, 3, H, D) -> out: (total, H, D)"""
|
| 155 |
+
q, k, v = qkv.unbind(dim=1)
|
| 156 |
+
return _varlen_sdpa(q, k, v, cu_seqlens, cu_seqlens,
|
| 157 |
+
max_seqlen, max_seqlen,
|
| 158 |
+
causal=causal, softmax_scale=softmax_scale)
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
def flash_attn_varlen_kvpacked_func(q, kv, cu_seqlens_q, cu_seqlens_k,
|
| 162 |
+
max_seqlen_q, max_seqlen_k,
|
| 163 |
+
dropout_p=0.0, softmax_scale=None, causal=False,
|
| 164 |
+
window_size=(-1, -1), softcap=0.0,
|
| 165 |
+
alibi_slopes=None, deterministic=False,
|
| 166 |
+
return_attn_probs=False):
|
| 167 |
+
"""q: (total_q, H, D), kv: (total_k, 2, H, D) -> out: (total_q, H, D)"""
|
| 168 |
+
k, v = kv.unbind(dim=1)
|
| 169 |
+
return _varlen_sdpa(q, k, v, cu_seqlens_q, cu_seqlens_k,
|
| 170 |
+
max_seqlen_q, max_seqlen_k,
|
| 171 |
+
causal=causal, softmax_scale=softmax_scale)
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
# ---------- with_kvcache (used by some SAM2 code paths) ----------
|
| 175 |
+
|
| 176 |
+
def flash_attn_with_kvcache(q, k_cache, v_cache, k=None, v=None,
|
| 177 |
+
rotary_cos=None, rotary_sin=None,
|
| 178 |
+
cache_seqlens=None, cache_batch_idx=None,
|
| 179 |
+
block_table=None, softmax_scale=None, causal=False,
|
| 180 |
+
window_size=(-1, -1), softcap=0.0,
|
| 181 |
+
rotary_interleaved=True, alibi_slopes=None,
|
| 182 |
+
num_splits=0, return_softmax_lse=False):
|
| 183 |
+
"""Simplified kv-cache attention fallback."""
|
| 184 |
+
# Combine current k/v with cache if provided
|
| 185 |
+
if k is not None:
|
| 186 |
+
k_full = torch.cat([k_cache, k], dim=1)
|
| 187 |
+
v_full = torch.cat([v_cache, v], dim=1)
|
| 188 |
+
else:
|
| 189 |
+
k_full = k_cache
|
| 190 |
+
v_full = v_cache
|
| 191 |
+
return flash_attn_func(q, k_full, v_full, softmax_scale=softmax_scale, causal=causal)
|
kaolin_stub/kaolin/__init__.py
CHANGED
|
@@ -1 +1,2 @@
|
|
| 1 |
-
|
|
|
|
|
|
| 1 |
+
"""Kaolin stub - minimum API for SAM3D."""
|
| 2 |
+
from . import ops, render, visualize
|
kaolin_stub/kaolin/ops/__init__.py
CHANGED
|
@@ -1 +1,2 @@
|
|
| 1 |
-
|
|
|
|
|
|
| 1 |
+
"""Kaolin ops stub."""
|
| 2 |
+
from . import mesh
|
kaolin_stub/kaolin/ops/mesh.py
CHANGED
|
@@ -1 +1 @@
|
|
| 1 |
-
|
|
|
|
| 1 |
+
"""Stub."""
|
kaolin_stub/kaolin/render/__init__.py
CHANGED
|
@@ -1 +1,2 @@
|
|
| 1 |
-
|
|
|
|
|
|
| 1 |
+
"""Kaolin render stub."""
|
| 2 |
+
from . import camera
|
kaolin_stub/kaolin/render/camera.py
CHANGED
|
@@ -1 +1,22 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Kaolin camera stub."""
|
| 2 |
+
import torch
|
| 3 |
+
import math
|
| 4 |
+
|
| 5 |
+
class PinholeIntrinsics:
|
| 6 |
+
def __init__(self, fov=None, focal_length=None, width=None, height=None, **kwargs):
|
| 7 |
+
self.fov = fov
|
| 8 |
+
self.focal_length = focal_length
|
| 9 |
+
self.width = width
|
| 10 |
+
self.height = height
|
| 11 |
+
|
| 12 |
+
class CameraExtrinsics:
|
| 13 |
+
def __init__(self, view_matrix=None, **kwargs):
|
| 14 |
+
self.view_matrix = view_matrix
|
| 15 |
+
|
| 16 |
+
class Camera:
|
| 17 |
+
def __init__(self, extrinsics=None, intrinsics=None, **kwargs):
|
| 18 |
+
self.extrinsics = extrinsics
|
| 19 |
+
self.intrinsics = intrinsics
|
| 20 |
+
@classmethod
|
| 21 |
+
def from_args(cls, **kwargs):
|
| 22 |
+
return cls(**kwargs)
|
kaolin_stub/kaolin/utils/__init__.py
CHANGED
|
@@ -1 +1,2 @@
|
|
| 1 |
-
|
|
|
|
|
|
| 1 |
+
"""kaolin.utils stub."""
|
| 2 |
+
from . import testing
|
kaolin_stub/kaolin/utils/testing.py
CHANGED
|
@@ -1 +1,4 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""kaolin.utils.testing stub."""
|
| 2 |
+
def check_tensor(tensor, shape=None, dtype=None, device=None, throw=True):
|
| 3 |
+
"""No-op tensor check."""
|
| 4 |
+
return True
|
kaolin_stub/kaolin/visualize/__init__.py
CHANGED
|
@@ -1 +1,4 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Kaolin visualize stub."""
|
| 2 |
+
class IpyTurntableVisualizer:
|
| 3 |
+
def __init__(self, *a, **kw): pass
|
| 4 |
+
def show(self, *a, **kw): pass
|
pytorch3d_stub/pytorch3d/__init__.py
CHANGED
|
@@ -1 +1,21 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""pytorch3d stub package with catch-all for missing submodules."""
|
| 2 |
+
import importlib
|
| 3 |
+
import warnings
|
| 4 |
+
|
| 5 |
+
__version__ = "0.0.0-stub"
|
| 6 |
+
|
| 7 |
+
def __getattr__(name):
|
| 8 |
+
if name.startswith("__") and name.endswith("__"):
|
| 9 |
+
raise AttributeError(name)
|
| 10 |
+
# Try importing submodule first
|
| 11 |
+
try:
|
| 12 |
+
return importlib.import_module(f".{name}", __name__)
|
| 13 |
+
except ImportError:
|
| 14 |
+
pass
|
| 15 |
+
warnings.warn(f"pytorch3d stub: {name} not implemented", stacklevel=2)
|
| 16 |
+
class _Dummy:
|
| 17 |
+
def __init__(self, *a, **kw): pass
|
| 18 |
+
def __call__(self, *a, **kw): return None
|
| 19 |
+
def to(self, *a, **kw): return self
|
| 20 |
+
_Dummy.__name__ = _Dummy.__qualname__ = name
|
| 21 |
+
return _Dummy
|
pytorch3d_stub/pytorch3d/renderer/__init__.py
CHANGED
|
@@ -1 +1,126 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""pytorch3d.renderer stub with catch-all for missing attributes."""
|
| 2 |
+
import torch
|
| 3 |
+
import math
|
| 4 |
+
import warnings
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def __getattr__(name):
|
| 8 |
+
"""Catch-all: return a dummy class/function for any missing attribute."""
|
| 9 |
+
if name.startswith("__") and name.endswith("__"):
|
| 10 |
+
raise AttributeError(name)
|
| 11 |
+
warnings.warn(f"pytorch3d.renderer stub: {name} not implemented, returning dummy", stacklevel=2)
|
| 12 |
+
class _Dummy:
|
| 13 |
+
def __init__(self, *a, **kw): pass
|
| 14 |
+
def __call__(self, *a, **kw): return None
|
| 15 |
+
def to(self, *a, **kw): return self
|
| 16 |
+
def forward(self, *a, **kw): return None
|
| 17 |
+
_Dummy.__name__ = _Dummy.__qualname__ = name
|
| 18 |
+
return _Dummy
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def look_at_view_transform(dist=1.0, elev=0.0, azim=0.0, degrees=True, eye=None,
|
| 22 |
+
at=((0, 0, 0),), up=((0, 1, 0),), device="cpu"):
|
| 23 |
+
if eye is not None:
|
| 24 |
+
if not isinstance(eye, torch.Tensor):
|
| 25 |
+
eye = torch.tensor(eye, dtype=torch.float32, device=device)
|
| 26 |
+
if eye.dim() == 1:
|
| 27 |
+
eye = eye.unsqueeze(0)
|
| 28 |
+
else:
|
| 29 |
+
elev_r = math.radians(float(elev)) if degrees else float(elev)
|
| 30 |
+
azim_r = math.radians(float(azim)) if degrees else float(azim)
|
| 31 |
+
x = dist * math.cos(elev_r) * math.sin(azim_r)
|
| 32 |
+
y = dist * math.sin(elev_r)
|
| 33 |
+
z = dist * math.cos(elev_r) * math.cos(azim_r)
|
| 34 |
+
eye = torch.tensor([[x, y, z]], dtype=torch.float32, device=device)
|
| 35 |
+
if not isinstance(at, torch.Tensor):
|
| 36 |
+
at = torch.tensor(at, dtype=torch.float32, device=device)
|
| 37 |
+
if at.dim() == 1: at = at.unsqueeze(0)
|
| 38 |
+
if not isinstance(up, torch.Tensor):
|
| 39 |
+
up = torch.tensor(up, dtype=torch.float32, device=device)
|
| 40 |
+
if up.dim() == 1: up = up.unsqueeze(0)
|
| 41 |
+
z_axis = eye - at
|
| 42 |
+
z_axis = z_axis / z_axis.norm(dim=-1, keepdim=True).clamp(min=1e-8)
|
| 43 |
+
x_axis = torch.cross(up.expand_as(z_axis), z_axis, dim=-1)
|
| 44 |
+
x_axis = x_axis / x_axis.norm(dim=-1, keepdim=True).clamp(min=1e-8)
|
| 45 |
+
y_axis = torch.cross(z_axis, x_axis, dim=-1)
|
| 46 |
+
R = torch.stack([x_axis, y_axis, z_axis], dim=-1)
|
| 47 |
+
T = -torch.bmm(R.transpose(1, 2), eye.unsqueeze(-1)).squeeze(-1)
|
| 48 |
+
return R, T
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def ray_bundle_to_ray_points(ray_bundle):
|
| 52 |
+
return None
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
class CamerasBase:
|
| 56 |
+
pass
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
class PerspectiveCameras(CamerasBase):
|
| 60 |
+
def __init__(self, focal_length=None, principal_point=None, R=None, T=None,
|
| 61 |
+
image_size=None, device="cpu", in_ndc=True, **kwargs):
|
| 62 |
+
self.device = device
|
| 63 |
+
self.focal_length = focal_length
|
| 64 |
+
self.principal_point = principal_point
|
| 65 |
+
self.R = R if R is not None else torch.eye(3, device=device).unsqueeze(0)
|
| 66 |
+
self.T = T if T is not None else torch.zeros(1, 3, device=device)
|
| 67 |
+
self.image_size = image_size
|
| 68 |
+
self.in_ndc = in_ndc
|
| 69 |
+
def to(self, device):
|
| 70 |
+
self.device = device; return self
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
class RasterizationSettings:
|
| 74 |
+
def __init__(self, image_size=256, blur_radius=0.0, faces_per_pixel=1, **kwargs):
|
| 75 |
+
self.image_size = image_size; self.blur_radius = blur_radius; self.faces_per_pixel = faces_per_pixel
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
class BlendParams:
|
| 79 |
+
def __init__(self, sigma=1e-4, gamma=1e-4, background_color=(0.0, 0.0, 0.0)):
|
| 80 |
+
self.sigma = sigma; self.gamma = gamma; self.background_color = background_color
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
class SoftSilhouetteShader:
|
| 84 |
+
def __init__(self, blend_params=None):
|
| 85 |
+
self.blend_params = blend_params or BlendParams()
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
class MeshRasterizer(torch.nn.Module):
|
| 89 |
+
def __init__(self, cameras=None, raster_settings=None):
|
| 90 |
+
super().__init__()
|
| 91 |
+
self.cameras = cameras; self.raster_settings = raster_settings
|
| 92 |
+
def forward(self, meshes, **kw):
|
| 93 |
+
raise NotImplementedError("MeshRasterizer stub")
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
class MeshRenderer(torch.nn.Module):
|
| 97 |
+
def __init__(self, rasterizer=None, shader=None):
|
| 98 |
+
super().__init__()
|
| 99 |
+
self.rasterizer = rasterizer; self.shader = shader
|
| 100 |
+
def forward(self, meshes, **kw):
|
| 101 |
+
raise NotImplementedError("MeshRenderer stub")
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
class TexturesVertex:
|
| 105 |
+
def __init__(self, verts_features=None):
|
| 106 |
+
self.verts_features_list = verts_features if isinstance(verts_features, list) else [verts_features]
|
| 107 |
+
def to(self, device): return self
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
class TexturesAtlas:
|
| 111 |
+
def __init__(self, atlas=None, **kw): self.atlas = atlas
|
| 112 |
+
def to(self, device): return self
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
class TexturesUV:
|
| 116 |
+
def __init__(self, **kw): pass
|
| 117 |
+
def to(self, device): return self
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
class HeterogeneousRayBundle:
|
| 121 |
+
def __init__(self, *a, **kw): pass
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
class RayBundle:
|
| 125 |
+
def __init__(self, origins=None, directions=None, lengths=None, xys=None, **kw):
|
| 126 |
+
self.origins = origins; self.directions = directions; self.lengths = lengths; self.xys = xys
|
pytorch3d_stub/pytorch3d/renderer/camera_utils.py
CHANGED
|
@@ -1 +1,11 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""pytorch3d.renderer.camera_utils stub."""
|
| 2 |
+
import torch
|
| 3 |
+
|
| 4 |
+
def camera_to_eye_at_up(camera):
|
| 5 |
+
"""Extract eye, at, up from camera. Minimal stub."""
|
| 6 |
+
R = camera.R if hasattr(camera, "R") else torch.eye(3).unsqueeze(0)
|
| 7 |
+
T = camera.T if hasattr(camera, "T") else torch.zeros(1, 3)
|
| 8 |
+
eye = -torch.bmm(R, T.unsqueeze(-1)).squeeze(-1)
|
| 9 |
+
at = torch.zeros_like(eye)
|
| 10 |
+
up = torch.tensor([[0.0, 1.0, 0.0]])
|
| 11 |
+
return eye, at, up
|
pytorch3d_stub/pytorch3d/renderer/cameras.py
CHANGED
|
@@ -1 +1,3 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
"""pytorch3d.renderer.cameras stub."""
|
| 2 |
+
from pytorch3d.renderer import PerspectiveCameras, CamerasBase
|
| 3 |
+
__all__ = ["PerspectiveCameras", "CamerasBase"]
|
pytorch3d_stub/pytorch3d/renderer/mesh/__init__.py
CHANGED
|
@@ -1 +1 @@
|
|
| 1 |
-
|
|
|
|
| 1 |
+
from pytorch3d.renderer import TexturesVertex
|
pytorch3d_stub/pytorch3d/renderer/mesh/textures.py
CHANGED
|
@@ -1 +1 @@
|
|
| 1 |
-
|
|
|
|
| 1 |
+
from pytorch3d.renderer import TexturesVertex
|
pytorch3d_stub/pytorch3d/structures/__init__.py
CHANGED
|
@@ -1 +1,48 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""pytorch3d.structures stub with catch-all."""
|
| 2 |
+
import torch
|
| 3 |
+
import warnings
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def __getattr__(name):
|
| 7 |
+
if name.startswith("__") and name.endswith("__"):
|
| 8 |
+
raise AttributeError(name)
|
| 9 |
+
warnings.warn(f"pytorch3d.structures stub: {name} not implemented", stacklevel=2)
|
| 10 |
+
class _Dummy:
|
| 11 |
+
def __init__(self, *a, **kw): pass
|
| 12 |
+
def to(self, *a, **kw): return self
|
| 13 |
+
_Dummy.__name__ = _Dummy.__qualname__ = name
|
| 14 |
+
return _Dummy
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class Meshes:
|
| 18 |
+
def __init__(self, verts=None, faces=None, textures=None):
|
| 19 |
+
if isinstance(verts, list):
|
| 20 |
+
self._verts_list = [v if isinstance(v, torch.Tensor) else torch.tensor(v, dtype=torch.float32) for v in verts]
|
| 21 |
+
elif isinstance(verts, torch.Tensor):
|
| 22 |
+
self._verts_list = [verts]
|
| 23 |
+
else:
|
| 24 |
+
self._verts_list = []
|
| 25 |
+
if isinstance(faces, list):
|
| 26 |
+
self._faces_list = [f if isinstance(f, torch.Tensor) else torch.tensor(f, dtype=torch.long) for f in faces]
|
| 27 |
+
elif isinstance(faces, torch.Tensor):
|
| 28 |
+
self._faces_list = [faces]
|
| 29 |
+
else:
|
| 30 |
+
self._faces_list = []
|
| 31 |
+
self.textures = textures
|
| 32 |
+
def verts_list(self): return self._verts_list
|
| 33 |
+
def faces_list(self): return self._faces_list
|
| 34 |
+
def verts_packed(self):
|
| 35 |
+
return torch.cat(self._verts_list, dim=0) if self._verts_list else torch.zeros(0, 3)
|
| 36 |
+
def faces_packed(self):
|
| 37 |
+
return torch.cat(self._faces_list, dim=0) if self._faces_list else torch.zeros(0, 3, dtype=torch.long)
|
| 38 |
+
def num_verts_per_mesh(self):
|
| 39 |
+
return torch.tensor([v.shape[0] for v in self._verts_list])
|
| 40 |
+
def __len__(self): return len(self._verts_list)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
class Pointclouds:
|
| 44 |
+
def __init__(self, points=None, features=None, normals=None):
|
| 45 |
+
self.points_list = points if isinstance(points, list) else ([points] if points is not None else [])
|
| 46 |
+
self.features_list = features if isinstance(features, list) else ([features] if features is not None else [])
|
| 47 |
+
self.normals_list = normals if isinstance(normals, list) else ([normals] if normals is not None else [])
|
| 48 |
+
def to(self, device): return self
|
pytorch3d_stub/pytorch3d/transforms/__init__.py
CHANGED
|
@@ -1 +1,244 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""pytorch3d.transforms stub with catch-all."""
|
| 2 |
+
import torch
|
| 3 |
+
import warnings
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def __getattr__(name):
|
| 7 |
+
if name.startswith("__") and name.endswith("__"):
|
| 8 |
+
raise AttributeError(name)
|
| 9 |
+
warnings.warn(f"pytorch3d.transforms stub: {name} not implemented", stacklevel=2)
|
| 10 |
+
def _dummy(*a, **kw): return None
|
| 11 |
+
_dummy.__name__ = name
|
| 12 |
+
return _dummy
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def quaternion_to_matrix(quaternions):
|
| 16 |
+
r, i, j, k = quaternions[..., 0], quaternions[..., 1], quaternions[..., 2], quaternions[..., 3]
|
| 17 |
+
two_s = 2.0 / (quaternions * quaternions).sum(-1)
|
| 18 |
+
o = torch.stack([
|
| 19 |
+
1 - two_s * (j*j + k*k), two_s * (i*j - k*r), two_s * (i*k + j*r),
|
| 20 |
+
two_s * (i*j + k*r), 1 - two_s * (i*i + k*k), two_s * (j*k - i*r),
|
| 21 |
+
two_s * (i*k - j*r), two_s * (j*k + i*r), 1 - two_s * (i*i + j*j),
|
| 22 |
+
], -1)
|
| 23 |
+
return o.reshape(quaternions.shape[:-1] + (3, 3))
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def matrix_to_quaternion(matrix):
|
| 27 |
+
if matrix.shape[-2:] != (3, 3):
|
| 28 |
+
raise ValueError(f"Invalid rotation matrix shape {matrix.shape}")
|
| 29 |
+
batch_dim = matrix.shape[:-2]
|
| 30 |
+
m00, m01, m02 = matrix[..., 0, 0], matrix[..., 0, 1], matrix[..., 0, 2]
|
| 31 |
+
m10, m11, m12 = matrix[..., 1, 0], matrix[..., 1, 1], matrix[..., 1, 2]
|
| 32 |
+
m20, m21, m22 = matrix[..., 2, 0], matrix[..., 2, 1], matrix[..., 2, 2]
|
| 33 |
+
trace = m00 + m11 + m22
|
| 34 |
+
qw = torch.sqrt(torch.clamp(trace + 1, min=1e-8)) / 2
|
| 35 |
+
qx = (m21 - m12) / (4 * qw + 1e-8)
|
| 36 |
+
qy = (m02 - m20) / (4 * qw + 1e-8)
|
| 37 |
+
qz = (m10 - m01) / (4 * qw + 1e-8)
|
| 38 |
+
return torch.stack([qw, qx, qy, qz], dim=-1)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def quaternion_multiply(a, b):
|
| 42 |
+
aw, ax, ay, az = a[..., 0], a[..., 1], a[..., 2], a[..., 3]
|
| 43 |
+
bw, bx, by, bz = b[..., 0], b[..., 1], b[..., 2], b[..., 3]
|
| 44 |
+
ow = aw*bw - ax*bx - ay*by - az*bz
|
| 45 |
+
ox = aw*bx + ax*bw + ay*bz - az*by
|
| 46 |
+
oy = aw*by - ax*bz + ay*bw + az*bx
|
| 47 |
+
oz = aw*bz + ax*by - ay*bx + az*bw
|
| 48 |
+
return torch.stack([ow, ox, oy, oz], dim=-1)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def quaternion_invert(quaternion):
|
| 52 |
+
scaling = torch.tensor([1, -1, -1, -1], dtype=quaternion.dtype, device=quaternion.device)
|
| 53 |
+
return quaternion * scaling
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def axis_angle_to_quaternion(axis_angle):
|
| 57 |
+
"""Convert rotations given as axis/angle to quaternions.
|
| 58 |
+
Args:
|
| 59 |
+
axis_angle: Rotations given as a vector in axis angle form,
|
| 60 |
+
as a tensor of shape (..., 3), where the magnitude is
|
| 61 |
+
the angle turned anticlockwise in radians around the
|
| 62 |
+
vector's direction.
|
| 63 |
+
Returns:
|
| 64 |
+
quaternions with real parts first, as tensor of shape (..., 4).
|
| 65 |
+
"""
|
| 66 |
+
angles = torch.norm(axis_angle, p=2, dim=-1, keepdim=True)
|
| 67 |
+
half_angles = angles * 0.5
|
| 68 |
+
eps = 1e-6
|
| 69 |
+
small_angles = angles.abs() < eps
|
| 70 |
+
sin_half = torch.where(small_angles, 0.5 * torch.ones_like(angles), torch.sin(half_angles) / angles)
|
| 71 |
+
cos_half = torch.where(small_angles, torch.ones_like(angles), torch.cos(half_angles))
|
| 72 |
+
quaternions = torch.cat([cos_half, axis_angle * sin_half], dim=-1)
|
| 73 |
+
return quaternions
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def axis_angle_to_matrix(axis_angle):
|
| 77 |
+
"""Convert rotations given as axis/angle to rotation matrices."""
|
| 78 |
+
return quaternion_to_matrix(axis_angle_to_quaternion(axis_angle))
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
class Transform3d:
|
| 82 |
+
def __init__(self, dtype=torch.float32, device="cpu", matrix=None):
|
| 83 |
+
if matrix is not None:
|
| 84 |
+
self._matrix = matrix.to(device=device, dtype=dtype)
|
| 85 |
+
self.device = self._matrix.device
|
| 86 |
+
self.dtype = self._matrix.dtype
|
| 87 |
+
else:
|
| 88 |
+
self._matrix = torch.eye(4, dtype=dtype, device=device).unsqueeze(0)
|
| 89 |
+
self.device = self._matrix.device
|
| 90 |
+
self.dtype = dtype
|
| 91 |
+
|
| 92 |
+
def get_matrix(self):
|
| 93 |
+
return self._matrix
|
| 94 |
+
|
| 95 |
+
def compose(self, *others):
|
| 96 |
+
m = self._matrix
|
| 97 |
+
for o in others:
|
| 98 |
+
om = o.get_matrix().to(device=m.device, dtype=m.dtype)
|
| 99 |
+
if om.shape[0] != m.shape[0]:
|
| 100 |
+
if m.shape[0] == 1:
|
| 101 |
+
m = m.expand(om.shape[0], -1, -1)
|
| 102 |
+
elif om.shape[0] == 1:
|
| 103 |
+
om = om.expand(m.shape[0], -1, -1)
|
| 104 |
+
m = m @ om
|
| 105 |
+
return Transform3d(matrix=m, device=str(m.device), dtype=m.dtype)
|
| 106 |
+
|
| 107 |
+
def transform_points(self, points):
|
| 108 |
+
if points.dim() == 2:
|
| 109 |
+
points = points.unsqueeze(0)
|
| 110 |
+
mat = self._matrix
|
| 111 |
+
points = points.to(device=mat.device, dtype=mat.dtype)
|
| 112 |
+
ones = torch.ones(*points.shape[:-1], 1, dtype=mat.dtype, device=mat.device)
|
| 113 |
+
pts4 = torch.cat([points, ones], dim=-1)
|
| 114 |
+
m = mat.expand(pts4.shape[0], -1, -1)
|
| 115 |
+
out = torch.bmm(pts4, m.transpose(-2, -1))
|
| 116 |
+
return out[..., :3]
|
| 117 |
+
|
| 118 |
+
def translate(self, x, y=None, z=None):
|
| 119 |
+
dev = self._matrix.device
|
| 120 |
+
dt = self._matrix.dtype
|
| 121 |
+
if isinstance(x, torch.Tensor) and x.dim() >= 1 and x.shape[-1] == 3:
|
| 122 |
+
t = x.to(device=dev, dtype=dt)
|
| 123 |
+
else:
|
| 124 |
+
t = torch.tensor([[x, y, z]], dtype=dt, device=dev)
|
| 125 |
+
if t.dim() == 1:
|
| 126 |
+
t = t.unsqueeze(0)
|
| 127 |
+
T = torch.eye(4, dtype=dt, device=dev).unsqueeze(0).expand(t.shape[0], -1, -1).clone()
|
| 128 |
+
T[:, :3, 3] = t
|
| 129 |
+
new_m = self._matrix @ T
|
| 130 |
+
return Transform3d(matrix=new_m, device=str(dev), dtype=dt)
|
| 131 |
+
|
| 132 |
+
def scale(self, x, y=None, z=None):
|
| 133 |
+
dev = self._matrix.device
|
| 134 |
+
dt = self._matrix.dtype
|
| 135 |
+
if isinstance(x, torch.Tensor):
|
| 136 |
+
x = x.to(device=dev, dtype=dt)
|
| 137 |
+
if x.dim() == 0:
|
| 138 |
+
s = x.expand(3)
|
| 139 |
+
elif x.shape[-1] == 3:
|
| 140 |
+
s = x.squeeze()
|
| 141 |
+
else:
|
| 142 |
+
s = x.expand(3)
|
| 143 |
+
else:
|
| 144 |
+
if y is None: y = x
|
| 145 |
+
if z is None: z = x
|
| 146 |
+
s = torch.tensor([x, y, z], dtype=dt, device=dev)
|
| 147 |
+
if s.dim() == 1:
|
| 148 |
+
s = s.unsqueeze(0)
|
| 149 |
+
S = torch.eye(4, dtype=dt, device=dev).unsqueeze(0).expand(s.shape[0], -1, -1).clone()
|
| 150 |
+
S[:, 0, 0] = s[:, 0]; S[:, 1, 1] = s[:, 1]; S[:, 2, 2] = s[:, 2]
|
| 151 |
+
new_m = self._matrix @ S
|
| 152 |
+
return Transform3d(matrix=new_m, device=str(dev), dtype=dt)
|
| 153 |
+
|
| 154 |
+
def to(self, device=None, dtype=None):
|
| 155 |
+
self._matrix = self._matrix.to(device=device, dtype=dtype)
|
| 156 |
+
self.device = self._matrix.device
|
| 157 |
+
if dtype is not None:
|
| 158 |
+
self.dtype = dtype
|
| 159 |
+
return self
|
| 160 |
+
|
| 161 |
+
def inverse(self):
|
| 162 |
+
inv_m = torch.inverse(self._matrix)
|
| 163 |
+
return Transform3d(matrix=inv_m, device=str(self._matrix.device), dtype=self._matrix.dtype)
|
| 164 |
+
|
| 165 |
+
def rotate(self, R):
|
| 166 |
+
dev = self._matrix.device
|
| 167 |
+
dt = self._matrix.dtype
|
| 168 |
+
R = R.to(device=dev, dtype=dt)
|
| 169 |
+
if R.dim() == 2:
|
| 170 |
+
R = R.unsqueeze(0)
|
| 171 |
+
T = torch.eye(4, dtype=dt, device=dev).unsqueeze(0).expand(R.shape[0], -1, -1).clone()
|
| 172 |
+
T[:, :3, :3] = R
|
| 173 |
+
new_m = self._matrix @ T
|
| 174 |
+
return Transform3d(matrix=new_m, device=str(dev), dtype=dt)
|
| 175 |
+
|
| 176 |
+
def stack(self, *others):
|
| 177 |
+
matrices = [self._matrix] + [o.get_matrix().to(device=self._matrix.device, dtype=self._matrix.dtype) for o in others]
|
| 178 |
+
stacked = torch.cat(matrices, dim=0)
|
| 179 |
+
return Transform3d(matrix=stacked, device=str(self._matrix.device), dtype=self._matrix.dtype)
|
| 180 |
+
|
| 181 |
+
def clone(self):
|
| 182 |
+
return Transform3d(matrix=self._matrix.clone(), device=str(self._matrix.device), dtype=self._matrix.dtype)
|
| 183 |
+
|
| 184 |
+
def __len__(self):
|
| 185 |
+
return self._matrix.shape[0]
|
| 186 |
+
|
| 187 |
+
def __getitem__(self, index):
|
| 188 |
+
m = self._matrix[index]
|
| 189 |
+
if m.dim() == 2:
|
| 190 |
+
m = m.unsqueeze(0)
|
| 191 |
+
return Transform3d(matrix=m, device=str(self._matrix.device), dtype=self._matrix.dtype)
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
class Rotate(Transform3d):
|
| 195 |
+
"""Transform3d initialized with a rotation matrix."""
|
| 196 |
+
def __init__(self, R=None, dtype=torch.float32, device="cpu"):
|
| 197 |
+
super().__init__(dtype=dtype, device=device)
|
| 198 |
+
if R is not None:
|
| 199 |
+
if isinstance(R, torch.Tensor):
|
| 200 |
+
R = R.to(device=device, dtype=dtype)
|
| 201 |
+
if R.dim() == 2:
|
| 202 |
+
R = R.unsqueeze(0)
|
| 203 |
+
T = torch.eye(4, dtype=dtype, device=device).unsqueeze(0).expand(R.shape[0], -1, -1).clone()
|
| 204 |
+
T[:, :3, :3] = R
|
| 205 |
+
self._matrix = T
|
| 206 |
+
self.device = self._matrix.device
|
| 207 |
+
|
| 208 |
+
|
| 209 |
+
class Translate(Transform3d):
|
| 210 |
+
"""Transform3d initialized with a translation."""
|
| 211 |
+
def __init__(self, x=0.0, y=0.0, z=0.0, dtype=torch.float32, device="cpu"):
|
| 212 |
+
super().__init__(dtype=dtype, device=device)
|
| 213 |
+
if isinstance(x, torch.Tensor):
|
| 214 |
+
t = x.to(device=device, dtype=dtype)
|
| 215 |
+
if t.dim() == 1:
|
| 216 |
+
t = t.unsqueeze(0)
|
| 217 |
+
else:
|
| 218 |
+
t = torch.tensor([[x, y, z]], dtype=dtype, device=device)
|
| 219 |
+
T = torch.eye(4, dtype=dtype, device=device).unsqueeze(0).expand(t.shape[0], -1, -1).clone()
|
| 220 |
+
T[:, :3, 3] = t
|
| 221 |
+
self._matrix = T
|
| 222 |
+
self.device = self._matrix.device
|
| 223 |
+
|
| 224 |
+
|
| 225 |
+
class Scale(Transform3d):
|
| 226 |
+
"""Transform3d initialized with a scale."""
|
| 227 |
+
def __init__(self, x=1.0, y=None, z=None, dtype=torch.float32, device="cpu"):
|
| 228 |
+
super().__init__(dtype=dtype, device=device)
|
| 229 |
+
if isinstance(x, torch.Tensor):
|
| 230 |
+
s = x.to(device=device, dtype=dtype)
|
| 231 |
+
if s.dim() == 0:
|
| 232 |
+
s = s.expand(3).unsqueeze(0)
|
| 233 |
+
elif s.dim() == 1 and s.shape[0] == 3:
|
| 234 |
+
s = s.unsqueeze(0)
|
| 235 |
+
elif s.dim() == 1 and s.shape[0] == 1:
|
| 236 |
+
s = s.expand(3).unsqueeze(0)
|
| 237 |
+
else:
|
| 238 |
+
if y is None: y = x
|
| 239 |
+
if z is None: z = x
|
| 240 |
+
s = torch.tensor([[x, y, z]], dtype=dtype, device=device)
|
| 241 |
+
S = torch.eye(4, dtype=dtype, device=device).unsqueeze(0).expand(s.shape[0], -1, -1).clone()
|
| 242 |
+
S[:, 0, 0] = s[:, 0]; S[:, 1, 1] = s[:, 1]; S[:, 2, 2] = s[:, 2]
|
| 243 |
+
self._matrix = S
|
| 244 |
+
self.device = self._matrix.device
|
pytorch3d_stub/pytorch3d/vis/__init__.py
CHANGED
|
@@ -1 +1,17 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""pytorch3d.vis stub."""
|
| 2 |
+
import importlib
|
| 3 |
+
import warnings
|
| 4 |
+
|
| 5 |
+
def __getattr__(name):
|
| 6 |
+
if name.startswith("__") and name.endswith("__"):
|
| 7 |
+
raise AttributeError(name)
|
| 8 |
+
try:
|
| 9 |
+
return importlib.import_module(f".{name}", __name__)
|
| 10 |
+
except ImportError:
|
| 11 |
+
pass
|
| 12 |
+
warnings.warn(f"pytorch3d.vis stub: {name} not implemented", stacklevel=2)
|
| 13 |
+
class _Dummy:
|
| 14 |
+
def __init__(self, *a, **kw): pass
|
| 15 |
+
def __call__(self, *a, **kw): return None
|
| 16 |
+
_Dummy.__name__ = _Dummy.__qualname__ = name
|
| 17 |
+
return _Dummy
|
pytorch3d_stub/pytorch3d/vis/plotly_vis.py
CHANGED
|
@@ -1 +1,42 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""pytorch3d.vis.plotly_vis stub with catch-all."""
|
| 2 |
+
import warnings
|
| 3 |
+
from collections import namedtuple
|
| 4 |
+
|
| 5 |
+
def __getattr__(name):
|
| 6 |
+
if name.startswith("__") and name.endswith("__"):
|
| 7 |
+
raise AttributeError(name)
|
| 8 |
+
# Return a callable dummy for any missing name
|
| 9 |
+
def _dummy(*a, **kw): return None
|
| 10 |
+
_dummy.__name__ = name
|
| 11 |
+
return _dummy
|
| 12 |
+
|
| 13 |
+
def plot_scene(plots, **kwargs):
|
| 14 |
+
return None
|
| 15 |
+
|
| 16 |
+
def get_camera_wireframe(**kwargs):
|
| 17 |
+
return None
|
| 18 |
+
|
| 19 |
+
# These need to be namedtuples with _asdict() support because plot_scene.py calls ._asdict()
|
| 20 |
+
AxisArgs = namedtuple("AxisArgs", ["showgrid", "backgroundcolor", "showticklabels", "tickcolor", "gridcolor", "zeroline"],
|
| 21 |
+
defaults=[True, "rgb(230,230,230)", True, "black", "rgb(200,200,200)", False])
|
| 22 |
+
Lighting = namedtuple("Lighting", ["ambient", "diffuse", "specular", "roughness", "fresnel"],
|
| 23 |
+
defaults=[0.5, 1.0, 0.3, 0.5, 0.2])
|
| 24 |
+
|
| 25 |
+
# Explicitly define all private functions that plot_scene imports
|
| 26 |
+
def _add_camera_trace(fig, cameras, trace_name, subplot_idx, ncols, camera_scale, **kw):
|
| 27 |
+
pass
|
| 28 |
+
|
| 29 |
+
def _add_pointcloud_trace(fig, pointclouds, trace_name, subplot_idx, ncols, max_points=20000, marker_size=1, **kw):
|
| 30 |
+
pass
|
| 31 |
+
|
| 32 |
+
def _add_ray_bundle_trace(fig, ray_bundle, trace_name, subplot_idx, ncols, *a, **kw):
|
| 33 |
+
pass
|
| 34 |
+
|
| 35 |
+
def _is_ray_bundle(obj):
|
| 36 |
+
return False
|
| 37 |
+
|
| 38 |
+
def _scale_camera_to_bounds(value, vrange, is_position):
|
| 39 |
+
return value
|
| 40 |
+
|
| 41 |
+
def _update_axes_bounds(verts_center, max_expand, current_layout):
|
| 42 |
+
pass
|