Build Tips

OpenCVのBuild
環境
Docker Image: pytorch/pytorch:1.12.1-cuda11.3-cudnn8-devel
CUDA、opencv-contrib対応
apt-get install -y build-essential libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev \
python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev git
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
cd opencv/
mkdir build
cd build
cmake -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
-D CMAKE_BUILD_TYPE=Release \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_ENABLE_NONFREE=ON \
-D BUILD_opencv_python3=ON \
-D BUILD_opencv_python2=OFF \
-D WITH_CUDA=ON \
-D PYTHON_LIBRARIES=/usr/lib/x86_64-linux-gnu/libpython3.7m.so \
-D PYTHON3_INCLUDE_PATH=/usr/include/python3.7m \
-D PYTHON3_LIBRARIES=/usr/lib/x86_64-linux-gnu/libpython3.7m.so \
-D PYTHON3_EXECUTABLE=/opt/conda/bin/python3 \
-D PYTHON3_INCLUDE_DIR=/usr/include/python3.7m \
-D PYTHON3_PACKAGES_PATH=/opt/conda/lib/python3.7/site-packages/ \
-D PYTHON3_NUMPY_INCLUDE_DIRS=/opt/conda/lib/python3.7/site-packages/numpy/core/include/ \
-D PYTHON2_EXECUTABLE="" \
-D PYTHON2_INCLUDE_DIR="" \
-D PYTHON2_LIBRARY="" \
-D PYTHON2_PACKAGES_PATH="" \
-D HAVE_opencv_python3=ON \
-D BUILD_TIFF=ON \
..
make -j`nproc`
make install
cd python_loader
python3 setup.py install
python3 -c "import cv2; print(cv2.__version__)"
condaとOpenCVのビルドは相性悪そう
参考

ORB-SLAM2のエラー
参考にした記事
この中国語の記事を参考にする(最近英語より中国語の方が詳しい記事がありますね、翻訳して読みましょう)。
事象
rosrun ORB_SLAM2 Mono Vocabulary/ORBvoc.txt Examples/Monocular/TUM1.yaml
GTK+ 2.x symbols detected. Using GTK+ 2.x and GTK+ 3 in the same process is not supported
原因
ORB-SLAM2が使用しているライブラリの内、gtk2に依存するライブラリとgtk3に依存するライブラリがあり、同時に実行したことが原因。
これはおそらく複数バージョンのOpenCVをホストにインストールしたことに起因する。
ORB-SLAM2プロジェクト内のどのshared objectファイルがgtk2を使用しているか調べる
ORB-SLAM2プロジェクト内にある.so
ファイルを列挙する
$ find . -name *.so
それぞれの.so
ファイルの依存ライブラリを表示し、gtk関連のライブラリからgtk2に依存するライブラリを見つける
$ ldd liborbslam2.so | grep gtk
libgtk-x11-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0
libgtk-3.so.0 => /usr/lib/x86_64-linux-gnu/libgtk-3.so.0
この場合であればlibgtx-x11-2.0.so.0がgtk2に依存している。
libgtx-x11-2.0.so.0を使用しているライブラリを特定する
gtk2は描画ライブラリなので、描画関連のライブラリであるOpenCVが原因ではないかと推定する。
そこでliborbslam2.so内のOpenCV関連のライブラリを列挙して、libgtk-x11-2.0.so.0を使用しているライブラリを特定する。
まずliborbslam2.so内のOpenCV関連のライブラリを列挙する。
$ ldd liborbslam2.so | grep opencv
libopencv_calib3d.so.3.2 => /usr/lib/x86_64-linux-gnu/libopencv_calib3d.so.3.2
libopencv_core.so.3.2 => /usr/lib/x86_64-linux-gnu/libopencv_core.so.3.2
libopencv_features2d.so.3.2 => /usr/lib/x86_64-linux-gnu/libopencv_features2d.so.3.2
libopencv_highgui.so.3.2 => /usr/lib/x86_64-linux-gnu/libopencv_highgui.so.3.2
libopencv_imgproc.so.3.2 => /usr/lib/x86_64-linux-gnu/libopencv_imgproc.so.3.2
libopencv_video.so.3.2 => /usr/lib/x86_64-linux-gnu/libopencv_video.so.3.2
次に出力されたライブラリを1つづつ見ていき、libgtk-x11-2.0.so.0を使用しているライブラリを特定する。
$ ldd /usr/lib/x86_64-linux-gnu/libopencv_highgui.so.3.2 | grep gtk2
libgtk-x11-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0
libopencv_highgui.so.3.2が問題のライブラリであることがわかった。
解決策
ORB-SLAM2が使用しているOpenCVをアップデートし、gtk3に統一する。
まずOpenCVを取り除く。
sudo apt-get remove libopencv-core3.2
一度ORB-SLAM2のbuildディレクトリを削除し、build.sh
とbuild_ros.sh
を実行し、ビルドし直す。
build_ros.sh
実行時にcv_bridge
が不足するのであれば次のコマンドを実行する。
sudo apt-get install ros-melodic-cv-bridge

