Skip to content

Comments

ML pipelines: RunInference - OSS Image Object detection, OSS Image Captioning, OSS Image Classification#37186

Open
Amar3tto wants to merge 21 commits intomasterfrom
oss-image-detection
Open

ML pipelines: RunInference - OSS Image Object detection, OSS Image Captioning, OSS Image Classification#37186
Amar3tto wants to merge 21 commits intomasterfrom
oss-image-detection

Conversation

@Amar3tto
Copy link
Collaborator

Please add a meaningful description for your change here


Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:

  • Mention the appropriate issue in your description (for example: addresses #123), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment fixes #<ISSUE NUMBER> instead.
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

See the Contributor Guide for more tips on how to make review process smoother.

To check the build health, please visit https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md

GitHub Actions Tests Status (on master branch)

Build python source distribution and wheels
Python tests
Java tests
Go tests

See CI.md for more information about GitHub Actions CI or the workflows README to see a list of phrases to trigger workflows.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @Amar3tto, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances Apache Beam's machine learning capabilities by integrating a new PyTorch-based image object detection pipeline. The pipeline leverages the RunInference transform for efficient batched GPU inference with open-source TorchVision models, processing images from cloud storage and outputting structured detection results to BigQuery. This addition is complemented by a new performance benchmark and corresponding documentation, ensuring that the pipeline's efficiency and resource usage can be consistently monitored and evaluated.

Highlights

  • New PyTorch Object Detection Example: Introduced a new example pipeline for PyTorch image object detection using Apache Beam's RunInference, capable of processing image URIs from GCS, performing batched GPU inference with TorchVision models, and writing results to BigQuery.
  • Dedicated Performance Benchmark: Added a new benchmark test (PytorchImageObjectDetectionBenchmarkTest) to measure and track the performance of the PyTorch image object detection pipeline, focusing on stable GPU inference workloads.
  • Updated Documentation and Dependencies: Included new Python dependencies for PyTorch object detection and updated the project's website with a dedicated performance page for the new benchmark, including placeholders for Looker Studio metrics.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Ignored Files
  • Ignored by pattern: .github/workflows/** (2)
    • .github/workflows/beam_Inference_Python_Benchmarks_Dataflow.yml
    • .github/workflows/load-tests-pipeline-options/beam_Inference_Python_Benchmarks_Dataflow_Pytorch_Image_Detection.txt
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@Amar3tto Amar3tto changed the title ML pipelines: RunInference - OSS Image Object detection ML pipelines: RunInference - OSS Image Object detection, OSS Image Captioning Dec 31, 2025
@codecov
Copy link

codecov bot commented Dec 31, 2025

Codecov Report

❌ Patch coverage is 0% with 28 lines in your changes missing coverage. Please review.
✅ Project coverage is 36.33%. Comparing base (358e007) to head (64187d9).
⚠️ Report is 13 commits behind head on master.

Files with missing lines Patch % Lines
...s/inference/pytorch_image_captioning_benchmarks.py 0.00% 14 Missing ⚠️
...rence/pytorch_image_object_detection_benchmarks.py 0.00% 14 Missing ⚠️

❗ There is a different number of reports uploaded between BASE (358e007) and HEAD (64187d9). Click for more details.

HEAD has 3 uploads less than BASE
Flag BASE (358e007) HEAD (64187d9)
python 4 1
Additional details and impacted files
@@              Coverage Diff              @@
##             master   #37186       +/-   ##
=============================================
- Coverage     55.28%   36.33%   -18.96%     
  Complexity     1676     1676               
=============================================
  Files          1067     1069        +2     
  Lines        167148   167178       +30     
  Branches       1208     1208               
=============================================
- Hits          92415    60737    -31678     
- Misses        72551   104259    +31708     
  Partials       2182     2182               
Flag Coverage Δ
python 40.60% <0.00%> (-40.46%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Amar3tto Amar3tto changed the title ML pipelines: RunInference - OSS Image Object detection, OSS Image Captioning ML pipelines: RunInference - OSS Image Object detection, OSS Image Captioning, OSS Image Classification Jan 21, 2026
@Amar3tto Amar3tto requested a review from damccorm February 7, 2026 05:40
@Amar3tto Amar3tto marked this pull request as ready for review February 7, 2026 05:41
@github-actions
Copy link
Contributor

github-actions bot commented Feb 7, 2026

Assigning reviewers:

R: @claudevdm for label python.
R: @liferoad for label build.
R: @shunping for label website.

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

@Amar3tto Amar3tto requested a review from damccorm February 12, 2026 14:40
@Amar3tto
Copy link
Collaborator Author

@Abacn Could you please help with review?

# limitations under the License.

name: Inference Python Benchmarks Dataflow
name: Inference Python Benchmarks Dataflow (1 part)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to split these into different workflows? If it is just about minimizing the time it takes to run, could we do one workflow with 2 jobs?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or if there is a reason, could you add a comment explaining it? (maybe what I'm suggesting would exhaust resources and we need different cron schedules?)

# ============ DoFns ============


class RateLimitDoFn(beam.DoFn):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we have this function? It will not effectively provide a global rate limit since multiple instances of this will be running in parallel

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating a rate limit is doable, but we would need to use a stateful DoFn to effectively do this. Basically the idea would be:

  1. Key all the incoming data with a single (non-unique) key
  2. For each incoming piece of data:
  • check stored state to see if it is ready to be released, and if not sleep until it is
  • Yield the element
  • Store the next release time (current time + delay) in state

Because this functionally single-threads the output, it may be too slow to achieve the target rate; if that's the case, in step (1) you can partition to N keys, and do the same thing for each of them, yielding at a rate of rate_per_sec/N

def run_inference(
self, batch: List[Dict[str, Any]], model, inference_args=None):

if model is not None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When could model be none?

if model is not None:
self._model = model
self._model.to(self.device)
self._model.eval()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't we already called this in load_model?

self._model.eval()
if self._processor is None:
from transformers import BlipProcessor
self._processor = BlipProcessor.from_pretrained(self.model_name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better pattern here might just be to return a class which contains both the processor and the model from load_model

return "blip_captioning"


class ClipRankModelHandler(ModelHandler):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same general comments as the Blip model handler apply here

lines = (
pipeline
|
'ReadGCSFile' >> beam.Create(list(read_gcs_file_lines(known_args.input)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just use built in Beam transforms to read from gcs instead of doing it all locally?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or alternately, if we're doing it locally, can we just do all of our Pub/Sub hydration without a beam pipeline

# ============ DoFns ============


class RateLimitDoFn(beam.DoFn):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have the same questions about this file as the previous one - also, can we split the shared functions out into a helper class?

return torch.from_numpy(arr).float() # float32, shape (3,224,224)


class RateLimitDoFn(beam.DoFn):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same general questions about this file

# ============ Main pipeline ============


def run(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this is a rightfitting pipeline, but does it actually use resource hints? https://docs.cloud.google.com/dataflow/docs/guides/right-fitting#python

@damccorm
Copy link
Contributor

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces three new ML inference pipelines for image classification, object detection, and image captioning using PyTorch, along with their corresponding benchmarks and documentation. The pipelines are well-structured and showcase advanced Beam features like RunInference with custom model handlers and stateful DoFns. My review focuses on improving scalability, robustness, and maintainability. I've identified a few key areas for improvement, including a scalability bottleneck in the data loading pipelines, several instances of broad exception handling that could mask errors, some potentially buggy logic, and a few copy-paste errors in the new documentation pages. Overall, this is a valuable contribution, and the suggested changes aim to make these examples more robust and easier to understand.

lines = (
pipeline
|
'ReadGCSFile' >> beam.Create(list(read_gcs_file_lines(known_args.input)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Reading the entire input file into a list with list(read_gcs_file_lines(...)) and passing it to beam.Create can cause memory issues on the client submitting the job, especially with large input files. This approach doesn't scale. A better approach is to use beam.io.ReadFromText which creates a distributed PCollection from the file.

Suggested change
'ReadGCSFile' >> beam.Create(list(read_gcs_file_lines(known_args.input)))
'ReadGCSFile' >> beam.io.ReadFromText(known_args.input)

Comment on lines +451 to +454
except Exception as e:
last_err = e
logging.warning("Batch size %s failed during warmup: %s", bs, e)
continue
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Catching a broad Exception during the batch size warmup can mask critical errors like model loading failures, configuration issues, or network problems. It's safer to catch more specific exceptions, such as RuntimeError for CUDA out-of-memory errors, and allow other unexpected errors to fail fast.

lines = (
pipeline
|
'ReadGCSFile' >> beam.Create(list(read_gcs_file_lines(known_args.input)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Reading the entire input file into a list with list(read_gcs_file_lines(...)) and passing it to beam.Create can cause memory issues on the client submitting the job, especially with large input files. This approach doesn't scale. A better approach is to use beam.io.ReadFromText which creates a distributed PCollection from the file.

Suggested change
'ReadGCSFile' >> beam.Create(list(read_gcs_file_lines(known_args.input)))
'ReadGCSFile' >> beam.io.ReadFromText(known_args.input)

# element is bytes message, assume it includes
# {"image_id": "...", "bytes": base64?} or just raw bytes.
import hashlib
b = element if isinstance(element, (bytes, bytearray)) else bytes(element)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

If element is a string, bytes(element) will raise a TypeError because it requires an encoding. You probably meant element.encode('utf-8'). The pipeline is constructed such that MakeKeyDoFn always receives a string (URI), so this bytes input mode appears to be unused and would fail if ever invoked with string inputs.

Suggested change
b = element if isinstance(element, (bytes, bytearray)) else bytes(element)
b = element if isinstance(element, (bytes, bytearray)) else element.encode('utf-8')

lines = (
pipeline
|
'ReadGCSFile' >> beam.Create(list(read_gcs_file_lines(known_args.input)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Reading the entire input file into a list with list(read_gcs_file_lines(...)) and passing it to beam.Create can cause memory issues on the client submitting the job, especially with large input files. This approach doesn't scale. A better approach is to use beam.io.ReadFromText which creates a distributed PCollection from the file.

Suggested change
'ReadGCSFile' >> beam.Create(list(read_gcs_file_lines(known_args.input)))
'ReadGCSFile' >> beam.io.ReadFromText(known_args.input)

This streaming pipeline performs image classification using an open-source PyTorch EfficientNet-B0 model optimized for T4 GPUs.
It reads image URIs from Pub/Sub, decodes and preprocesses them in parallel, and runs inference with adaptive batch sizing for optimal GPU utilization.

The following graphs show various metrics when running PyTorch Sentiment Analysis Streaming using Hugging Face DistilBERT base uncased model pipeline.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This description seems to be a copy-paste error from another benchmark page. It should describe the Image Classification pipeline, not Sentiment Analysis.

Suggested change
The following graphs show various metrics when running PyTorch Sentiment Analysis Streaming using Hugging Face DistilBERT base uncased model pipeline.
The following graphs show various metrics when running the PyTorch Image Classification EfficientNet-B0 Streaming (Right-fitting) GPU pipeline.

@@ -0,0 +1,44 @@
---
title: "PyTorch Language Modeling BERT base Performance"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The title of this page appears to be incorrect. It refers to 'PyTorch Language Modeling BERT base Performance', but the content is about the Image Classification EfficientNet-B0 pipeline.

Suggested change
title: "PyTorch Language Modeling BERT base Performance"
title: "PyTorch Image Classification EfficientNet-B0 Streaming (Right-fitting, Exactly-once) CPU Performance"

It reads image URIs from Pub/Sub, decodes and preprocesses them in parallel, and runs inference with adaptive batch sizing.
The pipeline ensures exactly-once semantics via stateful deduplication and idempotent BigQuery writes, allowing stable and reproducible performance measurements under continuous load.

The following graphs show various metrics when running PyTorch Sentiment Analysis Streaming using Hugging Face DistilBERT base uncased model pipeline.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This description seems to be a copy-paste error from another benchmark page. It should describe the Image Classification pipeline, not Sentiment Analysis.

Suggested change
The following graphs show various metrics when running PyTorch Sentiment Analysis Streaming using Hugging Face DistilBERT base uncased model pipeline.
The following graphs show various metrics when running the PyTorch Image Classification EfficientNet-B0 Streaming (Right-fitting, Exactly-once) CPU pipeline.

@@ -0,0 +1,44 @@
---
title: "PyTorch Language Modeling BERT base Performance"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The title of this page appears to be incorrect. It refers to 'PyTorch Language Modeling BERT base Performance', but the content is about the Image Classification EfficientNet-B0 pipeline.

Suggested change
title: "PyTorch Language Modeling BERT base Performance"
title: "PyTorch Image Classification EfficientNet-B0 Streaming (Right-fitting, Exactly-once) GPU Performance"

It reads image URIs from Pub/Sub, decodes and preprocesses them in parallel, and runs inference with adaptive batch sizing for optimal GPU utilization.
The pipeline ensures exactly-once semantics via stateful deduplication and idempotent BigQuery writes, allowing stable and reproducible performance measurements under continuous load.

The following graphs show various metrics when running PyTorch Sentiment Analysis Streaming using Hugging Face DistilBERT base uncased model pipeline.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This description seems to be a copy-paste error from another benchmark page. It should describe the Image Classification pipeline, not Sentiment Analysis.

Suggested change
The following graphs show various metrics when running PyTorch Sentiment Analysis Streaming using Hugging Face DistilBERT base uncased model pipeline.
The following graphs show various metrics when running the PyTorch Image Classification EfficientNet-B0 Streaming (Right-fitting, Exactly-once) GPU pipeline.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants