教師なし学習による物体検出とインスタンスセグメンテーション(CutLER,VideoCutLER,Detectron2, PyTorch, Python を使用)(Windows 上)
CutLER のインストールと動作確認を行う.
MaskCut は,画像から,複数のセグメンテーション・マスク (segmentation mask)を得る.ViT を使用
CutLER は,MaskCut の結果に対して物体検出を実行.教師なし学習と自己学習 (self-training) を使用.



【目次】
【文献】
Wang, Xudong and Girdhar, Rohit and Yu, Stella X and Misra, Ishan, Cut and Learn for Unsupervised Object Detection and Instance Segmentation, arXiv preprint arXiv:2301.11320, 2023.
【関連する外部ページ】
- GitHub のページ: https://github.com/facebookresearch/CutLER
- MaskCut の非公式のデモページ(HuggingFace 上)https://huggingface.co/spaces/facebook/MaskCut
前準備
Python 3.10,Git,CMake のインストール(Windows 上)
Pythonは,プログラミング言語の1つ. Gitは,分散型のバージョン管理システム. CMakeは,クロスプラットフォームのビルドシステム生成ツール.
【手順】
- Windows で,コマンドプロンプトを管理者権限で起動する(例:Windowsキーを押し,「cmd」と入力し,「管理者として実行」を選択)
次のコマンドを実行
次のコマンドは,Python ランチャーとPython 3.10とGitとCMakeをインストールし,Gitにパスを通すものである.
次のコマンドでインストールされるGitは 「git for Windows」と呼ばれるものであり, Git,MinGW などから構成されている.
winget install --scope machine Python.Launcher winget install --scope machine Python.Python.3.10 winget install --scope machine Git.Git powershell -command "$oldpath = [System.Environment]::GetEnvironmentVariable(\"Path\", \"Machine\"); $oldpath += \";c:\Program Files\Git\cmd\"; [System.Environment]::SetEnvironmentVariable(\"Path\", $oldpath, \"Machine\")" winget install --scope machine Kitware.CMake powershell -command "$oldpath = [System.Environment]::GetEnvironmentVariable(\"Path\", \"Machine\"); $oldpath += \";c:\Program Files\CMake\bin\"; [System.Environment]::SetEnvironmentVariable(\"Path\", $oldpath, \"Machine\")"
【関連する外部ページ】
- Python の公式ページ: https://www.python.org/
- Git の公式ページ: https://git-scm.com/
- CMake の公式ダウンロードページ: https://cmake.org/download/
【サイト内の関連ページ】
【関連項目】 Python, Git バージョン管理システム, Git の利用, CMake ビルドシステム生成ツール, CMake の使用方法
Build Tools for Visual Studio 2022,NVIDIA ドライバ,NVIDIA CUDA ツールキット 11.8,NVIDIA cuDNN 8.9.7 のインストール(Windows 上)
【サイト内の関連ページ】 NVIDIA グラフィックスボードを搭載しているパソコンの場合には, NVIDIA ドライバ, NVIDIA CUDA ツールキット, NVIDIA cuDNN のインストールを行う.
- Windows での Build Tools for Visual Studio 2022 のインストール: 別ページ »で説明
- Windows での NVIDIA ドライバ,NVIDIA CUDA ツールキット 11.8,NVIDIA cuDNN v8.9.7 のインストール手順: 別ページ »で説明
【関連する外部ページ】
- Build Tools for Visual Studio 2022 (ビルドツール for Visual Studio 2022)の公式ダウンロードページ: https://visualstudio.microsoft.com/ja/visual-cpp-build-tools/
- NVIDIA ドライバのダウンロードの公式ページ: https://www.nvidia.co.jp/Download/index.aspx?lang=jp
- NVIDIA CUDA ツールキットのアーカイブの公式ページ: https://developer.nvidia.com/cuda-toolkit-archive
- NVIDIA cuDNN のダウンロードの公式ページ: https://developer.nvidia.com/cudnn
PyTorch のインストール(Windows 上)
- Windows で,コマンドプロンプトを管理者権限で起動する(例:Windowsキーを押し,「cmd」と入力し,「管理者として実行」を選択)
- PyTorch のページを確認
- 次のようなコマンドを実行(実行するコマンドは,PyTorch のページの表示されるコマンドを使う).
次のコマンドを実行することにより, PyTorch 2.3 (NVIDIA CUDA 11.8 用)がインストールされる. 但し,Anaconda3を使いたい場合には別手順になる.
事前に NVIDIA CUDA のバージョンを確認しておくこと(ここでは,NVIDIA CUDA ツールキット 11.8 が前もってインストール済みであるとする).
PyTorch で,GPU が動作している場合には,「torch.cuda.is_available()」により,True が表示される.
python -m pip install -U --ignore-installed pip python -m pip uninstall -y torch torchvision torchaudio torchtext xformers python -m pip install -U torch torchvision torchaudio numpy --index-url https://download.pytorch.org/whl/cu118 python -c "import torch; print(torch.__version__, torch.cuda.is_available())"
Anaconda3を使いたい場合には, Anaconda プロンプト (Anaconda Prompt) を管理者として実行し, 次のコマンドを実行する. (PyTorch と NVIDIA CUDA との連携がうまくいかない可能性があるため,Anaconda3を使わないことも検討して欲しい).
conda install -y pytorch torchvision torchaudio pytorch-cuda=11.8 cudnn -c pytorch -c nvidia py -c "import torch; print(torch.__version__, torch.cuda.is_available())"
【サイト内の関連ページ】
【関連する外部ページ】
Python の opencv-python のインストール
- Windows で,コマンドプロンプトを管理者権限で起動する(例:Windowsキーを押し,「cmd」と入力し,「管理者として実行」を選択)
- opencv-python のインストール
* 「pip install ...」は,Python パッケージをインストールするための操作
python -m pip install -U opencv-python opencv-contrib-python
Detectron2 のインストール(Windows 上)
pycocotools のインストール(Windows 上)
pycocotools のインストールを行う.
- Windows で,コマンドプロンプトを管理者権限で起動する(例:Windowsキーを押し,「cmd」と入力し,「管理者として実行」を選択)
- pycocotools, COCO 2018 Panoptic Segmentation Task API のインストール
COCOデータセットをPythonで扱うためには,pycocotools を使う. pycocotools を使うことで,COCOデータセットのアノテーションを扱いやすくなる.
次のコマンドを実行
python -m pip install -U --ignore-installed pip python -m pip install -U cython wheel python -m pip install "git+https://github.com/philferriere/cocoapi.git#egg=pycocotools&subdirectory=PythonAPI" python -m pip install git+https://github.com/cocodataset/panopticapi.git

