Guide to Conda for TensorFlow and PyTorch | by Eric Hofesmann | Towards Data Science

It’s a real shame that the first experience that most people have with deep learning is having to spend days trying to figure out why the model they downloaded off of GitHub just... won’t... run....
Dependency issues are incredibly common when trying to run an off-the-shelf model. The most problematic of which is needing to have the correct version of CUDA for TensorFlow. TensorFlow has been prominent for a number of years meaning that even new models that are released could use an old version of TensorFlow. This wouldn’t be an issue except that it feels like every version of TensorFlow needs a specific version of CUDA where anything else is incompatible. Sadly, installing multiple versions of CUDA on the same machine can be a real pain!
After many years of headaches, thanks to the help of Anaconda I finally realized that installing TensorFlow and exactly the right CUDA version can be as easy as:
conda create --name tfconda activate tfconda install tensorflow-gpu
I had previously only used pip due to a shoddy understanding of the difference between pip and conda. Really just knowing that pip is the “official” Python package manager.
The primary difference between the two is that conda environments are not only for Python packages. Libraries like CUDA can be installed in your isolated environment. On the other hand, some packages do not exist in conda and you will have to install them through pip which is one reason that people might stray away from conda. Using both conda and pip together can be tricky at times, but I provide some tips to handle that later in this post.
If you want to start using conda, follow the anaconda installation instructions in this link. Basic commands for things like creating, activating, and deleting environments are very similar between pip and conda.
# pipvirtualenv env_name# condaconda create --name env_name
# pipsource /path/to/env_name/bin/activate# condaconda activate env_name
Note: After installing anaconda, it automatically creates and activates a base environment. It is recommended you create new environments yourself. Turn off the automatic activation with this command:
conda config --set auto_activate_base false
# pipcd /path/to/env_namerm -rf env_name# condaconda env remove -n env_name
Below are a few examples of how to load TensorFlow and PyTorch models that exist in the FiftyOne model zoo. FiftyOne is an open-source tool for machine learning engineers to store their data, labels, and model predictions in a way that can be easily modified, visualized, and analyzed. Included in FiftyOne is a zoo of computer vision models that are available with a single line of code and will serve to easily test our conda environment setups.
Note: Installing TensorFlow with GPU functionality requires a CUDA enabled card. Here is a list of CUDA capable GPUs.
conda create --name tf2conda activate tf2conda install tensorflow-gpu
FiftyOne supports image and video datasets in various formats. In these examples, I just have a directory of images that I will be loading into FiftyOne to generate model predictions on. You can use your own directory of images if you pass in the /path/to/dataset and specify that you are using a dataset type of fiftone.types.ImageDirectory.
I am using the FiftyOne command-line interface (CLI) for most of the work in this example.
pip install fiftyone# Create a dataset from the given data on diskfiftyone datasets create \    --name my_dataset \    --dataset-dir /path/to/dataset \    --type fiftyone.types.ImageDirectory
I can download the model I want to use and then check if the requirements for it are satisfied.
# Download a model from the zoofiftyone zoo models download centernet-hg104-512-coco-tf2# Ensure you installed all requirementsfiftyone zoo models requirements \    --ensure centernet-hg104-512-coco-tf2# This model is from the TF2 Model Zoo which must be installedeta install models
I will then apply the model to the dataset to generate predictions and visualize them in the FiftyOne App.
# Apply a model from the zoo to your datasetfiftyone zoo models apply \    centernet-hg104-512-coco-tf2 \      my_dataset \                        predictions                     # Launch the FiftyOne App to visualize your datasetfiftyone app launch my_dataset
conda create --name tf1conda activate tf1
You can search for available packages and then choose which TensorFlow version to install. This will install the corresponding CUDA version in your conda environment.
conda search tensorflow-gpuconda install tensorflow-gpu=1.15
I will be using the same procedure as in the TensorFlow 2 example except with a model that uses TensorFlow 1.
pip install fiftyone# Download a model from the zoofiftyone zoo models download mask-rcnn-resnet101-atrous-coco-tf# Create a dataset from the given data on diskfiftyone datasets create \    --name my_dataset \    --dataset-dir /path/to/dataset \    --type fiftyone.types.ImageDirectory# Apply a model from the zoo to your datasetfiftyone zoo models apply \    --error-level 1 \    mask-rcnn-resnet101-atrous-coco-tf \         my_dataset \                                  predictions                               # Launch the FiftyOne App to visualize your datasetfiftyone app launch my_dataset
Installing PyTorch is a bit easier because it is compiled with multiple versions of CUDA. This gives us the freedom to use whatever version of CUDA we want. The default installation instructions at the time of writing (January 2021) recommend CUDA 10.2 but there is a CUDA 11 compatible version of PyTorch.
conda create --name pytconda activate pytconda install pytorch torchvision torchaudio cudatoolkit=10.2 \    -c pytorchpip install fiftyone
For this example, I’ll use the FiftyOne Python API to perform nearly the same steps as we did previously using the command line. The only difference is the model we are using and that we are loading a dataset from the FiftyOne dataset zoo.
Note: Install PyTorch from source if you are using a Mac and need GPU support.
Three issues came up when I switched from pip to conda that took a bit of time to figure out.
If a package you want to use only exists in pip, you can use pip to install it inside of your conda environment. However, pip and conda don’t always play nice together.
The primary issue is that conda is not able to control packages that it did not install. So if pip is used inside of a conda environment, conda is unaware of the changes and may break the environment.
Follow these tips from Johnathan Helmus when using pip and conda together:
Never use conda after pip. Install everything that you can with conda , then install remaining packages with pip
Create new conda environments. Using pip can result in a broken environment, so always make a new isolated conda environment before installing pip packages.
If you need to install aconda package after having used pip, it is better to just make a new environment and reinstall everything in the correct order.
The default behavior when creating apip virtual environment is that when inside the new environment, you do not have access to globally installed pip packages. If you want access to global packages, you need to initialize your virtual environment with:
virtualenv env --system-site-packages
conda , on the other hand, lets you access global system packages by default. If you want the environment to be completely isolated from these global packages like with pip, you can create the conda environment based on a clone of an empty pip virtualenv.
virtualenv empty_envconda create -n env --clone empty_env
Ideally, you should avoid installing global pip packages anyway so this shouldn’t be an issue.
If you have a system-wide install of CUDA, then the LD_LIBRARY_PATH environment variable may point to the wrong location after installing CUDA inside of your conda environment.
To fix this, you will want to update LD_LIBRARY_PATH to point to the directory containing cuda within conda when you enter your conda environment (generally that directory is /path/to/conda/pkgs/). Then you’ll want to point it back to the system-wide install of CUDA when you leave the conda environment.
First, navigate to your conda environment location and create the following files and folders:
cd /path/to/anaconda/envs/my_envmkdir -p ./etc/conda/activate.dmkdir -p ./etc/conda/deactivate.dtouch ./etc/conda/activate.d/env_vars.shtouch ./etc/conda/deactivate.d/env_vars.sh
In activate.d/env_vars.sh add the following lines. Substitute /your/path with the path to the installation of CUDA in your conda environment.
#!/bin/bashexport OLD_LD_LIBRARY_PATH=${LD_LIBRARY_PATH}export LD_LIBRARY_PATH=/your/path:${LD_LIBRARY_PATH}
Generally, this /your/path would be something like:
export LD_LIBRARY_PATH=/home/user/conda/pkgs/cudatoolkit-10.2/lib:${LD_LIBRARY_PATH}
Then add the following lines in deactivate.d/env_vars.sh:
#!/bin/bashexport LD_LIBRARY_PATH=${OLD_LD_LIBRARY_PATH}unset OLD_LD_LIBRARY_PATH
Being able to install non-Python packages, like CUDA, in only a few commands with conda is a godsend. Using conda in conjunction with the FiftyOne model zoo means you can generate predictions from dozens of models in minutes. However, switching from pip virtual environments to conda environments can result in some unexpected behavior if you’re not prepared.