制作分割数据集

制作语义分割数据集大致需要

(1)使用labelme对数据进行标注,生成对应图片的json格式。

(2)批量转化 json文件为单通道的png文件

(3)生成训练和测试集

1. 用labelme标注的样本,然后批量重命名图片和json格式的样本

import os
path = "D:\\tu"
filelist = os.listdir(path) #该文件夹下所有的文件(包括文件夹)
i=1
for file in filelist: #遍历所有文件
  if file.endswith('.jpg'):
    Olddir=os.path.join(path,file) #原来的文件路径
    if os.path.isdir(Olddir): #如果是文件夹则跳过
      continue
    filename=os.path.splitext(file)[0] #文件名
    filetype=os.path.splitext(file)[1] #文件扩展名
    Newdir=os.path.join(path,str(i).zfill(6)+filetype) #用字符串函数zfill 以0补全所需位数
    os.rename(Olddir,Newdir)#重命名
  if file.endswith('.json'):
    Olddir=os.path.join(path,file) #原来的文件路径
    if os.path.isdir(Olddir): #如果是文件夹则跳过
      continue
    filename=os.path.splitext(file)[0] #文件名
    filetype=os.path.splitext(file)[1] #文件扩展名
    Newdir=os.path.join(path,str(i).zfill(6)+filetype) #用字符串函数zfill 以0补全所需位数
    os.rename(Olddir,Newdir)#重命名
  i = i + 1

批量提取同一文件夹下相同后缀的所有文件: 打开命令行,输入copy d:\tupian\*.json d:\yangben

2. 批量转化 json文件为单通道的png文件

1)先进入C:\Users\ss\Anaconda3\envs\labelme\Lib\site-packages\labelme\utils中查看是否有draw.py文件,如果没有,添加 draw.py文件如下:

import io
import os.path as osp
 
import numpy as np
import PIL.Image
import PIL.ImageDraw
import PIL.ImageFont
 
 
def label_colormap(N=256):
 
    def bitget(byteval, idx):
        return ((byteval & (1 << idx)) != 0)
 
    cmap = np.zeros((N, 3))
    for i in range(0, N):
        id = i
        r, g, b = 0, 0, 0
        for j in range(0, 8):
            r = np.bitwise_or(r, (bitget(id, 0) << 7 - j))
            g = np.bitwise_or(g, (bitget(id, 1) << 7 - j))
            b = np.bitwise_or(b, (bitget(id, 2) << 7 - j))
            id = (id >> 3)
        cmap[i, 0] = r
        cmap[i, 1] = g
        cmap[i, 2] = b
    cmap = cmap.astype(np.float32) / 255
    return cmap
 
 
def _validate_colormap(colormap, n_labels):
    if colormap is None:
        colormap = label_colormap(n_labels)
    else:
        assert colormap.shape == (colormap.shape[0], 3), \
            'colormap must be sequence of RGB values'
        assert 0 <= colormap.min() and colormap.max() <= 1, \
            'colormap must ranges 0 to 1'
    return colormap
 
 
# similar function as skimage.color.label2rgb
def label2rgb(
    lbl, img=None, n_labels=None, alpha=0.5, thresh_suppress=0, colormap=None,
):
    if n_labels is None:
        n_labels = len(np.unique(lbl))
 
    colormap = _validate_colormap(colormap, n_labels)
    colormap = (colormap * 255).astype(np.uint8)
 
    lbl_viz = colormap[lbl]
    lbl_viz[lbl == -1] = (0, 0, 0)  # unlabeled
 
    if img is not None:
        img_gray = PIL.Image.fromarray(img).convert('LA')
        img_gray = np.asarray(img_gray.convert('RGB'))
        # img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
        # img_gray = cv2.cvtColor(img_gray, cv2.COLOR_GRAY2RGB)
        lbl_viz = alpha * lbl_viz + (1 - alpha) * img_gray
        lbl_viz = lbl_viz.astype(np.uint8)
 
    return lbl_viz
 
 
