-
Notifications
You must be signed in to change notification settings - Fork 26.3k
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 validate images and text inputs order util for processors and test_processing_utils #33285
Merged
yonigozlan
merged 5 commits into
huggingface:main
from
yonigozlan:add-test-processing-utils
Sep 4, 2024
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
84367de
Add validate images and test processing utils
yonigozlan d7aeafc
Remove encoded text from possible inputs in tests
yonigozlan 1339446
Removed encoded inputs as valid in processing_utils
yonigozlan e776055
change text input check to be recursive
yonigozlan 4d18658
chnage text check to all element of lists and not just the first one
yonigozlan 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
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,164 @@ | ||
# coding=utf-8 | ||
# Copyright 2024 HuggingFace Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import unittest | ||
|
||
import numpy as np | ||
|
||
from transformers import is_torch_available, is_vision_available | ||
from transformers.processing_utils import _validate_images_text_input_order | ||
from transformers.testing_utils import require_torch, require_vision | ||
|
||
|
||
if is_vision_available(): | ||
import PIL | ||
|
||
if is_torch_available(): | ||
import torch | ||
|
||
|
||
@require_vision | ||
class ProcessingUtilTester(unittest.TestCase): | ||
def test_validate_images_text_input_order(self): | ||
# text string and PIL images inputs | ||
images = PIL.Image.new("RGB", (224, 224)) | ||
text = "text" | ||
# test correct text and images order | ||
valid_images, valid_text = _validate_images_text_input_order(images=images, text=text) | ||
self.assertEqual(valid_images, images) | ||
self.assertEqual(valid_text, text) | ||
# test incorrect text and images order | ||
valid_images, valid_text = _validate_images_text_input_order(images=text, text=images) | ||
self.assertEqual(valid_images, images) | ||
self.assertEqual(valid_text, text) | ||
|
||
# text list of string and numpy images inputs | ||
images = np.random.rand(224, 224, 3) | ||
text = ["text1", "text2"] | ||
# test correct text and images order | ||
valid_images, valid_text = _validate_images_text_input_order(images=images, text=text) | ||
self.assertTrue(np.array_equal(valid_images, images)) | ||
self.assertEqual(valid_text, text) | ||
# test incorrect text and images order | ||
valid_images, valid_text = _validate_images_text_input_order(images=text, text=images) | ||
self.assertTrue(np.array_equal(valid_images, images)) | ||
self.assertEqual(valid_text, text) | ||
|
||
# text nested list of string and list of pil images inputs | ||
images = [PIL.Image.new("RGB", (224, 224)), PIL.Image.new("RGB", (224, 224))] | ||
text = [["text1", "text2, text3"], ["text3", "text4"]] | ||
# test correct text and images order | ||
valid_images, valid_text = _validate_images_text_input_order(images=images, text=text) | ||
self.assertEqual(valid_images, images) | ||
self.assertEqual(valid_text, text) | ||
# test incorrect text and images order | ||
valid_images, valid_text = _validate_images_text_input_order(images=text, text=images) | ||
self.assertEqual(valid_images, images) | ||
self.assertEqual(valid_text, text) | ||
|
||
# list of strings and list of numpy images inputs | ||
images = [np.random.rand(224, 224, 3), np.random.rand(224, 224, 3)] | ||
text = ["text1", "text2"] | ||
# test correct text and images order | ||
valid_images, valid_text = _validate_images_text_input_order(images=images, text=text) | ||
self.assertTrue(np.array_equal(valid_images[0], images[0])) | ||
self.assertEqual(valid_text, text) | ||
# test incorrect text and images order | ||
valid_images, valid_text = _validate_images_text_input_order(images=text, text=images) | ||
self.assertTrue(np.array_equal(valid_images[0], images[0])) | ||
self.assertEqual(valid_text, text) | ||
|
||
# list of strings and nested list of numpy images inputs | ||
images = [[np.random.rand(224, 224, 3), np.random.rand(224, 224, 3)], [np.random.rand(224, 224, 3)]] | ||
text = ["text1", "text2"] | ||
# test correct text and images order | ||
valid_images, valid_text = _validate_images_text_input_order(images=images, text=text) | ||
self.assertTrue(np.array_equal(valid_images[0][0], images[0][0])) | ||
self.assertEqual(valid_text, text) | ||
# test incorrect text and images order | ||
valid_images, valid_text = _validate_images_text_input_order(images=text, text=images) | ||
self.assertTrue(np.array_equal(valid_images[0][0], images[0][0])) | ||
self.assertEqual(valid_text, text) | ||
|
||
# nested list of strings and nested list of PIL images inputs | ||
images = [ | ||
[PIL.Image.new("RGB", (224, 224)), PIL.Image.new("RGB", (224, 224))], | ||
[PIL.Image.new("RGB", (224, 224))], | ||
] | ||
text = [["text1", "text2, text3"], ["text3", "text4"]] | ||
# test correct text and images order | ||
valid_images, valid_text = _validate_images_text_input_order(images=images, text=text) | ||
self.assertEqual(valid_images, images) | ||
self.assertEqual(valid_text, text) | ||
# test incorrect text and images order | ||
valid_images, valid_text = _validate_images_text_input_order(images=text, text=images) | ||
self.assertEqual(valid_images, images) | ||
self.assertEqual(valid_text, text) | ||
|
||
# None images | ||
images = None | ||
text = "text" | ||
# test correct text and images order | ||
valid_images, valid_text = _validate_images_text_input_order(images=images, text=text) | ||
self.assertEqual(images, None) | ||
self.assertEqual(text, text) | ||
# test incorrect text and images order | ||
valid_images, valid_text = _validate_images_text_input_order(images=text, text=images) | ||
self.assertEqual(images, None) | ||
self.assertEqual(text, text) | ||
|
||
# None text | ||
images = PIL.Image.new("RGB", (224, 224)) | ||
text = None | ||
# test correct text and images order | ||
valid_images, valid_text = _validate_images_text_input_order(images=images, text=text) | ||
self.assertEqual(images, images) | ||
self.assertEqual(text, None) | ||
# test incorrect text and images order | ||
valid_images, valid_text = _validate_images_text_input_order(images=text, text=images) | ||
self.assertEqual(images, images) | ||
self.assertEqual(text, None) | ||
|
||
# incorrect inputs | ||
images = "text" | ||
text = "text" | ||
with self.assertRaises(ValueError): | ||
_validate_images_text_input_order(images=images, text=text) | ||
|
||
@require_torch | ||
def test_validate_images_text_input_order_torch(self): | ||
# text string and torch images inputs | ||
images = torch.rand(224, 224, 3) | ||
text = "text" | ||
# test correct text and images order | ||
valid_images, valid_text = _validate_images_text_input_order(images=images, text=text) | ||
self.assertTrue(torch.equal(valid_images, images)) | ||
self.assertEqual(valid_text, text) | ||
# test incorrect text and images order | ||
valid_images, valid_text = _validate_images_text_input_order(images=text, text=images) | ||
self.assertTrue(torch.equal(valid_images, images)) | ||
self.assertEqual(valid_text, text) | ||
|
||
# text list of string and list of torch images inputs | ||
images = [torch.rand(224, 224, 3), torch.rand(224, 224, 3)] | ||
text = ["text1", "text2"] | ||
# test correct text and images order | ||
valid_images, valid_text = _validate_images_text_input_order(images=images, text=text) | ||
self.assertTrue(torch.equal(valid_images[0], images[0])) | ||
self.assertEqual(valid_text, text) | ||
# test incorrect text and images order | ||
valid_images, valid_text = _validate_images_text_input_order(images=text, text=images) | ||
self.assertTrue(torch.equal(valid_images[0], images[0])) | ||
self.assertEqual(valid_text, text) |
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.
This can be re-written to be recursive