-
Notifications
You must be signed in to change notification settings - Fork 31
feat(live): add @pipeline decorator for simplified pipeline creation #900
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
Draft
rickstaa
wants to merge
5
commits into
livepeer:main
Choose a base branch
from
rickstaa:feat/pipeline-decorator
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.
Draft
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
378275f
feat(live): add @pipeline decorator for simplified pipeline creation
rickstaa c8381c4
fix(live): address PR review feedback on @pipeline decorator
rickstaa 428dbdf
feat(examples): add __main__ entrypoints to example pipelines
rickstaa bd5bf2b
fix(live): warn when @pipeline decorates a nested definition
rickstaa fe546af
feat(examples): replace __main__ entrypoints with test_examples script
rickstaa 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 hidden or 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.
This file contains hidden or 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,115 @@ | ||
| """Depth Estimation (MiDaS) -- @pipeline example with model loading. | ||
|
|
||
| Uses MiDaS Small to produce a real-time depth map from video frames. | ||
| Demonstrates prepare_models (download + load) and GPU inference in transform. | ||
|
|
||
| Usage: | ||
| python -m runner.live.infer \ | ||
| --pipeline '{ | ||
| "pipeline_cls": "examples.pipelines.depth_midas:DepthMidasPipeline" | ||
|
rickstaa marked this conversation as resolved.
Outdated
|
||
| }' | ||
| """ | ||
|
|
||
| import logging | ||
|
|
||
| import torch | ||
| from pydantic import Field | ||
|
|
||
| from runner.live.pipelines import pipeline, BaseParams | ||
| from runner.live.trickle import VideoFrame | ||
|
|
||
|
|
||
| class DepthParams(BaseParams): | ||
| """Parameters for depth estimation, adjustable mid-stream.""" | ||
|
|
||
| colormap: bool = Field( | ||
| default=True, | ||
| description="Apply colormap to depth output. False = grayscale.", | ||
| ) | ||
| near_clip: float = Field( | ||
| default=0.0, ge=0.0, le=1.0, | ||
| description="Clip depth values below this threshold (0.0 = no clip).", | ||
| ) | ||
| far_clip: float = Field( | ||
| default=1.0, ge=0.0, le=1.0, | ||
| description="Clip depth values above this threshold (1.0 = no clip).", | ||
| ) | ||
|
|
||
|
|
||
| @pipeline(name="depth-midas", params=DepthParams) | ||
| class DepthMidas: | ||
| """Real-time depth estimation using MiDaS Small. | ||
|
|
||
| Demonstrates: | ||
| - prepare_models: downloads the model at startup | ||
| - on_ready: loads model to GPU | ||
| - transform: runs inference per frame | ||
| - on_update: recomputes clip range when params change mid-stream | ||
| """ | ||
|
|
||
| def on_ready(self, **params): | ||
| self._near = params.get("near_clip", 0.0) | ||
| self._far = params.get("far_clip", 1.0) | ||
| self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | ||
| self.model = torch.hub.load("intel-isl/MiDaS", "MiDaS_small", pretrained=True) | ||
| self.model.to(self.device).eval() | ||
|
|
||
| self.transforms = torch.hub.load("intel-isl/MiDaS", "transforms").small_transform | ||
| logging.info(f"MiDaS Small loaded on {self.device}") | ||
|
|
||
| def transform(self, frame: VideoFrame, params: DepthParams) -> torch.Tensor: | ||
| """Run MiDaS depth estimation on each frame.""" | ||
| # frame.tensor is (B, H, W, C) in [-1.0, 1.0] | ||
| tensor = frame.tensor[0] # (H, W, C), single batch | ||
| h, w = tensor.shape[:2] | ||
|
|
||
| # Convert to uint8 RGB for MiDaS transforms | ||
| img = ((tensor + 1.0) / 2.0 * 255).byte().cpu().numpy() | ||
|
|
||
| # MiDaS expects (B, C, H, W) float32 | ||
| input_batch = self.transforms(img).to(self.device) | ||
|
|
||
| with torch.no_grad(): | ||
| depth = self.model(input_batch) # (1, h', w') | ||
|
|
||
| # Resize back to original resolution | ||
| depth = torch.nn.functional.interpolate( | ||
| depth.unsqueeze(1), size=(h, w), mode="bilinear", align_corners=False | ||
| ).squeeze() # (H, W) | ||
|
|
||
| # Normalize to [0, 1] and apply depth clipping | ||
| depth = (depth - depth.min()) / (depth.max() - depth.min() + 1e-8) | ||
| depth = torch.clamp(depth, self._near, self._far) | ||
| depth = (depth - self._near) / (self._far - self._near + 1e-8) | ||
|
|
||
| if params.colormap: | ||
| # Simple colormap: near=warm, far=cool | ||
| r = depth | ||
| g = 1.0 - torch.abs(depth - 0.5) * 2.0 | ||
| b = 1.0 - depth | ||
| out = torch.stack([r, g, b], dim=-1) # (H, W, 3) | ||
| else: | ||
| # Grayscale | ||
| out = depth.unsqueeze(-1).expand(-1, -1, 3) # (H, W, 3) | ||
|
|
||
| # Convert to [-1, 1] and add batch dim → (1, H, W, C) | ||
| out = (out * 2.0 - 1.0).unsqueeze(0) | ||
| return out.to(frame.tensor.device) | ||
|
|
||
| def on_update(self, **params): | ||
| """Update clip range when params change mid-stream via control_url.""" | ||
| self._near = params.get("near_clip", 0.0) | ||
| self._far = params.get("far_clip", 1.0) | ||
| logging.info(f"Depth clip range updated: [{self._near}, {self._far}]") | ||
|
|
||
| def on_stop(self): | ||
| logging.info("DepthMidas stopped") | ||
|
|
||
| @classmethod | ||
| def prepare_models(cls): | ||
| """Download MiDaS Small weights ahead of time.""" | ||
| torch.hub.load("intel-isl/MiDaS", "MiDaS_small", pretrained=True) | ||
| logging.info("MiDaS Small weights downloaded") | ||
|
|
||
|
|
||
| DepthMidasPipeline = DepthMidas | ||
This file contains hidden or 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,29 @@ | ||
| """Green Shift -- minimal @pipeline example (function form). | ||
|
|
||
| Boosts the green channel of every video frame. No model download, no GPU | ||
| inference -- just a tensor op. Ideal for verifying the pipeline infrastructure. | ||
|
|
||
| Usage: | ||
| python -m runner.live.infer \ | ||
| --pipeline '{"pipeline_cls":"examples.pipelines.green_shift:GreenShiftPipeline"}' | ||
|
rickstaa marked this conversation as resolved.
Outdated
|
||
| """ | ||
|
|
||
| import torch | ||
|
|
||
| from runner.live.pipelines import pipeline, BaseParams | ||
| from runner.live.trickle import VideoFrame | ||
|
|
||
|
|
||
| @pipeline(name="green-shift") | ||
| async def green_shift(frame: VideoFrame, params: BaseParams) -> torch.Tensor: | ||
| """Boost the green channel of every frame. | ||
|
|
||
| Frame tensor layout: (B, H, W, C), values in [-1.0, 1.0]. | ||
| Channel order: R=0, G=1, B=2. | ||
| """ | ||
| tensor = frame.tensor.clone() | ||
| tensor[:, :, :, 1] = torch.clamp(tensor[:, :, :, 1] + 0.3, -1.0, 1.0) | ||
| return tensor | ||
|
|
||
|
|
||
| GreenShiftPipeline = green_shift | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| from .interface import Pipeline, BaseParams | ||
| from .spec import PipelineSpec, builtin_pipeline_spec | ||
| from .create import pipeline | ||
|
|
||
| __all__ = ["Pipeline", "BaseParams", "PipelineSpec", "builtin_pipeline_spec"] | ||
| __all__ = ["Pipeline", "BaseParams", "PipelineSpec", "builtin_pipeline_spec", "pipeline"] |
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.
Uh oh!
There was an error while loading. Please reload this page.