Skip to content

Commit

Permalink
Merge pull request #59 from amyeroberts/revert-11-type-cast-before-no…
Browse files Browse the repository at this point in the history
…rmalize-yolos

Revert "Type cast before normalize yolos"
  • Loading branch information
amyeroberts authored Sep 21, 2022
2 parents 77b9ac0 + aba4888 commit 6279d07
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 58 deletions.
25 changes: 7 additions & 18 deletions src/transformers/models/yolos/feature_extraction_yolos.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,14 +385,14 @@ def get_size(image_size, size, max_size=None):
return rescaled_image, target

# Copied from transformers.models.detr.feature_extraction_detr.DetrFeatureExtractor._normalize
def _normalize(self, image, mean, std, target=None, rescale=False):
def _normalize(self, image, mean, std, target=None):
"""
Normalize the image with a certain mean and std.
If given, also normalize the target bounding boxes based on the size of the image.
"""

image = self.normalize(image, mean=mean, std=std, rescale=rescale)
image = self.normalize(image, mean=mean, std=std)
if target is None:
return image, None

Expand Down Expand Up @@ -456,15 +456,9 @@ def __call__(
padding (`bool`, *optional*, defaults to `True`):
Whether or not to pad images up to the largest image in a batch.
return_tensors (`str` or [`~utils.TensorType`], *optional*, defaults to `None`):
If set, will return a tensor of a particular framework.
Acceptable values are:
- `'tf'`: Return TensorFlow `tf.constant` objects.
- `'pt'`: Return PyTorch `torch.Tensor` objects.
- `'np'`: Return NumPy `np.ndarray` objects.
- `'jax'`: Return JAX `jnp.ndarray` objects.
- None: Return list of `np.ndarray` objects.
return_tensors (`str` or [`~utils.TensorType`], *optional*):
If set, will return tensors instead of NumPy arrays. If set to `'pt'`, return PyTorch `torch.Tensor`
objects.
Returns:
[`BatchFeature`]: A [`BatchFeature`] with the following fields:
Expand Down Expand Up @@ -572,22 +566,17 @@ def __call__(
for idx, image in enumerate(images):
images[idx] = self._resize(image=image, target=None, size=self.size, max_size=self.max_size)[0]

# if do_normalize=False, the casting to a numpy array won't happen, so we need to do it here
make_channel_first = True if isinstance(images[0], Image.Image) else images[0].shape[-1] in (1, 3)
images = [self.to_numpy_array(image, rescale=False, channel_first=make_channel_first) for image in images]

if self.do_normalize:
if annotations is not None:
for idx, (image, target) in enumerate(zip(images, annotations)):
image, target = self._normalize(
image=image, mean=self.image_mean, std=self.image_std, target=target, rescale=True
image=image, mean=self.image_mean, std=self.image_std, target=target
)
images[idx] = image
annotations[idx] = target
else:
images = [
self._normalize(image=image, mean=self.image_mean, std=self.image_std, rescale=True)[0]
for image in images
self._normalize(image=image, mean=self.image_mean, std=self.image_std)[0] for image in images
]

if padding:
Expand Down
40 changes: 0 additions & 40 deletions tests/models/yolos/test_feature_extraction_yolos.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import numpy as np

from parameterized import parameterized
from transformers.testing_utils import require_torch, require_vision, slow
from transformers.utils import is_torch_available, is_vision_available

Expand Down Expand Up @@ -335,42 +334,3 @@ def test_call_pytorch_with_coco_panoptic_annotations(self):
# verify size
expected_size = torch.tensor([800, 1066])
assert torch.allclose(encoding["labels"][0]["size"], expected_size)

@parameterized.expand(
[
("do_resize_True_do_normalize_True_padding_True", True, True, True),
("do_resize_True_do_normalize_True_padding_False", True, True, False),
("do_resize_True_do_normalize_False_padding_True", True, False, True),
("do_resize_True_do_normalize_False_padding_False", True, False, False),
("do_resize_False_do_normalize_True_padding_True", False, True, True),
("do_resize_False_do_normalize_True_padding_False", False, True, False),
("do_resize_False_do_normalize_False_padding_True", False, False, True),
("do_resize_False_do_normalize_False_padding_False", False, False, False),
]
)
def test_call_flags(self, _, do_resize, do_normalize, padding):
# Initialize feature_extractor
feature_extractor = self.feature_extraction_class(**self.feat_extract_dict)
feature_extractor.do_resize = do_resize
feature_extractor.do_normalize = do_normalize
# create random PIL images
image_inputs = prepare_image_inputs(self.feature_extract_tester, equal_resolution=False)

expected_shapes = [(3, *x.size[::-1]) for x in image_inputs]
if do_resize:
expected_shapes = [
(
self.feature_extract_tester.num_channels,
*self.feature_extract_tester.get_expected_values([img], batched=False),
)
for img in image_inputs
]
if padding:
channel, height, width = [max(x) for x in zip(*expected_shapes)]
expected_shapes = [(channel, height, width)] * self.feature_extract_tester.batch_size

pixel_values = feature_extractor(image_inputs, padding=padding, return_tensors=None)["pixel_values"]
self.assertEqual(len(pixel_values), self.feature_extract_tester.batch_size)
for idx, image in enumerate(pixel_values):
self.assertEqual(image.shape, expected_shapes[idx])
self.assertIsInstance(image, np.ndarray)

0 comments on commit 6279d07

Please sign in to comment.