语义分割的数据集制作

数据集制作

  • 安装labelme
  • 进行标注
  • 转换成类别索引图
  • 总结

这两天都没怎么学习,就试着跑了一个语义分割网络。然后就像跑一下自己的数据集。需要提前安装anaconda。

安装labelme

这个步骤比较简单,注意的就是版本问题,网上的很多教程都是需要替换label安装目录下的一个文件,版本不对的话会报错没有draw_label函数:module ‘labelme.utils’ has no attribute ‘draw_label‘
labelme的版本在3.17.0之前的都可以。参考链接里的评论

# python3
conda create --name=labelme python=3.6
 
source activate labelme
 
pip install pyqt5  
 
pip install labelm==3.16.7

进行标注

参考
打开anaconda终端(如下),激活labelme环境,打开软件。
语义分割的数据集制作_第1张图片

activate labelme
labelme

软件打开之后如图所示,具体的使用就是open打开图片,通过edit菜单栏中的Create Polygons(创建多边形),添加标注框并进行保存,得到json文件。语义分割的数据集制作_第2张图片

转换成类别索引图

利用已经生成的json文件生成类别索引图。因为labelme自带的软件,会在初始化的时候每次都重新定义类别对应的颜色,所以要替换掉labelme安装路径下的json_to_dataset.py文件。替换代码参考网址
用如下代码替换掉相应的文件

#!/usr/bin/python
# -*- coding: UTF-8 -*-
#!H:\Anaconda3\envs\new_labelme\python.exe
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():
    warnings.warn("This script is aimed to demonstrate how to convert the\n"
                  "JSON file to a single image dataset, and not to handle\n"
                  "multiple JSON files to generate a real-use dataset.")
 
    parser = argparse.ArgumentParser()
    parser.add_argument('json_file')
    parser.add_argument('-o', '--out', default=None)
    args = parser.parse_args()
 
    json_file = args.json_file
 
    #freedom
    list_path = os.listdir(json_file)
    print('freedom =', json_file)
    for i in range(0,len(list_path)):
        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)
            out_dir = osp.basename(path).replace('.', '_')
            save_file_name = out_dir
            out_dir = osp.join(osp.dirname(path), out_dir)
 
            if not osp.exists(json_file + '\\' + 'labelme_json'):
                os.mkdir(json_file + '\\' + 'labelme_json')
            labelme_json = json_file + '\\' + 'labelme_json'
 
            out_dir1 = labelme_json + '\\' + 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')
 
            if not osp.exists(json_file + '\\' + 'mask_png'):
                os.mkdir(json_file + '\\' + 'mask_png')
            mask_save2png_path = json_file + '\\' + 'mask_png'
            ################################
            #mask_pic = cv2.imread(out_dir1+'\\'+save_file_name+'_label.png',)
            #print('pic1_deep:',mask_pic.dtype)
 
            mask_dst = img_as_ubyte(lbl)  #mask_pic
            print('pic2_deep:',mask_dst.dtype)
            cv2.imwrite(mask_save2png_path+'\\'+save_file_name+'_label.png',mask_dst)
            ##################################
 
            with open(osp.join(out_dir1, 'label_names.txt'), 'w') as f:
                for lbl_name in lbl_names:
                    f.write(lbl_name + '\n')
 
            warnings.warn('info.yaml is being replaced by label_names.txt')
            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__':
    #base64path = argv[1]

将本程序替换掉labelme/cli中的相应文件—json_to_dataset.py 。在cmd中输入python json_to_dateset.py /path/用你的json文件夹的路径替换到path即可生成5个文件。
该代码会在json文件夹中会出现mask_png、labelme_json文件夹,mask_png中存放的是所有8位掩码文件!也即是本工程中使用的标签图。labelme_json文件夹中存放的是json解析后的图片。注意这里是8位深的掩码。
语义分割的数据集制作_第3张图片
这个部分我遇到了很多问题:
1.安装路径和网上对应不上。我的labelme安装路径在anaconda的虚拟环境labelme中,所以安装位置要在anaconda的evn文件夹中找。而网上的教程可能不是安装在虚拟化境中的,所以有些区别。
我的路径C:\Users\Hran\Opt\Anaconda3\envs\labelme\Lib\site-packages\labelme\cli\json_to_dataset.py
2.网上替换json_to_dataset.py文件的代码有很多,生成的都是8位深的mask图。我现在还没弄明白有什么区别,输出的内容是基本相同的5个文件。上面的代码是以其中的一个教程为例子,因为我的分割网络教程是参考这个的。而且有一些其他代码生成的并不是近似全黑的图片,近似全黑的图片是指将类别对应成0-255像素,这种图片在类别较少时集中在0附近,所以看上去是全黑的。但是不知道这影响不影响训练。全黑图片
3.除了上面的方法,还有其他的方法,但是转成的图片不是全黑图片,不知道是否需要在训练之前处理,我尝试了,训练时可以训练的,但是受条件所限没训练完,不知道结果咋样。这个我也试过了,对应我文件中的json_to_dataset - 2.py。链接

总结

在最后训练的时候,并不需要生成的5个文件。只需要原图片和mask图就可以了。

你可能感兴趣的:(语义分割)