LoginSignup
7
2

More than 1 year has passed since last update.

Detectron2をwindowsで動かす

Posted at

前置き

Detectron2というFacebookが開発したAIライブラリがいろいろできて使い勝手が良い。物体認識、インスタントセグメンテーション、キーポイント抽出と姿勢認識など。学習済みモデルが用意されているため、10枚程度のデータでも結構いい結果が得られる。アノテーションはCoco形式で用意すればよいので、CocoAnotatorなどが使える。

今までColabで動かしてたんですが、ColabではGPU制限に引っ掛かったりGPU性能が足りなくなりつつあったので、ローカルGPUを使うことに。当初は、Jupyterを立ち上げて、Colabのローカルランタイムを使ってたりもしたんですが、Windowsで直接使えたほうがよいなぁと。

detectron2-windowsというgithubもあったけど、最終更新が2年前で古い。要求されるcudaも古いので、入れるのがめんどう。いろいろ探していたら他にも二つほど、参考サイトを見つけた。

ここを参考にしたら最新cudaでdetectron2が動いたので以下にまとめおく。

前提

以下の設定・インストールが済んでいる

MSVCで入れたもの

実際の作業

detectron2という名前でAnaconda環境を作っていく。

conda create -n detectron2 python=3.7
conda activate detectron2
python -m pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu116
pip install cython
pip install pycocotools
git clone https://github.com/facebookresearch/detectron2.git

detectron2/setup.pyの80行目:extra_compile_args["nvcc"]に引数を1行追加する。

                "-D__CUDA_NO_HALF_CONVERSIONS__",
                "-D__CUDA_NO_HALF2_OPERATORS__",
+                "-DWITH_CUDA",
            ]
        else:
            define_macros += [("WITH_HIP", None)]

その後、detectron2をインストール。

pip install -e detectron2

これでセットアップ完了。

以下、テスト

pip install opencv-python
cd .\detectron2\tests
wget https://raw.githubusercontent.com/yogeshkumarpilli/detectron2/master/tests/test_window.py
python .\test_window.py
7
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
2