Model Slice

A developer guide for interacting with model slices using the Python SDK.

Open this Colab for an interactive tutorial on how to use model slices.

Model slices operate much like Catalog slices, both essentially functioning as saved searches. However, they have different functionalities. Catalog slices can be used in Catalog only, and model slices will save searches across model runs in a model. Constructing a model slice involves applying one or more filters to curate a collection of data rows. Users commonly combine filters to highlight high-impact data and save the results as a model slice.

End-to-end example: create and retrieve a model slices

Before you start

You must import these libraries to use the code examples in this section.

import labelbox as lb
import uuid

Replace with your API key

API_KEY = ""
# To get your API key go to: Workspace settings -> API -> Create API Key
client = lb.Client(api_key=API_KEY)

Create a model experiment and a model run

To interact with model slices, you must create a model experiment with a model run and then create a model slice through the platform. The steps below go over this process. See Slices Slicesfor more information

Create a model experiment

To create a model experiment, you will need to create an ontology. See Ontology for more information

#Create an ontology
classification_features = [
    lb.Classification(
        class_type=lb.Classification.Type.CHECKLIST,
        name="Quality Issues",
        options=[
            lb.Option(value="blurry", label="Blurry"),
            lb.Option(value="distorted", label="Distorted")
        ]
    )

]

ontology_builder = lb.OntologyBuilder(
    tools=[],
    classifications=classification_features
)

ontology = client.create_ontology(
  "Ontology from new features",
  ontology_builder.asdict(),
  media_type=lb.MediaType.Image
)

Attach the ontology during model creation.

model = client.create_model(
  name="Model Slice Demo",
  ontology_id=ontology.uid
)

Create a model run from a model experiment.

In this step, you will need to create a dataset to attach data rows to our model run.

# create a sample dataset to send to your model
global_key = str(uuid.uuid4())

test_img_url = {
    "row_data":
        "https://storage.googleapis.com/labelbox-datasets/image_sample_data/2560px-Kitano_Street_Kobe01s5s4110.jpeg",
    "global_key":
        global_key
}

dataset = client.create_dataset(name="foundry-demo-dataset")
task = dataset.create_data_rows([test_img_url])
task.wait_till_done()

print(f"Errors: {task.errors}")
print(f"Failed data rows: {task.failed_data_rows}")

# Create a model configuration
model_run_name = "Model Slice Demo"
example_config = {
    "learning_rate": 0.001, 
    "batch_size": 32, 
}

# Create a model run 
model_run = model.create_model_run(name=model_run_name, config=example_config)

# Send the data rows to your model 
model_run.upsert_data_rows(global_keys=[global_key])

Create a model slice in the UI

Creating a model slice is currently not supported through the SDK; however, we will generate a slice through the UI to showcase how to interact with model slices.

  1. Navigate to Model section of the Labelbox Platform, select the Experiment type, and select the model experiment that was created.
  1. You must have a filter created to save a slice. For this example, select Search your data dropdown and then select Data row.
  2. Change is one of dropdown to is not one of, then type "test" into the Search for an id search box. You can select any other filtering parameters to save a slice.
Example of final view after executing step 2 -3

Example of final view after executing step 2 -3

  1. Hit Enter and select Save slice.
  2. Give the slice a name and select Save.
  3. Your slice's name is above the Search your data dropdown. Select that dropdown and click Copy slice ID.
Example of final view after executing step 4 - 6

Example of final view after executing step 4 - 6

  1. Paste the Slice ID below:
SLICE_ID = ""

Get Model Slice

model_slice = client.get_model_slice(SLICE_ID)

Obtain Data Row IDs from Model Slice

Data row identifiers are objects that contain both the data row ID(s) and global keys.

data_row_identifiers = model_slice.get_data_row_identifiers(model_run.uid)

drids = [dr for dr in data_row_identifiers]

# get both global keys and data row ids 
# and utilize the hash method to combine both global keys and data row ids into a dictionary
for dr in drids: 
  print(f"Data row: {dr.id}, Global Key: {dr.global_key}, dr_gk: {dr.to_hash()}")

Obtain Data Row identifiers

data_rows = model_slice.get_data_row_identifiers(model_run.uid)

for data_row in data_rows:
    print(data_row)

Model slice attributes

# name (str)
model_slice.name

# description (str)
model_slice.description

# updated at (datetime)
model_slice.updated_at

# created at (datetime)
model_slice.created_at

# filter (list[dict])
model_slice.filter