def draw_label(label, img=None, label_names=None, colormap=None, **kwargs):
    """Draw pixel-wise label with colorization and label names.
 
    label: ndarray, (H, W)
        Pixel-wise labels to colorize.
    img: ndarray, (H, W, 3), optional
        Image on which the colorized label will be drawn.
    label_names: iterable
        List of label names.
    """
    import matplotlib.pyplot as plt
 
    backend_org = plt.rcParams['backend']
    plt.switch_backend('agg')
 
    plt.subplots_adjust(left=0, right=1, top=1, bottom=0,
                        wspace=0, hspace=0)
    plt.margins(0, 0)
    plt.gca().xaxis.set_major_locator(plt.NullLocator())
    plt.gca().yaxis.set_major_locator(plt.NullLocator())
 
    if label_names is None:
        label_names = [str(l) for l in range(label.max() + 1)]
 
    colormap = _validate_colormap(colormap, len(label_names))
 
    label_viz = label2rgb(
        label, img, n_labels=len(label_names), colormap=colormap, **kwargs
    )
    plt.imshow(label_viz)
    plt.axis('off')
 
    plt_handlers = []
    plt_titles = []
    for label_value, label_name in enumerate(label_names):
        if label_value not in label:
            continue
        fc = colormap[label_value]
        p = plt.Rectangle((0, 0), 1, 1, fc=fc)
        plt_handlers.append(p)
        plt_titles.append('{value}: {name}'
                          .format(value=label_value, name=label_name))
    plt.legend(plt_handlers, plt_titles, loc='lower right', framealpha=.5)
 
    f = io.BytesIO()
    plt.savefig(f, bbox_inches='tight', pad_inches=0)
    plt.cla()
    plt.close()
 
    plt.switch_backend(backend_org)
 
    out_size = (label_viz.shape[1], label_viz.shape[0])
    out = PIL.Image.open(f).resize(out_size, PIL.Image.BILINEAR).convert('RGB')
    out = np.asarray(out)
    return out
 
 
def draw_instances(
    image=None,
    bboxes=None,
    labels=None,
    masks=None,
    captions=None,
):
    import matplotlib
 
    # TODO(wkentaro)
    assert image is not None
    assert bboxes is not None
    assert labels is not None
    assert masks is None
    assert captions is not None
 
    viz = PIL.Image.fromarray(image)
    draw = PIL.ImageDraw.ImageDraw(viz)
 
    font_path = osp.join(
        osp.dirname(matplotlib.__file__),
        'mpl-data/fonts/ttf/DejaVuSans.ttf'
    )
    font = PIL.ImageFont.truetype(font_path)
 
    colormap = label_colormap(255)
    for bbox, label, caption in zip(bboxes, labels, captions):
        color = colormap[label]
        color = tuple((color * 255).astype(np.uint8).tolist())
 
        xmin, ymin, xmax, ymax = bbox
        draw.rectangle((xmin, ymin, xmax, ymax), outline=color)
        draw.text((xmin, ymin), caption, font=font)
 
    return np.asarray(viz)

2)再修改__init__.py文件,添加5行代码如下

from .draw import label_colormap
from .draw import _validate_colormap
from .draw import label2rgb
from .draw import draw_label
from .draw import draw_instances

3)将C:\Users\ss\Anaconda3\envs\labelme\Lib\site-packages\labelme\cli下的 json_to_dataset.py文件的代码替换如下:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import argparse
import json
import os
import os.path as osp
import base64
import warnings

import PIL.Image
import yaml

from labelme import utils

import cv2
import numpy as np
#from skimage import img_as_ubyte


