Semantic segmentation-Data Set preparation

Source: Internet
Author: User
Tags benchmark glob join mkdir python script
# Semantic Segmentation Data Set preparation
DataSet data Set downloadPascal VOC-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
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 ConversionAugmented PASCAL VOC Data set Ground truth labels is in the format of Matlab data files and needs to be converted:
STEP1 defines the mat2png script; Step2 converts the mat into PNG.
#!/usr/bin/env python # Martin Kersner, m.kersner@gmail.com # 2016/03/17 from __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.berkel ey.edu/~bharath2/codes/sbd/download.html # ' Gtcls ' key is for class segmentation # ' Gtinst ' key was for instance Segmentati On Def Mat2png_hariharan (Mat_file, key= ' Gtcls '): Mat = Scipy.io.loadmat (Mat_file, Mat_dtype=true, Squeeze_me=true, str Uct_as_record=false) return Mat[key].  Segmentation def 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 = None Output_path = None if Num_args = = 3:input_path = argv[1] Output_path = argv[2] Else:help () return Input_path, Output_path def CONVERT_MAT2

    PNG (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 is only, not path. def modify_image_name (Path, ext): Return os.path.basename (Path). Split ('. ') [0] + '. ' + ext def help (msg= '): Print (msg + ' Usage:python mat2png.py input_path output_path\n "in Put_path denotes path containing Matlab files for conversion.\n ' Output_path denotes PATH where converted Png fil
        Es 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
The Softmax loss function of Caffe can only accept one-dimensional ground truth labels. However, the ground truth labels in original PASCAL VOC 2012 is preserved in the form of RGB images and therefore requires dimensionality reduction:
STEP1 definition convert python script: convert_labels.py; Step2 convert Ground truth labels to 1D.
#!/usr/bin/env python #Martin kersner, m.kersner@gmail.com #2016/01/25 from __future__ import print_function I mport OS import sys import numpy as NP from Skimage.io import Imread, Imsave def 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, 12
             8, 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, 1
             28, 128): 14, (192, 128, 128): 15, (0, 64, 0): 16, (128, 64, 0): 17,

(0, 192, 0): (+, 192, 0): +, (0, +): +} return Palette def convert_from_color_Segmentation (arr_3d): arr_2d = Np.zeros ((arr_3d.shape[0], arr_3d.shape[1]), dtype=np.uint8) palette = Pascal_palet Te () for C, I in Palette.items (): M = np.all (arr_3d = = Np.array (c). Reshape (1, 1, 3), axis=2) arr_2d[m ] = I return arr_2d def main (): # ext = '. png ' # # path, txt_file, path_converted = Process_argumen TS (sys.argv) # Create dir for converted labels if not Os.path.isdir (path_converted): Os.makedirs (Path_con
            verted) 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) + ext img = imread (img_name) if (Len (Img.shap  e) > 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, ther 
               Efore " "Shouldn ' is processed by this script.\n" "Exiting.", File=sys.stderr) exit () def proc Ess_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 n Ames of images in path.\n "Names do not include extension of images.\n" New_path points to directory whe
        Re converted labels'll 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/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.