Skip to content

Commit

Permalink
Revert to and safely handle flag in owlvit config (huggingface#18750)
Browse files Browse the repository at this point in the history
  • Loading branch information
amyeroberts authored and oneraghavan committed Sep 26, 2022
1 parent acbb2bf commit 35b590b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
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

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

0 comments on commit 35b590b

Please sign in to comment.