-
Notifications
You must be signed in to change notification settings - Fork 125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Rename Classes UQL Operation #656
Changes from all commits
96c2ddc
385c63f
8eaf988
d816df7
090a863
3972fe8
e68e377
ea891c5
ca0e01a
d9abeb1
fbc8db1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
crowd.jpg: https://pixabay.com/users/wal_172619-12138562 | ||
license_plate.jpg: https://www.pexels.com/photo/kia-niros-driving-on-the-road-11320632/ | ||
dogs.jpg: https://www.pexels.com/photo/brown-and-white-dogs-sitting-on-field-3568134/ | ||
multi-fruit.jpg: https://www.freepik.com/free-photo/front-close-view-organic-nutrition-source-fresh-bananas-bundle-red-apples-orange-with-stem-dark-background_17119128.htm |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
from typing import Dict | ||
|
||
import numpy as np | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. great tests coverage There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @PawelPeczek-Roboflow Hey Pawel, I couldn't figure out or find a good example of passing Input Parameters into UQL operations for tests. Instead I created a hacky work around with parameterized tests to replace the Workflow Specification to test scenarios. Feel free to change this if you'd like; as I would also like to learn how to pass input parameters into UQL operations for future work. |
||
import pytest | ||
|
||
from inference.core.env import WORKFLOWS_MAX_CONCURRENT_STEPS | ||
from inference.core.managers.base import ModelManager | ||
from inference.core.workflows.core_steps.common.entities import StepExecutionMode | ||
from inference.core.workflows.core_steps.common.query_language.errors import ( | ||
OperationError, | ||
) | ||
from inference.core.workflows.execution_engine.core import ExecutionEngine | ||
from tests.workflows.integration_tests.execution.workflows_gallery_collector.decorators import ( | ||
add_to_workflows_gallery, | ||
) | ||
|
||
|
||
def build_class_remapping_workflow_definition( | ||
class_map: Dict[str, str], | ||
strict: bool, | ||
) -> dict: | ||
return { | ||
"version": "1.0", | ||
"inputs": [ | ||
{"type": "WorkflowImage", "name": "image"}, | ||
{"type": "WorkflowParameter", "name": "confidence", "default_value": 0.4}, | ||
], | ||
"steps": [ | ||
{ | ||
"type": "ObjectDetectionModel", | ||
"name": "model", | ||
"image": "$inputs.image", | ||
"model_id": "yolov8n-640", | ||
"confidence": "$inputs.confidence", | ||
}, | ||
{ | ||
"type": "DetectionsTransformation", | ||
"name": "class_rename", | ||
"predictions": "$steps.model.predictions", | ||
"operations": [ | ||
{ | ||
"type": "DetectionsRename", | ||
"strict": strict, | ||
"class_map": class_map, | ||
} | ||
], | ||
}, | ||
], | ||
"outputs": [ | ||
{ | ||
"type": "JsonField", | ||
"name": "original_predictions", | ||
"selector": "$steps.model.predictions", | ||
}, | ||
{ | ||
"type": "JsonField", | ||
"name": "renamed_predictions", | ||
"selector": "$steps.class_rename.predictions", | ||
}, | ||
], | ||
} | ||
|
||
|
||
@add_to_workflows_gallery( | ||
category="Workflows with data transformations", | ||
use_case_title="Workflow with detections class remapping", | ||
use_case_description=""" | ||
This workflow presents how to use Detections Transformation block that is going to | ||
change the name of the following classes: `apple`, `banana` into `fruit`. | ||
|
||
In this example, we use non-strict mapping, causing new class `fruit` to be added to | ||
pool of classes - you can see that if `banana` or `apple` is detected, the | ||
class name changes to `fruit` and class id is 1024. | ||
|
||
You can test the execution submitting image like | ||
[this](https://www.pexels.com/photo/four-trays-of-varieties-of-fruits-1300975/). | ||
""", | ||
workflow_definition=build_class_remapping_workflow_definition( | ||
class_map={"apple": "fruit", "banana": "fruit"}, | ||
strict=False, | ||
), | ||
workflow_name_in_app="detections-class-remapping", | ||
) | ||
def test_class_rename_workflow_with_non_strict_mapping( | ||
model_manager: ModelManager, | ||
fruit_image: np.ndarray, | ||
) -> None: | ||
workflow_definition = build_class_remapping_workflow_definition( | ||
class_map={"apple": "fruit", "banana": "fruit"}, | ||
strict=False, | ||
) | ||
|
||
workflow_init_parameters = { | ||
"workflows_core.model_manager": model_manager, | ||
"workflows_core.api_key": None, | ||
"workflows_core.step_execution_mode": StepExecutionMode.LOCAL, | ||
} | ||
execution_engine = ExecutionEngine.init( | ||
workflow_definition=workflow_definition, | ||
init_parameters=workflow_init_parameters, | ||
max_concurrent_steps=WORKFLOWS_MAX_CONCURRENT_STEPS, | ||
) | ||
|
||
# when | ||
result = execution_engine.run( | ||
runtime_parameters={ | ||
"image": fruit_image, | ||
"model_id": "yolov8n-640", | ||
}, | ||
) | ||
|
||
# then | ||
assert isinstance(result, list), "Expected result to be list" | ||
assert len(result) == 1, "Single image provided - single output expected" | ||
|
||
assert result[0]["renamed_predictions"]["class_name"].tolist() == [ | ||
"fruit", | ||
"fruit", | ||
"fruit", | ||
"orange", | ||
"fruit", | ||
], "Expected renamed set of classes to be the same as when test was created" | ||
assert result[0]["renamed_predictions"].class_id.tolist() == [ | ||
1024, | ||
1024, | ||
1024, | ||
49, | ||
1024, | ||
], "Expected renamed set of class ids to be the same as when test was created" | ||
assert len(result[0]["renamed_predictions"]) == len( | ||
result[0]["original_predictions"] | ||
), "Expected length of predictions no to change" | ||
|
||
|
||
def test_class_rename_workflow_with_strict_mapping_when_all_classes_are_remapped( | ||
model_manager: ModelManager, | ||
fruit_image: np.ndarray, | ||
) -> None: | ||
workflow_definition = build_class_remapping_workflow_definition( | ||
class_map={"apple": "fruit", "banana": "fruit", "orange": "my-orange"}, | ||
strict=True, | ||
) | ||
|
||
workflow_init_parameters = { | ||
"workflows_core.model_manager": model_manager, | ||
"workflows_core.api_key": None, | ||
"workflows_core.step_execution_mode": StepExecutionMode.LOCAL, | ||
} | ||
execution_engine = ExecutionEngine.init( | ||
workflow_definition=workflow_definition, | ||
init_parameters=workflow_init_parameters, | ||
max_concurrent_steps=WORKFLOWS_MAX_CONCURRENT_STEPS, | ||
) | ||
|
||
# when | ||
result = execution_engine.run( | ||
runtime_parameters={ | ||
"image": fruit_image, | ||
"model_id": "yolov8n-640", | ||
}, | ||
) | ||
|
||
# then | ||
assert isinstance(result, list), "Expected result to be list" | ||
assert len(result) == 1, "Single image provided - single output expected" | ||
|
||
assert result[0]["renamed_predictions"]["class_name"].tolist() == [ | ||
"fruit", | ||
"fruit", | ||
"fruit", | ||
"my-orange", | ||
"fruit", | ||
], "Expected renamed set of classes to be the same as when test was created" | ||
assert result[0]["renamed_predictions"].class_id.tolist() == [ | ||
0, | ||
0, | ||
0, | ||
1, | ||
0, | ||
], "Expected renamed set of class ids to be the same as when test was created" | ||
assert len(result[0]["renamed_predictions"]) == len( | ||
result[0]["original_predictions"] | ||
), "Expected length of predictions no to change" | ||
|
||
|
||
def test_class_rename_workflow_with_strict_mapping_when_not_all_classes_are_remapped( | ||
model_manager: ModelManager, | ||
fruit_image: np.ndarray, | ||
) -> None: | ||
workflow_definition = build_class_remapping_workflow_definition( | ||
class_map={"apple": "fruit", "banana": "fruit"}, | ||
strict=True, | ||
) | ||
|
||
workflow_init_parameters = { | ||
"workflows_core.model_manager": model_manager, | ||
"workflows_core.api_key": None, | ||
"workflows_core.step_execution_mode": StepExecutionMode.LOCAL, | ||
} | ||
execution_engine = ExecutionEngine.init( | ||
workflow_definition=workflow_definition, | ||
init_parameters=workflow_init_parameters, | ||
max_concurrent_steps=WORKFLOWS_MAX_CONCURRENT_STEPS, | ||
) | ||
|
||
# when | ||
with pytest.raises(OperationError): | ||
_ = execution_engine.run( | ||
runtime_parameters={ | ||
"image": fruit_image, | ||
"model_id": "yolov8n-640", | ||
}, | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please report image credits
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Complete