私的AI研究会 > YOLOv7_Colab3
前回の学習結果から 検出精度を向上させる学習方法について考えてみる
!python train.py --workers 8 --batch-size 16 --data janken_dataset.yaml --cfg cfg/training/yolov7x.yaml --weights 'yolov7x.pt' --name yolov7x_custom --hyp data/hyp.scratch.p5.yaml --epochs 300 --device 0 : : 299/299 14.3G 0.02072 0.004905 0.003531 0.02915 49 640: 100% 30/30 [00:43<00:00, 1.45s/it] Class Images Labels P R mAP@.5 mAP@.5:.95: 100% 4/4 [00:04<00:00, 1.22s/it] all 120 120 0.988 0.996 0.997 0.75 goo 120 40 0.99 1 0.997 0.733 choki 120 40 0.976 1 0.997 0.726 par 120 40 1 0.988 0.997 0.791 5 epochs completed in 3.583 hours. Optimizer stripped from runs/train/yolov7x_custom/weights/last.pt, 142.1MB Optimizer stripped from runs/train/yolov7x_custom/weights/best.pt, 142.1MB・epochs = 100
!python train.py --workers 8 --batch-size 16 --data janken_dataset.yaml --cfg cfg/training/yolov7x.yaml --weights 'yolov7x.pt' --name yolov7x_custom_100 --hyp data/hyp.scratch.p5.yaml --epochs 100 --device 0 : : Epoch gpu_mem box obj cls total labels img_size 99/99 14.2G 0.01923 0.006191 0.004937 0.03035 48 640: 100% 30/30 [00:41<00:00, 1.37s/it] Class Images Labels P R mAP@.5 mAP@.5:.95: 100% 4/4 [00:04<00:00, 1.03s/it] all 120 120 1 0.985 0.992 0.746 goo 120 40 0.999 1 0.995 0.72 choki 120 40 1 1 0.995 0.72 par 120 40 1 0.956 0.984 0.799 100 epochs completed in 1.317 hours. Optimizer stripped from runs/train/yolov7x_custom_100/weights/last.pt, 142.1MB Optimizer stripped from runs/train/yolov7x_custom_100/weights/best.pt, 142.1MB・epochs = 50
!python train.py --workers 8 --batch-size 16 --data janken_dataset.yaml --cfg cfg/training/yolov7x.yaml --weights 'yolov7x.pt' --name yolov7x_custom_100 --hyp data/hyp.scratch.p5.yaml --epochs 50 --device 0 : : Epoch gpu_mem box obj cls total labels img_size 49/49 14.3G 0.03021 0.006925 0.01155 0.04869 41 640: 100% 30/30 [00:39<00:00, 1.31s/it] Class Images Labels P R mAP@.5 mAP@.5:.95: 100% 4/4 [00:03<00:00, 1.16it/s] all 120 120 0.982 0.992 0.989 0.725 goo 120 40 0.984 1 0.996 0.694 choki 120 40 0.961 1 0.989 0.699 par 120 40 1 0.975 0.983 0.782 50 epochs completed in 0.647 hours. Optimizer stripped from runs/train/yolov7x_custom_50/weights/last.pt, 142.1MB Optimizer stripped from runs/train/yolov7x_custom_50/weights/best.pt, 142.1MB
(py38a) PS > cd /anaconda_win/work/yolov7-main (py38a) PS > python train.py --workers 8 --batch-size 2 --data janken_dataset.yaml --cfg cfg/training/yolov7x.yaml --weights 'yolov7x.pt' --name yolov7x_custom2 --hyp data/hyp.scratch.p5.yaml --epochs 300 --device 0 : : Epoch gpu_mem box obj cls total labels img_size 299/299 3.4G 0.01432 0.006242 0.003859 0.02442 5 64 Class Images Labels P R mAP@. all 120 120 0.979 0.995 0.995 0.73 goo 120 40 0.987 1 0.995 0.719 choki 120 40 0.976 1 0.994 0.698 par 120 40 0.975 0.985 0.995 0.772 300 epochs completed in 21.154 hours. Optimizer stripped from runs\train\yolov7x_custom213\weights\last.pt, 142.1MB Optimizer stripped from runs\train\yolov7x_custom213\weights\best.pt, 142.1MB
コマンドオプション | 引数 | 初期値 | 意味 |
--weights | str | yolov7s.pt | 学習済み重みモデルファイル |
--cfg | str | 使用するYOLOのバージョン(cfgフォルダからモデルの種類に合わせて指定) | |
--data | str | data/coco.yaml | 学習データの情報を記載したyamlファイルのパス |
--hyp | str | data/hyp.scratch.p5.yaml | ハイパーパラメータのパス |
--epochs | int | 300 | 学習回数指定 |
--batch-size | int | 16 | バッチサイズ指定 |
--img-size | list | [640, 640] | 学習画像のサイズを指定(--img-size 640 640) |
--rect | なし | - | rectangular training |
--resume | なし | - | 前回中断した学習(training)を途中から再開する |
--nosave | なし | - | 最終 checkpoint のみ保存 |
--notest | なし | - | only test final epoch |
--noautoanchor | なし | - | 自動アンカーチェックの無効化 |
--evolve | なし | - | evolve hyperparameters |
--bucket | str | gsutil bucket | |
--cache-images | str | ram | cashの保存先指定 (ram or disk) |
--image-weights | なし | - | use weighted image selection for training |
--device | str | 使用プロセッサの指定(0 or 0,1,2,3 or cpu) (指定なしの場合 cuda) | |
--multi-scale | なし | - | 画像サイズを±50%変化させる |
--single-cls | なし | - | train multi-class data as single-class |
--adam | なし | - | Adam optimizerを使用する (指定しなければ SGD が使用される) |
--sync-bn | なし | - | SyncBatchNormを使用する (DDPモードでのみ使用可能) |
--local_rank | int | -1 | DDP parameter, do not modify |
--workers | int | 8 | データローダーワーカーの最大数 |
--project | str | runs/train | 学習結果の記録フォルダパス |
--name | str | project/name | 学習結果の記録フォルダの下のフォルダ名(推論ごとにインクリメント) |
--entity | なし | None | W&B entity |
--exist-ok | なし | - | 学習結果を上書き保存(指定すれば上書き) |
--quad | なし | - | quad dataloader |
--linear-lr | なし | - | linear LR |
--label-smoothing | 0.0 | Label smoothing epsilon | |
--upload_dataset | なし | - | Upload dataset as W&B artifact table |
--bbox_interval | なし | -1 | Set bounding-box image logging interval for W&B' |
--save_period | int | -1 | 指定 epoch 毎にチェックポイントを保存する (-1 = (無効)) |
--artifact_alias | なし | latest | version of dataset artifact to be used |
--freeze | list | [0] | 学習しない層を指定 backbone of yolov7=50, first3=0 1 2' |
--v5-metric | なし | - | assume maximum recall as 1.0 in AP calculation |
コマンドオプション | 引数 | 初期値 | 意味 |
--weights | str | yolov7s.pt | 学習済み重みモデルファイル |
--source | str | inference/images | 推論対象の画像ソース(file/folder) のパス |
--img-size | int | 640 | 推論対象の画像のサイズ(pixel) |
--conf-thres | 0.25 | float | クラス判定の閾値 (数値が小さい程オブジェクトは増えるが、ノイズも増える |
--iou-thres | 0.45 | float | iou は Intersection Over Union (検出領域が重なっている割合、数値が大きいほど重なり度合いが高い) |
--device | str | 使用プロセッサの指定(0 or 0,1,2,3 or cpu) (指定なしの場合 cuda) | |
--view-img | なし | - | 推論結果の表示 (指定すれば表示する) |
--save-txt | なし | - | 推論結果(検出座標と予測クラス)をテキストファイルで残す (*.txt) |
--save-conf | なし | - | 推論結果(クラスの確率)をテキストファイルで残す (*.txt) |
--nosave | なし | - | 推論結果の記録 (指定すれば残さない) |
--classes | str | クラスフィルタ(--class 0, or --class 0 2 3) | |
--agnostic-nms | なし | - | class-agnostic NMS |
--augment | なし | - | 拡張推論 |
--update | なし | - | モデルをアップデートする |
--project | str | runs/detect | 推論結果の記録フォルダパス |
--name | str | exp | 推論結果の記録フォルダの下のフォルダ名(推論ごとにインクリメント) |
--exist-ok | なし | - | 推論結果を上書き保存(指定すれば上書き) |
## detect2.py (original: detect.py) ## ver 0.02 2023.06.18 'Esc' key Break ## ver 0.03 2023.07.19 FPS 計測表示 import argparse import time :
: # Run inference if device.type != 'cpu': model(torch.zeros(1, 3, imgsz, imgsz).to(device).type_as(next(model.parameters()))) # run once old_img_w = old_img_h = imgsz old_img_b = 1 break_flag = False ## 'Esc' key Break 2023/06/18 t0 = time.time() for path, img, im0s, vid_cap in dataset: if break_flag: ## 'Esc' key Break 2023/06/18 break img = torch.from_numpy(img).to(device) :
: # Stream results if view_img: cv2.imshow(str(p), im0) ## 'Esc' key Break 2023/06/18 c = cv2.waitKey(1) # 1 millisecond if c == 27: break_flag = True break # Save results (image with detections) :・実行速度(FPS) を表示させる。
(py38a) PS > cd /anaconda_win/work/yolov7-main (py38a) PS > python export.py --weights runs/train/yolov7x_custom/weights/best.pt (py38a) PS > python detect2.py --weights runs/train/yolov7x_custom/weights/best.pt --conf 0.25 --img-size 640 --source 0・batch-size = 16 , epochs = 100
(py38a) PS > cd /anaconda_win/work/yolov7-main (py38a) PS > python export.py --weights runs/train/yolov7x_custom_100/weights/best.pt (py38a) PS > python detect2.py --weights runs/train/yolov7x_custom_100/weights/best.pt --conf 0.25 --img-size 640 --source 0・batch-size = 16 , epochs = 50
(py38a) PS > cd /anaconda_win/work/yolov7-main (py38a) PS > python export.py --weights runs/train/yolov7x_custom_50/weights/best.pt (py38a) PS > python detect2.py --weights runs/train/yolov7x_custom_50/weights/best.pt --conf 0.25 --img-size 640 --source 0・batch-size = 2 , epochs = 300
(py38a) PS > cd /anaconda_win/work/yolov7-main (py38a) PS > python export.py --weights runs/train/yolov7x_custom2/weights/best.pt (py38a) PS > python detect2.py --weights runs/train/yolov7x_custom2/weights/best.pt --conf 0.25 --img-size 640 --source 0