Skip to content
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 421.image caption generator benchmark and added its data in bench… #218

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

octonawish-akcodes
Copy link
Collaborator

@octonawish-akcodes octonawish-akcodes commented Aug 25, 2024

This Pull Request introduces a new Image Caption Generation benchmark for SEBS, implemented using Python. This benchmark uses a hugging face model nlpconnect/vit-gpt2-image-captioning. Link to model https://huggingface.co/nlpconnect/vit-gpt2-image-captioning and its license under Apache 2.0 https://huggingface.co/datasets/choosealicense/licenses/blob/main/markdown/apache-2.0.md

cc @mcopik

Summary by CodeRabbit

  • New Features

    • Introduced configuration files for image captioning to manage resource allocation and execution limits.
    • Added functionality for generating input configurations for image captioning benchmarks.
    • Implemented image captioning using pre-trained models, enabling automatic caption generation for images.
    • Created requirements files to manage necessary Python dependencies for the image captioning functionality.
  • Bug Fixes

    • Ensured the image input generation handles cases where no images are found, preventing execution errors.

…marks-data submodule

Signed-off-by: Abhishek Kumar <abhishek22512@gmail.com>
Copy link

coderabbitai bot commented Aug 25, 2024

Walkthrough

The changes introduce multiple new files for an image captioning benchmark within a subproject. This includes configuration files, input generation functionality, and an implementation for generating captions using pre-trained models. Additionally, requirements files are added to specify necessary Python packages. The subproject reference has also been updated to a new commit in the version control system.

Changes

File Path Change Summary
benchmarks/700.image/701.image-captioning/config.json New configuration file added specifying timeout, memory allocation, and programming language.
benchmarks/700.image/701.image-captioning/input.py New input generation file added with functions for counting buckets and generating input configurations for images.
benchmarks/700.image/701.image-captioning/python/function.py New file implements image captioning using a pre-trained model, including functions for generating captions and handling events.
benchmarks/700.image/701.image-captioning/python/requirements.txt New file listing essential Python package dependencies for the image captioning project.
benchmarks/400.inference/421.image-captioning/config.json New configuration file added specifying timeout, memory allocation, and programming language.
benchmarks/400.inference/421.image-captioning/input.py New input generation file added with functions for counting buckets and generating input configurations for images.
benchmarks/400.inference/421.image-captioning/python/function.py New file implements image captioning using a pre-trained model, including functions for generating captions and handling events.
benchmarks/400.inference/421.image-captioning/python/requirements.txt New file listing essential Python package dependencies for the image captioning project.

Poem

🐇 In the code, new changes hop,
Image captions start to bop.
With config set and input made,
Captions bloom in sunlight's shade.
Transforming bytes to words with glee,
A rabbit's joy, so wild and free! 🌼


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 6d7b456 and f08e8b3.

Files selected for processing (5)
  • benchmarks-data (1 hunks)
  • benchmarks/700.image/701.image-captioning/config.json (1 hunks)
  • benchmarks/700.image/701.image-captioning/input.py (1 hunks)
  • benchmarks/700.image/701.image-captioning/python/function.py (1 hunks)
  • benchmarks/700.image/701.image-captioning/python/requirements.txt (1 hunks)
Files skipped from review due to trivial changes (2)
  • benchmarks-data
  • benchmarks/700.image/701.image-captioning/config.json

@octonawish-akcodes
Copy link
Collaborator Author

Benchmark PR data is here spcl/serverless-benchmarks-data#4

Signed-off-by: Abhishek Kumar <abhishek22512@gmail.com>
@octonawish-akcodes octonawish-akcodes changed the title Add 700.image caption generator benchmark and added its data in bench… Add 421.image caption generator benchmark and added its data in bench… Aug 26, 2024
Signed-off-by: Abhishek Kumar <abhishek22512@gmail.com>
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f08e8b3 and 2c2f62b.

