Learn how to filter model outputs by class to reduce clutter, streamline downstream logic, and focus on what matters to your application.
Estimated read time: 4 minutes
Models are often trained on many labels (e.g., COCO’s 80), but your application may only need a subset.
Use model.label_dictionary to see exactly which labels a model predicts (for detection, classification, or segmentation). Then, optionally restrict outputs to the classes you care about using output_class_set—reducing clutter and simplifying downstream logic.
Inspect labels without filtering
Two people sitting on a bench next to two bicycles. The people, bench, and bicycles are labeled.
Use this baseline run to confirm label names before filtering. Knowing the exact strings ensures you pass the right values to output_class_set later.
Example
Expect console output similar to the snippet below.
Example
Filter to a subset of classes
Two people sitting on a bench next to two bicycles. The bicycles are labeled.
Apply class filtering to focus overlays and downstream logic on just the labels you need. Adjust the set before loading the model so every result reports only the chosen classes.
Example
model.label_dictionary lists the label names the model can predict—use it to confirm exact strings (e.g., "bicycle" vs "bike").
output_class_set accepts a set or list of label strings. Omit it (or set to None) to return all classes.
Works the same for detection and segmentation models: non-selected classes are removed from boxes, masks, and overlays.
from degirum_tools import ModelSpec, Display, remote_assets
# Describe and load the model (no filtering)
model_spec = ModelSpec(
model_name="yolov8n_coco--640x640_quant_hailort_multidevice_1",
zoo_url="degirum/hailo",
inference_host_address="@local",
model_properties={
"device_type": ["HAILORT/HAILO8", "HAILORT/HAILO8L"],
},
)
model = model_spec.load_model()
# Discover available labels
labels = model.label_dictionary
print(f"Model predicts these {len(labels)} labels:", labels)
# Run on an image
image_source = remote_assets.urban_park_elephants
result = model(image_source)
# Visualize (shows all detected classes)
with Display("All classes (press 'q' to exit)") as output_display:
output_display.show_image(result.image_overlay)
from degirum_tools import ModelSpec, Display, remote_assets
# Choose the classes you want to keep in the outputs
classes_to_keep = {"bicycle"} # e.g., {"person", "car"}
# Describe and load the model with class filtering
filtered_spec = ModelSpec(
model_name="yolov8n_coco--640x640_quant_hailort_multidevice_1",
zoo_url="degirum/hailo",
inference_host_address="@local",
model_properties={
"device_type": ["HAILORT/HAILO8", "HAILORT/HAILO8L"],
"output_class_set": classes_to_keep,
},
)
filtered_model = filtered_spec.load_model()
# Run on an image
image_source = remote_assets.bikes
filtered_result = filtered_model(image_source)
# Visualize (overlay now includes only the filtered classes)
with Display("Filtered classes (press 'q' to exit)") as output_display:
output_display.show_image(filtered_result.image_overlay)