spconvの古いVer1.2.1のインストールが苦労したメモ

本サイトはアフィリエイト広告を利用しています。
AI

3D物体検出でspconvのライブラリを使っていたところエラーが出ました。

元々参照していたのが古いVer1.xのライブラリでした。

何とか環境維持しつつ、エラー消すために再度ビルドした内容を紹介します。

 

スポンサーリンク

spconvの古いVer1.2.1のインストールが苦労したメモ

3D物体検出(Voxelnet、Second)の手法でspconvというライブラリがよく使われています。

LIDARなどのカメラ座標を変換する際に使用されています。

GitHub - traveller59/spconv: Spatial Sparse Convolution Library
Spatial Sparse Convolution Library. Contribute to traveller59/spconv development by creating an account on GitHub.

 

最新のVer2.xはpipでspconvインストールできます。

但し、古いの3D物体検出のコードだと古いspconv(Ver1.x)が利用されています。

ソースもあるので再度コンパイルすれば良いのですが、結果エラー出るケースも多々です。

 

実行環境

あくまで下記の環境の一例ですが、エラーと対応した内容を参考までに紹介します。

  • Vitis AI 2.5
  • spconv v1.2.1
  • 第6回AIエッジコンテストのリファレンス環境

 

第6回AIエッジコンテストのリファレンスをVitis AI 2.5の環境で動かそうとしました。

 https://github.com/pometa0507/6th-ai-reference2

SECONDという3D物体検出の手法がベースとなっています。

上記例では古いspconv(v1.2.1)が使われていました。

 

最終的にはFPGAで使えるようにしたく、Vitis AIのdocker上の環境でテストしています。

Vitis AIの環境については下記記事で紹介しています。

VItis AI 2.5やPetaLinuxなどインストールしてみたメモ

Vitis AI GPU dockerの環境構築をしたメモ

 

本当に個人的なメモです。

一応その当時はVitis AI上で古いspconvを実行出来ましたが、正しい手順かは不明です。

(CUDAのVerの整合も取れていないです)あくまで参考までにお願いします

 

Vitis AI上でspconv1.2.1をビルドするとエラー

まずは何も考えずに、Vitis AIのGPU版の環境下で古いspconvをビルドすると…

conda activate vitis-ai-pytorch
git clone https://github.com/pometa0507/6th-ai-reference2.git
cd 6th-ai-reference2/
git clone --depth 1 --recursive  -b v1.2.1 https://github.com/traveller59/spconv.git
pip install -r docker/requirements.txt
cd spconv/
SPCONV_FORCE_BUILD_CUDA=1 python setup.py bdist_wheel

 

下記のようにGPUのCUDAのMakeが無いよ、とエラーがでます。

~~~~~~~~~~~~~~~~~~~~~~~
CMake Error at CMakeLists.txt:6 (project):
  No CMAKE_CUDA_COMPILER could be found.

  Tell CMake where to find the compiler by setting either the environment
  variable "CUDACXX" or the CMake cache entry CMAKE_CUDA_COMPILER to the full
  path to the compiler, or to the compiler name if it is in the PATH.
~~~~~~~~~~~~~~~~~~~~~~~
  File "setup.py", line 91, in build_extension
    subprocess.check_call(['cmake', ext.sourcedir] + cmake_args, cwd=self.build_temp, env=env)
  File "/opt/vitis_ai/conda/envs/vitis-ai-pytorch/lib/python3.7/subprocess.py", line 363, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['cmake', '/workspace/6th-ai-reference2/spconv', '-DCMAKE_PREFIX_PATH=/opt/vitis_ai/conda/envs/vitis-ai-pytorch/lib/python3.7/site-packages/torch', '-DPYBIND11_PYTHON_VERSION=3.7', '-DSPCONV_BuildTests=OFF', '-DPYTORCH_VERSION=10701', '-DCMAKE_CUDA_FLAGS="--expt-relaxed-constexpr" -D__CUDA_NO_HALF_OPERATORS__ -D__CUDA_NO_HALF_CONVERSIONS__', '-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=/workspace/6th-ai-reference2/spconv/build/lib.linux-x86_64-3.7/spconv', '-DCMAKE_BUILD_TYPE=Release']' returned non-zero exit status 1.

 

これはVitis AIのデフォルトにはcuda-toolokitとかが入っていないからだと思います。

Vitis AIのConda環境が複雑な環境なためです。下記記事で一度紹介しています。

Vitis AIでPytorchのConda環境を新しく構築してみた

 

ただ、cuda-toolokitを入れなおして環境変数とか触っても下記のようなエラーが出ます。

GPUのmake環境整えるの少し面倒だな…と思って、別の手段を試してみました。

