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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

语义分割 - 数据集准备

發(fā)布時間:2024/9/21 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 语义分割 - 数据集准备 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Dataset 數(shù)據(jù)集下載

  • PASCAL VOC 2012 dataset
  • augmented PASCAL VOC dataset
# augmented PASCAL VOC cd $DATASETS wget http://www.eecs.berkeley.edu/Research/Projects/CS/vision/grouping/semantic_contours/benchmark.tgz # 1.3 GB tar -zxvf benchmark.tgz mv benchmark_RELEASE VOC_aug# original PASCAL VOC 2012 wget http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar # 2 GB tar -xvf VOCtrainval_11-May-2012.tar mv VOCdevkit/VOC2012 VOC2012_orig && rm -r VOCdevkit

Data conversions 數(shù)據(jù)轉(zhuǎn)換

  • augmented PASCAL VOC 數(shù)據(jù)集的 ground truth labels 是以 Matlab data files的格式存在的,需要進(jìn)行轉(zhuǎn)換:
    • Step1 定義 mat2png 腳本;
    • Step2 轉(zhuǎn)換 mat 成 png.
#!/usr/bin/env python # Martin Kersner, m.kersner@gmail.com # 2016/03/17from __future__ import print_function import os import sys import glob import scipy.io from PIL import Image as PILImage# Mat to png conversion for http://www.cs.berkeley.edu/~bharath2/codes/SBD/download.html # 'GTcls' key is for class segmentation # 'GTinst' key is for instance segmentation def mat2png_hariharan(mat_file, key='GTcls'):mat = scipy.io.loadmat(mat_file, mat_dtype=True, squeeze_me=True, struct_as_record=False)return mat[key].Segmentationdef main():input_path, output_path = process_arguments(sys.argv) if os.path.isdir(input_path) and os.path.isdir(output_path):mat_files = glob.glob(os.path.join(input_path, '*.mat'))convert_mat2png(mat_files, output_path)else:help('Input or output path does not exist!\n')def process_arguments(argv):num_args = len(argv)input_path = Noneoutput_path = None if num_args == 3:input_path = argv[1]output_path = argv[2]else:help()return input_path, output_pathdef convert_mat2png(mat_files, output_path):if not mat_files:help('Input directory does not contain any Matlab files!\n')for mat in mat_files:numpy_img = mat2png_hariharan(mat)pil_img = PILImage.fromarray(numpy_img)pil_img.save(os.path.join(output_path, modify_image_name(mat, 'png')))# Extract name of image from given path, replace its extension with specified one # and return new name only, not path. def modify_image_name(path, ext):return os.path.basename(path).split('.')[0] + '.' + extdef help(msg=''):print(msg +'Usage: python mat2png.py INPUT_PATH OUTPUT_PATH\n''INPUT_PATH denotes path containing Matlab files for conversion.\n''OUTPUT_PATH denotes path where converted Png files ar going to be saved.', file=sys.stderr)exit()if __name__ == '__main__':main() cd $DATASETS/VOC_aug/dataset mkdir cls_png cd $DATASETSDIR ./mat2png.py $DATASETS/VOC_aug/dataset/cls $DATASETS/VOC_aug/dataset/cls_png
  • Caffe的softmax loss函數(shù)只能接受一維的 ground truth labels. 但 original PASCAL VOC 2012中的 ground truth labels 是以RGB圖像的形式保存的,因此需要降維:
    • Step1 定義轉(zhuǎn)換python腳本:convert_labels.py;
    • Step2 轉(zhuǎn)換 ground truth labels 為 1D.
#!/usr/bin/env python #Martin Kersner, m.kersner@gmail.com #2016/01/25 from __future__ import print_function import os import sys import numpy as np from skimage.io import imread, imsavedef pascal_palette():palette = {( 0, 0, 0) : 0 ,(128, 0, 0) : 1 ,( 0, 128, 0) : 2 ,(128, 128, 0) : 3 ,( 0, 0, 128) : 4 ,(128, 0, 128) : 5 ,( 0, 128, 128) : 6 ,(128, 128, 128) : 7 ,( 64, 0, 0) : 8 ,(192, 0, 0) : 9 ,( 64, 128, 0) : 10,(192, 128, 0) : 11,( 64, 0, 128) : 12,(192, 0, 128) : 13,( 64, 128, 128) : 14,(192, 128, 128) : 15,( 0, 64, 0) : 16,(128, 64, 0) : 17,( 0, 192, 0) : 18,(128, 192, 0) : 19,( 0, 64, 128) : 20 }return palettedef convert_from_color_segmentation(arr_3d):arr_2d = np.zeros((arr_3d.shape[0], arr_3d.shape[1]), dtype=np.uint8)palette = pascal_palette()for c, i in palette.items():m = np.all(arr_3d == np.array(c).reshape(1, 1, 3), axis=2)arr_2d[m] = ireturn arr_2ddef main():##ext = '.png'##path, txt_file, path_converted = process_arguments(sys.argv)# Create dir for converted labelsif not os.path.isdir(path_converted):os.makedirs(path_converted)with open(txt_file, 'rb') as f:for img_name in f:img_base_name = img_name.strip()img_name = os.path.join(path, img_base_name) + extimg = imread(img_name)if (len(img.shape) > 2):img = convert_from_color_segmentation(img)imsave(os.path.join(path_converted, img_base_name) + ext, img)else:print(img_name + " is not composed of three dimensions, therefore " "shouldn't be processed by this script.\n""Exiting." , file=sys.stderr)exit()def process_arguments(argv):if len(argv) != 4:help()path = argv[1]list_file = argv[2]new_path = argv[3]return path, list_file, new_path def help():print('Usage: python convert_labels.py PATH LIST_FILE NEW_PATH\n''PATH points to directory with segmentation image labels.\n''LIST_FILE denotes text file containing names of images in PATH.\n''Names do not include extension of images.\n''NEW_PATH points to directory where converted labels will be stored.', file=sys.stderr)exit()if __name__ == '__main__':main() cd $DATASETS/VOC2012_orig mkdir SegmentationClass_1D cd $DATASETSDIR ./convert_labels.py $DATASETS/VOC2012_orig/SegmentationClass/ \$DATASETS/VOC2012_orig/ImageSets/Segmentation/trainval.txt \$DATASETS/VOC2012_orig/SegmentationClass_1D/

總結(jié)

以上是生活随笔為你收集整理的语义分割 - 数据集准备的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。