-
Notifications
You must be signed in to change notification settings - Fork 137
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 block for running pt models from local filesystem #756
Open
grzegorz-roboflow
wants to merge
4
commits into
main
Choose a base branch
from
feature/ultralitycs-block
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
176 changes: 176 additions & 0 deletions
176
inference/core/workflows/core_steps/models/foundation/ultralytics/v1.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
import time | ||
from typing import List, Literal, Optional, Type, Union | ||
|
||
import numpy as np | ||
import supervision as sv | ||
from pydantic import ConfigDict, Field, PositiveInt | ||
|
||
try: | ||
from ultralytics import YOLO | ||
except ImportError: | ||
pass | ||
|
||
from inference.core.logger import logger | ||
from inference.core.workflows.execution_engine.entities.base import ( | ||
Batch, | ||
OutputDefinition, | ||
WorkflowImageData, | ||
) | ||
from inference.core.workflows.execution_engine.entities.types import ( | ||
BOOLEAN_KIND, | ||
FLOAT_ZERO_TO_ONE_KIND, | ||
INTEGER_KIND, | ||
OBJECT_DETECTION_PREDICTION_KIND, | ||
STRING_KIND, | ||
FloatZeroToOne, | ||
ImageInputField, | ||
StepOutputImageSelector, | ||
WorkflowImageSelector, | ||
WorkflowParameterSelector, | ||
) | ||
from inference.core.workflows.prototypes.block import ( | ||
BlockResult, | ||
WorkflowBlock, | ||
WorkflowBlockManifest, | ||
) | ||
|
||
LONG_DESCRIPTION = """ | ||
This block performs inference by executing locally stored ultralytics pth file. | ||
This block expects pth file to be available within local filesystem. | ||
""" | ||
|
||
|
||
class BlockManifest(WorkflowBlockManifest): | ||
model_config = ConfigDict( | ||
json_schema_extra={ | ||
"name": "Ultralytics", | ||
"version": "v1", | ||
"short_description": "Predict the location of objects with bounding boxes by inferring from locally stored pth file.", | ||
"long_description": LONG_DESCRIPTION, | ||
"license": "Apache-2.0", | ||
"block_type": "model", | ||
}, | ||
protected_namespaces=(), | ||
) | ||
type: Literal["roboflow_core/ultralytics@v1",] | ||
images: Union[WorkflowImageSelector, StepOutputImageSelector] = ImageInputField | ||
model_path: Union[WorkflowParameterSelector(kind=[STRING_KIND]), str] = Field( | ||
description="Path to locally stored pth file", | ||
examples=["/path/to/model.pth", "$inputs.class_agnostic_nms"], | ||
) | ||
device: Union[WorkflowParameterSelector(kind=[STRING_KIND]), str] = Field( | ||
default="cpu", | ||
description="Specifies the device for inference (e.g., cpu, cuda:0, mps or 0)", | ||
examples=["cuda:0", "$inputs.device"], | ||
) | ||
class_agnostic_nms: Union[ | ||
Optional[bool], WorkflowParameterSelector(kind=[BOOLEAN_KIND]) | ||
] = Field( | ||
default=False, | ||
description="Value to decide if NMS is to be used in class-agnostic mode.", | ||
examples=[True, "$inputs.class_agnostic_nms"], | ||
) | ||
confidence: Union[ | ||
FloatZeroToOne, | ||
WorkflowParameterSelector(kind=[FLOAT_ZERO_TO_ONE_KIND]), | ||
] = Field( | ||
default=0.4, | ||
description="Confidence threshold for predictions", | ||
examples=[0.3, "$inputs.confidence_threshold"], | ||
) | ||
iou_threshold: Union[ | ||
FloatZeroToOne, | ||
WorkflowParameterSelector(kind=[FLOAT_ZERO_TO_ONE_KIND]), | ||
] = Field( | ||
default=0.3, | ||
description="Parameter of NMS, to decide on minimum box intersection over union to merge boxes", | ||
examples=[0.4, "$inputs.iou_threshold"], | ||
) | ||
max_detections: Union[ | ||
PositiveInt, WorkflowParameterSelector(kind=[INTEGER_KIND]) | ||
] = Field( | ||
default=300, | ||
description="Maximum number of detections to return", | ||
examples=[300, "$inputs.max_detections"], | ||
) | ||
half_precision: Union[ | ||
Optional[bool], WorkflowParameterSelector(kind=[BOOLEAN_KIND]) | ||
] = Field( | ||
default=False, | ||
description="Enables half-precision (FP16) inference, which can speed up model inference on supported GPUs with minimal impact on accuracy.", | ||
examples=[True, "$inputs.half_precision"], | ||
) | ||
imgsz: Union[ | ||
int, | ||
WorkflowParameterSelector(kind=[INTEGER_KIND]), | ||
] = Field( | ||
default=640, | ||
description="Defines the image size for inference.", | ||
examples=[1280, "$inputs.imgsz"], | ||
) | ||
|
||
@classmethod | ||
def accepts_batch_input(cls) -> bool: | ||
return True | ||
|
||
@classmethod | ||
def describe_outputs(cls) -> List[OutputDefinition]: | ||
return [ | ||
OutputDefinition(name="inference_id", kind=[STRING_KIND]), | ||
OutputDefinition( | ||
name="predictions", kind=[OBJECT_DETECTION_PREDICTION_KIND] | ||
), | ||
] | ||
|
||
@classmethod | ||
def get_execution_engine_compatibility(cls) -> Optional[str]: | ||
return ">=1.0.0,<2.0.0" | ||
|
||
|
||
class UltralyticsBlockV1(WorkflowBlock): | ||
def __init__(self): | ||
self._model: Optional[YOLO] = None | ||
|
||
@classmethod | ||
def get_manifest(cls) -> Type[WorkflowBlockManifest]: | ||
return BlockManifest | ||
|
||
def run( | ||
self, | ||
images: Batch[WorkflowImageData], | ||
model_path: str, | ||
device: str, | ||
class_agnostic_nms: Optional[bool], | ||
confidence: Optional[float], | ||
iou_threshold: Optional[float], | ||
max_detections: Optional[int], | ||
half_precision: bool, | ||
imgsz: int, | ||
) -> BlockResult: | ||
if "YOLO" not in globals(): | ||
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. we must prevent running on hosted platform |
||
raise RuntimeError( | ||
"You must install ultralytics in order to use this block." | ||
) | ||
if not self._model: | ||
self._model = YOLO(model_path) | ||
|
||
predictions = [] | ||
for image in images: | ||
inf = self._model( | ||
image.numpy_image, | ||
imgsz=imgsz, | ||
conf=confidence, | ||
iou=iou_threshold, | ||
half=half_precision, | ||
max_det=max_detections, | ||
agnostic_nms=class_agnostic_nms, | ||
device=device, | ||
verbose=False, | ||
)[0] | ||
detections = sv.Detections.from_ultralytics(inf) | ||
predictions.append(detections) | ||
|
||
return [ | ||
{"inference_id": None, "predictions": prediction} | ||
for prediction in predictions | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,7 @@ def read_requirements(path): | |
), | ||
extras_require={ | ||
"sam": read_requirements("requirements/requirements.sam.txt"), | ||
"ultralytics": read_requirements("requirements/requirements.ultralytics.txt"), | ||
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. not sure if you committed this requirements file |
||
}, | ||
classifiers=[ | ||
"Programming Language :: Python :: 3", | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Let's add more verbose docs