# from sys import argv

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('json_file')
    parser.add_argument('-o', '--out', default=None)
    args = parser.parse_args()

    json_file = args.json_file

    list_path = os.listdir(json_file)

    for i in range(0, len(list_path)):
        if list_path[i].endswith('.json'):
            path = os.path.join(json_file, list_path[i])
            if os.path.isfile(path):

                data = json.load(open(path))
                
                img = utils.img_b64_to_arr(data['imageData'])
                
                lbl, lbl_names = utils.labelme_shapes_to_label(img.shape, data['shapes'])

                captions = ['%d: %s' % (l, name) for l, name in enumerate(lbl_names)]

                lbl_viz = utils.draw_label(lbl, img, captions)
                save_file_name = osp.basename(path).replace('.', '_')

                out_dir1 = osp.join(osp.dirname(path), 'labelme_results')
                if not osp.exists(out_dir1):
                    os.mkdir(out_dir1)
                out_dir1 = osp.join(out_dir1, save_file_name)
                if not osp.exists(out_dir1):
                    os.mkdir(out_dir1)

                PIL.Image.fromarray(img).save(out_dir1 + '\\' + save_file_name + '_img.png')
                PIL.Image.fromarray(lbl).save(out_dir1 + '\\' + save_file_name + '_label.png')

                PIL.Image.fromarray(lbl_viz).save(out_dir1 + '\\' + save_file_name +
                                                  '_label_viz.png')
                images_dir = osp.join(json_file, 'images_dir')
                if not osp.exists(images_dir):
                    os.mkdir(images_dir)
                labels_dir = osp.join(json_file, 'labels_dir')
                if not osp.exists(labels_dir):
                    os.mkdir(labels_dir)
                PIL.Image.fromarray(img).save(osp.join(images_dir, '{}_img.png'.format(save_file_name)))
                PIL.Image.fromarray(lbl).save(osp.join(labels_dir, '{}_label.png'.format(save_file_name)))

                with open(osp.join(out_dir1, 'label_names.txt'), 'w') as f:
                    for lbl_name in lbl_names:
                        f.write(lbl_name + '\n')

                info = dict(label_names=lbl_names)
                with open(osp.join(out_dir1, 'info.yaml'), 'w') as f:
                    yaml.safe_dump(info, f, default_flow_style=False)

                print('Saved to: %s' % out_dir1)


if __name__ == '__main__':
    main()

打开cmd环境,输入activate labelme 激活labelme

输入labelme_json_to_dataset E:\json\  进行批量转化json文件,其中E:\json\为json所在文件夹,转化后的文件保存在E:\json\labelme_results\,获取的label.png文件在E:\json\labels_dir。

3.16位label.png转化为8位图像

from PIL import Image
import numpy as np
import math
import os

path = "C:/Users/Administrator/Desktop/trainjson/labels_dir/"
newpath = "C:/Users/Administrator/Desktop/trainjson/labels_dir/"


def img16toeight():
    filelist = os.listdir(path)  # 该文件夹下所有的文件(包括文件夹)
    for file in filelist:
        whole_path = os.path.join(path, file)
        img = Image.open(whole_path)  # 打开图片img = Image.open(dir)#打开图片
        img = np.array(img)
        img = Image.fromarray(np.uint8(img))
        img.save(newpath + file)


img16toeight()

 

4. 生成训练集测试集

import os
import random

trainval_percent = 0.9
train_percent = 0.9 #train的数量为trainval的0.9
imgfilepath = 'E:/Ultra-Fast-Lane-Detection-master/CULANROOT/driver_161_90frame/images_dir'
imglist = os.listdir(imgfilepath)

num = len(imglist)
list = range(num)
tv = int(num*trainval_percent)
tr = int(tv*train_percent)
trainval = random.sample(list, tv)#从0-num-1中任意选择tv个数字
train = random.sample(trainval, tr)#从trainval中任意选择tr个数字

ftrainval = open('E:/Ultra-Fast-Lane-Detection-master/CULANROOT/list/trainval.txt', 'w')
ftest = open('E:/Ultra-Fast-Lane-Detection-master/CULANROOT/list/test.txt', 'w')
ftrain = open('E:/Ultra-Fast-Lane-Detection-master/CULANROOT/list/train.txt', 'w')
fval = open('E:/Ultra-Fast-Lane-Detection-master/CULANROOT/list/val.txt', 'w')

for i in list:
    name = "/driver_161_90frame/images_dir/"+imglist[i]+'\n'
    if i in trainval:
        ftrainval.write(name)
        if i in train:
            ftrain.write(name)
        else:
            fval.write(name)
    else:
        ftest.write(name)

