【深度学习】yolov8 seg实例分割训练,交通灯

news2024/10/6 22:32:24

文章目录

  • 一、类别定义
  • 二、标注后再清洗数据
  • 三、训练yolov8 seg
  • 四、部署
  • 五、代码资料

一、类别定义

类别0:

在这里插入图片描述

类别1:

在这里插入图片描述
类别2:

在这里插入图片描述

类别3:

在这里插入图片描述

类别4:

在这里插入图片描述
类别5:
在这里插入图片描述

类别6:

在这里插入图片描述

类别7:

在这里插入图片描述

二、标注后再清洗数据

删除没有标签json的图:

import os
import json

# Define the source directory containing the images and JSON files
src = r"G:\honglvdeng\images\xuanze_copy"

# List all files in the source directory
files = os.listdir(src)

# Separate image files and JSON files
image_files = [f for f in files if f.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif'))]
json_files = [f for f in files if f.lower().endswith('.json')]

# Create a set of base names of JSON files (without extension)
json_base_names = set(os.path.splitext(f)[0] for f in json_files)

# Iterate over the image files
for image_file in image_files:
    # Get the base name of the image file (without extension)
    base_name = os.path.splitext(image_file)[0]

    # Check if the corresponding JSON file exists
    if base_name not in json_base_names:
        # If not, delete the image file
        image_path = os.path.join(src, image_file)
        os.remove(image_path)
        print(f"Deleted image without annotation: {image_path}")

将圆形标签json转为txt yolo seg 多边形标签:

import os
import json
import math


def circle_to_polygon(center, radius, num_points=40):
    """Convert a circle to a polygon with a given number of points."""
    points = []
    for i in range(num_points):
        angle = 2 * math.pi * i / num_points
        x = center[0] + radius * math.cos(angle)
        y = center[1] + radius * math.sin(angle)
        points.append((x, y))
    return points


def convert_json_to_yolov8_format(json_file_path, output_dir):
    """Convert a JSON file to YOLOv8 segmentation format."""
    with open(json_file_path, 'r') as f:
        data = json.load(f)

    shapes = data.get('shapes', [])
    image_path = data.get('imagePath', '')
    image_height = data.get('imageHeight', 0)
    image_width = data.get('imageWidth', 0)

    yolov8_data = []

    for shape in shapes:
        if shape['shape_type'] == 'circle':
            center = shape['points'][0]
            edge = shape['points'][1]
            radius = math.sqrt((center[0] - edge[0]) ** 2 + (center[1] - edge[1]) ** 2)
            polygon_points = circle_to_polygon(center, radius)
            normalized_polygon_points = [(x / image_width, y / image_height) for x, y in polygon_points]
            yolov8_data.append({
                "label": shape['label'],
                "points": normalized_polygon_points
            })

    output_file_path = os.path.join(output_dir, os.path.splitext(os.path.basename(json_file_path))[0] + '.txt')

    with open(output_file_path, 'w') as f:
        for item in yolov8_data:
            label = item['label']
            points = ' '.join([f"{round(x, 6)} {round(y, 6)}" for x, y in item['points']])
            f.write(f"{label} {points}\n")


def process_directory(src):
    """Process all JSON files in the given directory."""
    for file_name in os.listdir(src):
        if file_name.endswith('.json'):
            json_file_path = os.path.join(src, file_name)
            convert_json_to_yolov8_format(json_file_path, src)


# Define the source directory containing the images and JSON files
src = r"G:\honglvdeng\images\xuanze_copy"
process_directory(src)

转移到新的数据集文件夹:

import os
import json
import math

import os
import json
import shutil

# Define the source directory containing the images and JSON files
src = r"G:\honglvdeng\images\xuanze_copy"
dst = r"G:\honglvdeng\images\xuanze_copy_yolo_seg_datasets"
os.makedirs(dst, exist_ok=True)

files = os.listdir(src)
image_files = [f for f in files if f.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif'))]
img_dst = os.path.join(dst, "images")
os.makedirs(img_dst, exist_ok=True)
for image_file in image_files:
    image_path = os.path.join(src, image_file)
    shutil.copy(image_path, img_dst)

json_files = [f for f in files if f.lower().endswith('.txt')]
json_dst = os.path.join(dst, "labels")
os.makedirs(json_dst, exist_ok=True)
for json_file in json_files:
    json_path = os.path.join(src, json_file)
    shutil.copy(json_path, json_dst)


三、训练yolov8 seg

train.py

from ultralytics import YOLO

# Load a model
model = YOLO("yolov8n-seg.pt")  # load a pretrained model (recommended for training)

# Train the model with 2 GPUs
results = model.train(data="hld-seg.yaml", epochs=100, imgsz=640, device=[0, 1, 2, 3], batch=16)

训练开启:

python -m torch.distributed.run --nproc_per_node 4 x05_train.py

训练结束:

在这里插入图片描述

四、部署

对摄像头实时画面进行分割:

在这里插入图片描述

五、代码资料

下载所有资料:

链接:https://pan.baidu.com/s/1NtLgkmRfoCCqDD5axi-HRw?pwd=78xw 
提取码:78xw 

在这里插入图片描述
在这里插入图片描述

帮助:

https://docs.qq.com/sheet/DUEdqZ2lmbmR6UVdU?tab=BB08J2

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1693261.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

kubernetes之prometheus kube-controller-manager。 scheduler报错问题

项目场景: prometheus scheduler及kube-controller-manager监控报错 问题描述 kubeadm搭建完kube-prometheus 会有这个报错 原因分析: rootmaster2:~# kubectl describe servicemonitor -n kube-system kube-controller-manager通过以上图片我们发现 k…

数据结构_链式二叉树(Chained binary tree)基础

✨✨所属专栏:数据结构✨✨ ✨✨作者主页:嶔某✨✨ 二叉树的遍历 前序、中序以及后序遍历 学习二叉树结构,最简单的方式就是遍历。所谓二叉树遍历(Traversal)是按照某种特定的规则,依次对二叉树中的结点进行相应的操作&#xff…

自己手写一个栈【C风格】

#include <iostream> //栈 #define MAX_SIZE 20 #define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0typedef int Status;//状态类型 typedef int ElemType;//元素类型typedef struct SqStack {ElemType data[MAX_SIZE];int top; };//初始化&#xff0c;方法1 …

安装 Ubuntu桌面版,详细步骤(附引导 U盘制作工具)

下载镜像 安装Ubuntu首先要下载镜像包&#xff0c;访问下面网址下载镜像包 https://releases.ubuntu.com/ 选择你要安装的Ubuntu版本 将 .iso 文件保存到所需位置&#xff0c;下面会使用此文件创建可引导 U盘。 制作 Ubuntu 引导 U 盘 首先要找到一个大于4G的U盘&#xff…

用Python Pygame做的一些好玩的小游戏

有些游戏的代码比较长就不公布了 1.简简单单 1.疯狂的鸡哥 你要准备的图片&#xff1a; 命名为&#xff1a;ji.png 代码&#xff1a; import pygame import random as r pygame.init() pygame.display.set_caption(aaa) pm pygame.display.set_mode((800,600))class Ls(py…

三前奏:获取/ 读取/ 评估数据【数据分析】

各位大佬好 &#xff0c;这里是阿川的博客 &#xff0c; 祝您变得更强 个人主页&#xff1a;在线OJ的阿川 大佬的支持和鼓励&#xff0c;将是我成长路上最大的动力 阿川水平有限&#xff0c;如有错误&#xff0c;欢迎大佬指正 前面的博客 数据分析—技术栈和开发环境搭建 …

Excel提取某一列的唯一值

点击【筛选】&#xff08;【高级筛选】&#xff09;&#xff0c;参数里&#xff1a; 列表区域&#xff1a;为需要选择唯一值的那一列复制到&#xff1a;生成唯一值的目标区域 据说新版本的excel有了unique()函数&#xff0c;可以很快捷的选择某一列的唯一值&#xff0c;但是博…

深度学习500问——Chapter09:图像分割(3)

文章目录 9.8 PSPNet 9.9 DeepLab系列 9.9.1 DeepLabv1 9.9.2 DeepLabv2 9.9.3 DeeoLabv3 9.9.4 DeepLabv3 9.8 PSPNet 场景解析对于无限制的开放词汇和不同场景来说是具有挑战性的。本文使用文中的 pyramid pooling module 实现基于不同区域的上下文集成&#xff0c;提出了PS…

Java进阶学习笔记14——模板方法设计模式

面试和看源码。 谈到设计模式&#xff1a; 1、解决了什么问题&#xff1f; 2、怎么写&#xff1f; 模板方法设计模式解决了什么问题&#xff1f; 解决方法中存在重复代码的问题。 写法&#xff1a; 1&#xff09;定义一个抽象类&#xff1a; 2&#xff09;在里面定义两个方…

阅读笔记——《ProFuzzBench: A Benchmark for Stateful Protocol Fuzzing》

【参考文献】Natella R, Pham V T. Profuzzbench: A benchmark for stateful protocol fuzzing[C]//Proceedings of the 30th ACM SIGSOFT international symposium on software testing and analysis. 2021: 662-665.【注】本文仅为作者个人学习笔记&#xff0c;如有冒犯&…

Java反射角度简单理解spring IOC容器

概述 Java反射&#xff08;Reflection&#xff09;是Java编程语言的一个特性&#xff0c;它允许在运行时对类、接口、字段和方法进行动态查询和操作。反射提供了一种在运行时查看和修改程序行为的能力&#xff0c;这通常用于实现一些高级功能&#xff0c;如框架(Spring)、ORM&…

【正点原子Linux连载】 第四十七章 音频驱动实验摘自【正点原子】ATK-DLRK3568嵌入式Linux驱动开发指南

1&#xff09;实验平台&#xff1a;正点原子ATK-DLRK3568开发板 2&#xff09;平台购买地址&#xff1a;https://detail.tmall.com/item.htm?id731866264428 3&#xff09;全套实验源码手册视频下载地址&#xff1a; http://www.openedv.com/docs/boards/xiaoxitongban 第四十…

EEGLAB的相关使用

目录 概念 1.安装EEGLAB 2.文件实例演示 导入数据集处理 &#xff08;1&#xff09;导入数据集 &#xff08;2&#xff09;画图 &#xff08;3&#xff09; 修改并存储数据集 &#xff08;4&#xff09; 保存数据集 &#xff08;5&#xff09; 删除数据集 &#xff0…

零基础HTML教程(33)--HTML5表单新功能

文章目录 1. 背景2. HTML5新增表单元素2.1 number (数字输入框)2.2 email (邮箱输入框)2.3 url (链接输入框)2.4 tel (电话输入框)2.5 range (范围选择框)2.6 color (颜色选择框)2.7 datetime (日期时间选择框)2.8 search (搜索框) 3. placeholder &#xff08;占位属性&#x…

php TP8 阿里云短信服务SDKV 2.0

安装&#xff1a;composer require alibabacloud/dysmsapi-20170525 2.0.24 官方文档&#xff1a;短信服务_SDK中心-阿里云OpenAPI开发者门户 (aliyun.com) 特别注意&#xff1a;传入参数获得值形式 正确&#xff1a; $PhoneNumbers $postData[PhoneNumbers];$signName $po…

Vue集成Iframe

一、应用场景&#xff0c;为什么要集成Iframe&#xff1f; 1、庞大项目拆分后&#xff0c;便于管理和部署&#xff0c;用集成Iframe的方法合并 2、避免功能重复开发&#xff0c;共用模块可单独开发为一个项目&#xff0c;既可独立部署&#xff0c;也可集成到中台系统 二、集成…

【feature selection】特征选择学习笔记

文章目录 1. 什么是特征选择2. 特征选择与特征提取的区别3. 特征选择的方法3.1 Filtering过滤法3.2 Wrapper包装法3.3 Embedding嵌入法 4. 特征选择示例4.1 方差选择法示例4.2 递归特征消除法示例 1. 什么是特征选择 特征选择是特征工程的内容, 其目标是寻找最优特征子集。剔除…

暴雨“彩虹”行业大模型加速器平台全新发布

近日&#xff0c;在第七届数字中国建设峰会期间&#xff0c;暴雨信息全新发布“彩虹”行业大模型加速器平台&#xff0c;聚焦于为客户降本增效减负&#xff0c;将海量通用数据与行业特有数据融合&#xff0c;专注于流程工艺的智能化改进&#xff0c;因地制宜深挖业务需求&#…

图像上下文学习|多模态基础模型中的多镜头情境学习

【原文】众所周知&#xff0c;大型语言模型在小样本上下文学习&#xff08;ICL&#xff09;方面非常有效。多模态基础模型的最新进展实现了前所未有的长上下文窗口&#xff0c;为探索其执行 ICL 的能力提供了机会&#xff0c;并提供了更多演示示例。在这项工作中&#xff0c;我…

【论文速读】GPT-1:Improving Language Understanding by Generative Pre-Training

摘要 自然语言理解包括广泛的不同的任务&#xff0c;如文本隐含、问题回答、语义相似性评估和文档分类。虽然大量的未标记文本语料库非常丰富&#xff0c;但用于学习这些特定任务的标记数据非常稀缺&#xff0c;这使得经过区别训练的模型要充分执行任务具有挑战性。我们证明&a…