制作自己的语义分割数据集

最近,需要制作自己的类似Camvid语义分割数据集的样式,找了很多文章,自己再总结一下。首先非常感谢https://blog.csdn.net/Winters____/article/details/105659632?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-2.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-2.control,通过这个终于转换成功。

1、使用labelMe标注自己的数据集。标注完毕后,会生成json文件。

2、将json文件和原图片放入一个文件夹。

3、下载https://github.com/wkentaro/labelme/tree/master/examples/semantic_segmentation,进入到examples/semantic_segmentation文件夹下执行

python ./labelme2voc.py C:/Users/lenovo/Desktop/val C:/Users/lenovo/Desktop/save --labels labels.txt

其中C:/Users/lenovo/Desktop/val为步骤2中存放的路径, C:/Users/lenovo/Desktop/save为生成的标注类别的彩色图片的路径,labels.txt存放自己数据及中的类别(具体仿照原labels.txt修改)。执行完毕后如下图所示:

注意:出现AttributeError: module 'labelme' has no attribute 'LabelFile'错误,通过pip install -U labelme解决。

4、将标注类别的彩色图片转换为可用于训练的灰度图


"""Removes the color map from segmentation annotations.
Removes the color map from the ground truth segmentation annotations and save
the results to output_dir.
"""
#源程序链接:https://github.com/tensorflow/models/tree/master/research/deeplab/datasets
#该程序用于将voc格式的索引图片转换为可用于语义分割的灰度图片,因为TensorFlow版本的缘故,在原程序上做了少许改动
#tf.__version__==1.12

import glob
import os.path
import numpy as np
from PIL import Image
import tensorflow as tf

#FLAGS = tf.compat.v1.flags.FLAGS
FLAGS = tf.flags.FLAGS
tf.flags.DEFINE_string('original_gt_folder',#读取voc格式的png图片路径
                                 'F:\Dataset_segmentaion\data_dataset_voc\SegmentationClassPNG',#default
                                 'Original ground truth annotations.')#help
tf.flags.DEFINE_string('segmentation_format', 'png', 'Segmentation format.')
tf.flags.DEFINE_string('output_dir',#保存路径
                                 'F:\Dataset_segmentaion\data_dataset_voc\labels',
                                 'folder to save modified ground truth annotations.')


def _remove_colormap(filename):
  """Removes the color map from the annotation.
  Args:
    filename: Ground truth annotation filename.
  Returns:
    Annotation without color map.
  """
  return np.array(Image.open(filename))


def _save_annotation(annotation, filename):
  """Saves the annotation as png file.
  Args:
    annotation: Segmentation annotation.
    filename: Output filename.
  """
  
  pil_image = Image.fromarray(annotation.astype(dtype=np.uint8))
  '''
  with tf.io.gfile.GFile(filename, mode='w') as f:
  #with open(filename, mode='w') as f:
    print(f)
    pil_image.save(f, 'PNG')
    '''
  pil_image.save(filename)


def main(unused_argv):
  # Create the output directory if not exists.
  if not os.path.exists(FLAGS.output_dir):
    os.makedirs(FLAGS.output_dir)
  #if not tf.io.gfile.isdir(FLAGS.output_dir):
    #tf.io.gfile.makedirs(FLAGS.output_dir)

  annotations = glob.glob(os.path.join(FLAGS.original_gt_folder,
                                       '*.' + FLAGS.segmentation_format))
  for annotation in annotations:
    raw_annotation = _remove_colormap(annotation)
    filename = os.path.basename(annotation)[:-4]
    _save_annotation(raw_annotation,
                     os.path.join(
                         FLAGS.output_dir,
                         filename + '.' + FLAGS.segmentation_format))


if __name__ == '__main__':
  #tf.compat.v1.app.run()
  tf.app.run()

修改第18行和第22行为自己的路径。18行为步骤3中SegmentationClassPNG的路径,22行为灰度图存放的路径。

到这里就转换完毕了~

5、通过下述代码,可以验证某一图片中存放的类别有哪些~

import numpy as np
from PIL import Image

img = Image.open('C:\\Users\\lenovo\\Desktop\\save\\labels\\22367.jpg')
img = np.array(img)
print(np.unique(img))

 

  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
制作自己的语义分割数据集可以按照以下步骤进行: 1. 收集图像数据:首先,需要收集一组包含所需语义类别的图像数据。可以通过在互联网上搜索并下载相关图像,或者使用自己拍摄的图像。 2. 标注图像:使用图像标注工具,对收集到的图像进行标注。语义分割需要为每个像素分配一个类别标签。可以使用矩形框、多边形或像素级标注来完成。 3. 创建标签图像:将标注的图像转换为标签图像。标签图像是与原始图像大小相同的灰度图像,每个像素的值表示所属类别。 4. 数据增强:进行数据增强可以增加数据集的多样性和数量。可以使用平移、旋转、缩放、镜像等技术对原始图像和标签图像进行变换。 5. 数据划分:将数据集划分为训练集、验证集和测试集。通常,训练集用于模型的训练,验证集用于调整模型参数和选择最佳模型,测试集用于评估模型性能。 6. 数据预处理:对图像和标签进行预处理,如调整大小、归一化等。可以根据具体需求选择适当的预处理方法。 7. 数据格式转换:将数据集转换为模型能够接受的格式,如TensorFlow的TFRecord或PyTorch的Dataset。 8. 数据加载:使用相应的深度学习框架加载数据集,并进行训练和评估。 需要注意的是,制作语义分割数据集是一项耗时且繁琐的任务,需要耐心和准确性。同时,确保标注的准确性和一致性对模型训练的结果至关重要。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值