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

Revert to rescale and safely handle flag in owlvit config #18750

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/transformers/image_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def convert_rgb(self, image):

return image.convert("RGB")

def rescale_image(self, image: np.ndarray, scale: Union[float, int]) -> np.ndarray:
def rescale(self, image: np.ndarray, scale: Union[float, int]) -> np.ndarray:
"""
Rescale a numpy image by scale amount
"""
Expand Down Expand Up @@ -163,7 +163,7 @@ def to_numpy_array(self, image, rescale=None, channel_first=True):
rescale = isinstance(image.flat[0], np.integer) if rescale is None else rescale

if rescale:
image = self.rescale_image(image.astype(np.float32), 1 / 255.0)
image = self.rescale(image.astype(np.float32), 1 / 255.0)

if channel_first and image.ndim == 3:
image = image.transpose(2, 0, 1)
Expand Down Expand Up @@ -214,9 +214,9 @@ def normalize(self, image, mean, std, rescale=False):
# type it may need rescaling.
elif rescale:
if isinstance(image, np.ndarray):
image = self.rescale_image(image.astype(np.float32), 1 / 255.0)
image = self.rescale(image.astype(np.float32), 1 / 255.0)
elif is_torch_tensor(image):
image = self.rescale_image(image.float(), 1 / 255.0)
image = self.rescale(image.float(), 1 / 255.0)

if isinstance(image, np.ndarray):
if not isinstance(mean, np.ndarray):
Expand Down
7 changes: 7 additions & 0 deletions src/transformers/models/owlvit/feature_extraction_owlvit.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ def __init__(
image_std=None,
**kwargs
):
# Early versions of the OWL-ViT config on the hub had "rescale" as a flag. This clashes with the
# vision feature extractor method `rescale` as it would be set as an attribute during the super().__init__
# call. This is for backwards compatibility.
if "rescale" in kwargs:
rescale_val = kwargs.pop("rescale")
kwargs["do_rescale"] = rescale_val

Comment on lines +91 to +94
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks good! Will we eventually remove this part?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes. I think we can leave it for a short period of time so that if anyone pushes to the hub and updates the model, pushing the feature extractor with it, then the rescale parameter will be renamed in the preprocessor_config.json. After that, we could add a warning if rescale is in detected in the config - asking the person to update their config as it will no longer be supported as a flag in the future.

super().__init__(**kwargs)
self.size = size
self.resample = resample
Expand Down