(本来は、使う環境でビルドするべきなのですが…)

-- Autodetected CUDA architecture(s):  7.5
-- Added CUDA NVCC flags for: -gencode;arch=compute_75,code=sm_75
CMake Error at /opt/vitis_ai/conda/envs/vitis-ai-pt1_7/lib/python3.7/site-packages/torch/share/cmake/Caffe2/Caffe2Config.cmake:96 (message):
  Your installed Caffe2 version uses cuDNN but I cannot find the cuDNN
  libraries.  Please set the proper cuDNN prefixes and / or install cuDNN.
Call Stack (most recent call first):
  /opt/vitis_ai/conda/envs/vitis-ai-pt1_7/lib/python3.7/site-packages/torch/share/cmake/Torch/TorchConfig.cmake:40 (find_package)
  CMakeLists.txt:22 (find_package)

 

新しいspconvをpipで入れてもエラー

新しいspconvを入れて試してもエラーになりました。pipで簡単に入れれます。

spconvのGithub上に各CUDAのVerに合わせたビルド済の環境が置かれています。

 https://github.com/traveller59/spconv

 

pipでインストールして、使おうとするとインポートでエラーになりました。

やはり古いspconvのソースファイルから少し弄る必要がありそうです。

ImportError: cannot import name 'non_max_suppression' from 'spconv.utils' 

  File "/workspace/6th-ai-reference2/second.pytorch/second/core/non_max_suppression/nms_gpu.py", line 8, in <module>
    from spconv.utils import non_max_suppression
ImportError: cannot import name 'non_max_suppression' from 'spconv.utils' (/opt/vitis_ai/conda/envs/vitis-ai-pt1_7/lib/python3.7/site-packages/spconv/utils/__init__.py)
(vitis-ai-pt1_7) Vitis-AI /workspace/6th-ai-reference2/notebook > 

 

 

Colab上でspconv1.2.1をビルドしたのを持ってくる

第6回AIエッジコンテストのリファレンスはColab上でも実施できました。

Colab上では簡単に古いspconvをビルド出来る環境が整っています。

 https://github.com/pometa0507/6th-ai-reference2/blob/master/notebook/colab/1-2_create_dataset_metadata_colab.ipynb

 

また2022年の秋ごろはColabのPythonのVerも3.7でした。

(Vitis AI上でデフォルトのCondaを入れ替えた環境でもPython3.7)

そのためColab上で一度ビルドしたものを、Vitis AI上に持っていき、動くのかテストしました。

(ただ本来CUDAのVerとか色々帳尻合わせないといけないと思います。)

 

※現在はColabのPythonのVer3.7から上がって、全く同じ手法は取れなくなっています。

おそらくPythonのVerを合わせれば同様なことは出来ると思います。

(再確認までは出来ていないです。すみません)

 

ソースファイルを修正する

その当時、単純にColabでビルドしたspconvをVitis AIに持っていってもエラーになりました。

インストールはできるのですが、pipの時と同様に下記エラーが出ました

ImportError: cannot import name 'non_max_suppression' from 'spconv.utils'

 

spconvのGithubの履歴を見て色々試したのですが、下記を直すと実行出来ました。

ビルド周辺の怪しいバグを手当たり次第、テストしていました。

BUG: bug fix that allows for a pip installation without the missing libcuhash.so error. Can now be uploaded to a pypi server by sioan · Pull Request #268 · traveller59/spconv
BUG: bug fix that allows for a pip installation without the missing libcuhash.so error. Can now be uploaded to a pypi se...

 

最終的には古いspconv(v1.2.1)のソースファイルに、上記修正を盛り込む形でした。

「setup.py」と「spconv/__init__.py」の2つのファイルを直しました。

数か所なので下記のようにColab上で対応した後に再ビルドしています。

 

Colab上での再度ビルド

その当時のColabでのビルドした履歴(Gist)も貼り付けておきます。

やっていることはリファレンスの手順と変わりません。

 

古いspconv(1.2.1)をインストール出来ました

何とかVitis AI環境上で古いspconv(1.2.1)がインストール出来ました。

(正直と色々と怪しいビルドなのですが…)

 

Successfully installed spconv-1.2.1

(vitis-ai-pytorch) Vitis-AI /workspace > conda activate vitis-ai-pt1_7
(vitis-ai-pt1_7) Vitis-AI /workspace > cd old-6th-ai-reference2/
(vitis-ai-pt1_7) Vitis-AI /workspace/old-6th-ai-reference2 > cd spconv/
(vitis-ai-pt1_7) Vitis-AI /workspace/old-6th-ai-reference2/spconv > pip install ./dist/spconv*.whl
Processing ./dist/spconv-1.2.1-cp37-cp37m-linux_x86_64.whl
Installing collected packages: spconv
Successfully installed spconv-1.2.1
(vitis-ai-pt1_7) Vitis-AI /workspace/old-6th-ai-reference2/spconv >

 