HEIC対応のImagemagickをDockerに構築する
下記の記事を参考にしました。
下記のDockerfileで生成されるイメージのサイズは1.9GBになります(大きすぎ)。
FROM debian:buster-slim
# Imagemagick Version
ARG VERSION=7.1.0-58
# for build-dep
RUN echo "deb-src http://deb.debian.org/debian buster main" >> /etc/apt/sources.list
RUN echo "deb-src http://deb.debian.org/debian-security buster/updates main" >> /etc/apt/sources.list
RUN echo "deb-src http://deb.debian.org/debian buster-updates main" >> /etc/apt/sources.list
RUN apt-get update
RUN apt-get install -y build-essential wget tar make sed autoconf libtool git-core
RUN apt-get install -y libwebp-dev libopenjp2-7-dev librsvg2-dev libde265-dev
RUN apt-get build-dep -y imagemagick libde265 libheif
WORKDIR /workspace
RUN git clone https://github.com/strukturag/libheif.git
# Default deb package repository provides an old libheif, so we should built it ourselves.
WORKDIR /workspace/libheif
RUN chmod +x ./autogen.sh && ./autogen.sh
RUN chmod +x ./configure && ./configure
RUN make -j`nproc` && make install
WORKDIR /workspace
RUN wget https://github.com/ImageMagick/ImageMagick/archive/refs/tags/${VERSION}.tar.gz
RUN tar -xvf ${VERSION}.tar.gz
WORKDIR /workspace/ImageMagick-${VERSION}
RUN chmod +x ./configure
RUn ./configure --with-heic=yes
RUN make -j`nproc` && make install
RUN ldconfig
WORKDIR /root
CMD ["/usr/bin/bash"]

