私的AI研究会 > PyTorch2
「使用シーンが限定される AI」を PyTorch を使って作成し機械学習の手順を実行してみる。
PC に接続したカメラデバイスを定点カメラとして運用して、カメラの視界に人物が写っているかを判定する AI を作成する。
ニューラルネットワークのトレーニング(学習)と、作成したニューラルネットワーク(学習済みモデル)を使用してカメラからの映像をリアルタイムで認識するプログラムを作成する。
下記の場所からダウンロードできる。
● Kaggle からダウンロードするには、Kaggle のアカウントを作成する必要がある。
● https://www.kaggle.com/ にアクセスしてアカウント登録後上記サイトにアクセスする。
このデータセットについて
(py37) $ cd ~/workspace_py37 (py37) $ mkdir forest-path-movie-dataset (py37) $ cd forest-path-movie-dataset (py37) $ mv ~/ダウンロード/archive.zip ./ (py37) $ unzip archive.zip (py37) $ ls all_file.csv archive.zip scene.csv scenes (py37) $ ls scenes scene-001 scene-016 scene-031 scene-046 scene-061 scene-076 scene-091 scene-002 scene-017 scene-032 scene-047 scene-062 scene-077 scene-092 scene-003 scene-018 scene-033 scene-048 scene-063 scene-078 scene-093 scene-004 scene-019 scene-034 scene-049 scene-064 scene-079 scene-094 scene-005 scene-020 scene-035 scene-050 scene-065 scene-080 scene-095 scene-006 scene-021 scene-036 scene-051 scene-066 scene-081 scene-096 scene-007 scene-022 scene-037 scene-052 scene-067 scene-082 scene-097 scene-008 scene-023 scene-038 scene-053 scene-068 scene-083 scene-098 scene-009 scene-024 scene-039 scene-054 scene-069 scene-084 scene-099 scene-010 scene-025 scene-040 scene-055 scene-070 scene-085 scene-100 scene-011 scene-026 scene-041 scene-056 scene-071 scene-086 scene-012 scene-027 scene-042 scene-057 scene-072 scene-087 scene-013 scene-028 scene-043 scene-058 scene-073 scene-088 scene-014 scene-029 scene-044 scene-059 scene-074 scene-089 scene-015 scene-030 scene-045 scene-060 scene-075 scene-090
(py37) $ mkdir ~/workspace_py37/chapter02
著者の用意したデータセットを利用することで、下記の時間のかかる作業を省略した。
$ ffmpeg -ss 30 -i movie1.mp4 -t 10 scene001.mp4
$ mkdir scene-001 $ ffmpeg -i scene001.mp4 -f image2 scene-001/%03d.jpg
$ vi all_file.csv file,scene,person,when scenes/scene-001/000.jpg,scene-001,0,daytime scenes/scene-001/001.jpg,scene-001,0,daytime scenes/scene-001/002.jpg,scene-001,0,daytime scenes/scene-001/003.jpg,scene-001,0,daytime :
(py37) $ cd ~/workspace_py37/chapter02/ (py37) $ cp ../sample/chapt02/chapt02_1.py ./chapt02_1a.py (py37) $ ls chapt02_1a.py
(py37) $ vi chapt02_1a.py # -*- coding: utf-8 -*- ##------------------------------------------ ## 「PyTorch で始める AI開発」 ## Chapter 02 / Section 005 ## PyTorch による学習 ## ## 2021.09.05 Masahiro Izutsu ##------------------------------------------ ## chapt02_1a.py (original: chapt02_1.py) import numpy as np import pandas as pd import itertools : # GPUを使うかどうか USE_DEVICE = 'cuda:0' if torch.cuda.is_available() else 'cpu' # データがあるディレクトリ #INPUT_DIR = 'forest-path-movie-dataset/' INPUT_DIR = '../forest-path-movie-dataset/' ## 2021.09.05 :
: # 学習時と評価時のバッチサイズ BATCH_SIZE = 16 BATCH_SIZE_VALID = 4 # データの読み込みスレッドの数 NUM_WORKERS = 2 # 試行時の学習エポック数 NUM_EPOCHS = 3 # 評価で試す学習率 LR_TESTS = [1e-3,2e-4,5e-5] # 試すウェイトは、人物の方が分散が大きいので、クラス1側を0.5より少なくする WEIGHT_TESTS = [0.1,0.2,0.3,0.4,0.5] :
※ 畳み込みニューラルネットワークの全結合層は、使用するモデルによって入力数や変数の名前が異なる。
モデルの入力数や名前は TorchVision のソースコード を参照のこと。
(py37) $ python3 chapt02_1a.py Downloading: "https://download.pytorch.org/models/resnet50-19c8e357.pth" to /home/mizutu/.cache/torch/hub/checkpoints/resnet50-19c8e357.pth 100%|██████████████████████████████████████| 97.8M/97.8M [00:02<00:00, 37.0MB/s] test #0 lr=0.001 weight=0.1 epoch #0: train_loss:0.2363504022359848 valid_loss:0.5292516350746155 score:{'daytime': 0.9271425043220548, 'twilight': 0.6267166042446941, 'midnight': 0.8371361132966169, 'total': 0.8636363636363635, 'average': 0.6363470319634703} : :
test | ir | weight | epoch | train_loss | valid_loss | score | ||||
daytime | twilight | midnight | total | average | ||||||
#0 | 0.001 | 0.1 | #0 | 0.2364 | 0.5293 | 0.9271 | 0.6267 | 0.8371 | 0.8636 | 0.6363 |
#1 | 0.1103 | 0.8016 | 0.7756 | 0.6842 | 0.9072 | 0.8105 | 0.7434 | |||
#2 | 0.0990 | 0.9960 | 0.8125 | 0.5770 | 0.9149 | 0.8242 | 0.7653 | |||
#1 | 0.001 | 0.2 | #0 | 0.2872 | 0.3695 | 0.8881 | 0.6651 | 0.9159 | 0.8747 | 0.7262 |
#1 | 0.1352 | 0.7838 | 0.8192 | 0.7269 | 0.9098 | 0.8398 | 0.7833 | |||
#2 | 0.0989 | 1.0708 | 0.8182 | 0.6981 | 0.9146 | 0.8379 | 0.7742 | |||
#2 | 0.001 | 0.3 | #0 | 0.3019 | 0.3734 | 0.9031 | 0.6618 | 0.9529 | 0.8964 | 0.7288 |
#1 | 0.1490 | 1.0838 | 0.8097 | 0.6520 | 0.9305 | 0.8347 | 0.7868 | |||
#2 | 0.1064 | 1.2363 | 0.8180 | 0.6051 | 0.9048 | 0.8241 | 0.7752 | |||
#3 | 0.001 | 0.4 | #0 | 0.2919 | 0.6846 | 0.8287 | 0.6301 | 0.9120 | 0.8377 | 0.7640 |
#1 | 0.1367 | 0.8502 | 0.8230 | 0.7028 | 0.9618 | 0.8587 | 0.7909 | |||
#2 | 0.1069 | 1.2713 | 0.8124 | 0.6369 | 0.9326 | 0.8359 | 0.7918 | |||
#4 | 0.001 | 0.5 | #0 | 0.2636 | 0.5122 | 0.8365 | 0.6950 | 0.9023 | 0.8464 | 0.8093 |
#1 | 0.1329 | 0.7969 | 0.8013 | 0.6094 | 0.9519 | 0.8358 | 0.7521 | |||
#2 | 0.0988 | 0.5957 | 0.7949 | 0.7269 | 0.9501 | 0.8411 | 0.7763 | |||
#5 | 0.0002 | 0.1 | #0 | 0.3702 | 0.4491 | 0.9492 | 0.4980 | 0.8862 | 0.8779 | 0.5604 |
#1 | 0.1707 | 0.5217 | 0.8722 | 0.5951 | 0.9050 | 0.8556 | 0.7003 | |||
#2 | 0.1280 | 0.5248 | 0.8477 | 0.5987 | 0.9132 | 0.8455 | 0.7297 | |||
#6 | 0.0002 | 0.2 | #0 | 0.4198 | 0.3595 | 0.9570 | 0.5725 | 0.9143 | 0.8987 | 0.5858 |
#1 | 0.1922 | 0.6013 | 0.8128 | 0.6217 | 0.9338 | 0.8355 | 0.7637 | |||
#2 | 0.1418 | 0.4839 | 0.8427 | 0.6529 | 0.9570 | 0.8645 | 0.7140 | |||
#7 | 0.0002 | 0.3 | #0 | 0.4340 | 0.3080 | 0.9749 | 0.5977 | 0.9069 | 0.9079 | 0.5987 |
#1 | 0.1956 | 0.6520 | 0.8160 | 0.6778 | 0.9460 | 0.8462 | 0.7885 | |||
#2 | 0.1433 | 0.5778 | 0.8214 | 0.6553 | 0.9409 | 0.8462 | 0.7397 | |||
#8 | 0.0002 | 0.4 | #0 | 0.4266 | 0.2868 | 0.9634 | 0.6075 | 0.9451 | 0.9179 | 0.6457 |
#1 | 0.1979 | 0.6624 | 0.8186 | 0.6570 | 0.9425 | 0.8446 | 0.7832 | |||
#2 | 0.1425 | 0.5556 | 0.8098 | 0.6387 | 0.9582 | 0.8455 | 0.7145 | |||
#9 | 0.0002 | 0.5 | #0 | 0.4089 | 0.2883 | 0.9430 | 0.6222 | 0.9555 | 0.9120 | 0.6080 |
#1 | 0.1903 | 0.6060 | 0.8101 | 0.6792 | 0.9477 | 0.8437 | 0.7839 | |||
#2 | 0.1368 | 0.5368 | 0.7882 | 0.6556 | 0.9598 | 0.8364 | 0.7388 | |||
#10 | 5e-05 | 0.1 | #0 | 0.4840 | 1.1392 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 |
#1 | 0.4038 | 0.8005 | 0.6384 | 0.3294 | 0.5691 | 0.5747 | 0.2911 | |||
#2 | 0.3025 | 0.5715 | 0.8955 | 0.3789 | 0.8248 | 0.8155 | 0.4921 | |||
#11 | 5e-05 | 0.2 | #0 | 0.6004 | 0.7843 | 0.3683 | 0.2360 | 0.4609 | 0.3817 | 0.2079 |
#1 | 0.4419 | 0.4972 | 0.8996 | 0.4021 | 0.8267 | 0.8173 | 0.5202 | |||
#2 | 0.3042 | 0.3926 | 0.9357 | 0.5370 | 0.8837 | 0.8726 | 0.5547 | |||
#12 | 5e-05 | 0.3 | #0 | 0.6308 | 0.6395 | 0.7414 | 0.3072 | 0.8011 | 0.7050 | 0.4411 |
#1 | 0.4462 | 0.4217 | 0.9071 | 0.4246 | 0.8745 | 0.8339 | 0.5925 | |||
#2 | 0.3083 | 0.3457 | 0.9454 | 0.5681 | 0.8962 | 0.8852 | 0.5730 | |||
#13 | 5e-05 | 0.4 | #0 | 0.6199 | 0.5721 | 0.8675 | 0.4716 | 0.9032 | 0.8262 | 0.6135 |
#1 | 0.4384 | 0.3947 | 0.9005 | 0.5246 | 0.8873 | 0.8431 | 0.6363 | |||
#2 | 0.3058 | 0.3231 | 0.9396 | 0.5721 | 0.9142 | 0.8890 | 0.5929 | |||
#14 | 5e-05 | 0.5 | #0 | 0.5846 | 0.5441 | 0.8328 | 0.7121 | 0.8860 | 0.8349 | 0.8163 |
#1 | 0.4284 | 0.3733 | 0.8775 | 0.5929 | 0.9288 | 0.8545 | 0.6957 | |||
#2 | 0.3038 | 0.3107 | 0.9331 | 0.6041 | 0.9247 | 0.8903 | 0.6111 |
機種 | 開始日時 | 終了日時 | 処理時間 (h:m) |
GeForce GTX 1050 Ti Intel® Core™ i7-6700 | 10/12 19:01 | 10/13 00:09 | 05:08 |
DELL Latitude 7520 Intel® Core™ i7-1185G7 CPU | 09/05 14:35 | 09/07 02:02 | 35:27 |
HP EliteDesk 800 Intel® Core™ i7-6700 CPU | 09/05 11:00 | 09/07 05:15 | 41:55 |
(py37) $ ls -l 合計 92172 -rw-rw-r-- 1 mizutu mizutu 94368495 9月 7 05:15 chapt02-model1.pth -rw-rw-r-- 1 mizutu mizutu 10164 9月 5 11:18 chapt02_1a.py
(py37) $ cd ~/workspace_py37/chapter02/ (py37) $ cp ../sample/chapt02/chapt02_2.py ./chapt02_2a.py (py37) $ ls chapt02-model1.pth chapt02_1a.py chapt02_2a.py
(py37) $ vi chapt02_2a.py # -*- coding: utf-8 -*- ##------------------------------------------ ## 「PyTorch で始める AI開発」 ## Chapter 02 / Section 006 ## PyTorch による学習 ## ## 2021.09.07 Masahiro Izutsu ##------------------------------------------ ## chapt02_2a.py (original: chapt02_2.py) import numpy as np from PIL import Image, ImageTk import cv2 : # GPUを使うかどうか USE_DEVICE = 'cuda:0' if torch.cuda.is_available() else 'cpu' # アプリケーションが実行中かどうか IS_RUN = True # 保存しておいたモデルを読み込む model = models.resnet50(pretrained=False) model.fc = nn.Linear(2048, 2) model.load_state_dict(torch.load('chapt02-model1.pth', map_location=torch.device(USE_DEVICE))) :
: def __init__(self, parent, **params): Frame.__init__(self, parent, params) # # OpenCVのカメラキャプチャーを用意する # self.cap = cv2.VideoCapture(0) ## 2021.09.07 #「forest path movie dataset」用のサンプルを利用する self.cap = cv2.VideoCapture('../sample/chapt02/chapt02-sample.mp4') :
(py37) mizutu@ubuntu-vbox:~/workspace_py37/chapter02$ python3 chapt02_2a.py [W NNPACK.cpp:80] Could not initialize NNPACK! Reason: Unsupported hardware. Exception in Tkinter callback Traceback (most recent call last): File "/home/mizutu/anaconda3/envs/py37/lib/python3.7/tkinter/__init__.py", line 1705, in __call__ return self.func(*args) File "/home/mizutu/anaconda3/envs/py37/lib/python3.7/tkinter/__init__.py", line 749, in callit func(*args) File "chapt02_2a.py", line 68, in updateFrame frame = frame[:,:,::-1] TypeError: 'NoneType' object is not subscriptable
(py37) $ cd ~/workspace_py37/chapter02/ (py37) $ cp ../sample/chapt02/chapt02_2a.py ./chapt02_2b.py (py37) $ vi chapt02_2b.py
(py37) $ python3 chapt02_2b.py --- Surveillance camera --- 4.5.2 Surveillance camera: Starting application... - Image File : ../sample/chapt02/chapt02-sample-a.mp4 - Language : jp - Program Title: y - Speed flag : y - Processed out: non [W NNPACK.cpp:80] Could not initialize NNPACK! Reason: Unsupported hardware. FPS average: 3.10 Finished.
以下の手順で変換した学習モデルはうまく動作していない。2021/09/05
正常に動作する手順は → OpenVINO™ を利用する(ONNXモデルコンバート検証)
torch.onnx.export(net, input, output_name, verbose=True, input_names=input_layer_names, output_names=output_layer_names)
引数 | 意味 |
net | 実行中のモデル |
input | モデルの入力値 |
output_name | 出力先のパス |
verbose | 変換中の細かいログを吐く(True/False) |
input_names | モデルの入力値に関する表示名を指定 |
output_names | モデルの出力値に関する表示名を指定 |
(py37) $ python3 chapt02_3.py ONNX Convert start !! [W NNPACK.cpp:80] Could not initialize NNPACK! Reason: Unsupported hardware. Finish !! (py37) mizutu@ubuntu-vbox:~/workspace_py37/chapter02$ ls chapt02-model1.onnx chapt02_1a.py chapt02_3.py chapt02-model1.pth chapt02_2a.py
[env_select.sh] Environment Select !! 1: Nomal 2: OpenVINO 3: Anaconda Prease input '1-3' : 2 ** OpenVINO environment select !! ** [openvino_setup.sh] OpenVINO environment initialized :
$ cd ~/workspace_py37/chapter02/ $ ls chapt02-model1.onnx chapt02_1a.py chapt02_3.py chapt02-model1.pth chapt02_2a.py $ python3 /opt/intel/openvino_2021/deployment_tools/model_optimizer/mo.py --input_model chapt02-model1.onnx Model Optimizer arguments: Common parameters: - Path to the Input Model: /home/mizutu/workspace_py37/chapter02/chapt02-model1.onnx - Path for generated IR: /home/mizutu/workspace_py37/chapter02/. - IR output name: chapt02-model1 - Log level: ERROR - Batch: Not specified, inherited from the model - Input layers: Not specified, inherited from the model - Output layers: Not specified, inherited from the model - Input shapes: Not specified, inherited from the model - Mean values: Not specified - Scale values: Not specified - Scale factor: Not specified - Precision of IR: FP32 - Enable fusing: True - Enable grouped convolutions fusing: True - Move mean values to preprocess section: None - Reverse input channels: False ONNX specific parameters: - Inference Engine found in: /opt/intel/openvino_2021/python/python3.8/openvino Inference Engine version: 2021.4.0-3839-cd81789d294-releases/2021/4 Model Optimizer version: 2021.4.0-3839-cd81789d294-releases/2021/4 [ SUCCESS ] Generated IR version 10 model. [ SUCCESS ] XML file: /home/mizutu/workspace_py37/chapter02/chapt02-model1.xml [ SUCCESS ] BIN file: /home/mizutu/workspace_py37/chapter02/chapt02-model1.bin [ SUCCESS ] Total execution time: 14.87 seconds. [ SUCCESS ] Memory consumed: 361 MB.
$ cd ~/workspace/tools/ $ ls model_check2.py model_data.py $ python3 model_check2.py -m ../../workspace_py37/chapter02/chapt02-model1.xml --- Model Check Program 2 --- 4.5.3-openvino OpenVINO inference_engine: 2021.4.0-3839-cd81789d294-releases/2021/4 Model Check Program 2: Starting application... - Model : ../../workspace_py37/chapter02/chapt02-model1.xml - Device : CPU input blob: name='input', N=1, C=3, H=224, W=224 Finished.
$ python3 model_data.py -m ../../workspace_py37/chapter02/chapt02-model1.xml --- OpenVINO™ Model Data Check --- 4.5.3-openvino OpenVINO inference_engine: 2021.4.0-3839-cd81789d294-releases/2021/4 OpenVINO™ Model Data Check: Starting application... - Model : ../../workspace_py37/chapter02/chapt02-model1.xml - Device : CPU - Image file : /home/mizutu/Images/photo4.jpg input blob: name='input', N=1, C=3, H=224, W=224 >>> Inference execution... *** STER 1 *** {'output': array([[-46.022556, 77.82302 ]], dtype=float32)} *** STER 2 *** [[-46.022556 77.82302 ]] *** STER 3 *** [-46.022556 77.82302 ] Finished.出力の値が違っている。 → ONNXモデルコンバート検証
(py37) $ python3 chapt02_4.py --- Surveillance camera --- 4.5.2 OpenVINO inference_engine: 2021.4.0-3839-cd81789d294-releases/2021/4 Surveillance camera: Starting application... - Image File : ../sample/chapt02/chapt02-sample.mp4 - m_detect : chapt02-model1.xml - Device : CPU - Language : jp - Input Shape : input - Output Shape : output - Program Title: y - Speed flag : y - Processed out: non FPS average: 12.50 Finished.