Detectron2 のインストール(Windows 上)
次の記事に記載の手順による.
https://dgmaxime.medium.com/how-to-easily-install-detectron2-on-windows-10-39186139101c
- Windows で,コマンドプロンプトを管理者権限で起動する(例:Windowsキーを押し,「cmd」と入力し,「管理者として実行」を選択)
- Detectron2 のダウンロード
python -m pip uninstall -y detectron2 cd /d c:%HOMEPATH% rmdir /s /q detectron2 git clone --recursive https://github.com/facebookresearch/detectron2 cd detectron2
- ソースコードの書き換え
notepad detectron2\layers\csrc\nms_rotated\nms_rotated_cuda.cu
次のように書き換える
- インストール
cd /d c:%HOMEPATH%\detectron2 python -m pip install -e .
(以下省略) - 引き続き,COCO 2018 Panoptic Segmentation Task API,
The Cityscapes Dataset 用スクリプトのインストール
pip install git+https://github.com/cocodataset/panopticapi.git pip install git+https://github.com/mcordts/cityscapesScripts.git
CutLER のインストール(Windows 上)
PyDenseCRF のインストール
【文献】 Efficient Inference in Fully Connected CRFs with Gaussian Edge Potentials Philipp Krähenbühl and Vladlen Koltun NIPS 2011
【関連する外部ページ】
- GitHub の公式のページ: https://github.com/lucasb-eyer/pydensecrf
- Windows で,コマンドプロンプトを管理者権限で起動する(例:Windowsキーを押し,「cmd」と入力し,「管理者として実行」を選択)
- インストール
次のコマンドを実行.
「Error compiling Cython file: from eigen cimport * pydensecrf\densecrf.pxd:1:0: 'eigen.pxd' not found」の エラーメッセージの回避のため「copy pydensecrf\eigen.pxd .」を実行している.
python -m pip install -U eigen cython cd /d c:%HOMEPATH% rmdir /s /q pydensecrf git clone https://github.com/lucasb-eyer/pydensecrf.git cd pydensecrf copy pydensecrf\eigen.pxd . python setup.py build python setup.py develop python setup.py install
PyDenseCRF のインストールで, 上のコマンドでエラーメッセージが出る場合には,次を実行
- 「Geospatial library wheels for Python on Windows」のページを開く
- Python のバージョンにあう「win_amd64.whl」のファイルをダウンロード.Python 3.10 を使っている場合には,pydensecrf‑1.0rc2‑cp310‑cp310‑win_amd64.whl をダウンロード
- 次のコマンドでインストール.「pydensecrf-1.0rc2-cp310-cp310-win_amd64.whl」のところは,実際にダウンロードしたファイル名に置き換える.
python -m pip install pydensecrf-1.0rc2-cp310-cp310-win_amd64.whl
- 動作確認
次の Python プログラムを実行.エラーメッセージは出ないことを確認.
import numpy as np import pydensecrf.densecrf as dcrf d = dcrf.DenseCRF2D(640, 480, 5) print(d)
CutLER のインストール(Windows 上)
- Windows で,コマンドプロンプトを管理者権限で起動する(例:Windowsキーを押し,「cmd」と入力し,「管理者として実行」を選択)
- CutLER のインストール
次のコマンドを実行.
cd /d c:%HOMEPATH% rmdir /s /q CutLER git clone --recursive https://github.com/facebookresearch/CutLER.git cd CutLER python -m pip install -U faiss_cpu python -m pip install -U opencv-python scikit-image scikit-learn shapely timm pyyaml colored fvcore gdown
MaskCut による画像セグメンテーション
公式ページの https://github.com/facebookresearch/CutLER の手順により行う.
次のコマンドの実行により,demo.jpg ができる.
cd /d c:%HOMEPATH%
cd CutLER
cd maskcut
python demo.py --img-path imgs/demo2.jpg --N 3 --tau 0.15 --vit-arch base --patch-size 8 --cpu --out-dir .
demo.jpg

