• 使用yolov8 进行实例分割训练


    1、基于windows 的ISAM标注

    直接下载安装包,解压后即可使用

    链接:https://pan.baidu.com/s/1u_6jk-7sj4CUK1DC0fDEXQ 
    提取码:c780 

    2、标注结果转yolo格式

    通过ISAM标注后的json文件路径

    原始json格式如下:

    ISAM.json 转 yolo.txt 代码如下:

    注意提前设置好自己的分类category_mapping 、原始路径及目标路径

    1. import json
    2. import os
    3. # 定义类别名称与ID号的映射
    4. # 需要注意的是,不需要按照ISAT的classesition.txt里面的定义来
    5. # 可以选择部分自己需要的类别, ID序号也可以重新填写(从0开始)
    6. category_mapping = {"hand":0, "body": 1, "head":2,"foot":3,"qunzi":4,"hair":5,"hat":6,"package":7,"huxu":8,"glass":9,"tool":10}
    7. # ISAT格式的实例分割标注文件
    8. ISAT_FOLDER = "./isam/source/"
    9. # YOLO格式的实例分割标注文件
    10. YOLO_FOLDER = "./isam/dest"
    11. # 创建YoloV8标注的文件夹
    12. if not os.path.exists(YOLO_FOLDER):
    13. os.makedirs(YOLO_FOLDER)
    14. # 载入所有的ISAT的JSON文件
    15. for filename in os.listdir(ISAT_FOLDER):
    16. if not filename.endswith(".json"):
    17. # 不是json格式, 跳过
    18. continue
    19. # 载入ISAT的JSON文件
    20. with open(os.path.join(ISAT_FOLDER, filename), "r") as f:
    21. isat = json.load(f)
    22. # 提取文件名(不带文件后缀)
    23. image_name = filename.split(".")[0]
    24. # Yolo格式的标注文件名, 后缀是txt
    25. yolo_filename = f"{image_name}.txt"
    26. # 写入信息
    27. with open(os.path.join(YOLO_FOLDER, yolo_filename), "w") as f:
    28. # 获取图像信息
    29. # - 图像宽度
    30. image_width = isat["info"]["width"]
    31. # - 图像高度
    32. image_height = isat["info"]["height"]
    33. # print(isat["objects"])
    34. # 获取实例标注数据
    35. for annotation in isat["objects"]:
    36. # 获取类别名称
    37. category_name = annotation["category"]
    38. # print(category_name)
    39. # 如果不在类别名称字典里面,跳过
    40. if category_name not in category_mapping:
    41. continue
    42. # 从字典里面查询类别ID
    43. category_id = category_mapping[category_name]
    44. # 提取分割信息
    45. segmentation = annotation["segmentation"]
    46. segmentation_yolo = []
    47. # 遍历所有的轮廓点
    48. print(segmentation)
    49. for segment in segmentation:
    50. # 提取轮廓点的像素坐标 x, y
    51. x, y = segment
    52. # 归一化处理
    53. x_center = x/image_width
    54. y_center = y/image_height
    55. # 添加到segmentation_yolo里面
    56. segmentation_yolo.append(f"{x_center:.4f} {y_center:.4f}")
    57. segmentation_yolo_str = " ".join(segmentation_yolo)
    58. # 添加一行Yolo格式的实例分割数据
    59. # 格式如下: class_id x1 y1 x2 y2 ... xn yn\n
    60. f.write(f"{category_id} {segmentation_yolo_str}\n")

    转化后文件内容:

    3、准备训练数据

    注意安装依赖

    pip install tqdm -i  https://mirrors.aliyun.com/pypi/simple

    1. import os
    2. import random
    3. from tqdm import tqdm
    4. # 指定 images 文件夹路径
    5. image_dir = "./isam/images"
    6. # 指定 labels 文件夹路径
    7. label_dir = "./isam/labels"
    8. # 创建一个空列表来存储有效图片的路径
    9. valid_images = []
    10. # 创建一个空列表来存储有效 label 的路径
    11. valid_labels = []
    12. # 遍历 images 文件夹下的所有图片
    13. for image_name in os.listdir(image_dir):
    14. # 获取图片的完整路径
    15. image_path = os.path.join(image_dir, image_name)
    16. # 获取图片文件的扩展名
    17. ext = os.path.splitext(image_name)[-1]
    18. # 根据扩展名替换成对应的 label 文件名
    19. label_name = image_name.replace(ext, ".txt")
    20. # 获取对应 label 的完整路径
    21. label_path = os.path.join(label_dir, label_name)
    22. # 判断 label 是否存在
    23. if not os.path.exists(label_path):
    24. # 删除图片
    25. os.remove(image_path)
    26. print("deleted:", image_path)
    27. else:
    28. # 将图片路径添加到列表中
    29. valid_images.append(image_path)
    30. # 将label路径添加到列表中
    31. valid_labels.append(label_path)
    32. # print("valid:", image_path, label_path)
    33. dirs = ["./isam/datasets/test", "./isam/datasets/train", "./isam/datasets/valid"]
    34. for d in dirs:
    35. _dir = os.path.join(d, "images")
    36. if not os.path.exists(_dir):
    37. os.makedirs(_dir)
    38. _dir = os.path.join(d, "labels")
    39. if not os.path.exists(_dir):
    40. os.makedirs(_dir)
    41. # 遍历每个有效图片路径
    42. for i in tqdm(range(len(valid_images))):
    43. image_path = valid_images[i]
    44. label_path = valid_labels[i]
    45. # 随机生成一个概率
    46. r = random.random()
    47. # 判断图片应该移动到哪个文件夹
    48. # train:validtest = 7:2:1
    49. if r < 0.1:
    50. # 移动到 test 文件夹
    51. destination = "./isam/datasets/test"
    52. elif r < 0.3:
    53. # 移动到 valid 文件夹
    54. destination = "./isam/datasets/valid"
    55. else:
    56. # 移动到 train 文件夹
    57. destination = "./isam/datasets/train"
    58. # 生成目标文件夹中图片的新路径
    59. image_destination_path = os.path.join(destination, "images", os.path.basename(image_path))
    60. # 移动图片到目标文件夹
    61. os.rename(image_path, image_destination_path)
    62. # 生成目标文件夹中 label 的新路径
    63. label_destination_path = os.path.join(destination, "labels", os.path.basename(label_path))
    64. # 移动 label 到目标文件夹
    65. os.rename(label_path, label_destination_path)
    66. print("train images:", train_images)
    67. # 输出有效label路径列表
    68. print("train labels:", train_labels)

    数据集分割结果

    4、创建conda虚拟环境

    conda create -n yolov8 python=3.10

    conda activate yolov8

    下载yolov8.2 代码 

    GitHub - ultralytics/ultralytics: NEW - YOLOv8 🚀 in PyTorch > ONNX > OpenVINO > CoreML > TFLite

    # 安装依赖

    pip install ultralytics

    5、准备训练配置文件

    下载预训练模型,放在项目根路径

    yolov8n-seg.pt

    yolov8n.pt

    下载ttf文件 存放位置 /root/.config/Ultralytics/Arial.ttf

    在datasets目录下添加文件

    1、coco128-seg.yaml  注意classes类型与之前标注的一致

    1. # Ultralytics YOLO , AGPL-3.0 license
    2. # COCO128-seg dataset https://www.kaggle.com/ultralytics/coco128 (first 128 images from COCO train2017) by Ultralytics
    3. # Example usage: yolo train data=coco128.yaml
    4. # parent
    5. # ├── ultralytics
    6. # └── datasets
    7. # └── coco128-seg ← downloads here (7 MB)
    8. # 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, ..]
    9. path: ../datasets # dataset root dir
    10. train: train/images # train images (relative to 'path') 128 images
    11. val: valid/images # val images (relative to 'path') 128 images
    12. test: test/images # test images (optional)
    13. # Classes {"hand":0, "body": 1, "head":2,"foot":3,"qunzi":4,"hair":5,"hat":6,"package":7,"huxu":8,"glass":9,"tool":10}
    14. names:
    15. 0: hand
    16. 1: body
    17. 2: head
    18. 3: foot
    19. 4: qunzi
    20. 5: hair
    21. 6: hat
    22. 7: package
    23. 8: huxu
    24. 9: glass
    25. 10: tool

    2、yolov8-seg.yaml  修改nc 分类个数即可

    1. # Ultralytics YOLO , AGPL-3.0 license
    2. # YOLOv8-seg instance segmentation model. For Usage examples see https://docs.ultralytics.com/tasks/segmenthttps://docs.ultralytics.com/tasks/segment
    3. # Parameters
    4. nc: 11 # number of classes
    5. scales: # model compound scaling constants, i.e. 'model=yolov8n-seg.yaml' will call yolov8-seg.yaml with scale 'n'
    6. # [depth, width, max_channels]
    7. n: [0.33, 0.25, 1024]
    8. s: [0.33, 0.50, 1024]
    9. m: [0.67, 0.75, 768]
    10. l: [1.00, 1.00, 512]
    11. x: [1.00, 1.25, 512]
    12. # YOLOv8.0n backbone
    13. backbone:
    14. # [from, repeats, module, args]
    15. - [-1, 1, Conv, [64, 3, 2]] # 0-P1/2
    16. - [-1, 1, Conv, [128, 3, 2]] # 1-P2/4
    17. - [-1, 3, C2f, [128, True]]
    18. - [-1, 1, Conv, [256, 3, 2]] # 3-P3/8
    19. - [-1, 6, C2f, [256, True]]
    20. - [-1, 1, Conv, [512, 3, 2]] # 5-P4/16
    21. - [-1, 6, C2f, [512, True]]
    22. - [-1, 1, Conv, [1024, 3, 2]] # 7-P5/32
    23. - [-1, 3, C2f, [1024, True]]
    24. - [-1, 1, SPPF, [1024, 5]] # 9
    25. # YOLOv8.0n head
    26. head:
    27. - [-1, 1, nn.Upsample, [None, 2, 'nearest']]
    28. - [[-1, 6], 1, Concat, [1]] # cat backbone P4
    29. - [-1, 3, C2f, [512]] # 12
    30. - [-1, 1, nn.Upsample, [None, 2, 'nearest']]
    31. - [[-1, 4], 1, Concat, [1]] # cat backbone P3
    32. - [-1, 3, C2f, [256]] # 15 (P3/8-small)
    33. - [-1, 1, Conv, [256, 3, 2]]
    34. - [[-1, 12], 1, Concat, [1]] # cat head P4
    35. - [-1, 3, C2f, [512]] # 18 (P4/16-medium)
    36. - [-1, 1, Conv, [512, 3, 2]]
    37. - [[-1, 9], 1, Concat, [1]] # cat head P5
    38. - [-1, 3, C2f, [1024]] # 21 (P5/32-large)
    39. - [[15, 18, 21], 1, Segment, [nc, 32, 256]] # Segment(P3, P4, P5)

    6、开始训练

    在根目录下添加train.py 文件

    执行 python train.py

    1. from ultralytics import YOLO
    2. # Load a model
    3. model = YOLO("datasets/yolov8-seg.yaml") # build a new model from scratch
    4. model = YOLO('yolov8n-seg.pt') # load a pretrained model (recommended for training)
    5. model = YOLO('datasets/yolov8-seg.yaml').load('yolov8n.pt') # build from YAML and transfer weights
    6. # Use the model
    7. model.train(data="datasets/coco128-seg.yaml", task="segment",mode="train",workers=0,batch=4,epochs=300,device=0) # train the model

    训练结果保存位置 Results saved to runs/segment/trainX

    7、运行模型预测

    编写 predict.py 脚本,执行 python predict.py

    1. from ultralytics import YOLO
    2. import cv2
    3. # Load a model
    4. model = YOLO('yolov8n-seg.pt') # load an official model
    5. model = YOLO('runs/segment/train/weights/best.pt') # load a custom trained
    6. # Predict with the model
    7. result = model('14.png',save=True) # predict on an image

    预测结果保存位置 Results saved to runs/segment/predict

    8、导出onnx文件

    1. # export_onnx.py
    2. from ultralytics import YOLO
    3. # Load a model
    4. model = YOLO('yolov8n-seg.pt') # load an official model
    5. model = YOLO('runs/segment/train/best.pt') # load a custom trained
    6. # Export the model
    7. model.export(format='onnx')

    本文参考:

    ISAM一款基于SAM的交互式半自动图像分割标注工具

    SAM标注+yolov8-seg实例分割的实时检测-知乎

    yolov8文档

  • 相关阅读:
    MySQL何时适合创建索引,需要注意什么以及创建原则
    星际争霸之小霸王之小蜜蜂(十七)--究极体
    springboot 配置kafka批量消费,并发消费
    软件工程毕业设计课题(73)微信小程序毕业设计PHP菜谱美食小程序系统设计与实现
    汽车娱乐系统解决方案,你了解多少?
    副业教程之如何通过出售API赚取美元含数据集和训练教程
    new CCDIKSolver( OOI.kira, iks ); // 创建逆运动学求解器
    ES 面试手册
    现代对称密码
    mars3d实现禁止地图移动,禁止地图左右平移,但是鼠标可以移动的效果。
  • 原文地址:https://blog.csdn.net/xcg340123/article/details/137878031