Files selected for processing (4)
  • benchmarks/400.inference/421.image-captioning/config.json (1 hunks)
  • benchmarks/400.inference/421.image-captioning/input.py (1 hunks)
  • benchmarks/400.inference/421.image-captioning/python/function.py (1 hunks)
  • benchmarks/400.inference/421.image-captioning/python/requirements.txt (1 hunks)
Files skipped from review due to trivial changes (1)
  • benchmarks/400.inference/421.image-captioning/config.json
Additional context used
Ruff
benchmarks/400.inference/421.image-captioning/python/function.py

34-34: Undefined name os

(F821)

Additional comments not posted (1)
benchmarks/400.inference/421.image-captioning/python/requirements.txt (1)

1-3: Verify Python version compatibility.

The listed package versions need to be verified for compatibility with the Python versions used in this project.

Comment on lines +18 to +42
input_files = []
for ext in ['*.jpg', '*.jpeg', '*.png']:
input_files.extend(glob.glob(os.path.join(data_dir, ext)))

if not input_files:
raise ValueError("No input files found in the provided directory.")

for file in input_files:
img = os.path.relpath(file, data_dir)
upload_func(0, img, file)

input_config = {
'object': {
'key': img,
'width': 200,
'height': 200
},
'bucket': {
'bucket': benchmarks_bucket,
'input': input_paths[0],
'output': output_paths[0]
}
}

return input_config
Copy link

Choose a reason for hiding this comment

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

Fix variable scope and enhance error handling.

The variable img is used outside its defining loop, which could lead to unexpected behavior if no files are found. Consider defining img outside the loop or handling this case explicitly.

Apply this diff to address the scope issue and enhance error handling:

-def generate_input(data_dir, size, benchmarks_bucket, input_paths, output_paths, upload_func):
+def generate_input(data_dir, size, benchmarks_bucket, input_paths, output_paths, upload_func):
     input_files = []
     for ext in ['*.jpg', '*.jpeg', '*.png']:
         input_files.extend(glob.glob(os.path.join(data_dir, ext)))
     
     if not input_files:
         raise ValueError("No input files found in the provided directory.")
+    img = None  # Define img outside the loop to ensure it's available later
     for file in input_files:
         img = os.path.relpath(file, data_dir)
         upload_func(0, img, file)
     
     if img is None:
         raise ValueError("No valid image files processed.")
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
input_files = []
for ext in ['*.jpg', '*.jpeg', '*.png']:
input_files.extend(glob.glob(os.path.join(data_dir, ext)))
if not input_files:
raise ValueError("No input files found in the provided directory.")
for file in input_files:
img = os.path.relpath(file, data_dir)
upload_func(0, img, file)
input_config = {
'object': {
'key': img,
'width': 200,
'height': 200
},
'bucket': {
'bucket': benchmarks_bucket,
'input': input_paths[0],
'output': output_paths[0]
}
}
return input_config
def generate_input(data_dir, size, benchmarks_bucket, input_paths, output_paths, upload_func):
input_files = []
for ext in ['*.jpg', '*.jpeg', '*.png']:
input_files.extend(glob.glob(os.path.join(data_dir, ext)))
if not input_files:
raise ValueError("No input files found in the provided directory.")
img = None # Define img outside the loop to ensure it's available later
for file in input_files:
img = os.path.relpath(file, data_dir)
upload_func(0, img, file)
if img is None:
raise ValueError("No valid image files processed.")

Comment on lines +1 to +53
import datetime
import io
from urllib.parse import unquote_plus
from PIL import Image
import torch
from transformers import VisionEncoderDecoderModel, ViTImageProcessor, AutoTokenizer
from . import storage

# Load the pre-trained ViT-GPT2 model
model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
image_processor = ViTImageProcessor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")

model.eval()

client = storage.storage.get_instance()

def generate_caption(image_bytes):
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
pixel_values = image_processor(images=image, return_tensors="pt").pixel_values