実行結果

元画像

CutLER を用いた教師無し学習のゼロショットの物体検出とインスタンスセグメンテーション
公式ページの https://github.com/facebookresearch/CutLER の手順により行う.
前準備として学習済みモデルのダウンロード,ソースコードの修正を行う
- 次のコマンドの実行により,学習済みモデルのダウンロードを行う.
cd /d c:%HOMEPATH% cd CutLER cd cutler curl -L -O http://dl.fbaipublicfiles.com/cutler/checkpoints/cutler_cascade_final.pth
- 次に,data\transforms\transform.py の中の「LINEAR」を「BILINEAR」に書き換える
cd /d c:%HOMEPATH% cd CutLER cd cutler notepad data\transforms\transform.py
ゼロショットの物体検出とインスタンスセグメンテーション
- 画像について,ゼロショットの物体検出とインスタンスセグメンテーションを行う
cd /d c:%HOMEPATH% cd CutLER cd cutler python demo/demo.py --config-file model_zoo/configs/CutLER-ImageNet/cascade_mask_rcnn_R_50_FPN_demo.yaml --input demo/imgs/*.jpg --opts MODEL.WEIGHTS cutler_cascade_final.pth
- ビデオについて,ゼロショットの物体検出とインスタンスセグメンテーションを行う
1-1.avi のとこどにはビデオファイル名を指定すること.
cd /d c:%HOMEPATH% cd CutLER cd cutler python demo/demo.py --config-file model_zoo/configs/CutLER-ImageNet/cascade_mask_rcnn_R_50_FPN_demo.yaml --video-input 1-1.avi --opts MODEL.WEIGHTS cutler_cascade_final.pth
- webcam
cd /d c:%HOMEPATH% cd CutLER cd cutler python demo/demo.py --config-file model_zoo/configs/CutLER-ImageNet/cascade_mask_rcnn_R_50_FPN_demo.yaml --webcam --opts MODEL.WEIGHTS cutler_cascade_final.pth