Similar to Label, A model's predictions is constructed from a Data object and its associated predicted Annotations.

When you bulk upload predictions, you will create a Label list that contains a list of predicted Labels, each of which is constructed by a Data (constructed from Data Row ids) and a list of Annotations.

Add predictions to a Model Run

Prepare predictions

from labelbox import Client
from labelbox.data.annotation_types import (
    Label, ImageData, LabelList,
    ObjectAnnotation, Rectangle, Point)
from labelbox.data.serialization import NDJsonConverter



predictions_all_data_rows = LabelList()

# Every item in predictions_all_data_rows corresponds to a data row. 
# Here, we show how to add predictions on one data row.
predictions_data_row = []
# add one bounding box
predictions_data_row.append(
  ObjectAnnotation(
    name = "<your_class_name>",
    value = Rectangle(start = Point(x=..., y=...),end=Point(x=..., y=...))
  )
)
# add another bounding box for the same data row. 
predictions_data_row.append(
  ObjectAnnotation(
    name = "<your_class_name>",
    value = Rectangle(start = Point(x=..., y=...),end=Point(x=..., y=...))
  )
)

# Append data row predictions to the global list of predictions
data_row = ImageData(uid = "<your_data_row_id>")
predictions_all_data_rows.append(
  Label(
    data = data_row, 
    annotations = predictions_data_row
  )
)

Match predictions with a Labelbox ontology

client = Client(api_key="<YOUR_API_KEY>")

#Various methods to get ontology
ontology_object = project.ontology()
ontology_object = client.get_ontology("<your_ontology_ID>")
ontology = OntologyBuilder.from_ontology(ontology)
ontology = OntologyBuilder.from_project(project)
#Assign feature schema IDs to the Label List
predictions_all_data_rows.assign_feature_schema_ids(ontology)

Convert to NDJson format

# To import predictions in Labelbox, you must convert the python types to NDJSON format. 
# You can do so by using NDJSONConverter as shown below.
prediction_ndjson = list(NDJsonConverter.serialize(predictions_all_data_rows))

Upload predictions to the Model Run

name = "<your_upload_job_name>"
upload_job = model_run.add_predictions(name=name, predictions=prediction_ndjson)

Monitor upload job

upload_job.wait_until_done()
print(upload_job.state)
print(upload_job.errors)