# Quick guide to using PyTorch in ParaView

**URL:** https://discourse.paraview.org/t/quick-guide-to-using-pytorch-in-paraview/9037
**Category:** Tips and Tricks
**Created:** [February 22, 2022, 2:24pm UTC](https://discourse.paraview.org/t/quick-guide-to-using-pytorch-in-paraview/9037 "2022-02-22T14:24:54Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![aschieb](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@aschieb](https://discourse.paraview.org/u/aschieb)
#### Post date: [February 22, 2022, 2:24pm UTC](https://discourse.paraview.org/t/quick-guide-to-using-pytorch-in-paraview/9037/1 "2022-02-22T14:24:54Z")

</div>

When it comes to using Machine Learning Frameworks alongside ParaView, PyTorch has proven itself to be much more flexible than others like Tensorflow.

This post will guide you through the steps of using the PyTorch Machine Learning framework with ParaView (tested on Linux and Windows). It will also cover how to use modules that are in a virtual environment thanks to a useful script included in ParaView.

Being able to use PyTorch in a ParaView plugin, or custom filter is for the most part equivalent to being able to successfully execute `import torch` in the ParaView embedded Python shell, which is itself equivalent to executing `import torch` in the `pvpython` interpreter

# Prerequisites

## ParaView

To let ParaView know which modules are available on your system, it has to be built against the right version of Python, typically the python that sits on your system under `usr/bin/python`. The pre-compiled binaries will not be able to let you import your own modules because it was compiled against a very basic version of Python containing only `numpy`.

This is why building from source is essential to let ParaView use the Python interpreter that exists on your system. To build ParaView from source, follow the [guide](https://gitlab.kitware.com/paraview/paraview/-/blob/master/Documentation/dev/build.md#getting-started-guide) and make sure that the right CMake options are activated :

PARAVIEW\_USE\_PYTHON needs to be set to ON

Python3\_EXECUTABLE must point to the desired Python interpreter on your system

# CUDA

Cuda is the powerful computing library that allows PyTorch to run on your GPU. To make sure that Cuda is available on your system, run the following command :

`python -c "import torch; print(torch.cuda.is_available())"`

Output should be `True`. Otherwise, make sure that you’re running a [Cuda capable GPU](https://developer.nvidia.com/cuda-gpus) on your system.

When building ParaView, the CMake option PARAVIEW\_USE\_CUDA might catch your attention. Note that this option does not need to be set to ON to be able to use Cuda with a filter that uses pytorch.

# Using a Virtual Environment

A very large set of projects use virtual environments to take care of module dependencies, and it is crucial to be able to use them in ParaView. Thankfully, it is not necessary to rebuild ParaView every time you want to change your virtual environment settings. A handy script is included by default in ParaView. There are two ways to indicate which virtual environment you wish to use :

- Set an environment variable `PV_VENV = /path/to/venv/`
- Add the `--venv /path/to/venv/` argument when executing ParaView

Then, open ParaView, and type the following command in the Python shell :  
`from paraview.web import venv`

The output should be : `ParaView is using venv : /path/to/venv/`

# Done!

You now have everything setup to use PyTorch in ParaView. If you need inspiration from projects which have used PyTorch or other ML frameworks in ParaView in the past, feel free to take a look at these

- [Drishti Maharjan](https://arxiv.org/search/cs?searchtype=author&query=Maharjan%2C+D), [Peter Zaspel](https://arxiv.org/search/cs?searchtype=author&query=Zaspel%2C+P) _Towards data-driven filters in Paraview_ : [arXiv:2108.05196](https://arxiv.org/abs/2108.05196) [cs.LG]
- [Xinyu Chen](https://arxiv.org/search/cs?searchtype=author&query=Chen%2C+X), [Qiang Guan](https://arxiv.org/search/cs?searchtype=author&query=Guan%2C+Q), [Li-Ta Lo](https://arxiv.org/search/cs?searchtype=author&query=Lo%2C+L), [Simon Su](https://arxiv.org/search/cs?searchtype=author&query=Su%2C+S), [James Ahrens](https://arxiv.org/search/cs?searchtype=author&query=Ahrens%2C+J), [Trilce Estrada](https://arxiv.org/search/cs?searchtype=author&query=Estrada%2C+T) _In situ TensorView: In situ Visualization of Convolutional Neural Networks_ : [arXiv:1806.07382](https://arxiv.org/abs/1806.07382) [cs.CV]
- [AI in ParaView and LidarView: Point cloud Deep Learning frameworks integration with Python plugins](https://www.kitware.com/ai-point-cloud-deep-learning-in-paraview-and-lidarview/)
- [Integrating Geometric Deep-Learning models into Paraview](https://www.kitware.com/integrating-geometric-deep-learning-models-into-paraview/)

---

<div class="post-metadata">

### Author: ![bagginstyrone](https://discourse.paraview.org/user_avatar/discourse.paraview.org/bagginstyrone/32/6405_2.png) [@bagginstyrone](https://discourse.paraview.org/u/bagginstyrone)
#### Post date: [May 13, 2022, 8:15am UTC](https://discourse.paraview.org/t/quick-guide-to-using-pytorch-in-paraview/9037/2 "2022-05-13T08:15:15Z")

</div>

This extensive guide has helped me enough to properly learn how to use PyTorch in the ParaView. The detailed instructions have given me a lot of help throughout the whole process and it has been quite fruitful for me.

---

<div class="post-metadata">

### Author: ![ltetrel](https://discourse.paraview.org/user_avatar/discourse.paraview.org/ltetrel/32/15636_2.png) [@ltetrel](https://discourse.paraview.org/u/ltetrel)
#### Post date: [March 3, 2023, 9:44am UTC](https://discourse.paraview.org/t/quick-guide-to-using-pytorch-in-paraview/9037/3 "2023-03-03T09:44:16Z")

</div>

It also worked on my side.

First, make sure to enable paraview web when compiling paraview before using this, because by default it is not enabled.  
`cmake -DPARAVIEW_ENABLE_WEB=ON`.

I created an issue regarding that: [https://gitlab.kitware.com/paraview/paraview/-/issues/21797](https://gitlab.kitware.com/paraview/paraview/-/issues/21797)

---

<div class="post-metadata">

### Author: ![Dai-ge](https://discourse.paraview.org/user_avatar/discourse.paraview.org/dai-ge/32/11690_2.png) [@Dai-ge](https://discourse.paraview.org/u/Dai-ge)
#### Post date: [May 27, 2023, 4:06am UTC](https://discourse.paraview.org/t/quick-guide-to-using-pytorch-in-paraview/9037/4 "2023-05-27T04:06:56Z")

</div>

It works on my size with python 3.9 and paraview 5.11.1 on Ubuntu 22.04  
For more specific details in the Cmake commands when building the paraview , add argument in cmake with -DPARAVIEW\_ENABLE\_WEB=ON -DPython3\_EXECUTABLE=/home/…/anaconda3/envs/your\_conda\_env/bin/python3.9, following the comment of Loic that paraview\_enable\_web is necessary.

Another thing need to be noticed is that when you use anaconda as your venv, you need to link the libstdc++.so.6 manually to you venv before you build the paraview.

sudo ln -sf /usr/lib/x86\_64-linux-gnu/libstdc++.so.6 /home/…/anaconda3/envs/paraview/lib/libstdc++.so.6

the command above works for me.
