YOLOv5を用いて、自作データセットでマスク検出してみた（画像）

<h1>目的</h1>
機械学習の知識を得るため。

YOLOv5の公式サイト：

https://docs.ultralytics.com/



<h1>実行環境</h1>
今回はGoogle Colaboratory環境で実行した。

ハードウェア情報は以下の通りです。

GPU: Intel(R) iris(R) Xe Graphics (GPUメモリ20GB搭載)
メモリ: 40GB

<h1>セットアップ</h1>

```YOLOv5_train_detect.ipynb
from google.colab import drive
drive.mount('/content/drive')
%cd ./drive/MyDrive
```

<h1>VOLOv5ライブラリーのインストール</h1>

```YOLOv5_train_detect.ipynb
!git clone https://github.com/ultralytics/yolov5  # clone
%cd yolov5
```

<h1>パッケージをインストールする</h1>

```YOLOv5_train_detect.ipynb
%pip install -qr requirements.txt
```

<h1>動作確認</h1>

```YOLOv5_train_detect.ipynb
!python detect.py --source /content/drive/MyDrive/datasets/coco128/images/train2017/000000000560.jpg --conf 0.25 --img-size 640
```
検知結果を出力する
```YOLOv5_train_detect.ipynb
import matplotlib.pyplot as plt
img = plt.imread('runs/detect/exp/000000000560.jpg')
plt.imshow(img)
plt.show()
```

![index.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3007201/78536fe0-0108-b1ec-656d-8ad3fce067a7.png)



<h1>学習用データの配置</h1>
データセットを以下のフォルダに配置します。

```
# yolov5/dataset/
```

今回使用したデータセットは：https://public.roboflow.com/object-detection/mask-wearing


<h1>configファイルの修正</h1>
データ用のconfigファイルをdata/mask.yamlに作成して、以下を記述する。
特に難しい記述は不要な印象

```mask.yml
train: /content/drive/MyDrive/yolov5/datasets/train/images
val: /content/drive/MyDrive/yolov5/datasets/valid/images

nc: 2
names: ['mask', 'no-mask']
```

<h1>学習の実行</h1>

```YOLOv5_train_detect.ipynb
!python train.py --img 640 --batch 16 --epochs 3 --data mask.yaml --weights yolov5s.pt --cache
```

実行のイメージ：