3D物体検出の評価を試しても結果はOK

念のためにデータセットを整えて、Vitis AI上でも3D物体検出の評価結果を確認します。

リファレンスの結果と同じになりました。

特にspconv関連でもエラー無しでした。とりあえずインストールがOKとします。

 https://github.com/pometa0507/6th-ai-reference2/blob/master/notebook/colab/1-4_inference_colab.ipynb

(vitis-ai-pt1_7) Vitis-AI /workspace/old-6th-ai-reference2/notebook > python ../second.pytorch/second/pytorch/train.py evaluate --config_path ../second.pytorch/second/configs/nuscenes/pointpainting.config --model_dir ../second.pytorch/checkpoints/pointpainting --measure_time=True --batch_size=4
[  41 1984  992]
Restoring parameters from /workspace/old-6th-ai-reference2/second.pytorch/checkpoints/pointpainting/voxelnet-24000.tckpt
feature_map_size [1, 248, 124]
Generate output labels...
[100.0%][===================>][4.57it/s][00:10>00:00]   
generate label finished(18.07/s). start eval:
avg example to torch time: 4.992 ms
avg prep time: 33.283 ms
avg voxel_feature_extractor time = 0.347 ms
avg middle forward time = 107.179 ms
avg rpn forward time = 49.485 ms
avg predict time = 23.398 ms
Evaluation nusc                                                                                             
Nusc v1.0-trainval Evaluation
car Nusc dist AP@0.5, 1.0, 2.0, 4.0 and TP errors
4.76, 6.25, 7.27, 7.33
trans_err, scale_err, orient_err, vel_err, attr_err: 0.2853, 0.2348, 2.4496, 1.0000, 0.9783
pedestrian Nusc dist AP@0.5, 1.0, 2.0, 4.0 and TP errors
0.00, 0.00, 0.00, 0.00
trans_err, scale_err, orient_err, vel_err, attr_err: 1.0000, 1.0000, 1.0000, 1.0000, 1.0000

 

Vitis AI上での実行したコマンド

Vitis AIのdocker上で実行したコマンドとしては下記となります。

(リファレンス参考にデータセットの前処理・学習などは実施済の環境です。)

    1  conda activate vitis-ai-pytorch
    2  wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-ubuntu1804.pin
    3  sudo mv cuda-ubuntu1804.pin /etc/apt/preferences.d/cuda-repository-pin-600
    #  wget https://developer.download.nvidia.com/compute/cuda/10.2/Prod/local_installers/cuda-repo-ubuntu1804-10-2-local-10.2.89-440.33.01_1.0-1_amd64.deb  
    4  sudo dpkg -i cuda-repo-ubuntu1804-10-2-local-10.2.89-440.33.01_1.0-1_amd64.deb
    5  sudo apt-key add /var/cuda-repo-10-2-local-10.2.89-440.33.01/7fa2af80.pub
    6  sudo apt-get update
    7  sudo apt-get install -y cuda-toolkit-10-2
    8  sh docker/dockerfiles/replace_pytorch.sh vitis-ai-pt1_7
    9  conda activate vitis-ai-pt1_7
   10  cd old-6th-ai-reference2/
   11  cd spconv/
   12  pip install ./dist/spconv*.whl
   13  cd ..
   14  pip install --user -r docker/requirements.txt
   15  cd notebook/
   16  python ../second.pytorch/second/pytorch/train.py evaluate --config_path ../second.pytorch/second/configs/nuscenes/pointpainting.config --model_dir ../second.pytorch/checkpoints/pointpainting --measure_time=True --batch_size=4

 

まとめ

古い3D物体検出を動かす時には古いspconvを使う機会があると思います。

ただGithubの履歴を見ながら、地道にエラーを消していくしかなかったです。

やはり可能な限り、新しい環境(mmdetection3dなど)を使いたいところです。

 

もし同様なエラー出ている方がいれば、ご参考にしてください。

 

今回の記事含めて、第6回AIエッジコンテストの一環でした。

下記にてコンテストでテスト・実施したことのまとめを紹介しています。

第6回AIエッジコンテストが凄く難しいけど勉強になった感想

第6回AIエッジコンテストが凄く難しいけど勉強になった感想
第6回AIエッジコンテストに参加していました。 RISC-Vを使って、画像とLIDAR点群からの3D物体検出が課題でした。 ただ貴重な勉強の機会になりましたし、自身への忘備録としても内容を紹介します。

 

コメント