Build torchvision from source

Do not miss this exclusive book on Binary Tree Problems. Get it now for free.

In this article, we have explained how to build torchvision from source step by step.

torchvision can be installed using pip directly but it may not be compatible with the Pytorch version, you are using. Additionally, the pip version of torchvision does not have all torchvision APIs available. For this, it is important to build torchvision for source.

Table of contents:

  1. Step 1: Get the source code of torchvision
  2. Step 2: Build and install
  3. Step 3: Build C++ API of torchvision

We will get started with the build steps.

Step 1: Get the source code of torchvision

git clone https://github.com/pytorch/vision.git
cd vision

Checkout to the version of torchvision you need. Note that only a specific version of torchvision is compartible with a particular version of PyTorch.

git checkout v0.14.0

Step 2: Build and install

For UNIX based systems like Ubuntu:

python setup.py install

If CUDA is avialable on the system, the GPU version is built automatically. If you want to build the CUDA version explicitly, use the following command before the above build command:

export FORCE_CUDA=1

Note that the above build creates the release version of torchvision and specific APIs may not be available, so to build torchvision with all APIs, we need to build in debug mode using the following command:

python setup.py build develop

For Mac OSX:

MACOSX_DEPLOYMENT_TARGET=10.9 CC=clang CXX=clang++ python setup.py install

Step 3: Build C++ API of torchvision

To build the C++ API of torchvision, use the following build commands:

mkdir build
cd build
cmake ..
make
make install

If you want to enable CUDA support, replace cmake command with:

cmake .. -DWITH_CUDA=on

With this article at OpenGenus, you must have the complete idea of building and installing torchvision from source.

Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.