YiftachEde commited on
Commit
ca0709a
·
verified ·
1 Parent(s): 6aac4cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +121 -59
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 to ensure compatibility
23
  print("Installing PyTorch3D from source...")
24
 
25
- # Install build dependencies
26
- os.system("apt-get update && apt-get install -y git build-essential libglib2.0-0 libsm6 libxrender-dev libxext6")
27
- os.system("pip install 'imageio>=2.5.0' 'ipywidgets>=7.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'")
 
 
 
28
  os.system("pip install fvcore iopath")
29
 
30
- # Clone and install PyTorch3D
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
- # Verify installation
36
- import_result = os.popen('python -c "import pytorch3d; print(\'PyTorch3D import successful\')" 2>&1').read()
 
 
 
 
 
 
 
 
 
 
 
37
  print(import_result)
38
 
39
- # If source installation fails, try the wheel as fallback
40
- if 'undefined symbol: _ZN3c104cuda9SetDeviceEi' in import_result or 'No module named' in import_result:
41
- print("Source installation failed, trying pre-built wheel...")
42
- os.system("pip install pytorch3d -f https://dl.fbaipublicfiles.com/pytorch3d/packaging/wheels/py310_cu118_pyt201/download.html")
43
 
44
- # Verify again
45
- import_result = os.popen('python -c "import pytorch3d; print(\'PyTorch3D import successful\')" 2>&1').read()
46
- print(import_result)
47
-
48
- # If all installation methods fail, try CPU-only version
49
- if 'undefined symbol: _ZN3c104cuda9SetDeviceEi' in import_result or 'No module named' in import_result:
50
- print("All GPU installation attempts failed, falling back to CPU-only version...")
51
- os.system("pip install pytorch3d")
 
 
 
 
 
52
 
53
  # Verify again
54
- import_result = os.popen('python -c "import pytorch3d; print(\'PyTorch3D import successful\')" 2>&1').read()
55
  print(import_result)
56
 
57
- # Create a workaround for the specific error if it still persists
58
- if 'undefined symbol: _ZN3c104cuda9SetDeviceEi' in import_result:
59
- print("Creating workaround for the specific error...")
 
 
 
 
60
 
61
- # Create a simple wrapper module that will handle the import error
62
- with open("pytorch3d_wrapper.py", "w") as f:
63
- f.write("""
 
 
 
 
 
 
 
 
 
64
  import torch
65
- import sys
66
 
67
- # Create mock PyTorch3D module
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
- def __call__(self, *args, **kwargs):
80
- return self
 
 
81
 
82
- # Try to import the real PyTorch3D
83
- try:
84
- import pytorch3d as real_pytorch3d
85
- sys.modules['pytorch3d'] = real_pytorch3d
86
- print("Successfully imported real PyTorch3D")
87
- except ImportError as e:
88
- if 'undefined symbol: _ZN3c104cuda9SetDeviceEi' in str(e):
89
- # If we get the specific error, use the mock implementation
90
- mock_module = MockPyTorch3D()
91
- sys.modules['pytorch3d'] = mock_module
92
- print(f"Using mock PyTorch3D due to: {e}")
93
- else:
94
- # For other import errors, just raise them
95
- raise e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  """)
97
-
98
- # Use the wrapper
99
- os.system("cp pytorch3d_wrapper.py $(python -c 'import site; print(site.getsitepackages()[0])')/pytorch3d_wrapper.py")
100
- os.system("echo 'from pytorch3d_wrapper import *' > $(python -c 'import site; print(site.getsitepackages()[0])')/pytorch3d.py")
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