with torch.no_grad():
generated_ids = model.generate(pixel_values, max_length=16, num_beams=4)
generated_text = tokenizer.decode(generated_ids[0], skip_special_tokens=True)

return generated_text

def handler(event):
bucket = event.get('bucket').get('bucket')
input_prefix = event.get('bucket').get('input')
key = unquote_plus(event.get('object').get('key'))

download_begin = datetime.datetime.now()
img = client.download_stream(bucket, os.path.join(input_prefix, key))
download_end = datetime.datetime.now()

process_begin = datetime.datetime.now()
caption = generate_caption(img)
process_end = datetime.datetime.now()

download_time = (download_end - download_begin) / datetime.timedelta(microseconds=1)
process_time = (process_end - process_begin) / datetime.timedelta(microseconds=1)

return {
'result': {
'caption': caption,
},
'measurement': {
'download_time': download_time,
'download_size': len(img),
'compute_time': process_time
}
}
Copy link

Choose a reason for hiding this comment

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

Fix undefined name and approve model usage.

The code uses os without importing it, which will cause a runtime error. The implementation of the model loading and image processing is correctly done.

Fix the missing import:

+import os
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
import datetime
import io
from urllib.parse import unquote_plus
from PIL import Image
import torch
from transformers import VisionEncoderDecoderModel, ViTImageProcessor, AutoTokenizer
from . import storage
# Load the pre-trained ViT-GPT2 model
model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
image_processor = ViTImageProcessor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
model.eval()
client = storage.storage.get_instance()
def generate_caption(image_bytes):
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
pixel_values = image_processor(images=image, return_tensors="pt").pixel_values
with torch.no_grad():
generated_ids = model.generate(pixel_values, max_length=16, num_beams=4)
generated_text = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
return generated_text
def handler(event):
bucket = event.get('bucket').get('bucket')
input_prefix = event.get('bucket').get('input')
key = unquote_plus(event.get('object').get('key'))
download_begin = datetime.datetime.now()
img = client.download_stream(bucket, os.path.join(input_prefix, key))
download_end = datetime.datetime.now()
process_begin = datetime.datetime.now()
caption = generate_caption(img)
process_end = datetime.datetime.now()
download_time = (download_end - download_begin) / datetime.timedelta(microseconds=1)
process_time = (process_end - process_begin) / datetime.timedelta(microseconds=1)
return {
'result': {
'caption': caption,
},
'measurement': {
'download_time': download_time,
'download_size': len(img),
'compute_time': process_time
}
}
import datetime
import io
from urllib.parse import unquote_plus
from PIL import Image
import torch
from transformers import VisionEncoderDecoderModel, ViTImageProcessor, AutoTokenizer
from . import storage
+import os
# Load the pre-trained ViT-GPT2 model
model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
image_processor = ViTImageProcessor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
model.eval()
client = storage.storage.get_instance()
def generate_caption(image_bytes):
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
pixel_values = image_processor(images=image, return_tensors="pt").pixel_values
with torch.no_grad():
generated_ids = model.generate(pixel_values, max_length=16, num_beams=4)
generated_text = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
return generated_text
def handler(event):
bucket = event.get('bucket').get('bucket')
input_prefix = event.get('bucket').get('input')
key = unquote_plus(event.get('object').get('key'))
download_begin = datetime.datetime.now()
img = client.download_stream(bucket, os.path.join(input_prefix, key))
download_end = datetime.datetime.now()
process_begin = datetime.datetime.now()
caption = generate_caption(img)
process_end = datetime.datetime.now()
download_time = (download_end - download_begin) / datetime.timedelta(microseconds=1)
process_time = (process_end - process_begin) / datetime.timedelta(microseconds=1)
return {
'result': {
'caption': caption,
},
'measurement': {
'download_time': download_time,
'download_size': len(img),
'compute_time': process_time
}
}
Tools
Ruff

34-34: Undefined name os

(F821)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants