-
Notifications
You must be signed in to change notification settings - Fork 91
Add 421.image caption generator benchmark and added its data in bench… #218
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
Open
octonawish-akcodes
wants to merge
3
commits into
spcl:master
Choose a base branch
from
octonawish-akcodes:benchmark2
base: master
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 1 commit
Commits
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
Submodule benchmarks-data
updated
from 6a17a4 to f407c2
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,6 @@ | ||
| { | ||
| "timeout": 60, | ||
| "memory": 256, | ||
| "languages": ["python"] | ||
| } | ||
|
|
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,40 @@ | ||
| import glob | ||
| import os | ||
|
|
||
| def buckets_count(): | ||
| return (1, 1) | ||
|
|
||
| ''' | ||
| Generate test, small, and large workload for image captioning benchmark. | ||
|
|
||
| :param data_dir: Directory where benchmark data is placed | ||
| :param size: Workload size | ||
| :param benchmarks_bucket: Storage container for the benchmark | ||
| :param input_paths: List of input paths | ||
| :param output_paths: List of output paths | ||
| :param upload_func: Upload function taking three params (bucket_idx, key, filepath) | ||
| ''' | ||
| def generate_input(data_dir, size, benchmarks_bucket, input_paths, output_paths, upload_func): | ||
| input_files = glob.glob(os.path.join(data_dir, '*.jpg')) + glob.glob(os.path.join(data_dir, '*.png')) + glob.glob(os.path.join(data_dir, '*.jpeg')) | ||
|
|
||
| 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 | ||
67 changes: 67 additions & 0 deletions
67
benchmarks/700.image/701.image-captioning/python/function.py
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,67 @@ | ||
| import datetime | ||
| import io | ||
| import os | ||
| 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 URL: https://huggingface.co/nlpconnect/vit-gpt2-image-captioning | ||
| # License: Apache 2.0 License (https://huggingface.co/datasets/choosealicense/licenses/blob/main/markdown/apache-2.0.md) | ||
| 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') | ||
| output_prefix = event.get('bucket').get('output') | ||
| 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() | ||
|
|
||
| upload_begin = datetime.datetime.now() | ||
| caption_file_name = os.path.splitext(key)[0] + '.txt' | ||
| caption_file_path = os.path.join(output_prefix, caption_file_name) | ||
| client.upload_stream(bucket, caption_file_path, io.BytesIO(caption.encode('utf-8'))) | ||
| upload_end = datetime.datetime.now() | ||
|
|
||
| download_time = (download_end - download_begin) / datetime.timedelta(microseconds=1) | ||
| upload_time = (upload_end - upload_begin) / datetime.timedelta(microseconds=1) | ||
| process_time = (process_end - process_begin) / datetime.timedelta(microseconds=1) | ||
octonawish-akcodes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return { | ||
| 'result': { | ||
| 'bucket': bucket, | ||
| 'key': caption_file_path | ||
| }, | ||
| 'measurement': { | ||
| 'download_time': download_time, | ||
| 'download_size': len(img), | ||
| 'upload_time': upload_time, | ||
| 'upload_size': len(caption.encode('utf-8')), | ||
| 'compute_time': process_time | ||
| } | ||
| } | ||
3 changes: 3 additions & 0 deletions
3
benchmarks/700.image/701.image-captioning/python/requirements.txt
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,3 @@ | ||
| transformers==4.44.2 | ||
| torch==2.4.0 | ||
octonawish-akcodes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| pillow==10.4.0 | ||
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.