```
train: weights=yolov5s.pt, cfg=, data=mask.yaml, hyp=data/hyps/hyp.scratch-low.yaml, epochs=100, batch_size=16, imgsz=640, rect=False, resume=False, nosave=False, noval=False, noautoanchor=False, noplots=False, evolve=None, bucket=, cache=ram, image_weights=False, device=, multi_scale=False, single_cls=False, optimizer=SGD, sync_bn=False, workers=8, project=runs/train, name=exp, exist_ok=False, quad=False, cos_lr=False, label_smoothing=0.0, patience=100, freeze=[0], save_period=-1, seed=0, local_rank=-1, entity=None, upload_dataset=False, bbox_interval=-1, artifact_alias=latest
github: up to date with https://github.com/ultralytics/yolov5 ✅
YOLOv5 🚀 v7.0-27-g454dae1 Python-3.7.15 torch-1.12.1+cu113 CPU

hyperparameters: lr0=0.01, lrf=0.01, momentum=0.937, weight_decay=0.0005, warmup_epochs=3.0, warmup_momentum=0.8, warmup_bias_lr=0.1, box=0.05, cls=0.5, cls_pw=1.0, obj=1.0, obj_pw=1.0, iou_t=0.2, anchor_t=4.0, fl_gamma=0.0, hsv_h=0.015, hsv_s=0.7, hsv_v=0.4, degrees=0.0, translate=0.1, scale=0.5, shear=0.0, perspective=0.0, flipud=0.0, fliplr=0.5, mosaic=1.0, mixup=0.0, copy_paste=0.0
ClearML: run 'pip install clearml' to automatically track, visualize and remotely train YOLOv5 🚀 in ClearML
Comet: run 'pip install comet_ml' to automatically track and visualize YOLOv5 🚀 runs in Comet
TensorBoard: Start with 'tensorboard --logdir runs/train', view at http://localhost:6006/
Overriding model.yaml nc=80 with nc=2

                 from  n    params  module                                  arguments                     
  0                -1  1      3520  models.common.Conv                      [3, 32, 6, 2, 2]              
  1                -1  1     18560  models.common.Conv                      [32, 64, 3, 2]                
  2                -1  1     18816  models.common.C3                        [64, 64, 1]                   
  3                -1  1     73984  models.common.Conv                      [64, 128, 3, 2]               
  4                -1  2    115712  models.common.C3                        [128, 128, 2]                 
  5                -1  1    295424  models.common.Conv                      [128, 256, 3, 2]              
  6                -1  3    625152  models.common.C3                        [256, 256, 3]                 
  7                -1  1   1180672  models.common.Conv                      [256, 512, 3, 2]              
  8                -1  1   1182720  models.common.C3                        [512, 512, 1]                 
  9                -1  1    656896  models.common.SPPF                      [512, 512, 5]                 
 10                -1  1    131584  models.common.Conv                      [512, 256, 1, 1]              
 11                -1  1         0  torch.nn.modules.upsampling.Upsample    [None, 2, 'nearest']          
 12           [-1, 6]  1         0  models.common.Concat                    [1]                           
 13                -1  1    361984  models.common.C3                        [512, 256, 1, False]          
 14                -1  1     33024  models.common.Conv                      [256, 128, 1, 1]              
 15                -1  1         0  torch.nn.modules.upsampling.Upsample    [None, 2, 'nearest']          
 16           [-1, 4]  1         0  models.common.Concat                    [1]                           
 17                -1  1     90880  models.common.C3                        [256, 128, 1, False]          
 18                -1  1    147712  models.common.Conv                      [128, 128, 3, 2]              
 19          [-1, 14]  1         0  models.common.Concat                    [1]                           
 20                -1  1    296448  models.common.C3                        [256, 256, 1, False]          
 21                -1  1    590336  models.common.Conv                      [256, 256, 3, 2]              
 22          [-1, 10]  1         0  models.common.Concat                    [1]                           
 23                -1  1   1182720  models.common.C3                        [512, 512, 1, False]          
 24      [17, 20, 23]  1     18879  models.yolo.Detect                      [2, [[10, 13, 16, 30, 33, 23], [30, 61, 62, 45, 59, 119], [116, 90, 156, 198, 373, 326]], [128, 256, 512]]
Model summary: 214 layers, 7025023 parameters, 7025023 gradients, 16.0 GFLOPs

Transferred 343/349 items from yolov5s.pt
optimizer: SGD(lr=0.01) with parameter groups 57 weight(decay=0.0), 60 weight(decay=0.0005), 60 bias
albumentations: Blur(p=0.01, blur_limit=(3, 7)), MedianBlur(p=0.01, blur_limit=(3, 7)), ToGray(p=0.01), CLAHE(p=0.01, clip_limit=(1, 4.0), tile_grid_size=(8, 8))
train: Scanning /content/drive/MyDrive/yolov5/datasets/train/labels.cache... 105 images, 0 backgrounds, 0 corrupt: 100% 105/105 [00:00<?, ?it/s]
train: Caching images (0.1GB ram): 100% 105/105 [00:02<00:00, 45.44it/s]
val: Scanning /content/drive/MyDrive/yolov5/datasets/valid/labels.cache... 29 images, 0 backgrounds, 0 corrupt: 100% 29/29 [00:00<?, ?it/s]
val: Caching images (0.0GB ram): 100% 29/29 [00:01<00:00, 28.73it/s]

AutoAnchor: 5.89 anchors/target, 0.999 Best Possible Recall (BPR). Current anchors are a good fit to dataset ✅
Plotting labels to runs/train/exp5/labels.jpg... 
Image sizes 640 train, 640 val
Using 2 dataloader workers
Logging results to runs/train/exp5
Starting training for 100 epochs...

requirements: YOLOv5 requirement "gitpython" not found, attempting AutoUpdate...
Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/
Collecting gitpython
  Downloading GitPython-3.1.29-py3-none-any.whl (182 kB)
Collecting gitdb<5,>=4.0.1
  Downloading gitdb-4.0.10-py3-none-any.whl (62 kB)
Collecting smmap<6,>=3.0.1
  Downloading smmap-5.0.0-py3-none-any.whl (24 kB)
Installing collected packages: smmap, gitdb, gitpython
Successfully installed gitdb-4.0.10 gitpython-3.1.29 smmap-5.0.0

requirements: 1 package updated per ['gitpython']
requirements: ⚠️ Restart runtime or rerun command for updates to take effect

train: weights=yolov5s.pt, cfg=, data=/content/drive/MyDrive/yolov5/data/mask.yaml, hyp=data/hyps/hyp.scratch-low.yaml, epochs=100, batch_size=16, imgsz=640, rect=False, resume=False, nosave=False, noval=False, noautoanchor=False, noplots=False, evolve=None, bucket=, cache=ram, image_weights=False, device=, multi_scale=False, single_cls=False, optimizer=SGD, sync_bn=False, workers=8, project=runs/train, name=exp, exist_ok=False, quad=False, cos_lr=False, label_smoothing=0.0, patience=100, freeze=[0], save_period=-1, seed=0, local_rank=-1, entity=None, upload_dataset=False, bbox_interval=-1, artifact_alias=latest
Command 'git fetch origin' timed out after 5 seconds
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 1 (delta 1), pack-reused 0
requirements: YOLOv5 requirements "ipython" "thop>=0.1.1" not found, attempting AutoUpdate...
Unpacking objects: 100% (3/3), done.
From https://github.com/ultralytics/yolov5
   443ef7f..357cde9  master     -> origin/master
Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/
Requirement already satisfied: ipython in /usr/local/lib/python3.8/dist-packages (7.9.0)
Collecting thop>=0.1.1
  Downloading thop-0.1.1.post2209072238-py3-none-any.whl (15 kB)
Requirement already satisfied: torch in /usr/local/lib/python3.8/dist-packages (from thop>=0.1.1) (1.13.0+cu116)
Requirement already satisfied: traitlets>=4.2 in /usr/local/lib/python3.8/dist-packages (from ipython) (5.6.0)
Requirement already satisfied: backcall in /usr/local/lib/python3.8/dist-packages (from ipython) (0.2.0)
Collecting jedi>=0.10
  Downloading jedi-0.18.2-py2.py3-none-any.whl (1.6 MB)
Requirement already satisfied: setuptools>=18.5 in /usr/local/lib/python3.8/dist-packages (from ipython) (57.4.0)
Requirement already satisfied: prompt-toolkit<2.1.0,>=2.0.0 in /usr/local/lib/python3.8/dist-packages (from ipython) (2.0.10)
Requirement already satisfied: decorator in /usr/local/lib/python3.8/dist-packages (from ipython) (4.4.2)
Requirement already satisfied: pygments in /usr/local/lib/python3.8/dist-packages (from ipython) (2.6.1)
Requirement already satisfied: pexpect in /usr/local/lib/python3.8/dist-packages (from ipython) (4.8.0)
Requirement already satisfied: pickleshare in /usr/local/lib/python3.8/dist-packages (from ipython) (0.7.5)
Requirement already satisfied: parso<0.9.0,>=0.8.0 in /usr/local/lib/python3.8/dist-packages (from jedi>=0.10->ipython) (0.8.3)
Requirement already satisfied: six>=1.9.0 in /usr/local/lib/python3.8/dist-packages (from prompt-toolkit<2.1.0,>=2.0.0->ipython) (1.15.0)
Requirement already satisfied: wcwidth in /usr/local/lib/python3.8/dist-packages (from prompt-toolkit<2.1.0,>=2.0.0->ipython) (0.2.5)
Requirement already satisfied: ptyprocess>=0.5 in /usr/local/lib/python3.8/dist-packages (from pexpect->ipython) (0.7.0)
Requirement already satisfied: typing-extensions in /usr/local/lib/python3.8/dist-packages (from torch->thop>=0.1.1) (4.4.0)
Installing collected packages: jedi, thop
Successfully installed jedi-0.18.2 thop-0.1.1.post2209072238

requirements: 2 packages updated per /content/drive/MyDrive/yolov5/requirements.txt
requirements: ⚠️ Restart runtime or rerun command for updates to take effect

YOLOv5 🚀 v7.0-24-gf8539a6 Python-3.8.16 torch-1.13.0+cu116 CPU

hyperparameters: lr0=0.01, lrf=0.01, momentum=0.937, weight_decay=0.0005, warmup_epochs=3.0, warmup_momentum=0.8, warmup_bias_lr=0.1, box=0.05, cls=0.5, cls_pw=1.0, obj=1.0, obj_pw=1.0, iou_t=0.2, anchor_t=4.0, fl_gamma=0.0, hsv_h=0.015, hsv_s=0.7, hsv_v=0.4, degrees=0.0, translate=0.1, scale=0.5, shear=0.0, perspective=0.0, flipud=0.0, fliplr=0.5, mosaic=1.0, mixup=0.0, copy_paste=0.0
ClearML: run 'pip install clearml' to automatically track, visualize and remotely train YOLOv5 🚀 in ClearML
Comet: run 'pip install comet_ml' to automatically track and visualize YOLOv5 🚀 runs in Comet
TensorBoard: Start with 'tensorboard --logdir runs/train', view at http://localhost:6006/
Overriding model.yaml nc=80 with nc=2

                 from  n    params  module                                  arguments                     
  0                -1  1      3520  models.common.Conv                      [3, 32, 6, 2, 2]              
  1                -1  1     18560  models.common.Conv                      [32, 64, 3, 2]                
  2                -1  1     18816  models.common.C3                        [64, 64, 1]                   
  3                -1  1     73984  models.common.Conv                      [64, 128, 3, 2]               
  4                -1  2    115712  models.common.C3                        [128, 128, 2]                 
  5                -1  1    295424  models.common.Conv                      [128, 256, 3, 2]              
  6                -1  3    625152  models.common.C3                        [256, 256, 3]                 
  7                -1  1   1180672  models.common.Conv                      [256, 512, 3, 2]              
  8                -1  1   1182720  models.common.C3                        [512, 512, 1]                 
  9                -1  1    656896  models.common.SPPF                      [512, 512, 5]                 
 10                -1  1    131584  models.common.Conv                      [512, 256, 1, 1]              
 11                -1  1         0  torch.nn.modules.upsampling.Upsample    [None, 2, 'nearest']          
 12           [-1, 6]  1         0  models.common.Concat                    [1]                           
 13                -1  1    361984  models.common.C3                        [512, 256, 1, False]          
 14                -1  1     33024  models.common.Conv                      [256, 128, 1, 1]              
 15                -1  1         0  torch.nn.modules.upsampling.Upsample    [None, 2, 'nearest']          
 16           [-1, 4]  1         0  models.common.Concat                    [1]                           
 17                -1  1     90880  models.common.C3                        [256, 128, 1, False]          
 18                -1  1    147712  models.common.Conv                      [128, 128, 3, 2]              
 19          [-1, 14]  1         0  models.common.Concat                    [1]                           
 20                -1  1    296448  models.common.C3                        [256, 256, 1, False]          
 21                -1  1    590336  models.common.Conv                      [256, 256, 3, 2]              
 22          [-1, 10]  1         0  models.common.Concat                    [1]                           
 23                -1  1   1182720  models.common.C3                        [512, 512, 1, False]          
 24      [17, 20, 23]  1     18879  models.yolo.Detect                      [2, [[10, 13, 16, 30, 33, 23], [30, 61, 62, 45, 59, 119], [116, 90, 156, 198, 373, 326]], [128, 256, 512]]
Model summary: 214 layers, 7025023 parameters, 7025023 gradients

Transferred 343/349 items from yolov5s.pt
optimizer: SGD(lr=0.01) with parameter groups 57 weight(decay=0.0), 60 weight(decay=0.0005), 60 bias
albumentations: Blur(p=0.01, blur_limit=(3, 7)), MedianBlur(p=0.01, blur_limit=(3, 7)), ToGray(p=0.01), CLAHE(p=0.01, clip_limit=(1, 4.0), tile_grid_size=(8, 8))
train: Scanning /content/drive/MyDrive/yolov5/datasets/train/labels.cache... 105 images, 0 backgrounds, 0 corrupt: 100% 105/105 [00:00<?, ?it/s]
train: Caching images (0.1GB ram): 100% 105/105 [00:02<00:00, 46.77it/s]
val: Scanning /content/drive/MyDrive/yolov5/datasets/valid/labels.cache... 29 images, 0 backgrounds, 0 corrupt: 100% 29/29 [00:00<?, ?it/s]
val: Caching images (0.0GB ram): 100% 29/29 [00:03<00:00,  9.37it/s]

AutoAnchor: 5.89 anchors/target, 0.999 Best Possible Recall (BPR). Current anchors are a good fit to dataset ✅
Plotting labels to runs/train/exp19/labels.jpg... 
Image sizes 640 train, 640 val
Using 2 dataloader workers
Logging results to runs/train/exp19
Starting training for 100 epochs...

      Epoch    GPU_mem   box_loss   obj_loss   cls_loss  Instances       Size
       0/99         0G     0.1213    0.06643    0.02934         50        640: 100% 7/7 [02:55<00:00, 25.10s/it]
                 Class     Images  Instances          P          R      mAP50   mAP50-95:   0% 0/1 [00:00<?, ?it/s]WARNING ⚠️ NMS time limit 1.950s exceeded
                 Class     Images  Instances          P          R      mAP50   mAP50-95: 100% 1/1 [00:16<00:00, 16.34s/it]
                   all         29        162    0.00209     0.0106    0.00107   0.000142

      Epoch    GPU_mem   box_loss   obj_loss   cls_loss  Instances       Size
       1/99         0G     0.1127    0.07942    0.02828         83        640: 100% 7/7 [02:42<00:00, 23.15s/it]
                 Class     Images  Instances          P          R      mAP50   mAP50-95:   0% 0/1 [00:00<?, ?it/s]WARNING ⚠️ NMS time limit 1.950s exceeded
                 Class     Images  Instances          P          R      mAP50   mAP50-95: 100% 1/1 [00:16<00:00, 16.54s/it]
                   all         29        162    0.00162      0.032   0.000846   0.000169

      Epoch    GPU_mem   box_loss   obj_loss   cls_loss  Instances       Size
       2/99         0G     0.1019     0.0914    0.02684        112        640: 100% 7/7 [02:44<00:00, 23.55s/it]
                 Class     Images  Instances          P          R      mAP50   mAP50-95:   0% 0/1 [00:00<?, ?it/s]WARNING ⚠️ NMS time limit 1.950s exceeded
                 Class     Images  Instances          P          R      mAP50   mAP50-95: 100% 1/1 [00:16<00:00, 16.56s/it]
                   all         29        162    0.00543     0.0458    0.00388    0.00145

      Epoch    GPU_mem   box_loss   obj_loss   cls_loss  Instances       Size
       3/99         0G    0.09635     0.0999    0.02387         81        640: 100% 7/7 [02:40<00:00, 22.94s/it]
                 Class     Images  Instances          P          R      mAP50   mAP50-95:   0% 0/1 [00:00<?, ?it/s]WARNING ⚠️ NMS time limit 1.950s exceeded
                 Class     Images  Instances          P          R      mAP50   mAP50-95: 100% 1/1 [00:16<00:00, 16.71s/it]
                   all         29        162      0.592     0.0282     0.0265    0.00832

      Epoch    GPU_mem   box_loss   obj_loss   cls_loss  Instances       Size
       4/99         0G    0.08721    0.09012    0.02096        106        640: 100% 7/7 [02:43<00:00, 23.40s/it]
                 Class     Images  Instances          P          R      mAP50   mAP50-95:   0% 0/1 [00:00<?, ?it/s]WARNING ⚠️ NMS time limit 1.950s exceeded
                 Class     Images  Instances          P          R      mAP50   mAP50-95: 100% 1/1 [00:16<00:00, 16.03s/it]
                   all         29        162      0.557     0.0915     0.0287     0.0105

      Epoch    GPU_mem   box_loss   obj_loss   cls_loss  Instances       Size
       5/99         0G    0.08064    0.07276    0.01982         72        640: 100% 7/7 [02:39<00:00, 22.75s/it]
                 Class     Images  Instances          P          R      mAP50   mAP50-95:   0% 0/1 [00:00<?, ?it/s]WARNING ⚠️ NMS time limit 1.950s exceeded
                 Class     Images  Instances          P          R      mAP50   mAP50-95: 100% 1/1 [00:15<00:00, 15.71s/it]
                   all         29        162      0.611      0.081     0.0514      0.018

      Epoch    GPU_mem   box_loss   obj_loss   cls_loss  Instances       Size
       6/99         0G    0.07785    0.07415    0.01932        129        640: 100% 7/7 [02:37<00:00, 22.49s/it]
                 Class     Images  Instances          P          R      mAP50   mAP50-95:   0% 0/1 [00:00<?, ?it/s]WARNING ⚠️ NMS time limit 1.950s exceeded
                 Class     Images  Instances          P          R      mAP50   mAP50-95: 100% 1/1 [00:15<00:00, 15.98s/it]
                   all         29        162      0.689      0.141      0.126     0.0377

      Epoch    GPU_mem   box_loss   obj_loss   cls_loss  Instances       Size
       7/99         0G    0.07889    0.06491    0.01934         52        640: 100% 7/7 [02:37<00:00, 22.48s/it]
                 Class     Images  Instances          P          R      mAP50   mAP50-95:   0% 0/1 [00:00<?, ?it/s]WARNING ⚠️ NMS time limit 1.950s exceeded
                 Class     Images  Instances          P          R      mAP50   mAP50-95: 100% 1/1 [00:15<00:00, 15.87s/it]
                   all         29        162      0.607      0.187     0.0846     0.0313

      Epoch    GPU_mem   box_loss   obj_loss   cls_loss  Instances       Size
       8/99         0G    0.07932    0.06554    0.01618         98        640: 100% 7/7 [02:38<00:00, 22.60s/it]
                 Class     Images  Instances          P          R      mAP50   mAP50-95: 100% 1/1 [00:15<00:00, 15.21s/it]
                   all         29        162      0.621      0.254      0.168     0.0579

      Epoch    GPU_mem   box_loss   obj_loss   cls_loss  Instances       Size
       9/99         0G     0.0745    0.06559    0.01803        101        640: 100% 7/7 [02:41<00:00, 23.08s/it]
                 Class     Images  Instances          P          R      mAP50   mAP50-95: 100% 1/1 [00:15<00:00, 15.33s/it]
                   all         29        162      0.619      0.236      0.191     0.0693

      Epoch    GPU_mem   box_loss   obj_loss   cls_loss  Instances       Size
      10/99         0G    0.07774    0.05583    0.01737         80        640: 100% 7/7 [02:36<00:00, 22.38s/it]
                 Class     Images  Instances          P          R      mAP50   mAP50-95: 100% 1/1 [00:14<00:00, 14.73s/it]
                   all         29        162      0.623      0.218      0.189     0.0568

      Epoch    GPU_mem   box_loss   obj_loss   cls_loss  Instances       Size
      11/99         0G    0.07289    0.06256    0.01686         88        640: 100% 7/7 [02:37<00:00, 22.50s/it]
                 Class     Images  Instances          P          R      mAP50   mAP50-95: 100% 1/1 [00:14<00:00, 14.57s/it]
                   all         29        162      0.672      0.278      0.259     0.0795

      Epoch    GPU_mem   box_loss   obj_loss   cls_loss  Instances       Size
      12/99         0G     0.0782    0.05958    0.01681        108        640: 100% 7/7 [02:39<00:00, 22.85s/it]
                 Class     Images  Instances          P          R      mAP50   mAP50-95: 100% 1/1 [00:14<00:00, 14.88s/it]
                   all         29        162      0.665      0.232      0.225     0.0865

      Epoch    GPU_mem   box_loss   obj_loss   cls_loss  Instances       Size
      13/99         0G    0.07574    0.06196    0.01782         76        640: 100% 7/7 [02:36<00:00, 22.35s/it]
                 Class     Images  Instances          P          R      mAP50   mAP50-95: 100% 1/1 [00:15<00:00, 15.74s/it]
                   all         29        162      0.153      0.583      0.179     0.0587

      Epoch    GPU_mem   box_loss   obj_loss   cls_loss  Instances       Size
      14/99         0G     0.0815    0.05924     0.0177        151        640: 100% 7/7 [02:38<00:00, 22.60s/it]
                 Class     Images  Instances          P          R      mAP50   mAP50-95: 100% 1/1 [00:15<00:00, 15.97s/it]
                   all         29        162      0.639      0.229      0.205     0.0822

      Epoch    GPU_mem   box_loss   obj_loss   cls_loss  Instances       Size
      15/99         0G    0.07427     0.0546    0.01764         98        640: 100% 7/7 [02:35<00:00, 22.25s/it]
                 Class     Images  Instances          P          R      mAP50   mAP50-95: 100% 1/1 [00:14<00:00, 14.25s/it]
                   all         29        162      0.669       0.32      0.244     0.0895

      Epoch    GPU_mem   box_loss   obj_loss   cls_loss  Instances       Size
      16/99         0G    0.07037    0.05111    0.01617        130        640: 100% 7/7 [02:44<00:00, 23.55s/it]
                 Class     Images  Instances          P          R      mAP50   mAP50-95: 100% 1/1 [00:14<00:00, 14.34s/it]
                   all         29        162      0.655      0.268      0.231     0.0599

      Epoch    GPU_mem   box_loss   obj_loss   cls_loss  Instances       Size
      17/99         0G    0.06646    0.05831    0.01645        203        640: 100% 7/7 [02:39<00:00, 22.73s/it]
                 Class     Images  Instances          P          R      mAP50   mAP50-95: 100% 1/1 [00:13<00:00, 13.91s/it]
                   all         29        162       0.68      0.254      0.342      0.129

      Epoch    GPU_mem   box_loss   obj_loss   cls_loss  Instances       Size
      18/99         0G    0.06685    0.05112     0.0178        124        640: 100% 7/7 [02:37<00:00, 22.53s/it]
                 Class     Images  Instances          P          R      mAP50   mAP50-95: 100% 1/1 [00:14<00:00, 14.27s/it]
                   all         29        162      0.695      0.278      0.272      0.105

      Epoch    GPU_mem   box_loss   obj_loss   cls_loss  Instances       Size
      19/99         0G     0.0637    0.05462    0.01597         62        640: 100% 7/7 [02:41<00:00, 23.09s/it]
                 Class     Images  Instances          P          R      mAP50   mAP50-95: 100% 1/1 [00:14<00:00, 14.33s/it]
                   all         29        162      0.166      0.547      0.234     0.0909

      Epoch    GPU_mem   box_loss   obj_loss   cls_loss  Instances       Size
      20/99         0G    0.06474    0.05678    0.01562        110        640: 100% 7/7 [02:34<00:00, 22.14s/it]
                 Class     Images  Instances          P          R      mAP50   mAP50-95: 100% 1/1 [00:13<00:00, 13.71s/it]
                   all         29        162      0.328      0.535      0.385      0.151

      Epoch    GPU_mem   box_loss   obj_loss   cls_loss  Instances       Size
      21/99         0G    0.05212    0.05954    0.01771        102        640: 100% 7/7 [02:33<00:00, 21.98s/it]
                 Class     Images  Instances          P          R      mAP50   mAP50-95: 100% 1/1 [00:13<00:00, 13.61s/it]
                   all         29        162      0.195      0.586       0.26      0.107

      Epoch    GPU_mem   box_loss   obj_loss   cls_loss  Instances       Size
      22/99         0G    0.06745    0.04546    0.01458        146        640:  14% 1/7 [00:23<02:21, 23.66s/it]

・・・・・・

```

