当前位置:   article > 正文

yolov8语义分割

yolov8语义分割

yolov8地址:
https://github.com/ultralytics/ultralytics
模型下载地址
https://github.com/ultralytics/assets/releases
官方教程:
https://docs.ultralytics.com/tasks/segment/#train

名声大噪的YOLO迎来YOLOv8,迅速包揽目标检测、实例分割新SOTA
YOLOv8 抛弃了前几代模型的 Anchor-Base。

YOLO 是一种基于图像全局信息进行预测的目标检测系统。自 2015 年 Joseph Redmon、Ali Farhadi 等人提出初代模型以来,领域内的研究者们已经对 YOLO 进行了多次更新迭代,模型性能越来越强大。现在,YOLOv8 已正式发布。
YOLOv8 是由小型初创公司 Ultralytics 创建并维护的,值得注意的是 YOLOv5 也是由该公司创建的。

与先前几个版本相比,YOLOv8 模型更快、更准确,同时为训练模型提供统一框架,以执行以下基本任务:

  • 目标检测;
  • 实例分割;
  • 图像分类。

一、基本环境安装(X86)

见:使用Yolov8进行目标检测并训练自己的数据集

二、预测

预测命令

会保存图片

yolo segment predict model=./weights/yolov8l-seg.pt source=./input/*.jpg save
  • 1

在这里插入图片描述
会保存视频

yolo segment predict model=./weights/yolov8l_23_0320.pt source=./a.mp4 save
  • 1

预测代码

from pathlib import Path

import cv2
import numpy as np
import torch
from PIL import Image

from ultralytics import YOLO
from ultralytics.yolo.data.build import load_inference_source
from ultralytics.yolo.utils import ROOT, SETTINGS


MODEL = './weights/yolov8l-seg.pt'
SOURCE = './input/bus.jpg'
#SOURCE = '0'

model = YOLO(MODEL)

img = cv2.imread(str(SOURCE))
output = model(source=img, save=True, conf=0.5,iou=0.7,save_txt=True,show=True)

cv2.waitKey(1000)
cv2.waitKey(0)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

在这里插入图片描述

三、C++ tensorrt 部署加速

3.1 环境安装配置

参考:yolov8+tensorrt C++部署加速

3.2 yolov8 语义分割分割结果解析

v8中output0输出8400个结果,每个结果的维度是116,而v5是25200个结果,每个结果的维度是117。简述一下v8输出的是啥,116为4+80+32,4为box的cx cy w h,80是每个类的置信度,32是分割需要用到的,和v5的区别在于少了目标的置信度,v5是4+1+80+32,这个1就是是否为目标的置信度。

3.3 pt转onnx

3.4 onnx转engine

训练

https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8s-seg.pt

yolo task=segment mode=train model=/home/diyun/Desktop/work/python_project/yolov8/weights/yolov8s-seg.pt data=/home/diyun/Desktop/work/python_project/yolov8/datasets/coco128-seg.yaml batch=2 imgsz=640 epochs=100 pretrained=True mosaic=0.0
  • 1

打标签

使用

1、YOLOv8实例分割训练自己的数据集保姆级教程

json转txt

# -*- coding: utf-8 -*-
import json
import os
import argparse
from tqdm import tqdm
 
 
def convert_label_json(json_dir, save_dir, classes):
    json_paths = os.listdir(json_dir)
    classes = classes.split(',')
 
    for json_path in tqdm(json_paths):
        # for json_path in json_paths:
        path = os.path.join(json_dir, json_path)
        with open(path, 'r') as load_f:
            json_dict = json.load(load_f)
        h, w = json_dict['imageHeight'], json_dict['imageWidth']
 
        # save txt path
        txt_path = os.path.join(save_dir, json_path.replace('json', 'txt'))
        txt_file = open(txt_path, 'w')
 
        for shape_dict in json_dict['shapes']:
            label = shape_dict['label']
            label_index = classes.index(label)
            points = shape_dict['points']
 
            points_nor_list = []
 
            for point in points:
                points_nor_list.append(point[0] / w)
                points_nor_list.append(point[1] / h)
 
            points_nor_list = list(map(lambda x: str(x), points_nor_list))
            points_nor_str = ' '.join(points_nor_list)
 
            label_str = str(label_index) + ' ' + points_nor_str + '\n'
            txt_file.writelines(label_str)
 
 
if __name__ == "__main__":
    """
    python json2txt_nomalize.py --json-dir my_datasets/color_rings/jsons --save-dir my_datasets/color_rings/txts --classes "cat,dogs"
    """
    parser = argparse.ArgumentParser(description='json convert to txt params')
    parser.add_argument('--json-dir', type=str,default='./json', help='json path dir')
    parser.add_argument('--save-dir', type=str,default='./txt' ,help='txt save dir')
    parser.add_argument('--classes', type=str, default='_background_,mangdao',help='classes')
    args = parser.parse_args()
    json_dir = args.json_dir
    save_dir = args.save_dir
    classes = args.classes
    convert_label_json(json_dir, save_dir, classes)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

训练配置文件


# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
path: /home/diyun/1_train_data/50_mandao  # dataset root dir
train: images  # train images (relative to 'path') 128 images
val: images  # val images (relative to 'path') 128 images
test: images # test images (optional)

# Classes
names:
  0: background
  1: mangdao

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

2、

yolo task=segment mode=train model=/home/huifeimao/Desktop/work/python_project/yolov8/weights/yolov8s-seg.pt data=/home/huifeimao/1_train_data/50_mandao/coco128-seg.yaml batch=2 imgsz=640 epochs=100 pretrained=True mosaic=0.0
  • 1

效果图

在这里插入图片描述

在这里插入图片描述

保存的结果是json文件

需要转换成yolo可以训练的txt格式

予以分割自动标注

推理并自动标注

yolo segment predict model=./weights/23_0731_mangdao.pt source=/home/huifeimao/1_train_data/50_mandao/train_23_0801/images/*.jpg save_txt
  • 1

予以分割结果yolo格式 转json格式

seg_yolo_to_json.py

import sys
sys.path.append('/home/ningningyang/cv/trafficlight_semi_automatic_labeler/policy')
import cv2
import os
import json
from tqdm import tqdm

IMG_FORMATS = ['.jpg']
TXT_FORMATS = ['.txt', '.yolov5']
label_participant_tag = ['background','mangdao']

label_tag = label_participant_tag
# Judge file is endswith ".jpg"
def is_jpg(path):
    ext = os.path.splitext(path)[1]
    ext = ext.lower()
    if ext in IMG_FORMATS:
        return True
    else:
        return False

# Judge file is endswith ".txt" or ".yolov5"
def is_txt(path):
    ext = os.path.splitext(path)[1]
    ext = ext.lower()
    if ext in TXT_FORMATS:
        return True
    else:
        return False

# Judge file is valid jpg file(readable and not empty)
def is_jpg_readable(path):
    ext = os.path.splitext(path)[1]
    ext = ext.lower()
    if ext in IMG_FORMATS:
        try:
            im0 = cv2.imread(path)
            if im0 is None:
                print("images empty: " + path)
                return False
            return True
        except:
            print("images error: " + path)
            return False
    else:
        return False

def load_filenames(dirpath, filenames, func="is_jpg"):
    for x in os.listdir(dirpath):
        file = os.path.join(dirpath, x)
        if os.path.isdir(file):
            load_filenames(file, filenames, func)
        else:
            if func == "is_jpg" and is_jpg(file):
                filenames.append(file)
            elif func == "is_jpg_readable" and is_jpg_readable(file):
                filenames.append(file)
            elif func == "is_txt" and is_txt(file):
                filenames.append(file)

# Load Darknet Foramt
def load_darknet(label_file_path, width=1920, height=1080):
    all_polygon = []
    file_object = open(label_file_path, 'r')
    try:
        for line in file_object:
            if line == ' ':
                continue
            d = line.split(' ')
            label = int(d[0])
            point_number= (len(d)-1)/2
            print("before point_number=",point_number)
            per_polygon=[]
            per_polygon.append(label)
            for i in range(int(point_number)):
                point_x = float(d[i*2+1]) * width
                point_y = float(d[i*2+2]) * height
                print("before point_x=",point_x)
                print("before point_y=", point_y)
                per_polygon.append(point_x)
                per_polygon.append(point_y)

            all_polygon.append(per_polygon)
    finally:
        file_object.close()

    print("before all_polygon=",all_polygon)
    return all_polygon

# Write Labelme Foramt(JSON)
def write_labelme(save_path, all_polygons, width=1920, height=1080):
    img_name = save_path.split('/')[-1].replace('.json', '.jpg')
    labelme_data = {'version': '5.2.1', 'flags': {}, 'shapes': [], 'imagePath': img_name, 'imageData': None,
                    'imageHeight': height, 'imageWidth': width}


    all_polygons_number = len(all_polygons)
    print("after all_polygons_number=", all_polygons_number)
    print("after all_polygons=", all_polygons)
    for i, polygon in enumerate(all_polygons):
        print("i=",i)
        print("after polygon=", polygon)
        labelme_bbx = {'group_id': None, 'description': "", 'shape_type': 'polygon', 'flags': {}}
        labelme_bbx['label'] = str(label_tag[int(polygon[0])])

        polygon_number =int(( len(polygon)-1)/2)
        print("after polygon_number=", polygon_number)
        points=[]
        for j in range(polygon_number):
            print("j=", j)
            polygon_point=[]
            point_x = float(polygon[ 2*j +1])
            point_y = float(polygon[ 2*j +2])

            polygon_point.append(point_x)
            polygon_point.append(point_y)
            print("after point_x=", point_x)
            print("after point_y=", point_y)
            #bbxs.append([point_x, point_y])
            points.append(polygon_point)
        labelme_bbx['points']=points


    labelme_data['shapes'].append(labelme_bbx)

    with open(save_path, 'w', encoding='utf-8') as fw:
        json.dump(labelme_data, fw, indent=4, ensure_ascii=False)

# Write Darknet Foramt(TXT)
def write_darknet(save_path, bbxs, width=1920, height=1080):
    with open(save_path, "w", encoding='utf-8') as f:
        for bbx in bbxs:
            label = str(bbx[0])
            cx = str(((bbx[1] + bbx[3]) * 0.5) / width)
            cy = str(((bbx[2] + bbx[4]) * 0.5) / height)
            w = str((bbx[3] - bbx[1]) / width)
            h = str((bbx[4] - bbx[2]) / height)
            bbx_str = "%s %s %s %s %s\n" % (label, cx, cy, w, h)
            f.writelines(bbx_str)


# Convert Darknet TO Labelme
def convert_darknet_to_labelme(yolo_path,json_path, width, height):
    filenames = []
    load_filenames(yolo_path, filenames, "is_txt")
    print("filenames=", filenames)
    for file in tqdm(filenames):
        print("file=",file)
        bbxs = load_darknet(file, width, height)
        all_polygons = load_darknet(file, width, height)
        #write_labelme(file.replace('.txt', '.json'), bbxs, width, height)
        json_path1=file.replace('.txt', '.json')
        json_path2=json_path1.replace(yolo_path, json_path)
        write_labelme(json_path2, all_polygons, width, height)


if __name__ == "__main__":

    yolo_path = "/home/huifeimao/1_train_data/50_mandao/train_23_0801/labels/"
    json_path = "/home/huifeimao/1_train_data/50_mandao/train_23_0801/json/"
    convert_darknet_to_labelme(yolo_path,json_path, 1920, 1080)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162

问题

RuntimeError: CUDA error: device-side assert triggered
CUDA kernel errors might be asynchronously reported at some other API call,so the stacktrace below might be incorrect.
For debugging consider passing CUDA_LAUNCH_BLOCKING=1.
Sentry is attempting to send 2 pending error messages
Waiting up to 2 seconds
Press Ctrl-C to quit
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

解决方法

类别里面加个background

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/727542
推荐阅读
相关标签
  

闽ICP备14008679号