Windows でYOLOv7をインストール,動作確認する手順は以下のとおりである.まず,pycocotoolsのインストールを行う.次に YOLOv7 のソースコードと学習済みモデルをダウンロードする.画像やカメラからの物体検出は,コマンドで可能である.このとき学習済みモデルを指定できる.姿勢推定は,公式ページで配布されているプログラムを利用できる.以上で,YOLOv7のインストールと動作確認ができる.Average PrecisionとAverage Recallの値を取得したいときは,COCO 2017データセットのダウンロード,展開し,test.pyを実行.詳細な手順は公式ドキュメントを参照.
【目次】
【文献】 Wang, Chien-Yao and Bochkovskiy, Alexey and Liao, Hong-Yuan Mark, YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors, arXiv preprint arXiv:2207.02696, 2022.
【関連する外部ページ】
Gitは,バージョン管理システム.ソースコードの管理や複数人での共同に役立つ.
【サイト内の関連ページ】
Windows での Git のインストール: 別ページ »で説明している.
【関連する外部ページ】
Git の公式ページ: https://git-scm.com/
【サイト内の関連ページ】
【関連する外部ページ】
Python の公式ページ: https://www.python.org/
【サイト内の関連ページ】
NVIDIA グラフィックスボードを搭載しているパソコンの場合には, NVIDIA ドライバ, NVIDIA CUDA ツールキット, NVIDIA cuDNN のインストールを行う.
【関連する外部ページ】
コマンドプロンプトを管理者として実行: 別ページ »で説明
PyTorch のページ: https://pytorch.org/index.html
次のコマンドは, PyTorch 2.0 (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 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())"
【サイト内の関連ページ】
【関連する外部ページ】
公式のGitHub のページ https://github.com/WongKinYiu/yolov7 に従ってインストールする.
コマンドプロンプトを管理者として実行: 別ページ »で説明
python -m pip install -U cython python -m pip install "git+https://github.com/philferriere/cocoapi.git#egg=pycocotools&subdirectory=PythonAPI"
cd %HOMEPATH% rmdir /s /q yolov7 git clone --recursive https://github.com/WongKinYiu/yolov7 cd yolov7 python -m pip install -r requirements.txt
参考ページ: https://github.com/WongKinYiu/yolov7
cd %HOMEPATH%\yolov7 curl -O -L https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7.pt curl -O -L https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7x.pt curl -O -L https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-w6.pt curl -O -L https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-e6.pt curl -O -L https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-d6.pt curl -O -L https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-e6e.pt curl -O -L https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-w6-pose.pt curl -O -L https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-mask.pt
Average Precision,Average Recall の値を確認する必要がない場合は,ここの手順は行う必要はない.
Windows での COCO 2017 のダウンロードと展開の手順は次の通り.
コマンドプロンプトを管理者として実行: 別ページ »で説明
7-Zipは,ファイルの圧縮や展開のツール.さまざまなフォーマットに対応している.
Windows では, コマンドプロンプトを管理者として開き, 次のコマンドを実行することにより, 7-Zip 23.01 のインストールを行うことができる.
mkdir %HOMEPATH%\7zip cd %HOMEPATH%\7zip curl -O https://www.7-zip.org/a/7z2301-x64.exe .\7z2301-x64.exe powershell -command "$oldpath = [System.Environment]::GetEnvironmentVariable(\"Path\", \"Machine\"); $oldpath += \";c:\Program Files\7-Zip\"; [System.Environment]::SetEnvironmentVariable(\"Path\", $oldpath, \"Machine\")"
コマンドプロンプトを管理者として開き ダウンロードと展開のため,次のコマンドを実行
cd %HOMEPATH% mkdir coco2017 mkdir coco2017\coco mkdir coco2017\coco\images cd %HOMEPATH%\coco2017 ECHO labels curl -O -L https://github.com/ultralytics/yolov5/releases/download/v1.0/coco2017labels-segments.zip ECHO 19G, 118k images curl -O http://images.cocodataset.org/zips/train2017.zip ECHO 1G, 5k images curl -O http://images.cocodataset.org/zips/test2017.zip ECHO 7G, 41k images (optional) curl -O http://images.cocodataset.org/zips/val2017.zip ECHO unlabeled curl -O http://images.cocodataset.org/zips/unlabeled2017.zip ECHO curl -O http://images.cocodataset.org/annotations/annotations_trainval2017.zip curl -O http://images.cocodataset.org/annotations/stuff_annotations_trainval2017.zip curl -O http://images.cocodataset.org/annotations/image_info_test2017.zip curl -O http://images.cocodataset.org/annotations/image_info_unlabeled2017.zip REM curl -O http://images.cocodataset.org/annotations/stuff_annotations_trainval2017.zip ECHO cd %HOMEPATH%\coco2017 "c:\Program Files\7-Zip\7z.exe" x coco2017labels-segments.zip cd %HOMEPATH%\coco2017\coco\images "c:\Program Files\7-Zip\7z.exe" x ..\..\train2017.zip "c:\Program Files\7-Zip\7z.exe" x ..\..\test2017.zip "c:\Program Files\7-Zip\7z.exe" x ..\..\val2017.zip "c:\Program Files\7-Zip\7z.exe" x ..\..\unlabeled2017.zip REM cd %HOMEPATH%\coco2017\coco REM "c:\Program Files\7-Zip\7z.exe" x ..\stuff_annotations_trainval2017.zip
└─%HOMEPATH%\coco2017\coco ├── annotations/ ├── captions_train2017.json ├── captions_val2017.json ├── image_info_test-dev2017.json ├── image_info_test2017.json ├── image_info_unlabeled2017.json ├── instances_train2017.json ├── instances_val2017.json ├── person_keypoints_train2017.json ├── person_keypoints_val2017.json ├── stuff_train2017.json └── stuff_val2017.json ├── images/ ├─test2017/ ├─train2017/ ├─unlabeled2017/ └─val2017/ ├── labels/ ├─train2017/ └─val2017/ ├─test-dev2017.txt ├─train2017.txt └─val2017.txt
【関連する外部ページ】
COCO データセットの公式ページ: https://cocodataset.org/#home
コマンドプロンプトを管理者として実行: 別ページ »で説明
cd %HOMEPATH%\yolov7 mklink /J coco ..\coco2017\coco
python test.py --data data/coco.yaml --img 640 --batch 32 --conf 0.001 --iou 0.65 --device 0 --weights yolov7.pt --name yolov7_640_val
画像ファイルについて物体検出を行い,確認表示する.
「source='https://media.roboflow.com/notebooks/examples/dog.jpeg'」のところは,ファイル名やURLを指定. yolov7.pt は COCO データセットで学習済みのモデルである. COCO データセットで学習済みの他のモデルもある. 詳しくは公式ページの https://github.com/WongKinYiu/yolov7
cd %HOMEPATH% cd yolov7 python detect.py --weights yolov7.pt --conf 0.25 --img-size 640 --source inference/images/horses.jpg
runs\detect\exp\horses.jpg
次のコマンドを実行.
python detect.py --weights yolov7.pt --conf 0.25 --img-size 1024 --source 0
https://github.com/WongKinYiu/yolov7/blob/main/tools/keypoint.ipynb に記載のプログラムを使用.
「image = cv2.imread('./person.jpg')」の行は画像ファイル名を設定する.
Python プログラムの実行
コマンドプロンプトで次を実行
cd %HOMEPATH%\yolov7 python
次の Python プログラムを実行する
https://github.com/WongKinYiu/yolov7/blob/main/tools/keypoint.ipynb に記載のプログラムを書き換えて使用.
import matplotlib.pyplot as plt import torch import cv2 from torchvision import transforms import numpy as np from utils.datasets import letterbox from utils.general import non_max_suppression_kpt from utils.plots import output_to_keypoint, plot_skeleton_kpts device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") weigths = torch.load('yolov7-w6-pose.pt', map_location=device) model = weigths['model'] _ = model.float().eval() if torch.cuda.is_available(): model.half().to(device) v = cv2.VideoCapture(0) while(v.isOpened()): r, f = v.read() if ( r == False ): break image = letterbox(f, 960, stride=64, auto=True)[0] image_ = image.copy() image = transforms.ToTensor()(image) image = torch.tensor(np.array([image.numpy()])) if torch.cuda.is_available(): image = image.half().to(device) output, _ = model(image) output = non_max_suppression_kpt(output, 0.25, 0.65, nc=model.yaml['nc'], nkpt=model.yaml['nkpt'], kpt_label=True) with torch.no_grad(): output = output_to_keypoint(output) nimg = image[0].permute(1, 2, 0) * 255 nimg = nimg.cpu().numpy().astype(np.uint8) nimg = cv2.cvtColor(nimg, cv2.COLOR_RGB2BGR) for idx in range(output.shape[0]): plot_skeleton_kpts(nimg, output[idx, 7:].T, 3) cv2.imshow("", nimg) if cv2.waitKey(1) & 0xFF == ord('q'): break v.release() cv2.destroyAllWindows()