<h1>推論テスト</h1>

```YOLOv5_train_detect.ipynb
!python detect.py --weights /content/yolov5/runs/train/exp4/weights/best.pt \
--source /content/drive/MyDrive/IMG_8585.JPG \
--conf 0.25 --img-size 640

```

実行のイメージ：

```
detect: weights=['/content/yolov5/runs/train/exp5/weights/best.pt'], source=/content/drive/MyDrive/IMG_8585.JPG, data=data/coco128.yaml, 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, dnn=False, vid_stride=1
YOLOv5 🚀 v7.0-27-g454dae1 Python-3.7.15 torch-1.12.1+cu113 CPU

Fusing layers... 
Model summary: 157 layers, 7015519 parameters, 0 gradients, 15.8 GFLOPs
image 1/1 /content/drive/MyDrive/IMG_8585.JPG: 480x640 (no detections), 297.6ms
Speed: 1.3ms pre-process, 297.6ms inference, 0.5ms NMS per image at shape (1, 3, 640, 640)
Results saved to runs/detect/exp5
```

<h1>学習結果の出力</h1>

```
import matplotlib.pyplot as plt
img = plt.imread('runs/detect/exp5/IMG_8585.JPG')
plt.imshow(img)
plt.show()
```

![index.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3007201/3b8f9174-1d20-e10d-2d96-e8ffa620af4b.png)



<h1>課題</h1>

VOLOv7を利用したかったが、GPUの条件が満たさなかったみたいので、CUDA無化になった。