YOLOV8学习

一、环境配置

1、下载源码:GitHub - ultralytics/ultralytics: NEW - YOLOv8 in PyTorch > ONNX > CoreML > TFLite

并命名为/YOLOV8/

2、创建虚拟环境:conda create -n yolov8 python=3.8 -y

打开环境:conda activate yolov8

安装pytorh:去官网找对应cuda版本的安装指令,这里使用

conda install pytorch==1.13.1 torchvision==0.14.1 torchaudio==0.13.1 pytorch-cuda=11.6 -c pytorch -c nvidia

 安装其它库:在/YOLOV8/路径下执行 pip install ultralytics

3、新建文件夹/pre_models/,下载预训练模型并放入其中。执行demo命令:

yolo predict model=pre_models/yolov8n.pt source=ultralytics/assets/bus.jpg

二、代码测试

1、在 /YOLOV8/ 路径下创建文件夹 /datasets/

2、以coco128(COCO2017的前128张图像组成的小数据集)进行训练和测试验证,其配置文件为 /YOLOV8/ultralytics/datasets/coco128.yaml

3、下载coco128数据集,地址为:https://www.kaggle.com/ultralytics/coco128,放在 /datasets/ 下

4、单卡:yolo task=detect mode=train model=pre_models/yolov8n.pt data=coco128.yaml device=0

多卡:yolo task=detect mode=train model=pre_models/yolov8n.pt data=coco128.yaml device=\'0,1,2,3\'

三、训练AntiUAV410

1、数据集准备,在 /datasets/ 下创建 /AntiUAV410/ ,结构如下

AntiUAV410

--images

----test

----train

----val

--labels

2、images下存放的是图像,labels下存放的是标签,三个txt存放的是图像的路径

3、在 /AntiUAV410/ 下新建 trans.py,实现原始数据的转换处理,主要从json文件中提取ground truth信息到txt

import json
import os

train_json = "/data3/publicData/Anti-UAV410/Anti-UAV/annotations/train.json"
test_json = "/data3/publicData/Anti-UAV410/Anti-UAV/annotations/test.json"
val_json = "/data3/publicData/Anti-UAV410/Anti-UAV/annotations/val.json"
jsons = [train_json, test_json, val_json]

labels_path = './datasets/AntiUAV410/labels/'
for paths in ['train', 'test', 'val']:
    path = os.path.join(labels_path, paths)
    if not os.path.exists(path):
        os.makedirs(path)

images_path = './images/'
files_path = './datasets/AntiUAV410/'

for json_ in jsons:
    files_txt = os.path.join(files_path, json_.split('/')[-1].split('.json')[0] + '.txt')
    f_files = open(files_txt,'w')

    with open(json_, 'r') as f:
        dataset = json.load(f)
    
    for ann in dataset['annotations']:
        image_id = ann['image_id']
        bbox = ann['bbox']
        file_name = dataset['images'][ann['image_id']-1]['file_name']
        classes = ann['category_id'] - 1
        h = dataset['images'][ann['image_id']-1]['height']
        w = dataset['images'][ann['image_id']-1]['width']
        
        strs = str(classes) + ' ' + str(bbox[0]/w) + ' ' + str(bbox[1]/h) + ' ' + str(bbox[2]/w) + ' ' + str(bbox[3]/h)

        path1 = os.path.join(labels_path, json_.split('/')[-1].split('.json')[0], file_name.split('/')[0])
        path2 = file_name.split('/')[1].split('.jpg')[0] + '.txt'
        label_txt = os.path.join(path1, path2)

        if not os.path.exists(path1):
            os.makedirs(path1)
        with open(label_txt, 'w') as f_txt:
            f_txt.write(strs)

        path3 = os.path.join(images_path, json_.split('/')[-1].split('.json')[0], file_name)
        f_files.write(path3 + '\n')

    f_files.close()

 4、测试单卡:yolo task=detect mode=train model=pre_models/yolov8n.pt data=antiuav410.yaml device=0

多卡训练:yolo task=detect mode=train model=pre_models/yolov8s.pt data=antiuav410.yaml device=\'0,1,2,3\' (model=xx.pt表示使用预训练权重)

yolo task=detect mode=train data=antiuav410.yaml model=yolov8s.yaml device=\'0,1,2,3\' (model=xx.yaml表示从头训练)

5、默认配置文件:/YOLOV8/ultralytics/yolo/cfg/default.yaml

你可能感兴趣的:(YOLO,学习)