Ontology SDK
  • Print
  • Share
  • Dark
    Light

Ontology SDK

  • Print
  • Share
  • Dark
    Light

To reach the ontologies repository press here

To reach the ontologies entity press here

To learn more about Dataloop's recipe & ontology go to What are Recipe & Ontology?

For a UI usage of recipe go to Ontology or Instructions pages.

Highlighted text require your input

Relations

Prep

import dtlpy as dl
if dl.token_expired():
    dl.login()
project = dl.projects.get(project_name='project_name')
dataset = project.datasets.get(dataset_name='dataset_name')

Add and Edit Ontology


 By Dataset Entity 


View Dataset's Labels

# as objects
labels = dataset.labels
# as instance map
labels = dataset.instance_map

Add Labels by Dataset

To reach the "add a label by dataset" function full reference, press here.

Please verify that you are not creating labels that already exist.

Add one Label

# Add one label
dataset.add_label(label_name='person')

Add Multiple Labels

# Add multiple
dataset.add_labels(label_list=['person', 'animal', 'object'])

Add a single label with a specific color

# Add single label with specific color
dataset.add_label(label_name='person', color=(34, 6, 231))

Add a single label with a specific color and attributes

# Add single label with specific color and attributes
dataset.add_label(label_name='person', color=(34, 6, 231), attributes=['big', 'small'])

Add Labels using Label object

# Create Labels list using Label object
labels = [
    dl.Label(tag='Donkey', color=(1, 1, 1)),
    dl.Label(tag='Mammoth', color=(34, 56, 7)),
    dl.Label(tag='Bird', color=(100, 14, 150))
]
#Add Labels to Dataset
dataset.add_labels(label_list=labels)
# or you can also create a recipe from the label list
recipe = dataset.recipes.create(recipe_name='My-Recipe-name', labels=labels)

Add a Label with children and attributes

# Add label with children and attributes
label = dl.Label(
    tag='Fish',
    attributes=['carnivore', 'herbivores'],
    color=(34, 6, 231),
    children=[
        dl.Label(
            tag='Shark',
            color=(34, 6, 231),
            attributes=['baby', 'old'],
        ),
        dl.Label(
            tag='Salmon',
            color=(34, 6, 231),
            attributes=['pink', 'norwegian'],
        )
    ]
)
dataset.add_labels(label_list=label)

Add multiple Labels with children and attributes

# Add multiple labels with children and attributes
# Create Labels list
labels = [  dl.Label(
    tag='Fish',
    attributes=['carnivore', 'herbivores'],
    color=(34, 6, 231),
    children=[
        dl.Label(
            tag='Shark',
            color=(34, 6, 231),
            attributes=['baby', 'old'],
        ),
        dl.Label(
            tag='Salmon',
            color=(34, 6, 231),
            attributes=['pink', 'norwegian'],
        )
    ]
),
dl.Label(
    tag='Meat',
    attributes=['carnivore', 'herbivores'],
    color=(34, 6, 231),
    children=[
        dl.Label(
            tag='Beef',
            color=(34, 6, 231),
            attributes=['Rib', 'Brisket'],
        ),
        dl.Label(
            tag='Lamb',
            color=(34, 6, 231),
            attributes=['baby', 'old'],
        )
    ]
)
]
#Add Labels to Dataset
dataset.add_labels(label_list=labels)
# or you can also create a recipe from the label list
recipe = dataset.recipes.create(recipe_name='My-Recipe-name', labels=labels)

Add hierarchy labels with nested

Different options for hierarchy label creation.
Option A:

#Option A
#add father label
labels = dataset.add_label(label_name="animal",color=(123,134,64),attributes=["Farm","Home"])
#add child label
labels = dataset.add_label(label_name="animal.Dog",color=(45,34,164),attributes=["Big","Small"])
#add grandchild label
labels = dataset.add_label(label_name="animal.Dog.poodle",attributes=["Black","Weight"])

Option B:

#Option B:only if you dont have attributes
#parent and grandparent (animal and dog) will be generate automaticly 
labels = dataset.add_label(label_name="animal.Dog.poodle")

Option C:

#Option C: with the Big Dict
nested_labels = [
    {'label_name': 'animal.Dog',
     'color': '#220605',
     'children': [{'label_name': 'poodle',
                   'color': '#298345'},
                  {'label_name': 'labrador',
                   'color': '#298651'}]},
    {'label_name': 'animal.cat',
     'color': '#287605',
     'children': [{'label_name': 'Persian',
                   'color': '#298345'},
                  {'label_name': 'Balinese',
                   'color': '#298651'}]}
]

Add Labels to a dataset:

