日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人工智能 > pytorch >内容正文

pytorch

使用labelme制作自己的深度学习图像分割数据集

發布時間:2024/1/23 pytorch 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用labelme制作自己的深度学习图像分割数据集 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

????????要實現深度學習圖像分割應用,首先要獲取圖像分割標注數據,比如PASCAL VOC、COCO、SBD等大型數據集,但這些數據集主要用于訓練預訓練模型和評價分割模型精度性能,針對實際應用還需要我們根據項目需求制作自己的訓練數據集。這里介紹一種基于labelme圖像標注工具的圖像分割數據集制作流程。

一、安裝圖像標注工具labelme

pip install labelme==3.6

安裝完成后在終端輸入命令labelme,如果出現標注軟件界面,則證明安裝成功。

二、圖像標注

首先獲取應用場景下足夠多的圖像,根據需要將圖像裁剪到固定尺寸,比如后期使用Unet模型訓練,那么需要將圖像裁剪為572*572,這些圖像上包含了需要檢測的各種類別。以遙感圖像道路分割為例,這里收集了20張圖像作為數據集。

?

?????????啟動labelme后,點擊OpenDir打開數據集路徑,通過點擊create polygongs開始在圖像上根據目標形狀進行區域繪制,繪制完成后雙擊鼠標,在彈出窗口中輸入類別名稱。

????????完成所有圖像的分割標注后,每張圖像會生成對應的json文件,點擊保存,進行下一步處理。?

三、制作分割實例圖像

????????圖像標注后,生成的json文件是保存的分割邊界坐標,還需要進一步處理,將描述文件轉換為分割實例圖像。通過以下腳本進行處理。

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。

????????為了方便后續的訓練,還需要將分割實例圖像轉換為標簽圖像,由于這里只進行單一類別分割,標簽圖像只需要用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)

????????生成標簽圖像后,會自動刪除原來的分割實例圖像。

?????????到這里,圖像分割數據集制作已經完成了,接下來就可以開始模型搭建和訓練了。

總結

以上是生活随笔為你收集整理的使用labelme制作自己的深度学习图像分割数据集的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。