私的AI研究会 > PyTorch5
「動画の背景を入れ替える」画像のセグメンテーションという手法を使って、画像中の人物を切り出し背景を入れ替える AIを作成する。
画像認識技術の違いについて サイト「画像分類・物体検出・セグメンテーションの比較 」から引用。
画像認識を利用することで、顔認証システムや不審者検知、文字認識(OCR)、不適切画像の検出、工場でのイレギュラー検知など幅広い分野で活用することができる。
種別 | 概要 | モデル | 処理結果 | 評価指数 | |
画像分類 Image Classification | 機械学習やディープラーニングモデルで、画像を何らかの主題に基づき分類する処理方法。教師あり学習では、ラベル付けされたデータセットを用いて、モデルを学習させていく。 | Xception VGG ResNet Inception MobileNet DenseNet | 各クラスの予測信頼度 | 混合行列 (Confusion Matrix) 正解率 (Accuracy) 適合率 (Precision) 再現率 (Recall) F値 (F-measure) ... | |
画像分類・物体位置特定 Image Classification・Localization | 画像内のクラスと物体の位置を検出する方法。画像から物体のクラスを予測する代わりに、クラスとその物体を含む矩形(バウンディングボックス)を予測する必要がある。1つの画像は1つのクラスを検出する。 | クラス名 クラスの予測信頼度 バウンディングボックス | mAP IoU | ||
物体検出 Object Detection | 画像内のクラスと物体の位置を検出。複数種類の物体を1つの画像で検出するとマルチクラスの物体検出。入力画像から固定サイズのウィンドウをすべての可能な位置で取得して、これらのパッチ(領域)を画像分類器に入力する。 | R-CNN Fast R-CNN SSD Mask R-CNN YOLO RetinaNet | クラス名 クラスの予測信頼度 バウンディングボックス | mAP (mean Average Precision) IoU (Intersection over Union) | |
セマンティック・セグメンテーション Semantic Segmentation | 画像のピクセルを「どの物体クラス(カテゴリー)に属するか」で分類する方法。画像上の全ピクセルをクラスに分類すること。同クラス間で重なりがある場合、同クラスの領域として認識するため、物体ごとの認識・カウントができない。 | U-NET MULTISCALE HYBRID CNN-CRF&br | 各ピクセルのクラス情報 | IoU and per-pixel accuracy | |
インスタント・セグメンテーション Instant Segmentation | 画像のピクセルを「どの物体クラス(カテゴリー)に属するか、どのインスタンスに属するか」で分類する方法。物体ごとの領域を分割しかつ物体の種類を認識すること。RoI(region of interest)に対して segmentation を行うので、画像全てのピクセルに対してラベルを振ることはしない。 | Mask R-CNN DeepMask FCIS | 各ピクセルのクラス情報 | average precision over different IoU thresholds | |
パノプティック・セグメンテーション Panoptic Segmentation | Semantic Segmentation とInstance Segmentationを組み合わせた方法。全てのピクセルにラベルが振られ、かつ数えられる物体に関しては、個別で認識した結果が返される。 | Panoptic Feature Pyramid Network UPSNet | 各ピクセルのクラス情報 | Panoptic Quality (PQ) |
サイト「オブジェクト検出とインスタンスセグメンテーション:詳細な概要~」から引用
マスク画像を出力するニューラルネットワークを使って画像編集を行う AI を作成する。
(py37) $ mkdir ~/workspace_py37/chapter05 (py37) $ cd ~/ダウンロード (py37) $ mv annotations_trainval2014.zip ~/workspace_py37/chapter05/ (py37) $ mv train2014.zip ~/workspace_py37/chapter05 (py37) $ cd ~/workspace_py37/chapter05 (py37) $ ls annotations_trainval2014.zip train2014.zip
(py37) $ unzip annotations_trainval2014.zip Archive: annotations_trainval2014.zip inflating: annotations/instances_train2014.json inflating: annotations/instances_val2014.json inflating: annotations/person_keypoints_train2014.json inflating: annotations/person_keypoints_val2014.json inflating: annotations/captions_train2014.json inflating: annotations/captions_val2014.json (py37) $ unzip train2014.zip Archive: train2014.zip creating: train2014/ extracting: train2014/COCO_train2014_000000270070.jpg extracting: train2014/COCO_train2014_000000101772.jpg : : extracting: train2014/COCO_train2014_000000291366.jpg extracting: train2014/COCO_train2014_000000533055.jpg extracting: train2014/COCO_train2014_000000467840.jpg (py37) $ ls annotations annotations_trainval2014.zip train2014 train2014.zip (py37) $ find ./train2014 -type f | wc -l 82783
(py37) $ rm annotations_trainval2014.zip (py37) $ rm train2014.zip (py37) $ ls annotations train2014
(py37) cd ~/workspace_py37/chapter05 (py37) $ cp ~/workspace_py37/sample/chapt05/chapt05_1.py chapt05_1a.py
(py37) $ python3 chapt05_1a.py loading annotations into memory... Done (t=19.70s) creating index... index created! (py37) $ ls -l 合計 7164 drwxrwxr-x 2 mizutu mizutu 4096 9月 22 04:20 annotations -rw-rw-r-- 1 mizutu mizutu 1889 9月 22 04:50 chapt05_1a.py drwxrwxr-x 2 mizutu mizutu 1294336 9月 22 05:29 imgs drwxrwxr-x 2 mizutu mizutu 1294336 9月 22 05:29 mask drwxrwxr-x 2 mizutu mizutu 4722688 8月 16 2014 train2014学習用データが「image」「mask」に出来上がる。
U-Net の畳み込み層に対してさらに改良を施した「DeepLab」のモデルを元に出力層のみを入れ替えたモデルを作成して転移学習を行う。
「DeepLab」のモデルは「ImageNet」という一般的な画像を使用して学習されたものが公開されているので「TorchHub」からダウンロードして使用する。
(py37) cd ~/workspace_py37/chapter05 (py37) $ cp ~/workspace_py37/sample/chapt05/chapt05_2.py chapt05_2a.py
(py37) $ cd ~/workspace_py37/chapter05 (py37) $ date 2021年 9月 27日 月曜日 19:40:49 JST (py37) $ python3 chapt05_2a.py Downloading: "https://github.com/pytorch/vision/archive/v0.6.0.zip" to /home/mizutu/.cache/torch/hub/v0.6.0.zip Downloading: "https://download.pytorch.org/models/deeplabv3_resnet101_coco-586e9e4e.pth" to /home/mizutu/.cache/torch/hub/checkpoints/deeplabv3_resnet101_coco-586e9e4e.pth 100%|████████████████████████████████████████| 233M/233M [00:13<00:00, 17.6MB/s] 100%|██████████████████████████████████| 11294/11294 [19:59:13<00:00, 6.37s/it] epoch #0: train_loss:0.13978596031665802 100%|██████████████████████████████████| 11294/11294 [19:48:03<00:00, 6.31s/it] epoch #1: train_loss:0.12665237486362457 100%|██████████████████████████████████| 11294/11294 [19:36:18<00:00, 6.25s/it] epoch #2: train_loss:0.12261106818914413 100%|██████████████████████████████████| 11294/11294 [19:36:11<00:00, 6.25s/it] epoch #3: train_loss:0.11953889578580856 100%|██████████████████████████████████| 11294/11294 [19:38:41<00:00, 6.26s/it] epoch #4: train_loss:0.11823559552431107 100%|██████████████████████████████████| 11294/11294 [19:35:09<00:00, 6.24s/it] epoch #5: train_loss:0.11668296158313751 100%|██████████████████████████████████| 11294/11294 [19:42:35<00:00, 6.28s/it] epoch #6: train_loss:0.11582669615745544 100%|██████████████████████████████████| 11294/11294 [19:34:58<00:00, 6.24s/it] epoch #7: train_loss:0.11430223286151886 (py37) $ ls -l 合計 245948 drwxrwxr-x 2 mizutu mizutu 4096 9月 23 04:28 annotations -rw-rw-r-- 1 mizutu mizutu 244638643 10月 4 09:12 chapt05-model1.pth -rw-rw-r-- 1 mizutu mizutu 1889 9月 23 04:31 chapt05_1a.py -rw-rw-r-- 1 mizutu mizutu 4015 9月 23 05:18 chapt05_2a.py drwxrwxr-x 2 mizutu mizutu 1204224 9月 23 04:52 imgs drwxrwxr-x 2 mizutu mizutu 1204224 9月 23 04:52 mask drwxrwxr-x 2 mizutu mizutu 4780032 8月 16 2014 train2014・GPU (GeForce GTX 1050 Ti) での実行結果
(py37) > python chapt05_2w.py Using cache found in C:\Users\izuts/.cache\torch\hub\pytorch_vision_v0.6.0 100%|██████████████████████████████████| 11294/11294 [5:56:55<00:00, 1.90s/it] epoch #0: train_loss:0.14030972123146057 100%|██████████████████████████████████| 11294/11294 [5:56:12<00:00, 1.89s/it] epoch #1: train_loss:0.1263476312160492 100%|██████████████████████████████████| 11294/11294 [5:52:53<00:00, 1.87s/it] epoch #2: train_loss:0.12263970822095871 100%|██████████████████████████████████| 11294/11294 [5:52:45<00:00, 1.87s/it] epoch #3: train_loss:0.12062573432922363 100%|██████████████████████████████████| 11294/11294 [5:53:30<00:00, 1.88s/it] epoch #4: train_loss:0.11797807365655899 100%|██████████████████████████████████| 11294/11294 [5:54:05<00:00, 1.88s/it] epoch #5: train_loss:0.11718424409627914 100%|██████████████████████████████████| 11294/11294 [5:53:51<00:00, 1.88s/it] epoch #6: train_loss:0.11532850563526154 100%|██████████████████████████████████| 11294/11294 [5:52:37<00:00, 1.87s/it] epoch #7: train_loss:0.11388455331325531※「DeepLab」のモデルは実行に時間がかかるため学習にも相当な時間を要する。
機種 | 開始日時 | 終了日時 | 処理時間 (h:m) |
GeForce GTX 1050 Ti Intel® Core™ i7-6700 | 10/14 05:05 | 10/16 04:18 | 46:47 |
Intel® NUC BXNUC10I5FNH Core™ i5-10210U CPU | 09/27 19:40 | 10/04 09:12 | 157:30 |
DELL Latitude 7520 Intel® Core™ i7-1185G7 CPU | 09/26 10:36 | 10/05 07:20 | 212:36 |
(py37) $ cd ~/workspace_py37/chapter05 (py37) $ cp ~/workspace_py37/sample/chapt05/chapt05_3.py chapt05_3a.py
(py37) $ cd ~/workspace_py37/chapter05/ (py37) $ python3 chapt05_3a.py・起動したダイアログから「人物動画ファイル」「背景動画ファイル」「出力先の動画ファイル」を指定する。