labels = dataset.add_label(label_name=nested_label)

Create a Recipe From from a Label list

Use Add Labels using Label object and Add multiple labels with children and attributes scripts to understand how to create a labels list using Label object.

#dont forget to create labels list using different scripts
recipe = dataset.recipes.create(recipe_name='My-Recipe-name', labels=labels)

Delete Labels by Dataset

dataset.delete_labels(label_names=['Cat', 'Dog'])

Update Label Features

dataset.update_label(label_name='Cat', color="#000080") #update existing label , if not exist fails
dataset.update_label(label_name='Cat', color="#fcba03", upsert=True) #update label, if not exist add it

 By Ontology Entity


Prep

📘 Please make sure you already run the top page prep script.

# Get recipe from list
recipe = dataset.recipes.list()[0]
# Or get specific recipe:
recipe = dataset.recipes.get(recipe_id='id')
# Get ontology from list
ontology = recipe.ontologies.list()[0]
# Or get specific ontology:
ontology = recipe.ontologies.get(ontology_id='id')

Labels

Labels is a python list, as such, you can use python list methods like sort(). Just make sure to use ontology.update() afterwards.

📘 You can also add labels by the ontology entity
ontology.add_labels(label_list=['Shark', 'whale', 'Animal.Donkey'],update_ontology=True) 

Child Labels

childLabel = ontology.labels[x].children[y].children[z]
#If you want to reach the pug label in the example
childLabel = ontology.labels[0].children[0].children[3]

Attributes

❗️ If you wish to add attributes to a child labels well as all the labels he inherits from, use append attribute to an existing label example.

Add attributes to the ontology

🚧 This script is for adding attributes to an ontology that does not already has attributes, if the ontology already has attributes use "Update attributes " instead.

ontology.attributes = ['NewAttribute', 'NewAttribute2']
#Or for child label
ontology.labels[x].children[y].attributes = ['NewAttribute', 'NewAttribute2']
ontology.update()

Update attributes of the ontology

  1. View your current attributes 

ontology.attributes
#Or for child label
ontology.labels[x].children[y].attributes

Output:

['CurrentFirstAttribute', 'CurrentSecondAttribute']

  2. Add new attributes to an existing one 

ontology.attributes = ['CurrentFirstAttribute', 'CurrentSecondAttribute', 'NewAttribute', 'NewAttribute2']
#Or for child labels
ontology.labels[x].children[y].attributes = ['CurrentFirstAttribute', 'CurrentSecondAttribute', 'NewAttribute', 'NewAttribute2']
ontology.update()


 By Annotation Entity


Prep

📘 Please make sure you already run the top page prep script

item = dataset.items.get(filepath='/your-image-file-path.jpg')
annotation = item.annotations.get(annotation_id='your-annotation-id-number')

Change annotation label to a different label

To change multiple annotations labels, use Change Annotation's Label To A New Label tutorial.

annotation.label = "label2"
annotation.update()

Edit Annotation's Attribute

annotation.attributes = ['leftEye']
annotation = annotation.update()

Link dataset labels to a different dataset

🚧 Please make sure you run the prep script with the dataset you wish to get the labels from.
  • Notice that if you delete a recipe it will delete it for all its linked datasets

Link dataset labels to a new dataset

new_dataset = project.datasets.create(dataset_name='new_dataset_with_labels',
                                          labels=dataset.labels)

Link dataset labels to an existing dataset

new_dataset = project.datasets.create(dataset_name='new_dataset_without_labels')
# Get from a list or recipes
recipe = new_dataset.recipes.list()[0]
# Or get recipe by id
recipe = new_dataset.recipes.get(recipe_id='recipe_id')
# Get from the list of ontologies
ontology = recipe.ontologies.list()[0]
# Or get ontology by id
ontology = recipe.ontologies.get(ontology='ontology_id')
# Add the labels to the dataset
ontology.add_labels(label_list=dataset.labels)
ontology.update()

Link dataset ontology to a different dataset

🚧 Please make sure you run the prep script with the dataset you wish to get the labels from.
  • Notice that if you delete a recipe it will delete it for all its linked datasets

Link dataset ontology to a new dataset

new_dataset = project.datasets.create(dataset_name='new_dataset_with_ontology',
                                          ontology_ids=dataset.ontology_ids)

Link dataset ontology to an existing dataset's recipe

new_dataset = project.datasets.create(dataset_name='new_dataset_without_ontology')
# get recipe
new_dataset_recipe = new_dataset.recipes.list()[0]
# Copy from a different dataset
new_dataset_recipe.ontologyIds = dataset.ontology_ids
# Update the new dataset
new_dataset_recipe.update()
Was This Article Helpful?