2

I created a virtualenv with python 3.10 and installed open3d and PyTorch according to the instructions on open3d-ml webpage: Open3d-ML but when I tested it with import open3d.ml.torch I get the error: Exception: Open3D was not built with PyTorch support!

Steps to reproduce

python3.10 -m venv .venv
source .venv/bin/activate
pip install --upgrade pip
pip install open3d
pip install torch torchvision torchaudio

Error

% python -c "import open3d.ml.torch as ml3d"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/xx/.venv/lib/python3.10/site-packages/open3d/ml/torch/__init__.py", line 34, in <module>
    raise Exception('Open3D was not built with PyTorch support!')
Exception: Open3D was not built with PyTorch support!

Environment:

% python3 --version
Python 3.10.9
% pip freeze
open3d==0.16.1
torch==1.13.1
torchaudio==0.13.1
torchvision==0.14.1

OS

macOS 12.6
Kernel Version: Darwin 21.6.0

I also checked below similar issues but they don't have answers:

https://github.com/isl-org/Open3D/discussions/5849

https://github.com/isl-org/Open3D-ML/issues/557

Open3D-ML and pytorch

According to this issue 5849 the problem can't be related only to MacOs because, in a docker with Ubuntu20.04, there is a similar error.

Does anyone know how we can tackle this?

