DeeplabV3+训练自己的数据集(二)

摘要:
数据集处理1.Labelme用于数据注释,如下所示:数据图片和注释json文件放在同一目录中。2.图像注释后的数据转换(1)训练数据集生成标签图pythonlabelme2voc。pyF:lamorddeeplabv3image--labellabels.txt。其中包括标签。txt是要分割的对象的类别。本项目包括:__无知__背景_黑人
数据集处理

一、数据标注

  使用labelme,如下:

  DeeplabV3+训练自己的数据集(二)第1张

  数据图片和标注json文件放到同一个目录下

二、图像标注后的数据转换

(1)训练数据集生成标签图

 
python labelme2voc.py F:lackborddeeplabv3image --labels labels.txt
其中,labels.txt中是需要分割的物体的类别。本项目包括:
__ignore__
_background_
blackboard
screen

(2)代码如下

#!/usr/bin/env python

from __future__ import print_function

import argparse
import glob
import json
import os
import os.path as osp
import sys

import numpy as np
import PIL.Image

import labelme


def main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter
    )
    parser.add_argument('--input_dir', default= r"F:lackborddeeplabv3image",help='input annotated directory')
    parser.add_argument('--output_dir', default= r"F:lackborddeeplabv3masks",help='output dataset directory')
    parser.add_argument('--labels', default = r"F:lackborddeeplabv3class_label.txt",help='labels file', )
    args = parser.parse_args()

    if osp.exists(args.output_dir):
        print('Output directory already exists:', args.output_dir)
        sys.exit(1)
    os.makedirs(args.output_dir)
    os.makedirs(osp.join(args.output_dir, 'JPEGImages'))
    os.makedirs(osp.join(args.output_dir, 'SegmentationClass'))
    os.makedirs(osp.join(args.output_dir, 'SegmentationClassPNG'))
    os.makedirs(osp.join(args.output_dir, 'SegmentationClassVisualization'))
    print('Creating dataset:', args.output_dir)

    class_names = []
    class_name_to_id = {}
    for i, line in enumerate(open(args.labels).readlines()):
        class_id = i - 1  # starts with -1
        class_name = line.strip()
        class_name_to_id[class_name] = class_id
        if class_id == -1:
            assert class_name == '__ignore__'
            continue
        elif class_id == 0:
            assert class_name == '_background_'
        class_names.append(class_name)
    class_names = tuple(class_names)
    print('class_names:', class_names)
    out_class_names_file = osp.join(args.output_dir, 'class_names.txt')
    with open(out_class_names_file, 'w') as f:
        f.writelines('
'.join(class_names))
    print('Saved class_names:', out_class_names_file)

    colormap = labelme.utils.label_colormap(255)

    for label_file in glob.glob(osp.join(args.input_dir, '*.json')):
        print('Generating dataset from:', label_file)
        with open(label_file,"r",encoding="utf-8") as f:
            base = osp.splitext(osp.basename(label_file))[0]
            out_img_file = osp.join(
                args.output_dir, 'JPEGImages', base + '.jpg')
            out_lbl_file = osp.join(
                args.output_dir, 'SegmentationClass', base + '.npy')
            out_png_file = osp.join(
                args.output_dir, 'SegmentationClassPNG', base + '.png')
            out_viz_file = osp.join(
                args.output_dir,
                'SegmentationClassVisualization',
                base + '.jpg',
            )

            data = json.load(f)

            label_file = label_file.rstrip(".json")
            print(label_file)
            # img_file = osp.join(osp.dirname(label_file), data['imagePath'])
            img_file =label_file +".jpg"
            print(img_file)
            img = np.asarray(PIL.Image.open(img_file))
            PIL.Image.fromarray(img).save(out_img_file)

            lbl = labelme.utils.shapes_to_label(
                img_shape=img.shape,
                shapes=data['shapes'],
                label_name_to_value=class_name_to_id,
            )
            labelme.utils.lblsave(out_png_file, lbl)

            np.save(out_lbl_file, lbl)

            viz = labelme.utils.draw_label(
                lbl, img, class_names, colormap=colormap)
            PIL.Image.fromarray(viz).save(out_viz_file)


if __name__ == '__main__':
    main()
执行后生成: DeeplabV3+训练自己的数据集(二)第2张

 DeeplabV3+训练自己的数据集(二)第3张

(3) mask灰度值的转换: 

去除mask的colormap ,则可以使用自带的 remove_gt_colormap.py 脚本进行转换
python datasets/remove_gt_colormap.py --original_gt_folder /lwh/models/research/deeplab/datasets/blackboard/png --output_dir /lwh/models/research/deeplab/datasets/blackboard/mask

(4)制作指引文件,为生成tfrecord数据格式做准备

import os,shutil
from PIL import Image
 


train_path = r'F:lackborddeeplabv3masks	rain'
filelist_train = sorted(os.listdir(train_path))
val_path = r'F:lackborddeeplabv3masksval'
filelist_val = sorted(os.listdir(val_path))
index_path = r'F:lackborddeeplabv3masksindex'

VOC_file_dir = index_path


VOC_train_file = open(os.path.join(VOC_file_dir, "train.txt"), 'w')
VOC_test_file = open(os.path.join(VOC_file_dir, "val.txt"), 'w')
VOC_train_file.close()
VOC_test_file.close()

VOC_train_file = open(os.path.join(VOC_file_dir, "train.txt"), 'a')
VOC_test_file = open(os.path.join(VOC_file_dir, "val.txt"), 'a')

for eachfile in filelist_train:
    (temp_name,temp_extention) = os.path.splitext(eachfile)
    img_name = temp_name
    VOC_train_file.write(img_name + '
')

for eachfile in filelist_val:
    (temp_name, temp_extention) = os.path.splitext(eachfile)
    img_name = temp_name
    VOC_test_file.write(img_name + '
')

VOC_train_file.close()
VOC_test_file.close()

(4)制作tfrecord文件

需要四个文件路径

DeeplabV3+训练自己的数据集(二)第4张

 image存放原始的训练图片,index存放指引文件,mask存放去除水雾的label图片,tfrecord为保存训练数据,运行下面脚本命令,生成训练数据

python build_voc2012_data.py --image_folder="/lwh/models/research/deeplab/datasets/CamVid/image" 
--semantic_segmentation_folder="/lwh/models/research/deeplab/datasets/CamVid/mask"
--list_folder="/lwh/models/research/deeplab/datasets/CamVid/index" --image_format="png" --label_format="png"
--output_dir="/lwh/models/research/deeplab/datasets/CamVid/tfrecord"
image_folder :数据集中原输入数据的文件目录地址
semantic_segmentation_folder:数据集中标签的文件目录地址
list_folder : 将数据集分类成训练集、验证集等的指示目录文件目录
image_format : 输入图片数据的格式
output_dir:制作的TFRecord存放的目录地址(自己创建)
 

免责声明:文章转载自《DeeplabV3+训练自己的数据集(二)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇mybatis学习 十一 缓存操作系统-PV操作的原理和几种常见问题下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

配置nginx

aNULL:!MD5:!...

oracle触发器调试

如果触发器执行成功,不会出现第4个图,不成功,会出现数据调试信息,具体报错位置会定位到。F7单步执行4.出错时,会出现调试数据,双击调试数据,可以复制出来...

SpringBoot入门 (三) 日志配置

上一篇博客文章记录了在spring-boot项目中读取的属性文件中配置的属性。本文将学习如何登录springboot项目。SpringBoot在内部使用CommonsLogging进行日志记录,但它也为其他日志记录框架提供默认配置,如JavautilLogging、Log4j2和Logback。在每种情况下,日志记录器都预先配置为使用控制台输出和可选文件输出...

Excel数据透视表、高级筛选

目录:1.数据透视表:数据透视表格式和操作说明:多个表一起创建数据透视表创建组创建计算字段创建计算项值显示方法切片器2。高级过滤:高级过滤和或关系精确过滤和模糊过滤通配符过滤原则查询不重复值(使用高级过滤)高级过滤区分大小写使用高级过滤查找空数据使用高级过滤查询两个表中相同的记录或未使用的记录过滤记录1和数据透视表1.正确的数据透视表格式:① 数据源的第一行...

sqlite3 数据类型 批量插入

SQLite3采用动态数据类型。存储值的数据类型与值本身相关,而不是由其字段类型决定。SQLite3的动态数据类型可以向后兼容其他数据库常用的静态类型,这意味着在使用静态数据类型的数据库中使用的数据表也可以在SQLite3中使用。在SQLite2数据库中,除了声明为主键的INTEGER列外,任何列都可以存储属于任何存储类型的值。...

ES基本查询总结

ES与数据库比较查询操作Elasticsearch中当我们设置Mapping完毕后,就可以按照设定的方式导入数据。以下内容的原文需要参考ES官方文档1、结构化检索针对字段类型:日期、时间、数字类型,以及精确的文本匹配。结构化检索特点:*1)结构化查询,我们得到的结果总是非是即否,要么存于集合之中,要么存在集合之外。term查询是简单的,它接受一个字段名以及我...