AUTOMATIC1111/stable-diffusion-webuiインストール時にblendmodes==2022が見つからない
事象
AUTOMATIC1111/stable-diffusion-webuiをインストールしようと次のエラーが発生。
################################################################
Install script for stable-diffusion + Web UI
Tested on Debian 11 (Bullseye)
################################################################
################################################################
Running on yutashx user
################################################################
################################################################
Repo already cloned, using it as install directory
################################################################
################################################################
Create and activate python venv
################################################################
################################################################
Launching launch.py...
################################################################
Python 3.7.13 (default, Mar 29 2022, 02:18:16)
[GCC 7.5.0]
Commit hash: ea9bd9fc7409109adcd61b897abc2c8881161256
Installing torch and torchvision
WARNING: The directory '/home/yutashx/.cache/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you should use sudo's -H flag.
Looking in indexes: https://pypi.org/simple, https://download.pytorch.org/whl/cu117
Collecting torch==1.13.1+cu117
Downloading https://download.pytorch.org/whl/cu117/torch-1.13.1%2Bcu117-cp37-cp37m-linux_x86_64.whl (1801.9 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.8/1.8 GB 10.3 MB/s eta 0:00:00
Collecting torchvision==0.14.1+cu117
Downloading https://download.pytorch.org/whl/cu117/torchvision-0.14.1%2Bcu117-cp37-cp37m-linux_x86_64.whl (24.3 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 24.3/24.3 MB 7.9 MB/s eta 0:00:00
Collecting typing-extensions
Downloading typing_extensions-4.4.0-py3-none-any.whl (26 kB)
Collecting numpy
Downloading numpy-1.21.6-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (15.7 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 15.7/15.7 MB 9.0 MB/s eta 0:00:00
Collecting requests
Downloading requests-2.28.2-py3-none-any.whl (62 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 62.8/62.8 KB 23.4 MB/s eta 0:00:00
Collecting pillow!=8.3.*,>=5.3.0
Downloading Pillow-9.4.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.3 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.3/3.3 MB 9.4 MB/s eta 0:00:00
Collecting charset-normalizer<4,>=2
Downloading charset_normalizer-3.0.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (170 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 170.5/170.5 KB 14.0 MB/s eta 0:00:00
Collecting idna<4,>=2.5
Downloading idna-3.4-py3-none-any.whl (61 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 61.5/61.5 KB 345.4 MB/s eta 0:00:00
Collecting urllib3<1.27,>=1.21.1
Downloading urllib3-1.26.14-py2.py3-none-any.whl (140 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 140.6/140.6 KB 6.0 MB/s eta 0:00:00
Collecting certifi>=2017.4.17
Downloading certifi-2022.12.7-py3-none-any.whl (155 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 155.3/155.3 KB 9.8 MB/s eta 0:00:00
Installing collected packages: charset-normalizer, urllib3, typing-extensions, pillow, numpy, idna, certifi, torch, requests, torchvision
Successfully installed certifi-2022.12.7 charset-normalizer-3.0.1 idna-3.4 numpy-1.21.6 pillow-9.4.0 requests-2.28.2 torch-1.13.1+cu117 torchvision-0.14.1+cu117 typing-extensions-4.4.0 urllib3-1.26.14
WARNING: You are using pip version 22.0.4; however, version 23.0 is available.
You should consider upgrading via the '/workspace/stable-diffusion-webui/venv/bin/python3 -m pip install --upgrade pip' command.
Installing gfpgan
Installing clip
Installing open_clip
Cloning Stable Diffusion into repositories/stable-diffusion-stability-ai...
Cloning Taming Transformers into repositories/taming-transformers...
Cloning K-diffusion into repositories/k-diffusion...
Cloning CodeFormer into repositories/CodeFormer...
Cloning BLIP into repositories/BLIP...
Installing requirements for CodeFormer
Installing requirements for Web UI
Traceback (most recent call last):
File "launch.py", line 360, in <module>
prepare_environment()
File "launch.py", line 309, in prepare_environment
run_pip(f"install -r {requirements_file}", "requirements for Web UI")
File "launch.py", line 137, in run_pip
return run(f'"{python}" -m pip {args} --prefer-binary{index_url_line}', desc=f"Installing {desc}", errdesc=f"Couldn't install {desc}")
File "launch.py", line 105, in run
raise RuntimeError(message)
RuntimeError: Couldn't install requirements for Web UI.
Command: "/workspace/stable-diffusion-webui/venv/bin/python3" -m pip install -r requirements_versions.txt --prefer-binary
Error code: 1
stdout: <empty>
stderr: WARNING: The directory '/home/yutashx/.cache/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you should use sudo's -H flag.
ERROR: Could not find a version that satisfies the requirement blendmodes==2022 (from versions: 2020, 2020.1, 2020.2, 2020.2.1, 2020.2.2, 2020.3.0, 2021, 2021.1, 2021.2, 2021.3, 2021.3.1, 2021.3.2, 2021.3.3)
ERROR: No matching distribution found for blendmodes==2022
エラー原因の特定
blendmodesを配布しているpypiのサイトへ行き、メタデータを見たところ次のような記載を発見
メタデータ
ライセンス: MIT License (MIT)
作者: FredHappyface
必須: Python >=3.8, <4.0
自分が使用していたPythonは3.7だったので、使用していたPythonのバージョンが古いことが原因である。
解決策
Python3.8以降のPythonをインストールする。