TheLostWanderer

Configure OpenCV and PyTorch on ArchLinux

Published on

Get OpenCV working

Lib for C++

  • Arch$ sudo pacman -S opencv vtk hdf5

For Python

== virtualenv ==

See:

Install using your package manager (for Arch): $ sudo pacman -S python-pipenv python-virtualenvwrapper

If not available: $ sudo pip3 install virtualenv virtualenvwrapper

== virtualenv and virtualenvwrapper ==

export WORKON_HOME=$HOME/.local/share/virtualenvswrapper
##export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper_lazy.sh

Create a virtual env to work on Python

mkvirtualenv cv4
pip install wheel numpy scipy matplotlib scikit-image scikit-learn ipython dlib opencv-contrib-python
deactivate

Useful commands: workon XXX / rmvirtualenv XXX -> from virtualenvwrapper

== pipenv ==

pipenv is made to build app. autoswtich is possible when moving to the project folder

  • $ sudo pacman -S python-pipenv

OR

  • $ pip install pipenv --user

You may have to update your path.

In the project folder:

$ pipenv install package will create a virtual env in this folder. Auto switch when moving to it.

Jupyter

jupyterlab is the “new” notebook, which is meant to replace it. At the moment, both are working fine and are compatible with each other. Documentation here

Install notebook: pip install notebook

Open a notebook: jupyter notebook mynotebook.ipynb

Install jupyterlab: pip install jupyterlab

Open a notebook: jupyter-lab mynotebook.ipynb

Gotchas

Matplotlib error while trying to display a figure

UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.

If you get this error, install tk as system package.

  • Ubuntu sudo apt install python3-tk
  • Arch: sudo pacman -S tk

Jupyter error

ImportError: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html

With pip. Otherwise check above link.

pip install ipywidgets jupyter_nbextensions_configurator
jupyter nbextension enable --py widgetsnbextension

Machine learning with pytorch

torch

pip install torch torchvision

pip other packages

pip install tensorboard

20201204 - torch does not support python3.9

Install python38 from AUR (or python37 or python36)

Create a new virtuel environment using the method of your choice and specifying the python version with the argument -p, ie mkvirtualenv opencv -p python3.8

detectron2

Install cuda first

sudo pacman -S cuda cudnn

get detectron2

Rebuild using latest cuda version (11.1 on 2020-12-04)

python -m pip install 'git+https://github.com/facebookresearch/detectron2.git' 

Install for pytorch 1.7 and cuda 11.0

python -m pip install detectron2 -f \
  https://dl.fbaipublicfiles.com/detectron2/wheels/cu110/torch1.7/index.htmlvv

Notes

Install packages:

pip freeze > requirements.txt
pip install -r requirements.txt