私的AI研究会 > PyTorch4
「物体検出と学習済みモデル」一般的に公開データセットで学習された AIを使う。
PyTorchで利用できる公開済みのモデルを流用して監視カメラアプリを作成する。
(py37) $ mkdir ~/workspace_py37/chapter04 (py37) $ cd ~workspace_py37/chapter04/ (py37) $ cp ../sample/chapt04/chapt04_1.py ./chapt04_1a.py
(py37) $ vi chapt04_1a.py # -*- coding: utf-8 -*- ##------------------------------------------ ## 「PyTorch で始める AI開発」 ## Chapter 04 / Section 010 ## PyTorch による学習 ## ## 2021.09.14 Masahiro Izutsu ##------------------------------------------ ## chapt04_1a.py (original: chapt04_1.py) import numpy as np import pandas as pd : # GPUを使うかどうか USE_DEVICE = 'cuda:0' if torch.cuda.is_available() else 'cpu' # データのあるディレクトリ #dataset = 'forest-path-movie-dataset' dataset = '../forest-path-movie-dataset/' # 2021.09.14 :
: results = model([filename], size=640) # 640pixelで認識する bbox = results.xyxy[0].detach().cpu().numpy() pred = 0 # 人物を認識したかどうか for preds in bbox: type = int(preds[5]) # 0~3が位置、4が可能性、5がクラス if type == 0: # type=0が'preson' pred = 1 # 人物を認識した break :
(py37) $ python3 chapt04_1a.py Downloading: "https://github.com/ultralytics/yolov5/archive/master.zip" to /home/mizutu/.cache/torch/hub/master.zip Traceback (most recent call last): : ModuleNotFoundError: No module named 'seaborn'
(py37) $ python3 chapt04_1a.py Downloading: "https://github.com/ultralytics/yolov5/archive/master.zip" to /home/mizutu/.cache/torch/hub/master.zip Matplotlib is building the font cache; this may take a moment. Downloading https://ultralytics.com/assets/Arial.ttf to /home/mizutu/.config/Ultralytics/Arial.ttf... 100%|████████████████████████████████████████| 755k/755k [00:00<00:00, 27.5MB/s] YOLOv5 🚀 2021-9-14 torch 1.8.2+cpu CPU Downloading https://github.com/ultralytics/yolov5/releases/download/v5.0/yolov5s.pt to /home/mizutu/.cache/torch/hub/ultralytics_yolov5_master/yolov5s.pt... 100%|██████████████████████████████████████| 14.1M/14.1M [00:00<00:00, 32.3MB/s] Fusing layers... Model Summary: 224 layers, 7266973 parameters, 0 gradients Adding AutoShape... AutoShape already enabled, skipping... 0%| | 0/7352 [00:00<?, ?it/s][W NNPACK.cpp:80] Could not initialize NNPACK! Reason: Unsupported hardware. 100%|███████████████████████████████████████| 7352/7352 [24:54<00:00, 4.92it/s] precision recall f1-score support 0 0.75 0.95 0.84 2675 1 0.97 0.82 0.89 4677 accuracy 0.87 7352 macro avg 0.86 0.89 0.87 7352 weighted avg 0.89 0.87 0.87 7352
(py37) $ cd ~workspace_py37/chapter04/ (py37) $ cp ../sample/chapt04/chapt04_2.py ./chapt04_2a.py
# -*- coding: utf-8 -*- ##------------------------------------------ ## 「PyTorch で始める AI開発」 ## Chapter 04 / Section 011 ## PyTorch による学習 ## ## 2021.09.14 Masahiro Izutsu ##------------------------------------------ ## chapt04_2a.py (original: chapt04_2.py) import numpy as np : def __init__(self, parent, **params): Frame.__init__(self, parent, params) # # OpenCVのカメラキャプチャーを用意する # self.cap = cv2.VideoCapture(0) # 2021.09.14 #「forest path movie dataset」用のサンプルを利用する self.cap = cv2.VideoCapture('../sample/chapt02/chapt02-sample-a.mp4') :
(py37) $ python3 chapt04_2a.py : return self.conv(torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1)) RuntimeError: Sizes of tensors must match except in dimension 1. Got 2 and 1 in dimension 3 (The offending index is 2)
(py37) $ python3 chapt04_2a.py Using cache found in /home/mizutu/.cache/torch/hub/ultralytics_yolov5_master YOLOv5 🚀 2021-9-16 torch 1.8.2+cpu CPU Fusing layers... [W NNPACK.cpp:80] Could not initialize NNPACK! Reason: Unsupported hardware. Model Summary: 224 layers, 7266973 parameters, 0 gradients Adding AutoShape... AutoShape already enabled, skipping...
(py37) $ cd ~workspace_py37/chapter04/ (py37) $ cp ../sample/chapt04/chapt04_2a.py ./chapt04_2b.py (py37) $ vi chapt04_2b.py
(py37) $ python3 chapt04_2b.py -h Using cache found in /home/mizutu/.cache/torch/hub/ultralytics_yolov5_master YOLOv5 🚀 2021-9-16 torch 1.8.2+cpu CPU Fusing layers... [W NNPACK.cpp:80] Could not initialize NNPACK! Reason: Unsupported hardware. Model Summary: 224 layers, 7266973 parameters, 0 gradients Adding AutoShape... AutoShape already enabled, skipping... --- Surveillance camera on YoloV5 --- 4.5.2 usage: chapt04_2b.py [-h] [-i IMAGE_FILE] [-l LANGUAGE] [-t TITLE] [-s SPEED] [-o IMAGE_OUT] optional arguments: -h, --help show this help message and exit -i IMAGE_FILE, --image IMAGE_FILE Absolute path to image file or cam for camera stream. -l LANGUAGE, --language LANGUAGE Language.(jp/en) Default value is 'jp' -t TITLE, --title TITLE Program title flag.(y/n) Default value is 'y' -s SPEED, --speed SPEED Speed display flag.(y/n) Default calue is 'y' -o IMAGE_OUT, --out IMAGE_OUT Processed image file path. Default value is 'non'
(py37) $ python3 chapt04_2b.py Using cache found in /home/mizutu/.cache/torch/hub/ultralytics_yolov5_master YOLOv5 🚀 2021-9-16 torch 1.8.2+cpu CPU Fusing layers... [W NNPACK.cpp:80] Could not initialize NNPACK! Reason: Unsupported hardware. Model Summary: 224 layers, 7266973 parameters, 0 gradients Adding AutoShape... AutoShape already enabled, skipping... --- Surveillance camera on YoloV5 --- 4.5.2 Surveillance camera on YoloV5: Starting application... - Image File : ../sample/chapt02/chapt02-sample-a.mp4 - Language : jp - Program Title: y - Speed flag : y - Processed out: non FPS average: 5.20 Finished.
(py37) $ git clone https://github.com/ultralytics/yolov5.git Cloning into 'yolov5'... remote: Enumerating objects: 9335, done. remote: Counting objects: 100% (26/26), done. remote: Compressing objects: 100% (20/20), done. remote: Total 9335 (delta 12), reused 13 (delta 6), pack-reused 9309 Receiving objects: 100% (9335/9335), 9.73 MiB | 20.76 MiB/s, done. Resolving deltas: 100% (6485/6485), done.
(py37) $ cd yolov5 (py37) $ vi requirements.txt # pip install -r requirements.txt # Base ---------------------------------------- matplotlib>=3.2.2 numpy>=1.18.5 #opencv-python>=4.1.2 Pillow>=8.0.0 PyYAML>=5.3.1 scipy>=1.4.1 torch>=1.7.0 torchvision>=0.8.1 tqdm>=4.41.0 :
(py37) $ cd ~/workspace_py37/chapter04/yolov5/ (py37) $ python3 detect.py --source 0 detect: weights=yolov5s.pt, source=0, imgsz=[640, 640], conf_thres=0.25, iou_thres=0.45, max_det=1000, device=, view_img=False, save_txt=False, save_conf=False, save_crop=False, nosave=False, classes=None, agnostic_nms=False, augment=False, visualize=False, update=False, project=runs/detect, name=exp, exist_ok=False, line_thickness=3, hide_labels=False, hide_conf=False, half=False YOLOv5 🚀 v5.0-430-gaa18599 torch 1.8.2+cpu CPU Fusing layers... [W NNPACK.cpp:80] Could not initialize NNPACK! Reason: Unsupported hardware. Model Summary: 224 layers, 7266973 parameters, 0 gradients QObject::moveToThread: Current thread (0x558a25f1c120) is not the object's thread (0x558a24532080). Cannot move to target thread (0x558a25f1c120) :
$ python detect.py --source 0 # Webカメラ file.jpg # イメージ file.mp4 # ビデオ path/ # フォルダ path/*.jpg # glob 'https://youtu.be/NUsoVlDFqZg' # YouTube 'http://example.com/media.mp4' # httpストリーム
(py37) $ cp -r ~/workspace_py37/sample/chapt04/testdata ./ (py37) $ cp ~/workspace_py37/sample/chapt04/testdata.yaml ./ (py37) $ ls CONTRIBUTING.md data models train.py Dockerfile detect.py requirements.txt tutorial.ipynb LICENSE export.py testdata utils README.md hubconf.py testdata.yaml val.py
(py37) $ python train.py --img 640 --batch 1 --epochs 20 --data testdata.yaml --weights yolov5s.pt :
(py37) $ ls ~/workspace_py37/chapter04/yolov5/runs/train/exp confusion_matrix.png train_batch0.jpg events.out.tfevents.1631733230.ubuntu-vbox.2603.0 train_batch1.jpg hyp.yaml train_batch2.jpg labels.jpg val_batch0_labels.jpg labels_correlogram.jpg val_batch0_pred.jpg opt.yaml val_batch1_labels.jpg results.csv val_batch1_pred.jpg results.png weights (py37) $ ls ~/workspace_py37/chapter04/yolov5/runs/train/exp/weights/ best.pt last.pt
上記の学習ではサンプルデータが 3つのテスト用なので正しい学習は行えない。
より多くののデータが用意できれば自前のデータで学習したモデルの使用ができる。
: import torch import sys # モデル定義 sys.path.append('yolov5') from models.yolo import Model :・学習したモデル情報を読み込む
# 学習した結果を読み込む ckpt = torch.load('yolov5/runs/train/exp/weights/best.pt') # パラメータを指定してモデルを作成する model = Model(ckpt['model'].yaml, ch=3, nc=1) # モデルの重みを復元する state_dict = ckpt['model'].float().state_dict() #model.load_state_dict(state_dict, strict=False, map_location=torch.device(USE_DEVICE)) model.load_state_dict(state_dict, strict=False)・モデルを実行する
model = model.autoshape() # 画像フォーマットを自動判断するように設定 # モデルを推論用に設定する model.eval() model.to(USE_DEVICE) : results = model(image, size=640) bbox = results.xyxy[0].detach().cpu().numpy()
(py37) $python3 chapt04_3a.py
(py37) $ python3 chapt04_1a.py Downloading: "https://github.com/ultralytics/yolov5/archive/master.zip" to /home/mizutu/.cache/torch/hub/master.zip Traceback (most recent call last): : ModuleNotFoundError: No module named 'seaborn'
(py37) $ python3 chapt04_2a.py : return self.conv(torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1)) RuntimeError: Sizes of tensors must match except in dimension 1. Got 2 and 1 in dimension 3 (The offending index is 2)
(py37) $ cd ~/run_app2 (py37) $ ./face_mask.sh [face_mask.sh] 'face_mask' Run !! DEVICE: CPU COMMAND: python3 face_mask.py -m_dt ./models/face-detection-adas-0001.xml -m_mk ./models/face_mask.xml -d CPU : QObject::moveToThread: Current thread (0x55c17e937720) is not the object's thread (0x55c17f1310f0). Cannot move to target thread (0x55c17e937720) :