ftrainval.close()
ftrain.close()
fval.close()
ftest .close()
<think>好的,我现在需要帮助用户解决如何创建和分割训练集、验证集和测试集的问题。首先,我需要回顾用户提供的引用内容,确保回答准确并正确引用。 用户引用了三个资料,分别涉及数据分割的方法、Python实现步骤以及数据分布一致性的重要性。接下来,我需要整理这些信息,按照用户的要求结构化回答。 首先,数据集的三个部分:训练集用于模型训练,验证集用于调参和模型选择,测试集用于最终评估。需要明确每个部分的作用,这样用户能理解为什么需要划分。 然后是具体方法,留出法、交叉验证、自助法。根据引用[1],留出法需要随机划分,但要注意保持数据分布的一致性,可能提到分层抽样。交叉验证,尤其是k折交叉验证,这里需要详细说明步骤,可能还要提到在深度学习中可能因为数据量大而不用。自助法适用于小数据集,但会有重复样本,未被选中的作为验证集,这点需要解释清楚。 接下来是操作步骤,用户提到Python实现,引用[2]中提到了读取数据路径、打乱、按比例切片。可能需要给出示例代码,比如用scikit-learn的train_test_split,或者自己手动分割。需要注意代码的可读性,并且符合用户提供的格式,比如代码块用Python语法高亮。 另外,用户提到数据分布一致性,引用[3]强调分布一致的重要性,可以使用分层采样、KDE对比等方法。这部分需要给出建议,比如使用分层划分,检查分布等。 最后,常见问题和注意事项,比如数据泄露,比例选择,数据增强只在训练集使用等。这些点需要简明扼要地提醒用户。 现在需要组织这些内容,结构清晰,分步骤说明方法、操作步骤,并给出代码示例。同时,确保每个引用正确标注,比如留出法引用[^1],数据分布引用[^3],Python代码引用[^2]。 检查是否所有数学公式使用正确,比如交叉验证的平均性能可能涉及到公式,但用户的问题中可能不需要复杂公式,所以可能不用。但如果有的话,需要用$$...$$格式。 最后生成相关问题,根据用户之前的问题,可能涉及划分比例的影响,交叉验证的细节,数据泄露的避免,分层采样的实现,数据增强的应用等。 确保回答使用中文,术语准确,结构清晰,符合用户的要求。</think>### 数据集划分方法详解 在机器学习或深度学习项目中,合理划分数据集是确保模型泛化能力的关键。以下是训练集、验证集和测试集的划分方法及实现步骤: --- #### **1. 数据集的作用与比例** - **训练集**:用于模型参数训练(占总数据60%-80%) - **验证集**:用于超参数调优和模型选择(占10%-20%) - **测试集**:用于最终评估模型性能(占10%-20%) 典型比例示例:$70\%:15\%:15\%$ 或 $80\%:10\%:10\%$。 --- #### **2. 划分方法** ##### **2.1 留出法(Holdout Method)** - **步骤**: 1. 随机打乱数据集; 2. 按比例直接切分为三部分。 - **优点**:简单高效,适合大数据集。 - **缺点**:随机性可能导致分布偏差,需确保各类别比例一致(如分类任务中使用分层抽样)。 ##### **2.2 交叉验证法(Cross-Validation)** - **适用场景**:数据量较小或需要更稳健的评估。 - **k折交叉验证**: 1. 将数据分为$k$个子集; 2. 每次用$k-1$个子集训练,剩余1个验证; 3. 重复$k$次后取平均性能。 数学表达: $$ \text{平均性能} = \frac{1}{k} \sum_{i=1}^{k} \text{第i次验证结果} $$ 深度学习项目中因计算成本高,通常仅用一次验证集。 ##### **2.3 自助法(Bootstrap)** - **原理**:通过有放回抽样生成训练集,未被抽中的样本作为验证集。 - **优点**:适合极小型数据集; - **缺点**:样本重复可能引入偏差。 --- #### **3. 代码实现(Python示例)** ##### **3.1 使用`scikit-learn`划分** ```python from sklearn.model_selection import train_test_split # 假设X为特征,y为标签 X_train, X_temp, y_train, y_temp = train_test_split(X, y, test_size=0.3, stratify=y) # 分层抽样 X_val, X_test, y_val, y_test = train_test_split(X_temp, y_temp, test_size=0.5, stratify=y_temp) ``` ##### **3.2 手动划分** ```python import numpy as np # 读取所有数据路径并打乱 data_paths = np.array([...]) np.random.shuffle(data_paths) # 按比例切分 train_size = int(0.7 * len(data_paths)) val_size = int(0.15 * len(data_paths)) train_set = data_paths[:train_size] val_set = data_paths[train_size:train_size+val_size] test_set = data_paths[train_size+val_size:] ``` --- #### **4. 关键注意事项** 1. **数据分布一致性**:验证集/测试集需与训练集分布一致(可通过分层抽样或统计检验如KS检验); 2. **数据泄露**:严禁测试集信息出现在训练过程中; 3. **增强仅用于训练集**:数据增强(如旋转、裁剪)应仅在训练阶段应用; 4. **时间序列数据**:需按时间顺序划分,避免未来信息泄漏。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值