"docs/source/vscode:/vscode.git/clone" did not exist on "af8d1dc3093e2735a3aa82cd9195f04900364bee"
Unverified Commit 1486d2ae authored by amyeroberts's avatar amyeroberts Committed by GitHub
Browse files

Move common image processing methods to BaseImageProcessor (#25089)

Move out common methods
parent d30cf3d0
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
# limitations under the License. # limitations under the License.
"""Image processor class for LeViT.""" """Image processor class for LeViT."""
from typing import Dict, Iterable, List, Optional, Union from typing import Dict, Iterable, Optional, Union
import numpy as np import numpy as np
...@@ -22,8 +22,6 @@ from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size ...@@ -22,8 +22,6 @@ from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size
from ...image_transforms import ( from ...image_transforms import (
center_crop, center_crop,
get_resize_output_image_size, get_resize_output_image_size,
normalize,
rescale,
resize, resize,
to_channel_dimension_format, to_channel_dimension_format,
) )
...@@ -184,49 +182,6 @@ class LevitImageProcessor(BaseImageProcessor): ...@@ -184,49 +182,6 @@ class LevitImageProcessor(BaseImageProcessor):
raise ValueError(f"Size dict must have keys 'height' and 'width'. Got {size.keys()}") raise ValueError(f"Size dict must have keys 'height' and 'width'. Got {size.keys()}")
return center_crop(image, size=(size["height"], size["width"]), data_format=data_format, **kwargs) return center_crop(image, size=(size["height"], size["width"]), data_format=data_format, **kwargs)
def rescale(
self,
image: np.ndarray,
scale: Union[int, float],
data_format: Optional[Union[str, ChannelDimension]] = None,
**kwargs,
) -> np.ndarray:
"""
Rescale an image by a scale factor. image = image * scale.
Args:
image (`np.ndarray`):
Image to rescale.
scale (`int` or `float`):
Scale to apply to the image.
data_format (`str` or `ChannelDimension`, *optional*):
The channel dimension format of the image. If not provided, it will be the same as the input image.
"""
return rescale(image, scale=scale, data_format=data_format, **kwargs)
def normalize(
self,
image: np.ndarray,
mean: Union[float, List[float]],
std: Union[float, List[float]],
data_format: Optional[Union[str, ChannelDimension]] = None,
**kwargs,
) -> np.ndarray:
"""
Normalize an image. image = (image - image_mean) / image_std.
Args:
image (`np.ndarray`):
Image to normalize.
mean (`float` or `List[float]`):
Image mean.
std (`float` or `List[float]`):
Image standard deviation.
data_format (`str` or `ChannelDimension`, *optional*):
The channel dimension format of the image. If not provided, it will be the same as the input image.
"""
return normalize(image, mean=mean, std=std, data_format=data_format, **kwargs)
def preprocess( def preprocess(
self, self,
images: ImageInput, images: ImageInput,
......
...@@ -24,7 +24,6 @@ from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size ...@@ -24,7 +24,6 @@ from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size
from ...image_transforms import ( from ...image_transforms import (
PaddingMode, PaddingMode,
get_resize_output_image_size, get_resize_output_image_size,
normalize,
pad, pad,
rescale, rescale,
resize, resize,
...@@ -492,18 +491,6 @@ class Mask2FormerImageProcessor(BaseImageProcessor): ...@@ -492,18 +491,6 @@ class Mask2FormerImageProcessor(BaseImageProcessor):
""" """
return rescale(image, rescale_factor, data_format=data_format) return rescale(image, rescale_factor, data_format=data_format)
def normalize(
self,
image: np.ndarray,
mean: Union[float, Iterable[float]],
std: Union[float, Iterable[float]],
data_format: Optional[ChannelDimension] = None,
) -> np.ndarray:
"""
Normalize the image with the given mean and standard deviation.
"""
return normalize(image, mean=mean, std=std, data_format=data_format)
def convert_segmentation_map_to_binary_masks( def convert_segmentation_map_to_binary_masks(
self, self,
segmentation_map: "np.ndarray", segmentation_map: "np.ndarray",
......
...@@ -24,7 +24,6 @@ from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size ...@@ -24,7 +24,6 @@ from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size
from ...image_transforms import ( from ...image_transforms import (
PaddingMode, PaddingMode,
get_resize_output_image_size, get_resize_output_image_size,
normalize,
pad, pad,
rescale, rescale,
resize, resize,
...@@ -503,18 +502,6 @@ class MaskFormerImageProcessor(BaseImageProcessor): ...@@ -503,18 +502,6 @@ class MaskFormerImageProcessor(BaseImageProcessor):
""" """
return rescale(image, rescale_factor, data_format=data_format) return rescale(image, rescale_factor, data_format=data_format)
def normalize(
self,
image: np.ndarray,
mean: Union[float, Iterable[float]],
std: Union[float, Iterable[float]],
data_format: Optional[ChannelDimension] = None,
) -> np.ndarray:
"""
Normalize the image with the given mean and standard deviation.
"""
return normalize(image, mean=mean, std=std, data_format=data_format)
def convert_segmentation_map_to_binary_masks( def convert_segmentation_map_to_binary_masks(
self, self,
segmentation_map: "np.ndarray", segmentation_map: "np.ndarray",
......
...@@ -22,8 +22,6 @@ from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size ...@@ -22,8 +22,6 @@ from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size
from ...image_transforms import ( from ...image_transforms import (
center_crop, center_crop,
get_resize_output_image_size, get_resize_output_image_size,
normalize,
rescale,
resize, resize,
to_channel_dimension_format, to_channel_dimension_format,
) )
...@@ -164,57 +162,6 @@ class MobileNetV1ImageProcessor(BaseImageProcessor): ...@@ -164,57 +162,6 @@ class MobileNetV1ImageProcessor(BaseImageProcessor):
size = get_size_dict(size) size = get_size_dict(size)
return center_crop(image, size=(size["height"], size["width"]), data_format=data_format, **kwargs) return center_crop(image, size=(size["height"], size["width"]), data_format=data_format, **kwargs)
def rescale(
self, image: np.ndarray, scale: float, data_format: Optional[Union[str, ChannelDimension]] = None, **kwargs
) -> np.ndarray:
"""
Rescale an image by a scale factor. image = image * scale.
Args:
image (`np.ndarray`):
Image to rescale.
scale (`float`):
The scaling factor to rescale pixel values by.
data_format (`str` or `ChannelDimension`, *optional*):
The channel dimension format for the output image. If unset, the channel dimension format of the input
image is used. Can be one of:
- `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
- `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
Returns:
`np.ndarray`: The rescaled image.
"""
return rescale(image, scale=scale, data_format=data_format, **kwargs)
def normalize(
self,
image: np.ndarray,
mean: Union[float, List[float]],
std: Union[float, List[float]],
data_format: Optional[Union[str, ChannelDimension]] = None,
**kwargs,
) -> np.ndarray:
"""
Normalize an image. image = (image - image_mean) / image_std.
Args:
image (`np.ndarray`):
Image to normalize.
mean (`float` or `List[float]`):
Image mean to use for normalization.
std (`float` or `List[float]`):
Image standard deviation to use for normalization.
data_format (`str` or `ChannelDimension`, *optional*):
The channel dimension format for the output image. If unset, the channel dimension format of the input
image is used. Can be one of:
- `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
- `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
Returns:
`np.ndarray`: The normalized image.
"""
return normalize(image, mean=mean, std=std, data_format=data_format, **kwargs)
def preprocess( def preprocess(
self, self,
images: ImageInput, images: ImageInput,
......
...@@ -22,8 +22,6 @@ from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size ...@@ -22,8 +22,6 @@ from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size
from ...image_transforms import ( from ...image_transforms import (
center_crop, center_crop,
get_resize_output_image_size, get_resize_output_image_size,
normalize,
rescale,
resize, resize,
to_channel_dimension_format, to_channel_dimension_format,
) )
...@@ -170,57 +168,6 @@ class MobileNetV2ImageProcessor(BaseImageProcessor): ...@@ -170,57 +168,6 @@ class MobileNetV2ImageProcessor(BaseImageProcessor):
raise ValueError(f"The `size` parameter must contain the keys `height` and `width`. Got {size.keys()}") raise ValueError(f"The `size` parameter must contain the keys `height` and `width`. Got {size.keys()}")
return center_crop(image, size=(size["height"], size["width"]), data_format=data_format, **kwargs) return center_crop(image, size=(size["height"], size["width"]), data_format=data_format, **kwargs)
def rescale(
self, image: np.ndarray, scale: float, data_format: Optional[Union[str, ChannelDimension]] = None, **kwargs
) -> np.ndarray:
"""
Rescale an image by a scale factor. image = image * scale.
Args:
image (`np.ndarray`):
Image to rescale.
scale (`float`):
The scaling factor to rescale pixel values by.
data_format (`str` or `ChannelDimension`, *optional*):
The channel dimension format for the output image. If unset, the channel dimension format of the input
image is used. Can be one of:
- `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
- `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
Returns:
`np.ndarray`: The rescaled image.
"""
return rescale(image, scale=scale, data_format=data_format, **kwargs)
def normalize(
self,
image: np.ndarray,
mean: Union[float, List[float]],
std: Union[float, List[float]],
data_format: Optional[Union[str, ChannelDimension]] = None,
**kwargs,
) -> np.ndarray:
"""
Normalize an image. image = (image - image_mean) / image_std.
Args:
image (`np.ndarray`):
Image to normalize.
mean (`float` or `List[float]`):
Image mean to use for normalization.
std (`float` or `List[float]`):
Image standard deviation to use for normalization.
data_format (`str` or `ChannelDimension`, *optional*):
The channel dimension format for the output image. If unset, the channel dimension format of the input
image is used. Can be one of:
- `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
- `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
Returns:
`np.ndarray`: The normalized image.
"""
return normalize(image, mean=mean, std=std, data_format=data_format, **kwargs)
def preprocess( def preprocess(
self, self,
images: ImageInput, images: ImageInput,
......
...@@ -23,7 +23,6 @@ from ...image_transforms import ( ...@@ -23,7 +23,6 @@ from ...image_transforms import (
center_crop, center_crop,
flip_channel_order, flip_channel_order,
get_resize_output_image_size, get_resize_output_image_size,
rescale,
resize, resize,
to_channel_dimension_format, to_channel_dimension_format,
) )
...@@ -161,26 +160,6 @@ class MobileViTImageProcessor(BaseImageProcessor): ...@@ -161,26 +160,6 @@ class MobileViTImageProcessor(BaseImageProcessor):
raise ValueError(f"The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}") raise ValueError(f"The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}")
return center_crop(image, size=(size["height"], size["width"]), data_format=data_format, **kwargs) return center_crop(image, size=(size["height"], size["width"]), data_format=data_format, **kwargs)
def rescale(
self,
image: np.ndarray,
scale: Union[int, float],
data_format: Optional[Union[str, ChannelDimension]] = None,
**kwargs,
):
"""
Rescale an image by a scale factor. image = image * scale.
Args:
image (`np.ndarray`):
Image to rescale.
scale (`int` or `float`):
Scale to apply to the image.
data_format (`str` or `ChannelDimension`, *optional*):
The channel dimension format of the image. If not provided, it will be the same as the input image.
"""
return rescale(image, scale=scale, data_format=data_format, **kwargs)
def flip_channel_order( def flip_channel_order(
self, image: np.ndarray, data_format: Optional[Union[str, ChannelDimension]] = None self, image: np.ndarray, data_format: Optional[Union[str, ChannelDimension]] = None
) -> np.ndarray: ) -> np.ndarray:
......
...@@ -25,7 +25,6 @@ from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size ...@@ -25,7 +25,6 @@ from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size
from ...image_transforms import ( from ...image_transforms import (
PaddingMode, PaddingMode,
get_resize_output_image_size, get_resize_output_image_size,
normalize,
pad, pad,
rescale, rescale,
resize, resize,
...@@ -487,19 +486,6 @@ class OneFormerImageProcessor(BaseImageProcessor): ...@@ -487,19 +486,6 @@ class OneFormerImageProcessor(BaseImageProcessor):
""" """
return rescale(image, rescale_factor, data_format=data_format) return rescale(image, rescale_factor, data_format=data_format)
# Copied from transformers.models.detr.image_processing_detr.DetrImageProcessor.normalize
def normalize(
self,
image: np.ndarray,
mean: Union[float, Iterable[float]],
std: Union[float, Iterable[float]],
data_format: Optional[ChannelDimension] = None,
) -> np.ndarray:
"""
Normalize the image with the given mean and standard deviation.
"""
return normalize(image, mean=mean, std=std, data_format=data_format)
# Copied from transformers.models.maskformer.image_processing_maskformer.MaskFormerImageProcessor.convert_segmentation_map_to_binary_masks # Copied from transformers.models.maskformer.image_processing_maskformer.MaskFormerImageProcessor.convert_segmentation_map_to_binary_masks
def convert_segmentation_map_to_binary_masks( def convert_segmentation_map_to_binary_masks(
self, self,
......
...@@ -23,7 +23,6 @@ from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size ...@@ -23,7 +23,6 @@ from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size
from ...image_transforms import ( from ...image_transforms import (
center_crop, center_crop,
center_to_corners_format, center_to_corners_format,
normalize,
rescale, rescale,
resize, resize,
to_channel_dimension_format, to_channel_dimension_format,
...@@ -210,19 +209,6 @@ class OwlViTImageProcessor(BaseImageProcessor): ...@@ -210,19 +209,6 @@ class OwlViTImageProcessor(BaseImageProcessor):
""" """
return rescale(image, rescale_factor, data_format=data_format, **kwargs) return rescale(image, rescale_factor, data_format=data_format, **kwargs)
def normalize(
self,
image: np.ndarray,
mean: List[float],
std: List[float],
data_format: Optional[Union[str, ChannelDimension]] = None,
**kwargs,
) -> np.ndarray:
"""
Normalize an image with a certain mean and standard deviation.
"""
return normalize(image, mean, std, data_format=data_format, **kwargs)
def preprocess( def preprocess(
self, self,
images: ImageInput, images: ImageInput,
......
...@@ -19,7 +19,7 @@ from typing import Dict, List, Optional, Union ...@@ -19,7 +19,7 @@ from typing import Dict, List, Optional, Union
import numpy as np import numpy as np
from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import center_crop, normalize, rescale, resize, to_channel_dimension_format from ...image_transforms import center_crop, resize, to_channel_dimension_format
from ...image_utils import ( from ...image_utils import (
IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_MEAN,
IMAGENET_DEFAULT_STD, IMAGENET_DEFAULT_STD,
...@@ -174,49 +174,6 @@ class PerceiverImageProcessor(BaseImageProcessor): ...@@ -174,49 +174,6 @@ class PerceiverImageProcessor(BaseImageProcessor):
image, size=(size["height"], size["width"]), resample=resample, data_format=data_format, **kwargs image, size=(size["height"], size["width"]), resample=resample, data_format=data_format, **kwargs
) )
def rescale(
self,
image: np.ndarray,
scale: Union[int, float],
data_format: Optional[Union[str, ChannelDimension]] = None,
**kwargs,
):
"""
Rescale an image by a scale factor. image = image * scale.
Args:
image (`np.ndarray`):
Image to rescale.
scale (`int` or `float`):
Scale to apply to the image.
data_format (`str` or `ChannelDimension`, *optional*):
The channel dimension format of the image. If not provided, it will be the same as the input image.
"""
return rescale(image, scale=scale, data_format=data_format, **kwargs)
def normalize(
self,
image: np.ndarray,
mean: Union[float, List[float]],
std: Union[float, List[float]],
data_format: Optional[Union[str, ChannelDimension]] = None,
**kwargs,
) -> np.ndarray:
"""
Normalize an image. image = (image - image_mean) / image_std.
Args:
image (`np.ndarray`):
Image to normalize.
mean (`float` or `List[float]`):
Image mean.
std (`float` or `List[float]`):
Image standard deviation.
data_format (`str` or `ChannelDimension`, *optional*):
The channel dimension format of the image. If not provided, it will be the same as the input image.
"""
return normalize(image, mean=mean, std=std, data_format=data_format, **kwargs)
def preprocess( def preprocess(
self, self,
images: ImageInput, images: ImageInput,
......
...@@ -22,8 +22,6 @@ from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size ...@@ -22,8 +22,6 @@ from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size
from ...image_transforms import ( from ...image_transforms import (
center_crop, center_crop,
get_resize_output_image_size, get_resize_output_image_size,
normalize,
rescale,
resize, resize,
to_channel_dimension_format, to_channel_dimension_format,
) )
...@@ -219,49 +217,6 @@ class PoolFormerImageProcessor(BaseImageProcessor): ...@@ -219,49 +217,6 @@ class PoolFormerImageProcessor(BaseImageProcessor):
raise ValueError(f"size must contain 'height' and 'width' as keys. Got {size.keys()}") raise ValueError(f"size must contain 'height' and 'width' as keys. Got {size.keys()}")
return center_crop(image, size=(size["height"], size["width"]), data_format=data_format, **kwargs) return center_crop(image, size=(size["height"], size["width"]), data_format=data_format, **kwargs)
def rescale(
self,
image: np.ndarray,
scale: Union[int, float],
data_format: Optional[Union[str, ChannelDimension]] = None,
**kwargs,
):
"""
Rescale an image by a scale factor. image = image * scale.
Args:
image (`np.ndarray`):
Image to rescale.
scale (`int` or `float`):
Scale to apply to the image.
data_format (`str` or `ChannelDimension`, *optional*):
The channel dimension format of the image. If not provided, it will be the same as the input image.
"""
return rescale(image, scale=scale, data_format=data_format, **kwargs)
def normalize(
self,
image: np.ndarray,
mean: Union[float, List[float]],
std: Union[float, List[float]],
data_format: Optional[Union[str, ChannelDimension]] = None,
**kwargs,
) -> np.ndarray:
"""
Normalize an image. image = (image - image_mean) / image_std.
Args:
image (`np.ndarray`):
Image to normalize.
image_mean (`float` or `List[float]`):
Image mean.
image_std (`float` or `List[float]`):
Image standard deviation.
data_format (`str` or `ChannelDimension`, *optional*):
The channel dimension format of the image. If not provided, it will be the same as the input image.
"""
return normalize(image, mean=mean, std=std, data_format=data_format, **kwargs)
def preprocess( def preprocess(
self, self,
images: ImageInput, images: ImageInput,
......
...@@ -19,7 +19,7 @@ from typing import Dict, List, Optional, Union ...@@ -19,7 +19,7 @@ from typing import Dict, List, Optional, Union
import numpy as np import numpy as np
from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format from ...image_transforms import resize, to_channel_dimension_format
from ...image_utils import ( from ...image_utils import (
IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_MEAN,
IMAGENET_DEFAULT_STD, IMAGENET_DEFAULT_STD,
...@@ -127,57 +127,6 @@ class PvtImageProcessor(BaseImageProcessor): ...@@ -127,57 +127,6 @@ class PvtImageProcessor(BaseImageProcessor):
image, size=(size["height"], size["width"]), resample=resample, data_format=data_format, **kwargs image, size=(size["height"], size["width"]), resample=resample, data_format=data_format, **kwargs
) )
def rescale(
self, image: np.ndarray, scale: float, data_format: Optional[Union[str, ChannelDimension]] = None, **kwargs
) -> np.ndarray:
"""
Rescale an image by a scale factor. image = image * scale.
Args:
image (`np.ndarray`):
Image to rescale.
scale (`float`):
The scaling factor to rescale pixel values by.
data_format (`str` or `ChannelDimension`, *optional*):
The channel dimension format for the output image. If unset, the channel dimension format of the input
image is used. Can be one of:
- `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
- `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
Returns:
`np.ndarray`: The rescaled image.
"""
return rescale(image, scale=scale, data_format=data_format, **kwargs)
def normalize(
self,
image: np.ndarray,
mean: Union[float, List[float]],
std: Union[float, List[float]],
data_format: Optional[Union[str, ChannelDimension]] = None,
**kwargs,
) -> np.ndarray:
"""
Normalize an image. image = (image - image_mean) / image_std.
Args:
image (`np.ndarray`):
Image to normalize.
mean (`float` or `List[float]`):
Image mean to use for normalization.
std (`float` or `List[float]`):
Image standard deviation to use for normalization.
data_format (`str` or `ChannelDimension`, *optional*):
The channel dimension format for the output image. If unset, the channel dimension format of the input
image is used. Can be one of:
- `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
- `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
Returns:
`np.ndarray`: The normalized image.
"""
return normalize(image, mean=mean, std=std, data_format=data_format, **kwargs)
def preprocess( def preprocess(
self, self,
images: ImageInput, images: ImageInput,
......
...@@ -21,7 +21,7 @@ from typing import Any, Dict, List, Optional, Tuple, Union ...@@ -21,7 +21,7 @@ from typing import Any, Dict, List, Optional, Tuple, Union
import numpy as np import numpy as np
from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import convert_to_rgb, normalize, pad, rescale, resize, to_channel_dimension_format from ...image_transforms import convert_to_rgb, pad, resize, to_channel_dimension_format
from ...image_utils import ( from ...image_utils import (
IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_MEAN,
IMAGENET_DEFAULT_STD, IMAGENET_DEFAULT_STD,
...@@ -212,49 +212,6 @@ class SamImageProcessor(BaseImageProcessor): ...@@ -212,49 +212,6 @@ class SamImageProcessor(BaseImageProcessor):
output_height, output_width = self._get_preprocess_shape(input_size, size["longest_edge"]) output_height, output_width = self._get_preprocess_shape(input_size, size["longest_edge"])
return resize(image, size=(output_height, output_width), resample=resample, data_format=data_format, **kwargs) return resize(image, size=(output_height, output_width), resample=resample, data_format=data_format, **kwargs)
def rescale(
self,
image: np.ndarray,
scale: Union[int, float],
data_format: Optional[Union[str, ChannelDimension]] = None,
**kwargs,
):
"""
Rescale an image by a scale factor. image = image * scale.
Args:
image (`np.ndarray`):
Image to rescale.
scale (`int` or `float`):
Scale to apply to the image.
data_format (`str` or `ChannelDimension`, *optional*):
The channel dimension format of the image. If not provided, it will be the same as the input image.
"""
return rescale(image, scale=scale, data_format=data_format, **kwargs)
def normalize(
self,
image: np.ndarray,
mean: Union[float, List[float]],
std: Union[float, List[float]],
data_format: Optional[Union[str, ChannelDimension]] = None,
**kwargs,
) -> np.ndarray:
"""
Normalize an image. image = (image - image_mean) / image_std.
Args:
image (`np.ndarray`):
Image to normalize.
mean (`float` or `List[float]`):
Image mean.
std (`float` or `List[float]`):
Image standard deviation.
data_format (`str` or `ChannelDimension`, *optional*):
The channel dimension format of the image. If not provided, it will be the same as the input image.
"""
return normalize(image, mean=mean, std=std, data_format=data_format, **kwargs)
def preprocess( def preprocess(
self, self,
images: ImageInput, images: ImageInput,
......
...@@ -20,7 +20,7 @@ from typing import Any, Dict, List, Optional, Tuple, Union ...@@ -20,7 +20,7 @@ from typing import Any, Dict, List, Optional, Tuple, Union
import numpy as np import numpy as np
from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format from ...image_transforms import resize, to_channel_dimension_format
from ...image_utils import ( from ...image_utils import (
IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_MEAN,
IMAGENET_DEFAULT_STD, IMAGENET_DEFAULT_STD,
...@@ -156,49 +156,6 @@ class SegformerImageProcessor(BaseImageProcessor): ...@@ -156,49 +156,6 @@ class SegformerImageProcessor(BaseImageProcessor):
image, size=(size["height"], size["width"]), resample=resample, data_format=data_format, **kwargs image, size=(size["height"], size["width"]), resample=resample, data_format=data_format, **kwargs
) )
def rescale(
self,
image: np.ndarray,
scale: Union[int, float],
data_format: Optional[Union[str, ChannelDimension]] = None,
**kwargs,
):
"""
Rescale an image by a scale factor. image = image * scale.
Args:
image (`np.ndarray`):
Image to rescale.
scale (`int` or `float`):
Scale to apply to the image.
data_format (`str` or `ChannelDimension`, *optional*):
The channel dimension format of the image. If not provided, it will be the same as the input image.
"""
return rescale(image, scale=scale, data_format=data_format, **kwargs)
def normalize(
self,
image: np.ndarray,
mean: Union[float, List[float]],
std: Union[float, List[float]],
data_format: Optional[Union[str, ChannelDimension]] = None,
**kwargs,
) -> np.ndarray:
"""
Normalize an image. image = (image - image_mean) / image_std.
Args:
image (`np.ndarray`):
Image to normalize.
image_mean (`float` or `List[float]`):
Image mean.
image_std (`float` or `List[float]`):
Image standard deviation.
data_format (`str` or `ChannelDimension`, *optional*):
The channel dimension format of the image. If not provided, it will be the same as the input image.
"""
return normalize(image, mean=mean, std=std, data_format=data_format, **kwargs)
def reduce_label(self, label: ImageInput) -> np.ndarray: def reduce_label(self, label: ImageInput) -> np.ndarray:
label = to_numpy_array(label) label = to_numpy_array(label)
# Avoid using underflow conversion # Avoid using underflow conversion
......
...@@ -19,7 +19,7 @@ from typing import Optional, Union ...@@ -19,7 +19,7 @@ from typing import Optional, Union
import numpy as np import numpy as np
from ...image_processing_utils import BaseImageProcessor, BatchFeature from ...image_processing_utils import BaseImageProcessor, BatchFeature
from ...image_transforms import get_image_size, pad, rescale, to_channel_dimension_format from ...image_transforms import get_image_size, pad, to_channel_dimension_format
from ...image_utils import ChannelDimension, ImageInput, make_list_of_images, to_numpy_array, valid_images from ...image_utils import ChannelDimension, ImageInput, make_list_of_images, to_numpy_array, valid_images
from ...utils import TensorType, logging from ...utils import TensorType, logging
...@@ -57,28 +57,6 @@ class Swin2SRImageProcessor(BaseImageProcessor): ...@@ -57,28 +57,6 @@ class Swin2SRImageProcessor(BaseImageProcessor):
self.do_pad = do_pad self.do_pad = do_pad
self.pad_size = pad_size self.pad_size = pad_size
def rescale(
self, image: np.ndarray, scale: float, data_format: Optional[Union[str, ChannelDimension]] = None, **kwargs
) -> np.ndarray:
"""
Rescale an image by a scale factor. image = image * scale.
Args:
image (`np.ndarray`):
Image to rescale.
scale (`float`):
The scaling factor to rescale pixel values by.
data_format (`str` or `ChannelDimension`, *optional*):
The channel dimension format for the output image. If unset, the channel dimension format of the input
image is used. Can be one of:
- `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
- `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
Returns:
`np.ndarray`: The rescaled image.
"""
return rescale(image, scale=scale, data_format=data_format, **kwargs)
def pad(self, image: np.ndarray, size: int, data_format: Optional[Union[str, ChannelDimension]] = None): def pad(self, image: np.ndarray, size: int, data_format: Optional[Union[str, ChannelDimension]] = None):
""" """
Pad an image to make the height and width divisible by `size`. Pad an image to make the height and width divisible by `size`.
......
...@@ -21,8 +21,6 @@ from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size ...@@ -21,8 +21,6 @@ from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size
from ...image_transforms import ( from ...image_transforms import (
center_crop, center_crop,
get_resize_output_image_size, get_resize_output_image_size,
normalize,
rescale,
resize, resize,
to_channel_dimension_format, to_channel_dimension_format,
) )
...@@ -208,56 +206,6 @@ class TvltImageProcessor(BaseImageProcessor): ...@@ -208,56 +206,6 @@ class TvltImageProcessor(BaseImageProcessor):
raise ValueError(f"Size must have 'height' and 'width' as keys. Got {size.keys()}") raise ValueError(f"Size must have 'height' and 'width' as keys. Got {size.keys()}")
return center_crop(image, size=(size["height"], size["width"]), data_format=data_format, **kwargs) return center_crop(image, size=(size["height"], size["width"]), data_format=data_format, **kwargs)
def rescale(
self,
image: np.ndarray,
scale: Union[int, float],
data_format: Optional[Union[str, ChannelDimension]] = None,
**kwargs,
):
"""
Rescale an image by a scale factor. image = image * scale.
Args:
image (`np.ndarray`):
Image to rescale.
scale (`int` or `float`):
Scale to apply to the image.
data_format (`str` or `ChannelDimension`, *optional*):
The channel dimension format of the image. If not provided, it will be the same as the input image.
"""
return rescale(image, scale=scale, data_format=data_format, **kwargs)
# Copied from transformers.models.vit.image_processing_vit.ViTImageProcessor.normalize
def normalize(
self,
image: np.ndarray,
mean: Union[float, List[float]],
std: Union[float, List[float]],
data_format: Optional[Union[str, ChannelDimension]] = None,
**kwargs,
) -> np.ndarray:
"""
Normalize an image. image = (image - image_mean) / image_std.
Args:
image (`np.ndarray`):
Image to normalize.
mean (`float` or `List[float]`):
Image mean to use for normalization.
std (`float` or `List[float]`):
Image standard deviation to use for normalization.
data_format (`str` or `ChannelDimension`, *optional*):
The channel dimension format for the output image. If unset, the channel dimension format of the input
image is used. Can be one of:
- `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
- `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
Returns:
`np.ndarray`: The normalized image.
"""
return normalize(image, mean=mean, std=std, data_format=data_format, **kwargs)
def _preprocess_image( def _preprocess_image(
self, self,
image: ImageInput, image: ImageInput,
......
...@@ -22,8 +22,6 @@ from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size ...@@ -22,8 +22,6 @@ from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size
from ...image_transforms import ( from ...image_transforms import (
center_crop, center_crop,
get_resize_output_image_size, get_resize_output_image_size,
normalize,
rescale,
resize, resize,
to_channel_dimension_format, to_channel_dimension_format,
) )
...@@ -187,49 +185,6 @@ class VideoMAEImageProcessor(BaseImageProcessor): ...@@ -187,49 +185,6 @@ class VideoMAEImageProcessor(BaseImageProcessor):
raise ValueError(f"Size must have 'height' and 'width' as keys. Got {size.keys()}") raise ValueError(f"Size must have 'height' and 'width' as keys. Got {size.keys()}")
return center_crop(image, size=(size["height"], size["width"]), data_format=data_format, **kwargs) return center_crop(image, size=(size["height"], size["width"]), data_format=data_format, **kwargs)
def rescale(
self,
image: np.ndarray,
scale: Union[int, float],
data_format: Optional[Union[str, ChannelDimension]] = None,
**kwargs,
):
"""
Rescale an image by a scale factor. image = image * scale.
Args:
image (`np.ndarray`):
Image to rescale.
scale (`int` or `float`):
Scale to apply to the image.
data_format (`str` or `ChannelDimension`, *optional*):
The channel dimension format of the image. If not provided, it will be the same as the input image.
"""
return rescale(image, scale=scale, data_format=data_format, **kwargs)
def normalize(
self,
image: np.ndarray,
mean: Union[float, List[float]],
std: Union[float, List[float]],
data_format: Optional[Union[str, ChannelDimension]] = None,
**kwargs,
) -> np.ndarray:
"""
Normalize an image. image = (image - image_mean) / image_std.
Args:
image (`np.ndarray`):
Image to normalize.
image_mean (`float` or `List[float]`):
Image mean.
image_std (`float` or `List[float]`):
Image standard deviation.
data_format (`str` or `ChannelDimension`, *optional*):
The channel dimension format of the image. If not provided, it will be the same as the input image.
"""
return normalize(image, mean=mean, std=std, data_format=data_format, **kwargs)
def _preprocess_image( def _preprocess_image(
self, self,
image: ImageInput, image: ImageInput,
......
...@@ -19,7 +19,7 @@ from typing import Any, Dict, Iterable, List, Optional, Tuple, Union ...@@ -19,7 +19,7 @@ from typing import Any, Dict, Iterable, List, Optional, Tuple, Union
import numpy as np import numpy as np
from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import PaddingMode, normalize, pad, rescale, resize, to_channel_dimension_format from ...image_transforms import PaddingMode, pad, resize, to_channel_dimension_format
from ...image_utils import ( from ...image_utils import (
IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_MEAN,
IMAGENET_STANDARD_STD, IMAGENET_STANDARD_STD,
...@@ -229,49 +229,6 @@ class ViltImageProcessor(BaseImageProcessor): ...@@ -229,49 +229,6 @@ class ViltImageProcessor(BaseImageProcessor):
output_size = get_resize_output_image_size(image, shorter=shorter, longer=longer, size_divisor=size_divisor) output_size = get_resize_output_image_size(image, shorter=shorter, longer=longer, size_divisor=size_divisor)
return resize(image, size=output_size, resample=resample, data_format=data_format, **kwargs) return resize(image, size=output_size, resample=resample, data_format=data_format, **kwargs)
def rescale(
self,
image: np.ndarray,
scale: Union[int, float],
data_format: Optional[Union[str, ChannelDimension]] = None,
**kwargs,
):
"""
Rescale an image by a scale factor. image = image * scale.
Args:
image (`np.ndarray`):
Image to rescale.
scale (`int` or `float`):
Scale to apply to the image.
data_format (`str` or `ChannelDimension`, *optional*):
The channel dimension format of the image. If not provided, it will be the same as the input image.
"""
return rescale(image, scale=scale, data_format=data_format, **kwargs)
def normalize(
self,
image: np.ndarray,
mean: Union[float, List[float]],
std: Union[float, List[float]],
data_format: Optional[Union[str, ChannelDimension]] = None,
**kwargs,
) -> np.ndarray:
"""
Normalize an image. image = (image - image_mean) / image_std.
Args:
image (`np.ndarray`):
Image to normalize.
mean (`float` or `List[float]`):
Image mean.
std (`float` or `List[float]`):
Image standard deviation.
data_format (`str` or `ChannelDimension`, *optional*):
The channel dimension format of the image. If not provided, it will be the same as the input image.
"""
return normalize(image, mean=mean, std=std, data_format=data_format, **kwargs)
def _pad_image( def _pad_image(
self, self,
image: np.ndarray, image: np.ndarray,
......
...@@ -19,7 +19,7 @@ from typing import Dict, List, Optional, Union ...@@ -19,7 +19,7 @@ from typing import Dict, List, Optional, Union
import numpy as np import numpy as np
from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format from ...image_transforms import resize, to_channel_dimension_format
from ...image_utils import ( from ...image_utils import (
IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_MEAN,
IMAGENET_STANDARD_STD, IMAGENET_STANDARD_STD,
...@@ -127,57 +127,6 @@ class ViTImageProcessor(BaseImageProcessor): ...@@ -127,57 +127,6 @@ class ViTImageProcessor(BaseImageProcessor):
image, size=(size["height"], size["width"]), resample=resample, data_format=data_format, **kwargs image, size=(size["height"], size["width"]), resample=resample, data_format=data_format, **kwargs
) )
def rescale(
self, image: np.ndarray, scale: float, data_format: Optional[Union[str, ChannelDimension]] = None, **kwargs
) -> np.ndarray:
"""
Rescale an image by a scale factor. image = image * scale.
Args:
image (`np.ndarray`):
Image to rescale.
scale (`float`):
The scaling factor to rescale pixel values by.
data_format (`str` or `ChannelDimension`, *optional*):
The channel dimension format for the output image. If unset, the channel dimension format of the input
image is used. Can be one of:
- `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
- `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
Returns:
`np.ndarray`: The rescaled image.
"""
return rescale(image, scale=scale, data_format=data_format, **kwargs)
def normalize(
self,
image: np.ndarray,
mean: Union[float, List[float]],
std: Union[float, List[float]],
data_format: Optional[Union[str, ChannelDimension]] = None,
**kwargs,
) -> np.ndarray:
"""
Normalize an image. image = (image - image_mean) / image_std.
Args:
image (`np.ndarray`):
Image to normalize.
mean (`float` or `List[float]`):
Image mean to use for normalization.
std (`float` or `List[float]`):
Image standard deviation to use for normalization.
data_format (`str` or `ChannelDimension`, *optional*):
The channel dimension format for the output image. If unset, the channel dimension format of the input
image is used. Can be one of:
- `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
- `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
Returns:
`np.ndarray`: The normalized image.
"""
return normalize(image, mean=mean, std=std, data_format=data_format, **kwargs)
def preprocess( def preprocess(
self, self,
images: ImageInput, images: ImageInput,
......
...@@ -23,8 +23,6 @@ from ...image_transforms import ( ...@@ -23,8 +23,6 @@ from ...image_transforms import (
center_crop, center_crop,
convert_to_rgb, convert_to_rgb,
get_resize_output_image_size, get_resize_output_image_size,
normalize,
rescale,
resize, resize,
to_channel_dimension_format, to_channel_dimension_format,
) )
...@@ -173,49 +171,6 @@ class ViTHybridImageProcessor(BaseImageProcessor): ...@@ -173,49 +171,6 @@ class ViTHybridImageProcessor(BaseImageProcessor):
raise ValueError(f"The `size` parameter must contain the keys (height, width). Got {size.keys()}") raise ValueError(f"The `size` parameter must contain the keys (height, width). Got {size.keys()}")
return center_crop(image, size=(size["height"], size["width"]), data_format=data_format, **kwargs) return center_crop(image, size=(size["height"], size["width"]), data_format=data_format, **kwargs)
def rescale(
self,
image: np.ndarray,
scale: Union[int, float],
data_format: Optional[Union[str, ChannelDimension]] = None,
**kwargs,
):
"""
Rescale an image by a scale factor. image = image * scale.
Args:
image (`np.ndarray`):
Image to rescale.
scale (`int` or `float`):
Scale to apply to the image.
data_format (`str` or `ChannelDimension`, *optional*):
The channel dimension format of the image. If not provided, it will be the same as the input image.
"""
return rescale(image, scale=scale, data_format=data_format, **kwargs)
def normalize(
self,
image: np.ndarray,
mean: Union[float, List[float]],
std: Union[float, List[float]],
data_format: Optional[Union[str, ChannelDimension]] = None,
**kwargs,
) -> np.ndarray:
"""
Normalize an image. image = (image - image_mean) / image_std.
Args:
image (`np.ndarray`):
Image to normalize.
image_mean (`float` or `List[float]`):
Image mean.
image_std (`float` or `List[float]`):
Image standard deviation.
data_format (`str` or `ChannelDimension`, *optional*):
The channel dimension format of the image. If not provided, it will be the same as the input image.
"""
return normalize(image, mean=mean, std=std, data_format=data_format, **kwargs)
def preprocess( def preprocess(
self, self,
images: ImageInput, images: ImageInput,
......
...@@ -24,7 +24,6 @@ from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size ...@@ -24,7 +24,6 @@ from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size
from ...image_transforms import ( from ...image_transforms import (
center_crop, center_crop,
get_resize_output_image_size, get_resize_output_image_size,
normalize,
rescale, rescale,
resize, resize,
to_channel_dimension_format, to_channel_dimension_format,
...@@ -222,29 +221,6 @@ class VivitImageProcessor(BaseImageProcessor): ...@@ -222,29 +221,6 @@ class VivitImageProcessor(BaseImageProcessor):
image = image - (scale / 2) image = image - (scale / 2)
return rescale(image, scale=scale, data_format=data_format, **kwargs) return rescale(image, scale=scale, data_format=data_format, **kwargs)
def normalize(
self,
image: np.ndarray,
mean: Union[float, List[float]],
std: Union[float, List[float]],
data_format: Optional[Union[str, ChannelDimension]] = None,
**kwargs,
) -> np.ndarray:
"""
Normalize an image. image = (image - image_mean) / image_std.
Args:
image (`np.ndarray`):
Image to normalize.
image_mean (`float` or `List[float]`):
Image mean.
image_std (`float` or `List[float]`):
Image standard deviation.
data_format (`str` or `ChannelDimension`, *optional*):
The channel dimension format of the image. If not provided, it will be the same as the input image.
"""
return normalize(image, mean=mean, std=std, data_format=data_format, **kwargs)
def _preprocess_image( def _preprocess_image(
self, self,
image: ImageInput, image: ImageInput,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment