训练yolov8+SAM的过程记录

news/2024/7/10 22:41:03 标签: YOLO

1-首先将拿到的数据集进行重新命名(dataset1:是经过校色之后裁剪的图片;dataset2:原图)
图片文件从1.jpg开始命名的代码:

folder_path = r'C:\Users\23608\Desktop\Luli_work\data\fanStudent\tongueseg\Fan\Fan\.jpg'
new_folder = r'C:\Users\23608\Desktop\Luli_work\data\fanStudent\tongueseg\imgOrig'

jpg_files = [f for f in os.listdir(folder_path) if f.endswith('.jpg')]
n = 1
for i, jpg_file in enumerate(jpg_files):
    new_filename = f'{
     n}.jpg'
    n =n+1
    
    # 构造原始文件和新文件的完整路径
    original_path = os.path.join(folder_path, jpg_file)
    new_path = os.path.join(new_folder, new_filename)
    
    # 复制文件到新文件夹并重命名
    shutil.copy(original_path, new_path)
    
print("重命名完成!")

2-将数据预处理之后的数据上传到服务器,接着使用yolov8SAM代码将代码中的舌体掩码跑出来:
数据存放位置
Imgorig:/share1/luli/tongueseg/data/dataset2/imgOrig/
IMgcrop:/share1/luli/tongueseg/data/dataset1/imgCrop/

微调SAM的查找

How to Fine-Tune Segment Anything

1. How to Fine-Tune Segment Anything

We gave an overview of the SAM architecture in the introduction section. The image encoder has a complex architecture with many parameters. To fine-tune the model, it makes sense for us to focus on the mask decoder which is lightweight and therefore easier, faster and more memory efficient to fine-tune.

In order to fine tune SAM, we need to extract the underlying pieces of its architecture (image and prompt encoders, mask decoder). We cannot use SamPredictor.predict (link) for two reasons:

· We want to fine tune only the mask decoder
· This function calls SamPredictor.predict_torch which has the @torch.no_grad() decorator (link), which prevents us from computing gradients

Thus, we need to examine the SamPredictor.predict function and call the appropriate functions with gradient calculation enabled on the part we want to fine tune (the mask decoder). Doing this is also a good way to learn more about how SAM works.

2. Creating a Custom Dataset

We need three things to fine tune our model:(这里其实并没有说GT、datase的具体类型)

· Images on which to draw segmentations
· Segmentation ground truth masks
· Prompts to feed into the model

后续在代码里面可以看到图片代码里面是png格式,mask掩码是黑白二值图。

我现在使用的是labelme标记的舌头,json文件,需要把json文件转化成二值图。

这里涉及到3个数据类型的转化:

· png图片转json文件(也就是使用yolov8+SAM对数据集进行简单的分割之后再人工进行微调,此时的微调使用的是labelme格式是json,这里使用到TongueSAM里马赛克json里面的代码)
· json文件转成png图片,这里使用到labelme里面的自己的代码(参考链接)[labelme] json格式批量转换为mask.png,步骤入下:
1.使用labelme制作语义分割数据集,生成.json格式文件,将所有放置于一个文件夹下。
2.找到labelme安装位置的json_to_dataset.py文件,(可以使用Everything软件)
用下面的代码替换里面的代码:

import argparse
import json
import os
import os.path as osp
import warnings
import copy
import numpy as np
import PIL.Image
from skimage import io
import yaml
from labelme import utils

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('json_file')   # 标注文件json所在的文件夹
    parser.add_argument('-o', '--out', default=None)
    args = parser.parse_args()

    json_file = args.json_file

    list = os.listdir(json_file)   # 获取json文件列表
    for i in range(0, len(list)):
        path = os.path.join(json_file, list[i])  # 获取每个json文件的绝对路径
        filename = list[i][:-5]       # 提取出.json前的字符作为文件名,以便后续保存Label图片的时候使用
        extension = list[i][-4:]
        if extension 

http://www.niftyadmin.cn/n/5390507.html

相关文章

如何在Python中执行Shell脚本?

Python执行Shell命令 1、背景概述2、Python集成Shell及数据交互 1、背景概述 Python作为一种强大的脚本语言,其易用性和灵活性使得它成为自动化任务的理想选择。在Python中执行Shell脚本可以实现一些操作系统级的功能,使程序更加灵活、易理解和易维护 在…

Qt应用软件【文件篇】INI文件读写

文章目录 INI文件简介INI文件的主要特点INI文件的应用场景Qt INI文件相关API汇总Qt读取INI文件代码示例Qt写入INI文件代码示例Qt修改INI文件代码示例INI文件编码格式问题INI文件简介 INI文件,全称Initialization File,是一种简单的文本文件,用于存储配置信息和参数。它由多…

IOBR2 更新(学习自备)

IOBR查看其收录的相关基因集(自备)_肿瘤 tme特征 iobr-CSDN博客 IOBR2:多维度解析肿瘤微环境 - 知乎 (zhihu.com) 学习手册:https://iobr.github.io/book/ (里面有详细教程) 系统综合的分析工具(Immuno-Oncology Bi…

Linux 网络命令指南

目录 配置IP地址和子网掩码 网络接口的详细信息 测试与目标主机的连通性 下载文件或内容 远程登录,进行远程管理和协作 CentOS / Red Hat(使用 firewalld) 关闭防火墙 开启防火墙 配置TCP端口(假设使用3306端口&#xff…

273.【华为OD机试真题】园区参观路径(动态规划-JavaPythonC++JS实现)

🚀点击这里可直接跳转到本专栏,可查阅顶置最新的华为OD机试宝典~ 本专栏所有题目均包含优质解题思路,高质量解题代码(Java&Python&C++&JS分别实现),详细代码讲解,助你深入学习,深度掌握! 文章目录 一. 题目-园区参观路径二.解题思路三.题解代码Python题解…

提高自定义词汇表上的 RAG 性能

原文地址:improve-rag-performance-on-custom-vocabulary Code:Improve RAG performance on custom vocabulary.ipynb 2024 年 2 月 9 日 糟糕的检索系统会导致混乱、沮丧和幻觉。 新的嵌入模型比以往更加强大。我们根据 MTEB 等基准对其进行了全面评…

低于API等级30的应用将无法在上述应用商店

minSdkVersion minSdkVersion用于指定应用兼容的最低Android版本(API等级)。 如果APP某些功能无法支持低版本Android系统的设备,可以配置minSdkVersion确保APP只能安装到指定Android版本以上的设备。HBuilder|HBuilderX中可在manifest.json中…

IP地址地理位置查询

在当今数字化时代,网络连接已成为我们日常生活的重要组成部分。而互联网上的每个设备都被分配了一个唯一的标识符,即IP地址。IP地址不仅仅是用于设备间通信的标识符,它还可以提供有关设备所在地理位置的重要信息。因此,IP地址地理…