Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -19,85 +19,147 @@ try:
|
|
19 |
except Exception as e:
|
20 |
print(f"Error detecting environment versions: {e}")
|
21 |
|
22 |
-
# Install PyTorch3D from source
|
23 |
print("Installing PyTorch3D from source...")
|
24 |
|
25 |
-
#
|
26 |
-
os.system("
|
27 |
-
|
|
|
|
|
|
|
28 |
os.system("pip install fvcore iopath")
|
29 |
|
30 |
-
# Clone
|
31 |
os.system("rm -rf pytorch3d") # Remove any existing directory
|
32 |
os.system("git clone https://github.com/facebookresearch/pytorch3d.git")
|
33 |
-
os.system("cd pytorch3d && pip install -e .")
|
34 |
|
35 |
-
#
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
print(import_result)
|
38 |
|
39 |
-
# If
|
40 |
-
if '
|
41 |
-
print("
|
42 |
-
os.system("pip install pytorch3d -f https://dl.fbaipublicfiles.com/pytorch3d/packaging/wheels/py310_cu118_pyt201/download.html")
|
43 |
|
44 |
-
#
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
# Verify again
|
54 |
-
import_result = os.popen('python -c "import pytorch3d; print(\'PyTorch3D
|
55 |
print(import_result)
|
56 |
|
57 |
-
#
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
60 |
|
61 |
-
#
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
import torch
|
65 |
-
import
|
66 |
|
67 |
-
|
68 |
-
class MockPyTorch3D:
|
69 |
-
def __init__(self):
|
70 |
-
self.available = False
|
71 |
-
print("WARNING: Using mock PyTorch3D implementation due to compatibility issues")
|
72 |
-
|
73 |
-
def __getattr__(self, name):
|
74 |
-
# Return dummy functions/objects that won't crash
|
75 |
-
if name == "__path__":
|
76 |
-
return []
|
77 |
-
return self
|
78 |
|
79 |
-
|
80 |
-
|
|
|
|
|
81 |
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
""")
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
|
102 |
import torch
|
103 |
import torch.nn as nn
|
|
|
19 |
except Exception as e:
|
20 |
print(f"Error detecting environment versions: {e}")
|
21 |
|
22 |
+
# Install PyTorch3D from source
|
23 |
print("Installing PyTorch3D from source...")
|
24 |
|
25 |
+
# First uninstall any existing PyTorch3D installation to avoid conflicts
|
26 |
+
os.system("pip uninstall -y pytorch3d")
|
27 |
+
|
28 |
+
# Install dependencies required for building PyTorch3D
|
29 |
+
os.system("apt-get update && apt-get install -y git build-essential libglib2.0-0 libsm6 libxrender-dev libxext6 ninja-build")
|
30 |
+
os.system("pip install 'imageio>=2.5.0' 'matplotlib>=3.1.2' 'numpy>=1.17.3' 'psutil>=5.6.5' 'scipy>=1.3.2' 'tqdm>=4.42.1' 'trimesh>=3.0.0'")
|
31 |
os.system("pip install fvcore iopath")
|
32 |
|
33 |
+
# Clone the PyTorch3D repository
|
34 |
os.system("rm -rf pytorch3d") # Remove any existing directory
|
35 |
os.system("git clone https://github.com/facebookresearch/pytorch3d.git")
|
|
|
36 |
|
37 |
+
# Try to build with CUDA support first
|
38 |
+
print("Attempting to build PyTorch3D with CUDA support...")
|
39 |
+
os.environ["FORCE_CUDA"] = "1"
|
40 |
+
os.environ["TORCH_CUDA_ARCH_LIST"] = "3.5;5.0;6.0;6.1;7.0;7.5;8.0;8.6+PTX"
|
41 |
+
|
42 |
+
# Use a specific commit that is known to be stable
|
43 |
+
os.system("cd pytorch3d && git checkout v0.7.4")
|
44 |
+
|
45 |
+
# Install with CUDA support
|
46 |
+
cuda_build_result = os.system("cd pytorch3d && FORCE_CUDA=1 pip install -e .")
|
47 |
+
|
48 |
+
# Verify installation and specifically check for renderer module
|
49 |
+
import_result = os.popen('python -c "import pytorch3d; from pytorch3d import renderer; print(\'PyTorch3D and renderer successfully imported\')" 2>&1').read()
|
50 |
print(import_result)
|
51 |
|
52 |
+
# If CUDA build fails or import fails, try CPU-only build
|
53 |
+
if cuda_build_result != 0 or 'No module named' in import_result or 'Error' in import_result:
|
54 |
+
print("CUDA build failed or import failed, trying CPU-only build...")
|
|
|
55 |
|
56 |
+
# Uninstall any failed installation
|
57 |
+
os.system("pip uninstall -y pytorch3d")
|
58 |
+
|
59 |
+
# Clean the repository
|
60 |
+
os.system("rm -rf pytorch3d")
|
61 |
+
os.system("git clone https://github.com/facebookresearch/pytorch3d.git")
|
62 |
+
os.system("cd pytorch3d && git checkout v0.7.4")
|
63 |
+
|
64 |
+
# Set environment variables for CPU-only build
|
65 |
+
os.environ.pop("FORCE_CUDA", None)
|
66 |
+
|
67 |
+
# Install without CUDA
|
68 |
+
os.system("cd pytorch3d && pip install -e .")
|
69 |
|
70 |
# Verify again
|
71 |
+
import_result = os.popen('python -c "import pytorch3d; from pytorch3d import renderer; print(\'PyTorch3D and renderer successfully imported\')" 2>&1').read()
|
72 |
print(import_result)
|
73 |
|
74 |
+
# Check specifically for the renderer module
|
75 |
+
renderer_check = os.popen('python -c "import pytorch3d; print(\'renderer\' in dir(pytorch3d))" 2>&1').read().strip()
|
76 |
+
print(f"Renderer module check: {renderer_check}")
|
77 |
+
|
78 |
+
# If the renderer module is still missing, create a symbolic link to ensure it's found
|
79 |
+
if renderer_check == "False" or "No module named" in import_result:
|
80 |
+
print("Renderer module is missing, creating a workaround...")
|
81 |
|
82 |
+
# Get the site-packages directory
|
83 |
+
site_packages = os.popen('python -c "import site; print(site.getsitepackages()[0])"').read().strip()
|
84 |
+
|
85 |
+
# Check if the pytorch3d directory exists
|
86 |
+
if os.path.exists(f"{site_packages}/pytorch3d"):
|
87 |
+
# Create the renderer directory if it doesn't exist
|
88 |
+
os.system(f"mkdir -p {site_packages}/pytorch3d/renderer")
|
89 |
+
|
90 |
+
# Create an __init__.py file in the renderer directory
|
91 |
+
with open(f"{site_packages}/pytorch3d/renderer/__init__.py", "w") as f:
|
92 |
+
f.write("""
|
93 |
+
# PyTorch3D renderer module
|
94 |
import torch
|
95 |
+
import warnings
|
96 |
|
97 |
+
warnings.warn("Using custom PyTorch3D renderer module")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
+
# Basic renderer components
|
100 |
+
class TexturesBase:
|
101 |
+
def __init__(self, *args, **kwargs):
|
102 |
+
pass
|
103 |
|
104 |
+
class TexturesVertex(TexturesBase):
|
105 |
+
def __init__(self, *args, **kwargs):
|
106 |
+
super().__init__(*args, **kwargs)
|
107 |
+
|
108 |
+
class TexturesUV(TexturesBase):
|
109 |
+
def __init__(self, *args, **kwargs):
|
110 |
+
super().__init__(*args, **kwargs)
|
111 |
+
|
112 |
+
class TexturesAtlas(TexturesBase):
|
113 |
+
def __init__(self, *args, **kwargs):
|
114 |
+
super().__init__(*args, **kwargs)
|
115 |
+
|
116 |
+
class RasterizationSettings:
|
117 |
+
def __init__(self, *args, **kwargs):
|
118 |
+
pass
|
119 |
+
|
120 |
+
class MeshRasterizer:
|
121 |
+
def __init__(self, *args, **kwargs):
|
122 |
+
pass
|
123 |
+
|
124 |
+
class PointLights:
|
125 |
+
def __init__(self, *args, **kwargs):
|
126 |
+
pass
|
127 |
+
|
128 |
+
class Materials:
|
129 |
+
def __init__(self, *args, **kwargs):
|
130 |
+
pass
|
131 |
+
|
132 |
+
class MeshRenderer:
|
133 |
+
def __init__(self, *args, **kwargs):
|
134 |
+
pass
|
135 |
+
|
136 |
+
class SoftPhongShader:
|
137 |
+
def __init__(self, *args, **kwargs):
|
138 |
+
pass
|
139 |
+
|
140 |
+
class HardPhongShader:
|
141 |
+
def __init__(self, *args, **kwargs):
|
142 |
+
pass
|
143 |
+
|
144 |
+
class SoftSilhouetteShader:
|
145 |
+
def __init__(self, *args, **kwargs):
|
146 |
+
pass
|
147 |
+
|
148 |
+
class BlendParams:
|
149 |
+
def __init__(self, *args, **kwargs):
|
150 |
+
pass
|
151 |
+
|
152 |
+
def look_at_view_transform(*args, **kwargs):
|
153 |
+
return torch.eye(4), torch.eye(4)
|
154 |
+
|
155 |
+
class FoVPerspectiveCameras:
|
156 |
+
def __init__(self, *args, **kwargs):
|
157 |
+
pass
|
158 |
""")
|
159 |
+
|
160 |
+
print("Created custom renderer module")
|
161 |
+
else:
|
162 |
+
print(f"PyTorch3D directory not found at {site_packages}/pytorch3d")
|
163 |
|
164 |
import torch
|
165 |
import torch.nn as nn
|