Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
transformers
Commits
1486d2ae
Unverified
Commit
1486d2ae
authored
Jul 26, 2023
by
amyeroberts
Committed by
GitHub
Jul 26, 2023
Browse files
Move common image processing methods to BaseImageProcessor (#25089)
Move out common methods
parent
d30cf3d0
Changes
41
Show whitespace changes
Inline
Side-by-side
Showing
20 changed files
with
8 additions
and
741 deletions
+8
-741
src/transformers/models/levit/image_processing_levit.py
src/transformers/models/levit/image_processing_levit.py
+1
-46
src/transformers/models/mask2former/image_processing_mask2former.py
...ormers/models/mask2former/image_processing_mask2former.py
+0
-13
src/transformers/models/maskformer/image_processing_maskformer.py
...sformers/models/maskformer/image_processing_maskformer.py
+0
-13
src/transformers/models/mobilenet_v1/image_processing_mobilenet_v1.py
...mers/models/mobilenet_v1/image_processing_mobilenet_v1.py
+0
-53
src/transformers/models/mobilenet_v2/image_processing_mobilenet_v2.py
...mers/models/mobilenet_v2/image_processing_mobilenet_v2.py
+0
-53
src/transformers/models/mobilevit/image_processing_mobilevit.py
...ansformers/models/mobilevit/image_processing_mobilevit.py
+0
-21
src/transformers/models/oneformer/image_processing_oneformer.py
...ansformers/models/oneformer/image_processing_oneformer.py
+0
-14
src/transformers/models/owlvit/image_processing_owlvit.py
src/transformers/models/owlvit/image_processing_owlvit.py
+0
-14
src/transformers/models/perceiver/image_processing_perceiver.py
...ansformers/models/perceiver/image_processing_perceiver.py
+1
-44
src/transformers/models/poolformer/image_processing_poolformer.py
...sformers/models/poolformer/image_processing_poolformer.py
+0
-45
src/transformers/models/pvt/image_processing_pvt.py
src/transformers/models/pvt/image_processing_pvt.py
+1
-52
src/transformers/models/sam/image_processing_sam.py
src/transformers/models/sam/image_processing_sam.py
+1
-44
src/transformers/models/segformer/image_processing_segformer.py
...ansformers/models/segformer/image_processing_segformer.py
+1
-44
src/transformers/models/swin2sr/image_processing_swin2sr.py
src/transformers/models/swin2sr/image_processing_swin2sr.py
+1
-23
src/transformers/models/tvlt/image_processing_tvlt.py
src/transformers/models/tvlt/image_processing_tvlt.py
+0
-52
src/transformers/models/videomae/image_processing_videomae.py
...transformers/models/videomae/image_processing_videomae.py
+0
-45
src/transformers/models/vilt/image_processing_vilt.py
src/transformers/models/vilt/image_processing_vilt.py
+1
-44
src/transformers/models/vit/image_processing_vit.py
src/transformers/models/vit/image_processing_vit.py
+1
-52
src/transformers/models/vit_hybrid/image_processing_vit_hybrid.py
...sformers/models/vit_hybrid/image_processing_vit_hybrid.py
+0
-45
src/transformers/models/vivit/image_processing_vivit.py
src/transformers/models/vivit/image_processing_vivit.py
+0
-24
No files found.
src/transformers/models/levit/image_processing_levit.py
View file @
1486d2ae
...
...
@@ -14,7 +14,7 @@
# limitations under the License.
"""Image processor class for LeViT."""
from
typing
import
Dict
,
Iterable
,
List
,
Optional
,
Union
from
typing
import
Dict
,
Iterable
,
Optional
,
Union
import
numpy
as
np
...
...
@@ -22,8 +22,6 @@ from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size
from
...image_transforms
import
(
center_crop
,
get_resize_output_image_size
,
normalize
,
rescale
,
resize
,
to_channel_dimension_format
,
)
...
...
@@ -184,49 +182,6 @@ class LevitImageProcessor(BaseImageProcessor):
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
)
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
(
self
,
images
:
ImageInput
,
...
...
src/transformers/models/mask2former/image_processing_mask2former.py
View file @
1486d2ae
...
...
@@ -24,7 +24,6 @@ from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size
from
...image_transforms
import
(
PaddingMode
,
get_resize_output_image_size
,
normalize
,
pad
,
rescale
,
resize
,
...
...
@@ -492,18 +491,6 @@ class Mask2FormerImageProcessor(BaseImageProcessor):
"""
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
(
self
,
segmentation_map
:
"np.ndarray"
,
...
...
src/transformers/models/maskformer/image_processing_maskformer.py
View file @
1486d2ae
...
...
@@ -24,7 +24,6 @@ from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size
from
...image_transforms
import
(
PaddingMode
,
get_resize_output_image_size
,
normalize
,
pad
,
rescale
,
resize
,
...
...
@@ -503,18 +502,6 @@ class MaskFormerImageProcessor(BaseImageProcessor):
"""
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
(
self
,
segmentation_map
:
"np.ndarray"
,
...
...
src/transformers/models/mobilenet_v1/image_processing_mobilenet_v1.py
View file @
1486d2ae
...
...
@@ -22,8 +22,6 @@ from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size
from
...image_transforms
import
(
center_crop
,
get_resize_output_image_size
,
normalize
,
rescale
,
resize
,
to_channel_dimension_format
,
)
...
...
@@ -164,57 +162,6 @@ class MobileNetV1ImageProcessor(BaseImageProcessor):
size
=
get_size_dict
(
size
)
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
(
self
,
images
:
ImageInput
,
...
...
src/transformers/models/mobilenet_v2/image_processing_mobilenet_v2.py
View file @
1486d2ae
...
...
@@ -22,8 +22,6 @@ from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size
from
...image_transforms
import
(
center_crop
,
get_resize_output_image_size
,
normalize
,
rescale
,
resize
,
to_channel_dimension_format
,
)
...
...
@@ -170,57 +168,6 @@ class MobileNetV2ImageProcessor(BaseImageProcessor):
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
)
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
(
self
,
images
:
ImageInput
,
...
...
src/transformers/models/mobilevit/image_processing_mobilevit.py
View file @
1486d2ae
...
...
@@ -23,7 +23,6 @@ from ...image_transforms import (
center_crop
,
flip_channel_order
,
get_resize_output_image_size
,
rescale
,
resize
,
to_channel_dimension_format
,
)
...
...
@@ -161,26 +160,6 @@ class MobileViTImageProcessor(BaseImageProcessor):
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
)
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
(
self
,
image
:
np
.
ndarray
,
data_format
:
Optional
[
Union
[
str
,
ChannelDimension
]]
=
None
)
->
np
.
ndarray
:
...
...
src/transformers/models/oneformer/image_processing_oneformer.py
View file @
1486d2ae
...
...
@@ -25,7 +25,6 @@ from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size
from
...image_transforms
import
(
PaddingMode
,
get_resize_output_image_size
,
normalize
,
pad
,
rescale
,
resize
,
...
...
@@ -487,19 +486,6 @@ class OneFormerImageProcessor(BaseImageProcessor):
"""
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
def
convert_segmentation_map_to_binary_masks
(
self
,
...
...
src/transformers/models/owlvit/image_processing_owlvit.py
View file @
1486d2ae
...
...
@@ -23,7 +23,6 @@ from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size
from
...image_transforms
import
(
center_crop
,
center_to_corners_format
,
normalize
,
rescale
,
resize
,
to_channel_dimension_format
,
...
...
@@ -210,19 +209,6 @@ class OwlViTImageProcessor(BaseImageProcessor):
"""
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
(
self
,
images
:
ImageInput
,
...
...
src/transformers/models/perceiver/image_processing_perceiver.py
View file @
1486d2ae
...
...
@@ -19,7 +19,7 @@ from typing import Dict, List, Optional, Union
import
numpy
as
np
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
(
IMAGENET_DEFAULT_MEAN
,
IMAGENET_DEFAULT_STD
,
...
...
@@ -174,49 +174,6 @@ class PerceiverImageProcessor(BaseImageProcessor):
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
(
self
,
images
:
ImageInput
,
...
...
src/transformers/models/poolformer/image_processing_poolformer.py
View file @
1486d2ae
...
...
@@ -22,8 +22,6 @@ from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size
from
...image_transforms
import
(
center_crop
,
get_resize_output_image_size
,
normalize
,
rescale
,
resize
,
to_channel_dimension_format
,
)
...
...
@@ -219,49 +217,6 @@ class PoolFormerImageProcessor(BaseImageProcessor):
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
)
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
(
self
,
images
:
ImageInput
,
...
...
src/transformers/models/pvt/image_processing_pvt.py
View file @
1486d2ae
...
...
@@ -19,7 +19,7 @@ from typing import Dict, List, Optional, Union
import
numpy
as
np
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
(
IMAGENET_DEFAULT_MEAN
,
IMAGENET_DEFAULT_STD
,
...
...
@@ -127,57 +127,6 @@ class PvtImageProcessor(BaseImageProcessor):
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
(
self
,
images
:
ImageInput
,
...
...
src/transformers/models/sam/image_processing_sam.py
View file @
1486d2ae
...
...
@@ -21,7 +21,7 @@ from typing import Any, Dict, List, Optional, Tuple, Union
import
numpy
as
np
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
(
IMAGENET_DEFAULT_MEAN
,
IMAGENET_DEFAULT_STD
,
...
...
@@ -212,49 +212,6 @@ class SamImageProcessor(BaseImageProcessor):
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
)
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
(
self
,
images
:
ImageInput
,
...
...
src/transformers/models/segformer/image_processing_segformer.py
View file @
1486d2ae
...
...
@@ -20,7 +20,7 @@ from typing import Any, Dict, List, Optional, Tuple, Union
import
numpy
as
np
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
(
IMAGENET_DEFAULT_MEAN
,
IMAGENET_DEFAULT_STD
,
...
...
@@ -156,49 +156,6 @@ class SegformerImageProcessor(BaseImageProcessor):
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
:
label
=
to_numpy_array
(
label
)
# Avoid using underflow conversion
...
...
src/transformers/models/swin2sr/image_processing_swin2sr.py
View file @
1486d2ae
...
...
@@ -19,7 +19,7 @@ from typing import Optional, Union
import
numpy
as
np
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
...utils
import
TensorType
,
logging
...
...
@@ -57,28 +57,6 @@ class Swin2SRImageProcessor(BaseImageProcessor):
self
.
do_pad
=
do_pad
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
):
"""
Pad an image to make the height and width divisible by `size`.
...
...
src/transformers/models/tvlt/image_processing_tvlt.py
View file @
1486d2ae
...
...
@@ -21,8 +21,6 @@ from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size
from
...image_transforms
import
(
center_crop
,
get_resize_output_image_size
,
normalize
,
rescale
,
resize
,
to_channel_dimension_format
,
)
...
...
@@ -208,56 +206,6 @@ class TvltImageProcessor(BaseImageProcessor):
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
)
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
(
self
,
image
:
ImageInput
,
...
...
src/transformers/models/videomae/image_processing_videomae.py
View file @
1486d2ae
...
...
@@ -22,8 +22,6 @@ from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size
from
...image_transforms
import
(
center_crop
,
get_resize_output_image_size
,
normalize
,
rescale
,
resize
,
to_channel_dimension_format
,
)
...
...
@@ -187,49 +185,6 @@ class VideoMAEImageProcessor(BaseImageProcessor):
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
)
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
(
self
,
image
:
ImageInput
,
...
...
src/transformers/models/vilt/image_processing_vilt.py
View file @
1486d2ae
...
...
@@ -19,7 +19,7 @@ from typing import Any, Dict, Iterable, List, Optional, Tuple, Union
import
numpy
as
np
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
(
IMAGENET_STANDARD_MEAN
,
IMAGENET_STANDARD_STD
,
...
...
@@ -229,49 +229,6 @@ class ViltImageProcessor(BaseImageProcessor):
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
)
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
(
self
,
image
:
np
.
ndarray
,
...
...
src/transformers/models/vit/image_processing_vit.py
View file @
1486d2ae
...
...
@@ -19,7 +19,7 @@ from typing import Dict, List, Optional, Union
import
numpy
as
np
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
(
IMAGENET_STANDARD_MEAN
,
IMAGENET_STANDARD_STD
,
...
...
@@ -127,57 +127,6 @@ class ViTImageProcessor(BaseImageProcessor):
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
(
self
,
images
:
ImageInput
,
...
...
src/transformers/models/vit_hybrid/image_processing_vit_hybrid.py
View file @
1486d2ae
...
...
@@ -23,8 +23,6 @@ from ...image_transforms import (
center_crop
,
convert_to_rgb
,
get_resize_output_image_size
,
normalize
,
rescale
,
resize
,
to_channel_dimension_format
,
)
...
...
@@ -173,49 +171,6 @@ class ViTHybridImageProcessor(BaseImageProcessor):
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
)
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
(
self
,
images
:
ImageInput
,
...
...
src/transformers/models/vivit/image_processing_vivit.py
View file @
1486d2ae
...
...
@@ -24,7 +24,6 @@ from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size
from
...image_transforms
import
(
center_crop
,
get_resize_output_image_size
,
normalize
,
rescale
,
resize
,
to_channel_dimension_format
,
...
...
@@ -222,29 +221,6 @@ class VivitImageProcessor(BaseImageProcessor):
image
=
image
-
(
scale
/
2
)
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
(
self
,
image
:
ImageInput
,
...
...
Prev
1
2
3
Next
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment