Skip to content

Instantly share code, notes, and snippets.

@fabioperez
Created August 6, 2019 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabioperez/0ea329e86ee5c695d1faccc806ed2d7b to your computer and use it in GitHub Desktop.
Save fabioperez/0ea329e86ee5c695d1faccc806ed2d7b to your computer and use it in GitHub Desktop.
LSD-seg label conversion
import argparse
import json
import os
import numpy as np
from PIL import Image
from tqdm import tqdm
def swap_labels(np_original_gt_im, class_convert_mat):
np_processed_gt_im = np.zeros(np_original_gt_im.shape)
for swap in class_convert_mat:
ind_swap = np.where(np_original_gt_im == swap[0])
np_processed_gt_im[ind_swap] = swap[1]
processed_gt_im = Image.fromarray(np.uint8(np_processed_gt_im))
return processed_gt_im
def convert_citylabelTo16label():
with open('data/synthia2cityscapes_info.json', 'r') as f:
paramdic = json.load(f)
class_ind = paramdic['city2common']
city_gt_dir = "data/cityscapes/gtFine"
split_list = ["train", "test", "val"]
original_suffix = "labelIds"
processed_suffix = "label16IDs"
for split in tqdm(split_list):
base_dir = os.path.join(city_gt_dir, split)
place_list = os.listdir(base_dir)
for place in tqdm(place_list):
target_dir = os.path.join(base_dir, place)
pngfn_list = os.listdir(target_dir)
original_pngfn_list = [x for x in pngfn_list if original_suffix in x]
for pngfn in tqdm(original_pngfn_list):
gt_fn = os.path.join(target_dir, pngfn)
original_gt_im = Image.open(gt_fn)
processed_gt_im = swap_labels(np.array(original_gt_im), class_ind)
outfn = gt_fn.replace(original_suffix, processed_suffix)
processed_gt_im.save(outfn, 'PNG')
def convert_synthialabelTo16label():
with open('data/synthia2cityscapes_info.json', 'r') as f:
paramdic = json.load(f)
class_ind = np.array(paramdic['synthia2common'])
synthia_gt_dir = "data/RAND_CITYSCAPES/GT"
# original_dir = os.path.join(synthia_gt_dir, "LABELS") # Original dir but this contains strange files
original_dir = "data/segmentation_annotation/SYNTHIA/GT/parsed_LABELS" # Not original. Downloaded from http://crcv.ucf.edu/data/adaptationseg/ICCV_dataset.zip
processed_dir = os.path.join(synthia_gt_dir, "LABELS16")
original_pngfn_list = os.listdir(original_dir)
for pngfn in tqdm(original_pngfn_list):
gt_fn = os.path.join(original_dir, pngfn)
original_gt_im = Image.open(gt_fn)
processed_gt_im = swap_labels(np.array(original_gt_im), class_ind)
outfn = os.path.join(processed_dir, pngfn)
processed_gt_im.save(outfn, 'PNG')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Convert Label Ids')
parser.add_argument('dataset', type=str, choices=["city", "synthia"])
args = parser.parse_args()
if args.dataset == "city":
convert_citylabelTo16label()
else:
convert_synthialabelTo16label()
{
"classes":16,
"city2common":[
[0, 255],
[1, 255],
[2, 255],
[3, 255],
[4, 255],
[5, 255],
[6, 255],
[7, 0],
[8, 1],
[9, 255],
[10, 255],
[11, 2],
[12, 3],
[13, 4],
[14, 255],
[15, 255],
[16, 255],
[17, 5],
[18, 255],
[19, 6],
[20, 7],
[21, 8],
[22, 255],
[23, 9],
[24, 10],
[25, 11],
[26, 12],
[27, 255],
[28, 13],
[29, 255],
[30, 255],
[31, 255],
[32, 14],
[33, 15],
[-1, 255]
],
"synthia2common":[
[0, 255],
[1, 9],
[2, 2],
[3, 0],
[4, 1],
[5, 4],
[6, 8],
[7, 5],
[8, 12],
[9, 7],
[10, 10],
[11, 15],
[12, 14],
[13, 255],
[14, 255],
[15, 6],
[16, 255],
[17, 11],
[18, 255],
[19, 13],
[20, 255],
[21, 3],
[22, 255]
],
"city_label": [
"unlabeled",
"ego vehicle",
"rectification border",
"out of roi",
"static",
"dynamic",
"ground",
"road",
"sidewalk",
"parking",
"rail track",
"building",
"wall",
"fence",
"guard rail",
"bridge",
"tunnel",
"pole",
"polegroup",
"traffic light",
"traffic sign",
"vegetation",
"terrain",
"sky",
"person",
"rider",
"car",
"truck",
"bus",
"caravan",
"trailer",
"train",
"motorcycle",
"bicycle",
"license plate"
],
"synthia_label": [
"void",
"sky",
"Building",
"Road",
"Sidewalk",
"Fence",
"Vegetation",
"Pole",
"Car",
"Traffic sign",
"Pedestrian",
"Bicycle",
"Motorcycle",
"Parking-slot",
"Road-work",
"Traffic light",
"Terrain",
"Rider",
"Truck",
"Bus",
"Train",
"Wall",
"Lanemarking"
],
"common_label":[
"road",
"sidewalk",
"building",
"wall",
"fence",
"pole",
"light",
"sign",
"vegetation",
"sky",
"person",
"rider",
"car",
"bus",
"motocycle",
"bicycle"
],
"palette":[
[128,64,128],
[244,35,232],
[70,70,70],
[102,102,156],
[190,153,153],
[153,153,153],
[250,170,30],
[220,220,0],
[107,142,35],
[70,130,180],
[220,20,60],
[255,0,0],
[0,0,142],
[0,60,100],
[0,0,230],
[119,11,32],
[0,0,0]],
"mean":[
73.158359210711552,
82.908917542625858,
72.392398761941593],
"std":[
47.675755341814678,
48.494214368814916,
47.736546325441594]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment