pythonpython-3.xpytorchopen3d

Installing Open3d-Ml with Pytorch (on MacOs)


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?


Solution

  • 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.