lost_and_found / labels.py
kumuji's picture
Upload 6 files
8d32397 verified
#!/usr/bin/python
#
# Cityscapes labels
#
from collections import namedtuple
#--------------------------------------------------------------------------------
# Definitions
#--------------------------------------------------------------------------------
# a label and all meta information
Label = namedtuple( 'Label' , [
'name' , # The identifier of this label, e.g. 'car', 'person', ... .
# We use them to uniquely name a class
'id' , # An integer ID that is associated with this label.
# The IDs are used to represent the label in ground truth images
# An ID of -1 means that this label does not have an ID and thus
# is ignored when creating ground truth images (e.g. license plate).
'trainId' , # An integer ID that overwrites the ID above, when creating ground truth
# images for training.
# For training, multiple labels might have the same ID. Then, these labels
# are mapped to the same class in the ground truth images. For the inverse
# mapping, we use the label that is defined first in the list below.
# For example, mapping all void-type classes to the same ID in training,
# might make sense for some approaches.
'category' , # The name of the category that this label belongs to
'categoryId' , # The ID of this category. Used to create ground truth images
# on category level.
'hasInstances', # Whether this label distinguishes between single instances or not
'ignoreInEval', # Whether pixels having this class as ground truth label are ignored
# during evaluations or not
'color' , # The color of this label
] )
#--------------------------------------------------------------------------------
# A list of all labels
#--------------------------------------------------------------------------------
# Please adapt the train IDs as appropriate for you approach.
# Note that you might want to ignore labels with ID 255 during training.
# Make sure to provide your results using the original IDs and not the training IDs.
# Note that many IDs are ignored in evaluation and thus you never need to predict these!
labels = [
# name id trainId hasInstances ignoreInEval color
Label( 'unlabeled' , 0 , 0 , False , True , ( 0, 0, 0) ),
Label( 'ego vehicle' , 0 , 0 , False , True , ( 0, 0, 0) ),
Label( 'rectification border' , 0 , 0 , False , True , ( 0, 0, 0) ),
Label( 'out of roi' , 0 , 0 , False , True , ( 0, 0, 0) ),
Label( 'background' , 0 , 0 , False , False , ( 0, 0, 0) ),
Label( 'free' , 1 , 1 , False , False , (128, 64,128) ),
Label( '01' , 2 , 2 , True , False , ( 0, 0,142) ),
Label( '02' , 3 , 2 , True , False , ( 0, 0,142) ),
Label( '03' , 4 , 2 , True , False , ( 0, 0,142) ),
Label( '04' , 5 , 2 , True , False , ( 0, 0,142) ),
Label( '05' , 6 , 2 , True , False , ( 0, 0,142) ),
Label( '06' , 7 , 2 , True , False , ( 0, 0,142) ),
Label( '07' , 8 , 2 , True , False , ( 0, 0,142) ),
Label( '08' , 9 , 2 , True , False , ( 0, 0,142) ),
Label( '09' , 10 , 2 , True , False , ( 0, 0,142) ),
Label( '10' , 11 , 2 , True , False , ( 0, 0,142) ),
Label( '11' , 12 , 2 , True , False , ( 0, 0,142) ),
Label( '12' , 13 , 2 , True , False , ( 0, 0,142) ),
Label( '13' , 14 , 2 , True , False , ( 0, 0,142) ),
Label( '14' , 15 , 2 , True , False , ( 0, 0,142) ),
Label( '15' , 16 , 2 , True , False , ( 0, 0,142) ),
Label( '16' , 17 , 2 , True , False , ( 0, 0,142) ),
Label( '17' , 18 , 2 , True , False , ( 0, 0,142) ),
Label( '18' , 19 , 2 , True , False , ( 0, 0,142) ),
Label( '19' , 20 , 2 , True , False , ( 0, 0,142) ),
Label( '20' , 21 , 2 , True , False , ( 0, 0,142) ),
Label( '21' , 22 , 2 , True , False , ( 0, 0,142) ),
Label( '22' , 23 , 2 , True , False , ( 0, 0,142) ),
Label( '23' , 24 , 2 , True , False , ( 0, 0,142) ),
Label( '24' , 25 , 2 , True , False , ( 0, 0,142) ),
Label( '25' , 26 , 2 , True , False , ( 0, 0,142) ),
Label( '26' , 27 , 2 , True , False , ( 0, 0,142) ),
Label( '27' , 28 , 2 , True , False , ( 0, 0,142) ),
Label( '28' , 29 , 2 , True , False , ( 0, 0,142) ),
Label( '29' , 30 , 2 , True , False , ( 0, 0,142) ),
Label( '30' , 31 , 0 , True , False , ( 0, 0, 0) ),
Label( '31' , 32 , 2 , True , False , ( 0, 0,142) ),
Label( '32' , 33 , 0 , True , False , ( 0, 0, 0) ),
Label( '33' , 34 , 0 , True , False , ( 0, 0, 0) ),
Label( '34' , 35 , 2 , True , False , ( 0, 0,142) ),
Label( '35' , 36 , 0 , True , False , ( 0, 0, 0) ),
Label( '36' , 37 , 0 , True , False , ( 0, 0, 0) ),
Label( '37' , 38 , 0 , True , False , ( 0, 0, 0) ),
Label( '38' , 39 , 0 , True , False , ( 0, 0, 0) ),
Label( '39' , 40 , 2 , True , False , ( 0, 0,142) ),
Label( '40' , 41 , 2 , True , False , ( 0, 0,142) ),
Label( '41' , 42 , 2 , True , False , ( 0, 0,142) ),
Label( '42' , 43 , 2 , True , False , ( 0, 0,142) ),
]
#--------------------------------------------------------------------------------
# Create dictionaries for a fast lookup
#--------------------------------------------------------------------------------
name2label = { label.name : label for label in labels }
id2label = { label.id : label for label in labels }
trainId2label = { label.trainId : label for label in reversed(labels) }
category2labels = {}
for label in labels:
category = label.category
if category in category2labels:
category2labels[category].append(label)
else:
category2labels[category] = [label]
#--------------------------------------------------------------------------------
# Assure single instance name
#--------------------------------------------------------------------------------
def assureSingleInstanceName( name ):
# if the name is known, it is not a group
if name in name2label:
return name
# test if the name actually denotes a group
if not name.endswith("group"):
return name
# remove group
name = name[:-len("group")]
# test if the new name exists
if not name in name2label:
return None
# test if the new name denotes a label that actually has instances
if not name2label[name].hasInstances:
return None
# all good then
return name
#--------------------------------------------------------------------------------
# Main for testing
#--------------------------------------------------------------------------------
if __name__ == "__main__":
# Print all the labels
print "List of cityscapes labels:"
print
print " {:>13} | {:>3} | {:>7} | {:>14} | {:>7} | {:>12} | {:>12}".format( 'name', 'id', 'trainId', 'category', 'categoryId', 'hasInstances', 'ignoreInEval' )
print " " + ('-' * 88)
for label in labels:
print " {:>13} | {:>3} | {:>7} | {:>14} | {:>7} | {:>12} | {:>12}".format( label.name, label.id, label.trainId, label.category, label.categoryId, label.hasInstances, label.ignoreInEval )
print
print "Example usages:"
# Map from name to label
name = 'car'
id = name2label[name].id
print "ID of label '{name}': {id}".format( name=name, id=id )
# Map from ID to label
category = id2label[id].category
print "Category of label with ID '{id}': {category}".format( id=id, category=category )
# Map from trainID to label
trainId = 0
name = trainId2label[trainId].name
print "Name of label with trainID '{id}': {name}".format( id=trainId, name=name )
# Print list of label names for each train ID
print "Labels for train IDs: ", trainId2label.keys()
print " ",
for trainId in trainId2label:
print trainId2label[trainId].name + "," ,
print