Tangwx

Tangwx

博客网站

yolov5自分のデータセットをトレーニングする

yolov5 独自のデータセットをトレーニングする#

1. yolo v5のソースコードをダウンロード#

GitHub でyolov5を検索し、ultralytics/**yolov5** プロジェクトを見つけて、.zipファイルをダウンロードします。ここでは5.0バージョンをダウンロードしました。または、git bash でhttps://github.com/ultralytics/yolov5.git を入力してコードをダウンロードします。`yolov5-5.0.zip` ファイルをダウンロードした場合、ダウンロードが完了したら、コード編集の場所に解凍します。

2. Anaconda を使用して仮想環境を作成#

Anaconda 環境がない場合は、Python 環境を直接使用することもできます。

Anaconda Prompt でconda create --name yolov5 python=3.8と入力します。

y と入力してエンターを押し、次にconda activate yolov5コマンドを入力して仮想環境に入ります。

yoloV5 は要件Python>= 3.7.0環境を必要とし、PyTorch> = 1.7。を含みます。

次に、解凍した YOLO V5 プロジェクトフォルダに入り、pip install -r requirements.txtコマンドを使用してプロジェクトに必要な依存パッケージをダウンロードします(Anaconda がない場合は、このコマンドを直接使用して依存ライブラリをインストールできます。デフォルトで Python がインストールされていると仮定します)。

インストールが完了したら、PyTorchの公式サイトにアクセスします。ここでは以下の構成を選択します:

PyTorch BuildStable (1.10.2)を選択

Your OSWindowsを選択

PackagePipを選択します。ここでは pip を選択することをお勧めします。conda ではエラーが発生し続けます。

LanguagePythonを選択

Compute PlatformCUDA 10.2を選択します。グラフィックカードがある場合はこれを選択し、ない場合はCPUを選択します。

Run this Command:が表示されます。

pip3 install torch==1.10.2+cu102 torchvision==0.11.3+cu102 torchaudio===0.10.2+cu102 -f https://download.pytorch.org/whl/cu102/torch_stable.html

上記のコマンドをコンソールにコピーして、pytorch をインストールします。Successfulと表示されれば成功です。

3. VOC 形式の標準フォルダを作成#

yolov5-5.0\の下にmake_voc_dir.pyを作成します。

import os
os.makedirs('VOCdevkit/VOC2007/Annotations')
os.makedirs('VOCdevkit/VOC2007/JPEGImages')

make_voc_dir.pyを実行します。

\yolov5-5.0\VOCdevkit\VOC2007\Annotationsxml形式のファイルを保存します。

\yolov5-5.0\VOCdevkit\VOC2007\JPEGImagesJPG形式のファイルを保存します。

4. xml 形式を yolo 形式に変換#

yolov5-5.0\の下にvoc_to_yolo.pyを作成します。

import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
import random
from shutil import copyfile

classes = ["crack","helmet"]  # このリストにはあなたのクラスが格納されています

TRAIN_RATIO = 90  # トレーニングの割合


# フォルダ内の隠しファイルを削除
def clear_hidden_files(path):
    dir_list = os.listdir(path)
    for i in dir_list:
        abspath = os.path.join(os.path.abspath(path), i)
        if os.path.isfile(abspath):
            if i.startswith("._"):
                os.remove(abspath)
        else:
            clear_hidden_files(abspath)


# 幅と高さを正規化する操作 size:元の画像の幅と高さ
def convert(size, box):
    dw = 1. / size[0]
    dh = 1. / size[1]
    x = (box[0] + box[1]) / 2.0
    y = (box[2] + box[3]) / 2.0
    w = box[1] - box[0]
    h = box[3] - box[2]
    x = x * dw
    w = w * dw
    y = y * dh
    h = h * dh
    return (x, y, w, h)


# XMLを解析
def convert_annotation(image_id):
    in_file = open('VOCdevkit/VOC2007/Annotations/%s.xml' % image_id,'rb')
    out_file = open('VOCdevkit/VOC2007/YOLOLabels/%s.txt' % image_id, 'w')
    tree = ET.parse(in_file)
    root = tree.getroot()
    size = root.find('size')
    w = int(size.find('width').text)
    h = int(size.find('height').text)

    for obj in root.iter('object'):
        difficult = obj.find('difficult').text
        cls = obj.find('name').text
        if cls not in classes or int(difficult) == 1:
            continue
        cls_id = classes.index(cls)
        xmlbox = obj.find('bndbox')
        b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
             float(xmlbox.find('ymax').text))
        bb = convert((w, h), b)
        out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
    in_file.close()
    out_file.close()


wd = os.getcwd()
data_base_dir = os.path.join(wd, "VOCdevkit/")
if not os.path.isdir(data_base_dir):
    os.mkdir(data_base_dir)
work_sapce_dir = os.path.join(data_base_dir, "VOC2007/")
if not os.path.isdir(work_sapce_dir):
    os.mkdir(work_sapce_dir)
annotation_dir = os.path.join(work_sapce_dir, "Annotations/")
if not os.path.isdir(annotation_dir):
    os.mkdir(annotation_dir)
clear_hidden_files(annotation_dir)
image_dir = os.path.join(work_sapce_dir, "JPEGImages/")
if not os.path.isdir(image_dir):
    os.mkdir(image_dir)
clear_hidden_files(image_dir)
yolo_labels_dir = os.path.join(work_sapce_dir, "YOLOLabels/")
if not os.path.isdir(yolo_labels_dir):
    os.mkdir(yolo_labels_dir)
clear_hidden_files(yolo_labels_dir)
yolov5_images_dir = os.path.join(data_base_dir, "images/")
if not os.path.isdir(yolov5_images_dir):
    os.mkdir(yolov5_images_dir)
clear_hidden_files(yolov5_images_dir)
yolov5_labels_dir = os.path.join(data_base_dir, "labels/")
if not os.path.isdir(yolov5_labels_dir):
    os.mkdir(yolov5_labels_dir)
clear_hidden_files(yolov5_labels_dir)
yolov5_images_train_dir = os.path.join(yolov5_images_dir, "train/")
if not os.path.isdir(yolov5_images_train_dir):
    os.mkdir(yolov5_images_train_dir)
clear_hidden_files(yolov5_images_train_dir)
yolov5_images_test_dir = os.path.join(yolov5_images_dir, "val/")
if not os.path.isdir(yolov5_images_test_dir):
    os.mkdir(yolov5_images_test_dir)
clear_hidden_files(yolov5_images_test_dir)
yolov5_labels_train_dir = os.path.join(yolov5_labels_dir, "train/")
if not os.path.isdir(yolov5_labels_train_dir):
    os.mkdir(yolov5_labels_train_dir)
clear_hidden_files(yolov5_labels_train_dir)
yolov5_labels_test_dir = os.path.join(yolov5_labels_dir, "val/")
if not os.path.isdir(yolov5_labels_test_dir):
    os.mkdir(yolov5_labels_test_dir)
clear_hidden_files(yolov5_labels_test_dir)

train_file = open(os.path.join(wd, "yolov5_train.txt"), 'w')
test_file = open(os.path.join(wd, "yolov5_val.txt"), 'w')
train_file.close()
test_file.close()
train_file = open(os.path.join(wd, "yolov5_train.txt"), 'a')
test_file = open(os.path.join(wd, "yolov5_val.txt"), 'a')
list_imgs = os.listdir(image_dir)  # list image_one files
prob = random.randint(1, 100)
print("Probability: %d" % prob)
for i in range(0, len(list_imgs)):
    path = os.path.join(image_dir, list_imgs[i])
    if os.path.isfile(path):
        image_path = image_dir + list_imgs[i]
        voc_path = list_imgs[i]
        (nameWithoutExtention, extention) = os.path.splitext(os.path.basename(image_path))
        (voc_nameWithoutExtention, voc_extention) = os.path.splitext(os.path.basename(voc_path))
        annotation_name = nameWithoutExtention + '.xml'
        annotation_path = os.path.join(annotation_dir, annotation_name)
        label_name = nameWithoutExtention + '.txt'
        label_path = os.path.join(yolo_labels_dir, label_name)
    prob = random.randint(1, 100)
    print("Probability: %d" % prob)
    if (prob < TRAIN_RATIO):  # train dataset
        if os.path.exists(annotation_path):
            train_file.write(image_path + '\n')
            convert_annotation(nameWithoutExtention)  # convert label
            copyfile(image_path, yolov5_images_train_dir + voc_path)
            copyfile(label_path, yolov5_labels_train_dir + label_name)
    else:  # test dataset
        if os.path.exists(annotation_path):
            test_file.write(image_path + '\n')
            convert_annotation(nameWithoutExtention)  # convert label
            copyfile(image_path, yolov5_images_test_dir + voc_path)
            copyfile(label_path, yolov5_labels_test_dir + label_name)
train_file.close()
test_file.close()

voc_to_yolo.pyを実行します。

(yoloV5) E:\PythonCode\yoloV5_toukui\yolov5-5.0>python voc_to_yolo.py
Probability: 6
Probability: 79
Probability: 26
Probability: 19
Probability: 64
Probability: 5
Probability: 80
Probability: 40
Probability: 46
Probability: 23
Probability: 87
Probability: 19
Probability: 71
Probability: 62
Probability: 53
Probability: 74
Probability: 10
Probability: 19
Probability: 90
Probability: 35
Probability: 100
Probability: 27
Probability: 77
Probability: 65
Probability: 34
Probability: 95
Probability: 43

\yolov5-5.0\VOCdevkitフォルダ内にimageslabelsフォルダが生成され、フォルダ内にはtrain(トレーニングサンプル)とval(検証サンプル)フォルダがあり、フォルダ内の yolov5 はすでにラベルを正規化処理しています。中のパラメータはそれぞれ | クラス数 | 中心点座標の正規化結果 | 幅と高さの結果 | です;また、\yolov5-5.0フォルダ内にyolov5_train.txtyolov5_val.txtの 2 つのファイルが生成されました。

さらに、\yolov5-5.0\VOCdevkit\VOC2007\YOLOLabels\内のファイルはプロジェクトに影響を与えないため、直接削除できます。

5. yaml 設定ファイルを修正#

\yolov5-5.0\data\フォルダに入り、voc.yamlファイルを開きます。

元のvoc.yamlファイル

# PASCAL VOC dataset http://host.robots.ox.ac.uk/pascal/VOC/
# Train command: python train.py --data voc.yaml
# Default dataset location is next to /yolov5:
#   /parent_folder
#     /VOC
#     /yolov5


# download command/URL (optional)
download: bash data/scripts/get_voc.sh

# train and val data as 1) directory: path/images/, 2) file: path/images.txt, or 3) list: [path1/images/, path2/images/]
train: ../VOC/images/train/  # 16551 images
val: ../VOC/images/val/  # 4952 images

# number of classes
nc: 20

# class names
names: [ 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog',
         'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor' ]

修正後のvoc.yamlファイル

# PASCAL VOC dataset http://host.robots.ox.ac.uk/pascal/VOC/
# Train command: python train.py --data voc.yaml
# Default dataset location is next to /yolov5:
#   /parent_folder
#     /VOC
#     /yolov5


# download command/URL (optional)
download: bash data/scripts/get_voc.sh

# train and val data as 1) directory: path/images/, 2) file: path/images.txt, or 3) list: [path1/images/, path2/images/]
train: VOCdevkit/images/train/ # train: ../VOC/images/train/  # 16551 images
val: VOCdevkit/images/val/ # val: ../VOC/images/val/  # 4952 images

# number of classes
nc: 2 # nc: 20

# class names
names: [ 'crack', 'helmet' ]
# names: [ 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog',
#          'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor' ]

trainvalを目的のパスに修正します。

ncはクラスの数です。

namesはクラス名です。

6. トレーニングを開始#

6.1 重みファイルのダウンロード#

\yolov5-5.0\weights\.ptファイルを置きます。download_weights.shを実行して公式の.ptファイルをダウンロードすることもできますし、GitHub から直接ダウンロードすることもできます。

ここではyolov5m.ptの標準モデルを使用します。

6.2 パラメータの修正#

次にtrain.pyを開き、if __name__ == '__main__':を見つけてパラメータを修正します。


重みファイルのパスをdefault='weights/yolov5m.pt'に修正します。

parser.add_argument('--weights', type=str, default='weights/yolov5m.pt'


config は変更しても変更しなくてもかまいませんが、ここではdefault='models/yolov5m.yaml'に修正します。

parser.add_argument('--cfg', type=str, default='models/yolov5m.yaml',

ここでのyolov5m.yamlanchors:kmeansを使用してクラスタリングする必要があります。

このブログを参考にして設定できますYOLOv5 独自のデータセットをトレーニングする


データファイルのパスをdefault='data/voc.yaml',に修正します。

parser.add_argument('--data', type=str, default='data/voc.yaml',


hypはランダム数に関連するパラメータで、変更する必要はありません。


epochsはトレーニングの回数で、ここでは 300 回ですdefault=300


batch-sizeは一度に与えるバッチ数で、ここではメモリ制限のために 1 回だけ与えますdefault=1
parser.add_argument('--batch-size', type=int, default=1,


img-sizeは入力画像のサイズで、ここでは 640x640 ですdefault=[640, 640]


他のパラメータは変更する必要はありません。これでトレーニングを開始できます!!!


ここでエラーAttributeError: Can't get attribute 'SPPF' on <module 'models.common' from 'E:\\PythonCode\\yoloV5_toukui\\yolov5-5.0\\models\\common.py'>が発生します。

解決方法:

Tags6 のmodel/common.pyに行き、SPPF クラスを見つけて、それを Tags5 の model/common.py にコピーします。これでコードにもこのクラスが追加されます。また、warningsパッケージをインポートする必要があります。

SPPF クラスの内容を以下のように追加し、common.pyにコピーしてください。import warningsを上に置くことを忘れないでください:

import warnings

class SPPF(nn.Module):
    # Spatial Pyramid Pooling - Fast (SPPF) layer for YOLOv5 by Glenn Jocher
    def __init__(self, c1, c2, k=5):  # equivalent to SPP(k=(5, 9, 13))
        super().__init__()
        c_ = c1 // 2  # hidden channels
        self.cv1 = Conv(c1, c_, 1, 1)
        self.cv2 = Conv(c_ * 4, c2, 1, 1)
        self.m = nn.MaxPool2d(kernel_size=k, stride=1, padding=k // 2)

    def forward(self, x):
        x = self.cv1(x)
        with warnings.catch_warnings():
            warnings.simplefilter('ignore')  # suppress torch 1.9.0 max_pool2d() warning
            y1 = self.m(x)
            y2 = self.m(y1)
            return self.cv2(torch.cat([x, y1, y2, self.m(y2)], 1))

完璧に解決しました。


train.pyを実行します。

(yoloV5_toukui) E:\PythonCode\yoloV5_toukui\yolov5-5.0>python train.py
github: skipping check (not a git repository)
YOLOv5  2021-4-11 torch 1.10.1+cpu CPU

Namespace(adam=False, artifact_alias='latest', batch_size=16, bbox_interval=-1, bucket='', cache_images=False, cfg='models/yolov5m.yaml', data='data/voc.yaml', device='', entity=None, epochs=300, evolve=False, exist_ok=False, global_rank=-1, hyp='data/hyp.scratch.yaml', image_weights=False, img_size=[640, 640], label_smoothing=0.0, linear_lr=False, local_rank=-1, multi_scale=False, name='exp', noautoanchor=False, nosave=False, notest=False, project='runs/train', quad=False, rect=False, resume=False, save_dir='runs\\train\\exp', save_period=-1, single_cls=False, sync_bn=False, total_batch_size=16, upload_dataset=False, weights='weights/yolov5m.pt', workers=8, world_size=1)
tensorboard: Start with 'tensorboard --logdir runs/train', view at http://localhost:6006/
hyperparameters: lr0=0.01, lrf=0.2, 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
wandb: Install Weights & Biases for YOLOv5 logging with 'pip install wandb' (recommended)
Overriding model.yaml nc=80 with nc=2

                 from  n    params  module                                  arguments
  0                -1  1      5280  models.common.Focus                     [3, 48, 3]
  1                -1  1     41664  models.common.Conv                      [48, 96, 3, 2]
  2                -1  1     65280  models.common.C3                        [96, 96, 2]
  3                -1  1    166272  models.common.Conv                      [96, 192, 3, 2]
  4                -1  1    629760  models.common.C3                        [192, 192, 6]
  5                -1  1    664320  models.common.Conv                      [192, 384, 3, 2]
  6                -1  1   2512896  models.common.C3                        [384, 384, 6]
  7                -1  1   2655744  models.common.Conv                      [384, 768, 3, 2]
  8                -1  1   1476864  models.common.SPP                       [768, 768, [5, 9, 13]]
  9                -1  1   4134912  models.common.C3                        [768, 768, 2, False]
 10                -1  1    295680  models.common.Conv                      [768, 384, 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   1182720  models.common.C3                        [768, 384, 2, False]
 14                -1  1     74112  models.common.Conv                      [384, 192, 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    296448  models.common.C3                        [384, 192, 2, False]
 18                -1  1    332160  models.common.Conv                      [192, 192, 3, 2]
 19          [-1, 14]  1         0  models.common.Concat                    [1]
 20                -1  1   1035264  models.common.C3                        [384, 384, 2, False]
 21                -1  1   1327872  models.common.Conv                      [384, 384, 3, 2]
 22          [-1, 10]  1         0  models.common.Concat                    [1]
 23                -1  1   4134912  models.common.C3                        [768, 768, 2, False]
 24      [17, 20, 23]  1     28287  models.yolo.Detect                      [2, [[10, 13, 16, 30, 33, 23], [30, 61, 62, 45, 59, 119], [116, 90, 156, 198, 373, 326]], [192, 384, 768]]
D:\software\Anaconda3\envs\yoloV5_toukui\lib\site-packages\torch\functional.py:445: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at  ..\aten\src\ATen\native\TensorShape.cpp:2157.)
  return _VF.meshgrid(tensors, **kwargs)  # type: ignore[attr-defined]
Model Summary: 391 layers, 21060447 parameters, 21060447 gradients, 50.4 GFLOPS

Transferred 402/506 items from weights/yolov5m.pt
Scaled weight_decay = 0.0005
Optimizer groups: 86 .bias, 86 conv.weight, 83 other
train: Scanning 'VOCdevkit\labels\train' images and labels... 23 found, 0 missing, 0 empty, 0 corrupted: 100%|| 23/23
train: New cache created: VOCdevkit\labels\train.cache
val: Scanning 'VOCdevkit\labels\val' images and labels... 3 found, 0 missing, 0 empty, 0 corrupted: 100%|| 3/3 [00:00<
val: New cache created: VOCdevkit\labels\val.cache
Plotting labels...

最終結果

   Epoch   gpu_mem       box       obj       cls     total    labels  img_size
   299/299     1.21G   0.04496   0.02003   0.02956   0.09454         1       640:   4%|███                                                                   | 1/23 [00:00<00:03,  5.90it/   299/299     1.21G   0.06122   0.02847    0.0249    0.1146         6       640:   4%|███                                                                   | 1/23 [00:00<00:03,  5.90it/   299/299     1.21G   0.06122   0.02847    0.0249    0.1146         6       640:  13%|█████████▏                                                            | 3/23 [00:00<00:03,     299/299     1.21G   0.05973   0.02952   0.02186    0.1111         3       640:  13%|█████████▏                                                            | 3/23 [00:00<00:03,     299/299     1.21G   0.05973   0.02952   0.02186    0.1111         3       640:  17%|████████████▏                                                         | 4/23 [00:00<00:03   299/299     1.21G   0.05656   0.02924   0.02161    0.1074         2       640:  17%|████████████▏                                                         | 4/23 [00:00<00:03   299/299     1.21G   0.05656   0.02924   0.02161    0.1074         2       640:  22%|███████████████▏                                                      | 5/23 [00:00<00   299/299     1.21G   0.05358   0.02645    0.0194   0.09942         1       640:  22%|███████████████▏                                                      | 5/23 [00:01<00   299/299     1.21G   0.05662   0.02863   0.01956    0.1048         5       640:  26%|██████████████████▎                                                   | 6/23 [00:01   299/299     1.21G   0.05662   0.02863   0.01956    0.1048         5       640:  30%|█████████████████████▎                                                | 7/23 [00   299/299     1.21G   0.05714   0.02914   0.02038    0.1067         2       640:  30%|█████████████████████▎                                                | 7/23 [00   299/299     1.21G   0.05714   0.02914   0.02038    0.1067         2       640:  35%|████████████████████████▎                                             | 8/23    299/299     1.21G   0.05828   0.03214    0.0208    0.1112         5       640:  35%|████████████████████████▎                                             | 8/23    299/299     1.21G   0.05828   0.03214    0.0208    0.1112         5       640:  39%|███████████████████████████▍                                          | 9/   299/299     1.21G   0.06111   0.03179   0.02086    0.1138         3       640:  39%|███████████████████████████▍                                          | 9/   299/299     1.21G   0.06111   0.03179   0.02086    0.1138         3       640:  43%|██████████████████████████████                                       | 1   299/299     1.21G   0.05965   0.03091   0.01985    0.1104         1       640:  43%|██████████████████████████████                                       | 1   299/299     1.21G   0.05965   0.03091   0.01985    0.1104         1       640:  48%|█████████████████████████████████                                       299/299     1.21G   0.06099   0.03505   0.01993     0.116        10       640:  48%|█████████████████████████████████                                       299/299     1.21G   0.06099   0.03505   0.01993     0.116        10       640:  52%|████████████████████████████████████                                 299/299     1.21G   0.06144   0.03449   0.01998    0.1159         3       640:  52%|████████████████████████████████████                                 299/299     1.21G   0.06144   0.03449   0.01998    0.1159         3       640:  57%|███████████████████████████████████████                           299/299     1.21G   0.06225   0.03479   0.02013    0.1172         3       640:  57%|███████████████████████████████████████                           299/299     1.21G   0.06225   0.03479   0.02013    0.1172         3       640:  61%|██████████████████████████████████████████                     299/299     1.21G   0.06229   0.03422   0.02033    0.1168         3       640:  61%|██████████████████████████████████████████                     299/299     1.21G   0.06229   0.03422   0.02033    0.1168         3       640:  65%|█████████████████████████████████████████████               299/299     1.21G   0.06267   0.03524   0.02006     0.118        10       640:  65%|█████████████████████████████████████████████               299/299     1.21G   0.06267   0.03524   0.02006     0.118        10       640:  70%|████████████████████████████████████████████████         299/299     1.21G   0.06301   0.03419   0.01984     0.117         1       640:  70%|████████████████████████████████████████████████         299/299     1.21G   0.06301   0.03419   0.01984     0.117         1       640:  74%|███████████████████████████████████████████████████   299/299     1.21G    0.0642   0.03599   0.02012    0.1203        11       640:  74%|███████████████████████████████████████████████████   299/299     1.21G    0.0642   0.03599   0.02012    0.1203        11       640:  78%|███████████████████████████████████████████████████   299/299     1.21G   0.06377   0.03509   0.02048    0.1193         1       640:  78%|███████████████████████████████████████████████████   299/299     1.21G   0.06377   0.03509   0.02048    0.1193         1       640:  83%|███████████████████████████████████████████████████   299/299     1.21G   0.06399   0.03754   0.02059    0.1221         8       640:  83%|███████████████████████████████████████████████████   299/299     1.21G   0.06399   0.03754   0.02059    0.1221         8       640:  87%|███████████████████████████████████████████████████   299/299     1.21G   0.06392   0.03851   0.02078    0.1232         6       640:  87%|███████████████████████████████████████████████████   299/299     1.21G   0.06392   0.03851   0.02078    0.1232         6       640:  91%|███████████████████████████████████████████████████   299/299     1.21G   0.06446   0.03858   0.02099     0.124         4       640:  91%|███████████████████████████████████████████████████   299/299     1.21G   0.06446   0.03858   0.02099     0.124         4       640:  96%|███████████████████████████████████████████████████   299/299     1.21G   0.06421   0.03818   0.02112    0.1235         2       640:  96%|███████████████████████████████████████████████████   299/299     1.21G   0.06421   0.03818   0.02112    0.1235         2       640: 100%|███████████████████████████████████████████████████   299/299     1.21G   0.06421   0.03818   0.02112    0.1235         2       640: 100%|█████████████████████████████████████████████████████████████████████| 23/23 [00:04<00:00,  5.65it/s]
               Class      Images      Labels           P           R      [email protected]  [email protected]:.95:  50%|█████████████████████████████▌                             |               Class      Images      Labels           P           R      [email protected]  [email protected]:.95: 100%|█████████████████████████████████████████████               Class      Images      Labels           P           R      [email protected]  [email protected]:.95: 100%|███████████████████████████████████████████████████████████| 2/2 [00:00<00:00,  5.45it/s]
                 all           3          12       0.182        0.55       0.177      0.0306
               crack           3          10       0.166         0.1      0.0881      0.0248
              helmet           3           2       0.199           1       0.265      0.0364
300 epochs completed in 0.638 hours.

Optimizer stripped from runs\train\exp4\weights\last.pt, 42.5MB
Optimizer stripped from runs\train\exp4\weights\best.pt, 42.5MB

各パラメータの意味:

box 回帰ボックスの損失

obj 信頼度の損失

cls クラスの損失

total 総損失

[email protected] [email protected]:.95 信頼度が 0.5~0.95 の信頼度の map 値

7. トレーニングした重みファイルを使用して認識#

detect.pyを開き、if __name__ == '__main__':を見つけます。

事前トレーニングされた重みを読み込みますdefault='weights/yolov5s.pt'default='runs/train/exp/weights/last.pt'に修正します。

テストする画像をdata/imagesに置きます。

python detect.pyを実行し、実行が完了すると、runsの下にdetectフォルダが生成され、その中にexpフォルダがあり、予測後の結果が保存されています。

動画ストリームの検出を行うには、sourceのファイルパスを動画のパスに変更するだけです。

parser.add_argument('--source', type=str, default='data/images', help='source') 

これを

parser.add_argument('--source', type=str, default='/Desktop/a.mp4', help='source') 

に修正します。

8 USB カメラを使用して認識#

detect.pyを開き、if __name__ == '__main__':を見つけます。

パスを 0 に設定します。

parser.add_argument('--source', type=str, default='0'

次に、yolov5-5.0\utils\datasets.pyの 279 行〜282 行をコメントアウトします。

if 'youtube.com/' in url or 'youtu.be/' in url:  # if source is YouTube video
                check_requirements(('pafy', 'youtube_dl'))
                import pafy
                url = pafy.new(url).getbest(preftype="mp4").url

その後、python detect.pyを実行すれば OK です。

9 tensorboard を使用してトレーニングプロセスを可視化#

読み込み中...
文章は、創作者によって署名され、ブロックチェーンに安全に保存されています。