-
Notifications
You must be signed in to change notification settings - Fork 32k
Update feature extractor methods to enable type cast before normalize #18499
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
Merged
amyeroberts
merged 4 commits into
huggingface:main
from
amyeroberts:type-cast-before-normalize-update-methods
Aug 17, 2022
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f5cdb14
Update methods to optionally rescale
amyeroberts 134e7a7
Cast images to numpy arrays in call to enable consistent behaviour wi…
amyeroberts 1ace93b
Remove accidental clip changes
amyeroberts e1407f8
Update tests to reflect the scaling logic
amyeroberts 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
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 |
|---|---|---|
|
|
@@ -58,13 +58,13 @@ def test_conversion_image_to_array(self): | |
| array3 = feature_extractor.to_numpy_array(image, rescale=False) | ||
| self.assertTrue(array3.dtype, np.uint8) | ||
| self.assertEqual(array3.shape, (3, 16, 32)) | ||
| self.assertTrue(np.array_equal(array1, array3.astype(np.float32) / 255.0)) | ||
| self.assertTrue(np.array_equal(array1, array3.astype(np.float32) * (1 / 255.0))) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was changed to reflect the rescaling logic. The max difference between the arrays before this change was ~5e-8. |
||
|
|
||
| # Conversion with no rescale and not channel first | ||
| array4 = feature_extractor.to_numpy_array(image, rescale=False, channel_first=False) | ||
| self.assertTrue(array4.dtype, np.uint8) | ||
| self.assertEqual(array4.shape, (16, 32, 3)) | ||
| self.assertTrue(np.array_equal(array2, array4.astype(np.float32) / 255.0)) | ||
| self.assertTrue(np.array_equal(array2, array4.astype(np.float32) * (1 / 255.0))) | ||
|
|
||
| def test_conversion_array_to_array(self): | ||
| feature_extractor = ImageFeatureExtractionMixin() | ||
|
|
@@ -74,13 +74,13 @@ def test_conversion_array_to_array(self): | |
| array1 = feature_extractor.to_numpy_array(array) | ||
| self.assertTrue(array1.dtype, np.float32) | ||
| self.assertEqual(array1.shape, (3, 16, 32)) | ||
| self.assertTrue(np.array_equal(array1, array.transpose(2, 0, 1).astype(np.float32) / 255.0)) | ||
| self.assertTrue(np.array_equal(array1, array.transpose(2, 0, 1).astype(np.float32) * (1 / 255.0))) | ||
|
|
||
| # Same with no permute | ||
| array2 = feature_extractor.to_numpy_array(array, channel_first=False) | ||
| self.assertTrue(array2.dtype, np.float32) | ||
| self.assertEqual(array2.shape, (16, 32, 3)) | ||
| self.assertTrue(np.array_equal(array2, array.astype(np.float32) / 255.0)) | ||
| self.assertTrue(np.array_equal(array2, array.astype(np.float32) * (1 / 255.0))) | ||
|
|
||
| # Force rescale to False | ||
| array3 = feature_extractor.to_numpy_array(array, rescale=False) | ||
|
|
@@ -110,13 +110,13 @@ def test_conversion_torch_to_array(self): | |
| array1 = feature_extractor.to_numpy_array(array) | ||
| self.assertTrue(array1.dtype, np.float32) | ||
| self.assertEqual(array1.shape, (3, 16, 32)) | ||
| self.assertTrue(np.array_equal(array1, array.transpose(2, 0, 1).astype(np.float32) / 255.0)) | ||
| self.assertTrue(np.array_equal(array1, array.transpose(2, 0, 1).astype(np.float32) * (1 / 255.0))) | ||
|
|
||
| # Same with no permute | ||
| array2 = feature_extractor.to_numpy_array(array, channel_first=False) | ||
| self.assertTrue(array2.dtype, np.float32) | ||
| self.assertEqual(array2.shape, (16, 32, 3)) | ||
| self.assertTrue(np.array_equal(array2, array.astype(np.float32) / 255.0)) | ||
| self.assertTrue(np.array_equal(array2, array.astype(np.float32) * (1 / 255.0))) | ||
|
|
||
| # Force rescale to False | ||
| array3 = feature_extractor.to_numpy_array(array, rescale=False) | ||
|
|
@@ -160,7 +160,7 @@ def test_conversion_array_to_image(self): | |
| self.assertTrue(np.array_equal(np.array(image2), array)) | ||
|
|
||
| # If the array has floating type, it's rescaled by default. | ||
| image3 = feature_extractor.to_pil_image(array.astype(np.float32) / 255.0) | ||
| image3 = feature_extractor.to_pil_image(array.astype(np.float32) * (1 / 255.0)) | ||
| self.assertTrue(isinstance(image3, PIL.Image.Image)) | ||
| self.assertTrue(np.array_equal(np.array(image3), array)) | ||
|
|
||
|
|
@@ -170,7 +170,7 @@ def test_conversion_array_to_image(self): | |
| self.assertTrue(np.array_equal(np.array(image4), array)) | ||
|
|
||
| # And with floats + channel first. | ||
| image5 = feature_extractor.to_pil_image(array.transpose(2, 0, 1).astype(np.float32) / 255.0) | ||
| image5 = feature_extractor.to_pil_image(array.transpose(2, 0, 1).astype(np.float32) * (1 / 255.0)) | ||
| self.assertTrue(isinstance(image5, PIL.Image.Image)) | ||
| self.assertTrue(np.array_equal(np.array(image5), array)) | ||
|
|
||
|
|
@@ -201,7 +201,7 @@ def test_conversion_tensor_to_image(self): | |
| self.assertTrue(np.array_equal(np.array(image4), array)) | ||
|
|
||
| # And with floats + channel first. | ||
| image5 = feature_extractor.to_pil_image(tensor.permute(2, 0, 1).float() / 255.0) | ||
| image5 = feature_extractor.to_pil_image(tensor.permute(2, 0, 1).float() * (1 / 255.0)) | ||
| self.assertTrue(isinstance(image5, PIL.Image.Image)) | ||
| self.assertTrue(np.array_equal(np.array(image5), array)) | ||
|
|
||
|
|
@@ -316,7 +316,7 @@ def test_normalize_image(self): | |
| self.assertEqual(normalized_image.shape, (3, 16, 32)) | ||
|
|
||
| # During the conversion rescale and channel first will be applied. | ||
| expected = array.transpose(2, 0, 1).astype(np.float32) / 255.0 | ||
| expected = array.transpose(2, 0, 1).astype(np.float32) * (1 / 255.0) | ||
| np_mean = np.array(mean).astype(np.float32)[:, None, None] | ||
| np_std = np.array(std).astype(np.float32)[:, None, None] | ||
| expected = (expected - np_mean) / np_std | ||
|
|
||
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 checks whether the image is a PIL image, NumPy array or PyTorch tensor, but this method only expects NumPy arrays. So I'd update to raise a
ValueErrorin case theimageisn't an instance of np.ndarray.