使用labelme制作自己的深度学习图像分割数据集
????????要實現(xiàn)深度學習圖像分割應(yīng)用,首先要獲取圖像分割標注數(shù)據(jù),比如PASCAL VOC、COCO、SBD等大型數(shù)據(jù)集,但這些數(shù)據(jù)集主要用于訓練預(yù)訓練模型和評價分割模型精度性能,針對實際應(yīng)用還需要我們根據(jù)項目需求制作自己的訓練數(shù)據(jù)集。這里介紹一種基于labelme圖像標注工具的圖像分割數(shù)據(jù)集制作流程。
一、安裝圖像標注工具labelme
pip install labelme==3.6
安裝完成后在終端輸入命令labelme,如果出現(xiàn)標注軟件界面,則證明安裝成功。
二、圖像標注
首先獲取應(yīng)用場景下足夠多的圖像,根據(jù)需要將圖像裁剪到固定尺寸,比如后期使用Unet模型訓練,那么需要將圖像裁剪為572*572,這些圖像上包含了需要檢測的各種類別。以遙感圖像道路分割為例,這里收集了20張圖像作為數(shù)據(jù)集。
?
?????????啟動labelme后,點擊OpenDir打開數(shù)據(jù)集路徑,通過點擊create polygongs開始在圖像上根據(jù)目標形狀進行區(qū)域繪制,繪制完成后雙擊鼠標,在彈出窗口中輸入類別名稱。
????????完成所有圖像的分割標注后,每張圖像會生成對應(yīng)的json文件,點擊保存,進行下一步處理。?
三、制作分割實例圖像
????????圖像標注后,生成的json文件是保存的分割邊界坐標,還需要進一步處理,將描述文件轉(zhuǎn)換為分割實例圖像。通過以下腳本進行處理。
import glob import json import os import os.path as osp import numpy as np import PIL.Image import labelmedef main():input_dir = './datasets/train/images/'output_dir= 'data_dataset_voc'labels = './label.txt'os.makedirs(output_dir)os.makedirs(osp.join(output_dir, 'JPEGImages'))os.makedirs(osp.join(output_dir, 'SegmentationClass'))os.makedirs(osp.join(output_dir, 'SegmentationClassPNG'))os.makedirs(osp.join(output_dir, 'SegmentationClassVisualization'))os.makedirs(osp.join(output_dir, 'SegmentationObject'))os.makedirs(osp.join(output_dir, 'SegmentationObjectPNG'))os.makedirs(osp.join(output_dir, 'SegmentationObjectVisualization'))print('Creating dataset:', output_dir)class_names = []class_name_to_id = {}for i, line in enumerate(open(labels).readlines()):class_id = i - 1 # starts with -1class_name = line.strip()class_name_to_id[class_name] = class_idif class_id == -1:assert class_name == '__ignore__'continueelif 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(output_dir, 'class_names.txt')with open(out_class_names_file, 'w') as f:f.writelines('\n'.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(input_dir, '*.json')):print('Generating dataset from:', label_file)with open(label_file) as f:base = osp.splitext(osp.basename(label_file))[0]out_img_file = osp.join(output_dir, 'JPEGImages', base + '.jpg')out_cls_file = osp.join(output_dir, 'SegmentationClass', base + '.npy')out_clsp_file = osp.join(output_dir, 'SegmentationClassPNG', base + '.png')out_clsv_file = osp.join(output_dir,'SegmentationClassVisualization',base + '.jpg',)out_ins_file = osp.join(output_dir, 'SegmentationObject', base + '.npy')out_insp_file = osp.join(output_dir, 'SegmentationObjectPNG', base + '.png')out_insv_file = osp.join(output_dir,'SegmentationObjectVisualization',base + '.jpg',)data = json.load(f)img_file = osp.join(osp.dirname(label_file), data['imagePath'])img = np.asarray(PIL.Image.open(img_file))PIL.Image.fromarray(img).save(out_img_file)cls, ins = labelme.utils.shapes_to_label(img_shape=img.shape,shapes=data['shapes'],label_name_to_value=class_name_to_id,type='instance',)ins[cls == -1] = 0 # ignore it.# class labellabelme.utils.lblsave(out_clsp_file, cls)np.save(out_cls_file, cls)clsv = labelme.utils.draw_label(cls, img, class_names, colormap=colormap)PIL.Image.fromarray(clsv).save(out_clsv_file)# instance labellabelme.utils.lblsave(out_insp_file, ins)np.save(out_ins_file, ins)instance_ids = np.unique(ins)instance_names = [str(i) for i in range(max(instance_ids) + 1)]insv = labelme.utils.draw_label(ins, img, instance_names)PIL.Image.fromarray(insv).save(out_insv_file)if __name__ == '__main__':main()運行腳本,生成分割實例圖像。
????????接下來,新建一個datasets目錄,在datasets下新建train和validation文件夾,trian和validation目錄下分別新建images和instance文件夾,將原始圖像放入images,分割實例圖像放入validation。
????????為了方便后續(xù)的訓練,還需要將分割實例圖像轉(zhuǎn)換為標簽圖像,由于這里只進行單一類別分割,標簽圖像只需要用0和1兩種值標注,因此生成的標簽圖像幾乎為黑。
通過以下腳本生成標簽圖像:
import glob import cv2 import osfor item in glob.glob('./train/instances/' + '*.png'):img = cv2.imread(item, 0)img[img == 38] = 1img = cv2.resize(img, (388, 388))os.remove(item)cv2.imwrite(item, img)????????生成標簽圖像后,會自動刪除原來的分割實例圖像。
?????????到這里,圖像分割數(shù)據(jù)集制作已經(jīng)完成了,接下來就可以開始模型搭建和訓練了。
總結(jié)
以上是生活随笔為你收集整理的使用labelme制作自己的深度学习图像分割数据集的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MindSDK+yolov5部署及pyt
- 下一篇: AI自动P图:maskrcnn+inpa