Bruce
  • 415
  • 2
  • 19
  • I haven't tried Open3D-ML yet. To begin with, if I looked at the readme correctly, CUDA won't be an option so it might need to be CPU only. Additionally it looks they're supporting very specific versions of Torch (PyTorch 1.8.2 CPU (or [1.12.0+cu116](https://github.com/isl-org/Open3D-ML/blob/master/requirements-torch-cuda.txt)). I would try first getting a version of PyTorch 1.8.2 CPU installed, then building Open3D from source with ML/Pytorch support (using CMake): just in case are no prebuild pip wheels for your Mac CPU (e.g. M1, M2, etc.).... – George Profenza Jan 18 '23 at 16:14
  • ...that may be a quite the time sink though. From a pragmatic point of view I'd see if I can borrow/use a Ubuntu machine to try it out first and see if it has the features you need before investing into the MacOS CPU only version. (Maybe you can try it out on a free Ubuntu VM via Google Colab or similar services ?) – George Profenza Jan 18 '23 at 16:17
  • Thanks, George for your comments. I tried Open3d-ML on ubuntu 20.04 and it worked fine (it was an older version that worked with python 3.9). Now I want to install it on my Mac (I changed my laptop). I tried installing Torch 1.8.2 on my Mac, and after some struggle, I could find the [macos-arm64](https://download.pytorch.org/whl/torch_stable.html) wheels and installed `torch-1.8.1-cp38-none-macosx_11_0_arm64.whl` manually in a venv with python3.8. Then I installed [open3d-0.14.1-cp38-cp38-linux_aarch64.whl](https://github.com/isl-org/Open3D/releases/tag/v0.14.1) but I get an error ... – Bruce Jan 19 '23 at 14:30
  • This is the error: `ImportError: dlopen(/venv38/lib/python3.8/site-packages/torch/_C.cpython-38-darwin.so, 0x0002): tried: '/venv38/lib/python3.8/site-packages/torch/_C.cpython-38-darwin.so' (mach-o file, but is an incompatible architecture (have (x86_64), need (arm64e)))` – Bruce Jan 19 '23 at 14:31
  • @GeorgeProfenza so you believe `Exception: Open3D was not built with PyTorch support!` error is because of a mismatch in the PyTorch versions? In the Docker it has the same issue https://github.com/isl-org/Open3D/discussions/5849 – Bruce Jan 19 '23 at 14:42
  • Great to hear you've tried it on Ubuntu already. The says it can't open the torch native libraries as they been compiled for x64 architecture (not arm64e (M1 or M2)). Even without open3d you should be able to test if pytorch is installed correctly and runs on your mac (e.g. you can train a basic model or at least run inference with no errors). Only after that works look into Open3D-ML is my advice. Be aware you need to use the version of PyTorch compatible with Open3D ([1.12.0](https://github.com/isl-org/Open3D-ML/blob/master/requirements-torch.txt#L2)). – George Profenza Jan 19 '23 at 17:11
  • 1
    Also notice in requirements-torch this section: `sys_platform != 'darwin'`. This hints that the precompiled version of pytorch cpu is not compatible with OSX. (I do see [articles about M1 Pytorch](https://towardsdatascience.com/installing-pytorch-on-apple-m1-chip-with-gpu-acceleration-3351dc44d67c), I don't have an M1 mac to test and don't know if there's a M1 wheel for PyTorch 1.12.0). To be on the safe side I'd go with the slow/tedious route: 1. git clone and build PyTorch (e.g. checkout 1.12.0) [from source](https://github.com/pytorch/pytorch#from-source) 2. git clone and build Open3D – George Profenza Jan 19 '23 at 17:35
  • ...[from source](http://www.open3d.org/docs/release/compilation.html#ubuntu-macos), using CMake to set the paths to PyTorch and enable Open3D-ML. – George Profenza Jan 19 '23 at 17:35
  • I tried the PyTorch with the M1 chip and MPS acceleration. I tried a small model and Pytorch works fine but it is `pytorch 1.13.1` installed with this command: ```# MPS acceleration is available on MacOS 12.3+``` ```pip3 install torch torchvision torchaudio``` I also tried installing Open3d from source with Pytorch support but ran into different issues. Maybe I create a separate issue for it. Thanks for your hint, now at least I'm sure the problem is Pytorch incompatibility with Open3d. – Bruce Jan 20 '23 at 09:40
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/251282/discussion-between-george-profenza-and-bruce). – George Profenza Jan 20 '23 at 09:59

2 Answers2

2

Finally, I decided to build Open3D from the source for Mac M1. I followed almost the official open3d page and thanks to this medium in one of the replies.

Build Open3d-ml with Pytorch on Mac M1

For the OS environment see the main question.

conda create --name open3d-ml-build python==3.10
conda activate open3d-ml-build
# install pytorch from pytorch.org
conda install pytorch torchvision torchaudio -c pytorch

# now clone open3d in a desired location
git clone --branch v0.16.1 git@github.com:isl-org/Open3D.git ./foo/open3d-0.16-build
cd open3d-0.16-build
mkdir build && cd build
git clone git@github.com:isl-org/Open3D-ML.git

Now make sure you are in the activeted conda env.

Build

(takes very long and a lot of memory)

Note on Mac M1 you don't have Cuda but Metal Performance Shaders (MPS) so I made CUDA Flags OFF in the cmake configuration.

which python3
>> /Users/XX/miniconda3/envs/open3d-ml-build/bin/python3

# in the build direcotry
cmake -DBUILD_CUDA_MODULE=OFF -DGLIBCXX_USE_CXX11_ABI=OFF \
-DBUILD_PYTORCH_OPS=ON -DBUILD_CUDA_MODULE=OFF \
-DBUNDLE_OPEN3D_ML=ON -DOPEN3D_ML_ROOT=Open3D-ML \
-DBUILD_JUPYTER_EXTENSION:BOOL=OFF \
-DPython3_ROOT=/Users/XX/miniconda3/envs/open3d-ml-build/bin/python3 ..

make -j$(sysctl -n hw.physicalcpu) [verbose=1]

If it fails, try it again or run it with verbose and look for fatal error.

Install

# Install pip package in the current python environment
make install-pip-package

# if error: Module Not found yapf
pip install yapf

# Create Python package in build/lib
make python-package

# Create pip wheel in build/lib
# This creates a .whl file that you can install manually.
make pip-package

sanity check

Again in the activated conda environment
# if not installed
pip install tensorboard

python3 -c "import open3d; import open3d.ml.torch"

pip freeze | grep torch
torch==1.13.1
torchaudio==0.13.1
torchvision==0.14.1

If you don't get any errors you should be good to go.

Bruce
  • 415
  • 2
  • 19
0

You will need to enable PyTorch / Tensorflow when building with cmake - have a look at https://medium.com/@kidargueta/installing-open3d-ml-for-3d-computer-vision-with-pytorch-d640a6862e19 and http://www.open3d.org/docs/release/compilation.html

cmake -DBUILD_CUDA_MODULE=ON -DGLIBCXX_USE_CXX11_ABI=OFF -DBUILD_PYTORCH_OPS=ON -DBUILD_CUDA_MODULE=ON -DBUNDLE_OPEN3D_ML=ON -DOPEN3D_ML_ROOT=Open3D-ML -DBUILD_JUPYTER_EXTENSION:BOOL=ON -DBUILD_WEBRTC=ON -DOPEN3D_ML_ROOT=https://github.com/isl-org/Open3D-ML.git -DPython3_ROOT=/path/to/your/conda-env/bin/python ..

  • I'm not building Open3d from the source. I'm installing it via `pip install open3d` and this is according to Open3d-Ml github page. But as George mentioned the problem is the incompatibility of Pytorch versions ( in my case 1.13.1) with Open3d that gives this error. So the error is misleading. I managed to make it work on a Docker with a Linux base image running on Mac M1. – Bruce Jan 26 '23 at 11:35