Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
chenpangpang
transformers
Commits
1689aea7
Unverified
Commit
1689aea7
authored
Jul 26, 2023
by
amyeroberts
Committed by
GitHub
Jul 26, 2023
Browse files
Move center_crop to BaseImageProcessor (#25122)
parent
659829b6
Changes
18
Hide whitespace changes
Inline
Side-by-side
Showing
18 changed files
with
29 additions
and
419 deletions
+29
-419
src/transformers/image_processing_utils.py
src/transformers/image_processing_utils.py
+25
-1
src/transformers/models/beit/image_processing_beit.py
src/transformers/models/beit/image_processing_beit.py
+1
-23
src/transformers/models/bit/image_processing_bit.py
src/transformers/models/bit/image_processing_bit.py
+0
-25
src/transformers/models/chinese_clip/image_processing_chinese_clip.py
...mers/models/chinese_clip/image_processing_chinese_clip.py
+0
-23
src/transformers/models/clip/image_processing_clip.py
src/transformers/models/clip/image_processing_clip.py
+0
-25
src/transformers/models/deit/image_processing_deit.py
src/transformers/models/deit/image_processing_deit.py
+1
-25
src/transformers/models/efficientformer/image_processing_efficientformer.py
...odels/efficientformer/image_processing_efficientformer.py
+0
-25
src/transformers/models/efficientnet/image_processing_efficientnet.py
...mers/models/efficientnet/image_processing_efficientnet.py
+1
-25
src/transformers/models/flava/image_processing_flava.py
src/transformers/models/flava/image_processing_flava.py
+1
-25
src/transformers/models/levit/image_processing_levit.py
src/transformers/models/levit/image_processing_levit.py
+0
-24
src/transformers/models/mobilenet_v1/image_processing_mobilenet_v1.py
...mers/models/mobilenet_v1/image_processing_mobilenet_v1.py
+0
-23
src/transformers/models/mobilenet_v2/image_processing_mobilenet_v2.py
...mers/models/mobilenet_v2/image_processing_mobilenet_v2.py
+0
-25
src/transformers/models/mobilevit/image_processing_mobilevit.py
...ansformers/models/mobilevit/image_processing_mobilevit.py
+0
-25
src/transformers/models/poolformer/image_processing_poolformer.py
...sformers/models/poolformer/image_processing_poolformer.py
+0
-25
src/transformers/models/tvlt/image_processing_tvlt.py
src/transformers/models/tvlt/image_processing_tvlt.py
+0
-25
src/transformers/models/videomae/image_processing_videomae.py
...transformers/models/videomae/image_processing_videomae.py
+0
-25
src/transformers/models/vit_hybrid/image_processing_vit_hybrid.py
...sformers/models/vit_hybrid/image_processing_vit_hybrid.py
+0
-25
src/transformers/models/vivit/image_processing_vivit.py
src/transformers/models/vivit/image_processing_vivit.py
+0
-25
No files found.
src/transformers/image_processing_utils.py
View file @
1689aea7
...
@@ -23,7 +23,7 @@ import numpy as np
...
@@ -23,7 +23,7 @@ import numpy as np
from
.dynamic_module_utils
import
custom_object_save
from
.dynamic_module_utils
import
custom_object_save
from
.feature_extraction_utils
import
BatchFeature
as
BaseBatchFeature
from
.feature_extraction_utils
import
BatchFeature
as
BaseBatchFeature
from
.image_transforms
import
normalize
,
rescale
from
.image_transforms
import
center_crop
,
normalize
,
rescale
from
.image_utils
import
ChannelDimension
from
.image_utils
import
ChannelDimension
from
.utils
import
(
from
.utils
import
(
IMAGE_PROCESSOR_NAME
,
IMAGE_PROCESSOR_NAME
,
...
@@ -571,6 +571,30 @@ class BaseImageProcessor(ImageProcessingMixin):
...
@@ -571,6 +571,30 @@ class BaseImageProcessor(ImageProcessingMixin):
"""
"""
return
normalize
(
image
,
mean
=
mean
,
std
=
std
,
data_format
=
data_format
,
**
kwargs
)
return
normalize
(
image
,
mean
=
mean
,
std
=
std
,
data_format
=
data_format
,
**
kwargs
)
def
center_crop
(
self
,
image
:
np
.
ndarray
,
size
:
Dict
[
str
,
int
],
data_format
:
Optional
[
Union
[
str
,
ChannelDimension
]]
=
None
,
**
kwargs
,
)
->
np
.
ndarray
:
"""
Center crop an image to `(size["height"], size["width"])`. If the input size is smaller than `crop_size` along
any edge, the image is padded with 0's and then center cropped.
Args:
image (`np.ndarray`):
Image to center crop.
size (`Dict[str, int]`):
Size of the output 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.
"""
size
=
get_size_dict
(
size
)
if
"height"
not
in
size
or
"width"
not
in
size
:
raise
ValueError
(
f
"The size dictionary must have keys 'height' and 'width'. Got
{
size
.
keys
()
}
"
)
return
center_crop
(
image
,
size
=
(
size
[
"height"
],
size
[
"width"
]),
data_format
=
data_format
,
**
kwargs
)
VALID_SIZE_DICT_KEYS
=
({
"height"
,
"width"
},
{
"shortest_edge"
},
{
"shortest_edge"
,
"longest_edge"
},
{
"longest_edge"
})
VALID_SIZE_DICT_KEYS
=
({
"height"
,
"width"
},
{
"shortest_edge"
},
{
"shortest_edge"
,
"longest_edge"
},
{
"longest_edge"
})
...
...
src/transformers/models/beit/image_processing_beit.py
View file @
1689aea7
...
@@ -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
center_crop
,
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
,
...
@@ -167,28 +167,6 @@ class BeitImageProcessor(BaseImageProcessor):
...
@@ -167,28 +167,6 @@ class BeitImageProcessor(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
center_crop
(
self
,
image
:
np
.
ndarray
,
size
:
Dict
[
str
,
int
],
data_format
:
Optional
[
Union
[
str
,
ChannelDimension
]]
=
None
,
**
kwargs
,
)
->
np
.
ndarray
:
"""
Center crop an image to (size["height"], size["width"]). If the input size is smaller than `size` along any
edge, the image is padded with 0's and then center cropped.
Args:
image (`np.ndarray`):
Image to center crop.
size (`Dict[str, int]`):
Size of the output 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.
"""
size
=
get_size_dict
(
size
,
default_to_square
=
True
,
param_name
=
"size"
)
return
center_crop
(
image
,
size
=
(
size
[
"height"
],
size
[
"width"
]),
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
...
...
src/transformers/models/bit/image_processing_bit.py
View file @
1689aea7
...
@@ -20,7 +20,6 @@ import numpy as np
...
@@ -20,7 +20,6 @@ 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
(
from
...image_transforms
import
(
center_crop
,
convert_to_rgb
,
convert_to_rgb
,
get_resize_output_image_size
,
get_resize_output_image_size
,
resize
,
resize
,
...
@@ -147,30 +146,6 @@ class BitImageProcessor(BaseImageProcessor):
...
@@ -147,30 +146,6 @@ class BitImageProcessor(BaseImageProcessor):
output_size
=
get_resize_output_image_size
(
image
,
size
=
size
[
"shortest_edge"
],
default_to_square
=
False
)
output_size
=
get_resize_output_image_size
(
image
,
size
=
size
[
"shortest_edge"
],
default_to_square
=
False
)
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
center_crop
(
self
,
image
:
np
.
ndarray
,
size
:
Dict
[
str
,
int
],
data_format
:
Optional
[
Union
[
str
,
ChannelDimension
]]
=
None
,
**
kwargs
,
)
->
np
.
ndarray
:
"""
Center crop an image. If the image is too small to be cropped to the size given, it will be padded (so the
returned result will always be of size `size`).
Args:
image (`np.ndarray`):
Image to center crop.
size (`Dict[str, int]`):
Size of the output image in the form of a dictionary with keys `height` and `width`.
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.
"""
size
=
get_size_dict
(
size
)
if
"height"
not
in
size
or
"width"
not
in
size
:
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
preprocess
(
def
preprocess
(
self
,
self
,
images
:
ImageInput
,
images
:
ImageInput
,
...
...
src/transformers/models/chinese_clip/image_processing_chinese_clip.py
View file @
1689aea7
...
@@ -20,7 +20,6 @@ import numpy as np
...
@@ -20,7 +20,6 @@ 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
(
from
...image_transforms
import
(
center_crop
,
convert_to_rgb
,
convert_to_rgb
,
get_resize_output_image_size
,
get_resize_output_image_size
,
resize
,
resize
,
...
@@ -147,28 +146,6 @@ class ChineseCLIPImageProcessor(BaseImageProcessor):
...
@@ -147,28 +146,6 @@ class ChineseCLIPImageProcessor(BaseImageProcessor):
)
)
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
center_crop
(
self
,
image
:
np
.
ndarray
,
size
:
Dict
[
str
,
int
],
data_format
:
Optional
[
Union
[
str
,
ChannelDimension
]]
=
None
,
**
kwargs
,
)
->
np
.
ndarray
:
"""
Center crop an image. If the image is too small to be cropped to the size given, it will be padded (so the
returned result will always be of size `size`).
Args:
image (`np.ndarray`):
Image to center crop.
size (`Dict[str, int]`):
Size of the output image in the form of a dictionary with keys `height` and `width`.
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.
"""
size
=
get_size_dict
(
size
)
return
center_crop
(
image
,
size
=
(
size
[
"height"
],
size
[
"width"
]),
data_format
=
data_format
,
**
kwargs
)
def
preprocess
(
def
preprocess
(
self
,
self
,
images
:
ImageInput
,
images
:
ImageInput
,
...
...
src/transformers/models/clip/image_processing_clip.py
View file @
1689aea7
...
@@ -20,7 +20,6 @@ import numpy as np
...
@@ -20,7 +20,6 @@ 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
(
from
...image_transforms
import
(
center_crop
,
convert_to_rgb
,
convert_to_rgb
,
get_resize_output_image_size
,
get_resize_output_image_size
,
resize
,
resize
,
...
@@ -147,30 +146,6 @@ class CLIPImageProcessor(BaseImageProcessor):
...
@@ -147,30 +146,6 @@ class CLIPImageProcessor(BaseImageProcessor):
output_size
=
get_resize_output_image_size
(
image
,
size
=
size
[
"shortest_edge"
],
default_to_square
=
False
)
output_size
=
get_resize_output_image_size
(
image
,
size
=
size
[
"shortest_edge"
],
default_to_square
=
False
)
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
center_crop
(
self
,
image
:
np
.
ndarray
,
size
:
Dict
[
str
,
int
],
data_format
:
Optional
[
Union
[
str
,
ChannelDimension
]]
=
None
,
**
kwargs
,
)
->
np
.
ndarray
:
"""
Center crop an image. If the image is too small to be cropped to the size given, it will be padded (so the
returned result will always be of size `size`).
Args:
image (`np.ndarray`):
Image to center crop.
size (`Dict[str, int]`):
Size of the output image in the form of a dictionary with keys `height` and `width`.
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.
"""
size
=
get_size_dict
(
size
)
if
"height"
not
in
size
or
"width"
not
in
size
:
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
preprocess
(
def
preprocess
(
self
,
self
,
images
:
ImageInput
,
images
:
ImageInput
,
...
...
src/transformers/models/deit/image_processing_deit.py
View file @
1689aea7
...
@@ -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
,
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
,
...
@@ -135,30 +135,6 @@ class DeiTImageProcessor(BaseImageProcessor):
...
@@ -135,30 +135,6 @@ class DeiTImageProcessor(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
center_crop
(
self
,
image
:
np
.
ndarray
,
size
:
Dict
[
str
,
int
],
data_format
:
Optional
[
Union
[
str
,
ChannelDimension
]]
=
None
,
**
kwargs
,
)
->
np
.
ndarray
:
"""
Center crop an image to `(crop_size["height"], crop_size["width"])`. If the input size is smaller than
`crop_size` along any edge, the image is padded with 0's and then center cropped.
Args:
image (`np.ndarray`):
Image to center crop.
size (`Dict[str, int]`):
Size of the output 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.
"""
size
=
get_size_dict
(
size
)
if
"height"
not
in
size
or
"width"
not
in
size
:
raise
ValueError
(
f
"The size dictionary must have keys 'height' and 'width'. Got
{
size
.
keys
()
}
"
)
return
center_crop
(
image
,
size
=
(
size
[
"height"
],
size
[
"width"
]),
data_format
=
data_format
,
**
kwargs
)
def
preprocess
(
def
preprocess
(
self
,
self
,
images
:
ImageInput
,
images
:
ImageInput
,
...
...
src/transformers/models/efficientformer/image_processing_efficientformer.py
View file @
1689aea7
...
@@ -20,7 +20,6 @@ import numpy as np
...
@@ -20,7 +20,6 @@ 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
(
from
...image_transforms
import
(
center_crop
,
get_resize_output_image_size
,
get_resize_output_image_size
,
resize
,
resize
,
to_channel_dimension_format
,
to_channel_dimension_format
,
...
@@ -149,30 +148,6 @@ class EfficientFormerImageProcessor(BaseImageProcessor):
...
@@ -149,30 +148,6 @@ class EfficientFormerImageProcessor(BaseImageProcessor):
raise
ValueError
(
f
"Size must contain 'height' and 'width' keys or 'shortest_edge' key. Got
{
size
.
keys
()
}
"
)
raise
ValueError
(
f
"Size must contain 'height' and 'width' keys or 'shortest_edge' key. Got
{
size
.
keys
()
}
"
)
return
resize
(
image
,
size
=
size
,
resample
=
resample
,
data_format
=
data_format
,
**
kwargs
)
return
resize
(
image
,
size
=
size
,
resample
=
resample
,
data_format
=
data_format
,
**
kwargs
)
def
center_crop
(
self
,
image
:
np
.
ndarray
,
size
:
Dict
[
str
,
int
],
data_format
:
Optional
[
Union
[
str
,
ChannelDimension
]]
=
None
,
**
kwargs
,
)
->
np
.
ndarray
:
"""
Center crop an image. If the image is too small to be cropped to the size given, it will be padded (so the
returned result will always be of size `size`).
Args:
image (`np.ndarray`):
Image to center crop.
size (`Dict[str, int]`):
Size of the output image in the form of a dictionary with keys `height` and `width`.
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.
"""
size
=
get_size_dict
(
size
)
if
"height"
not
in
size
or
"width"
not
in
size
:
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
preprocess
(
def
preprocess
(
self
,
self
,
images
:
ImageInput
,
images
:
ImageInput
,
...
...
src/transformers/models/efficientnet/image_processing_efficientnet.py
View file @
1689aea7
...
@@ -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
,
rescale
,
resize
,
to_channel_dimension_format
from
...image_transforms
import
rescale
,
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
,
...
@@ -144,30 +144,6 @@ class EfficientNetImageProcessor(BaseImageProcessor):
...
@@ -144,30 +144,6 @@ class EfficientNetImageProcessor(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
center_crop
(
self
,
image
:
np
.
ndarray
,
size
:
Dict
[
str
,
int
],
data_format
:
Optional
[
Union
[
str
,
ChannelDimension
]]
=
None
,
**
kwargs
,
)
->
np
.
ndarray
:
"""
Center crop an image to `(crop_size["height"], crop_size["width"])`. If the input size is smaller than
`crop_size` along any edge, the image is padded with 0's and then center cropped.
Args:
image (`np.ndarray`):
Image to center crop.
size (`Dict[str, int]`):
Size of the output 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.
"""
size
=
get_size_dict
(
size
)
if
"height"
not
in
size
or
"width"
not
in
size
:
raise
ValueError
(
f
"The size dictionary 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
(
def
rescale
(
self
,
self
,
image
:
np
.
ndarray
,
image
:
np
.
ndarray
,
...
...
src/transformers/models/flava/image_processing_flava.py
View file @
1689aea7
...
@@ -22,7 +22,7 @@ from typing import Any, Dict, Iterable, List, Optional, Tuple, Union
...
@@ -22,7 +22,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
center_crop
,
resize
,
to_channel_dimension_format
from
...image_transforms
import
resize
,
to_channel_dimension_format
from
...image_utils
import
(
from
...image_utils
import
(
OPENAI_CLIP_MEAN
,
OPENAI_CLIP_MEAN
,
OPENAI_CLIP_STD
,
OPENAI_CLIP_STD
,
...
@@ -359,30 +359,6 @@ class FlavaImageProcessor(BaseImageProcessor):
...
@@ -359,30 +359,6 @@ class FlavaImageProcessor(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
center_crop
(
self
,
image
:
np
.
ndarray
,
size
:
Dict
[
str
,
int
],
data_format
:
Optional
[
Union
[
str
,
ChannelDimension
]]
=
None
,
**
kwargs
,
)
->
np
.
ndarray
:
"""
Center crop an image to `(size["height"], size["width"])`. If the input size is smaller than `crop_size` along
any edge, the image is padded with 0's and then center cropped.
Args:
image (`np.ndarray`):
Image to center crop.
size (`Dict[str, int]`):
Size of the output 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.
"""
size
=
get_size_dict
(
size
)
if
"height"
not
in
size
or
"width"
not
in
size
:
raise
ValueError
(
f
"The size dictionary must contain 'height' and 'width' keys. Got
{
size
.
keys
()
}
"
)
return
center_crop
(
image
,
size
=
(
size
[
"height"
],
size
[
"width"
]),
data_format
=
data_format
,
**
kwargs
)
def
map_pixels
(
self
,
image
:
np
.
ndarray
)
->
np
.
ndarray
:
def
map_pixels
(
self
,
image
:
np
.
ndarray
)
->
np
.
ndarray
:
return
(
1
-
2
*
LOGIT_LAPLACE_EPS
)
*
image
+
LOGIT_LAPLACE_EPS
return
(
1
-
2
*
LOGIT_LAPLACE_EPS
)
*
image
+
LOGIT_LAPLACE_EPS
...
...
src/transformers/models/levit/image_processing_levit.py
View file @
1689aea7
...
@@ -20,7 +20,6 @@ import numpy as np
...
@@ -20,7 +20,6 @@ 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
(
from
...image_transforms
import
(
center_crop
,
get_resize_output_image_size
,
get_resize_output_image_size
,
resize
,
resize
,
to_channel_dimension_format
,
to_channel_dimension_format
,
...
@@ -159,29 +158,6 @@ class LevitImageProcessor(BaseImageProcessor):
...
@@ -159,29 +158,6 @@ class LevitImageProcessor(BaseImageProcessor):
image
,
size
=
(
size_dict
[
"height"
],
size_dict
[
"width"
]),
resample
=
resample
,
data_format
=
data_format
,
**
kwargs
image
,
size
=
(
size_dict
[
"height"
],
size_dict
[
"width"
]),
resample
=
resample
,
data_format
=
data_format
,
**
kwargs
)
)
def
center_crop
(
self
,
image
:
np
.
ndarray
,
size
:
Dict
[
str
,
int
],
data_format
:
Optional
[
Union
[
str
,
ChannelDimension
]]
=
None
,
**
kwargs
,
)
->
np
.
ndarray
:
"""
Center crop an image.
Args:
image (`np.ndarray`):
Image to center crop.
size (`Dict[str, int]`):
Dict `{"height": int, "width": int}` specifying the size of the output image after cropping.
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.
"""
size
=
get_size_dict
(
size
)
if
"height"
not
in
size
or
"width"
not
in
size
:
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
preprocess
(
def
preprocess
(
self
,
self
,
images
:
ImageInput
,
images
:
ImageInput
,
...
...
src/transformers/models/mobilenet_v1/image_processing_mobilenet_v1.py
View file @
1689aea7
...
@@ -20,7 +20,6 @@ import numpy as np
...
@@ -20,7 +20,6 @@ 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
(
from
...image_transforms
import
(
center_crop
,
get_resize_output_image_size
,
get_resize_output_image_size
,
resize
,
resize
,
to_channel_dimension_format
,
to_channel_dimension_format
,
...
@@ -140,28 +139,6 @@ class MobileNetV1ImageProcessor(BaseImageProcessor):
...
@@ -140,28 +139,6 @@ class MobileNetV1ImageProcessor(BaseImageProcessor):
output_size
=
get_resize_output_image_size
(
image
,
size
=
size
[
"shortest_edge"
],
default_to_square
=
False
)
output_size
=
get_resize_output_image_size
(
image
,
size
=
size
[
"shortest_edge"
],
default_to_square
=
False
)
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
center_crop
(
self
,
image
:
np
.
ndarray
,
size
:
Dict
[
str
,
int
],
data_format
:
Optional
[
Union
[
str
,
ChannelDimension
]]
=
None
,
**
kwargs
,
)
->
np
.
ndarray
:
"""
Center crop an image to (size["height"], size["width"]). If the input size is smaller than `size` along any
edge, the image is padded with 0's and then center cropped.
Args:
image (`np.ndarray`):
Image to center crop.
size (`Dict[str, int]`):
Size of the output 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.
"""
size
=
get_size_dict
(
size
)
return
center_crop
(
image
,
size
=
(
size
[
"height"
],
size
[
"width"
]),
data_format
=
data_format
,
**
kwargs
)
def
preprocess
(
def
preprocess
(
self
,
self
,
images
:
ImageInput
,
images
:
ImageInput
,
...
...
src/transformers/models/mobilenet_v2/image_processing_mobilenet_v2.py
View file @
1689aea7
...
@@ -20,7 +20,6 @@ import numpy as np
...
@@ -20,7 +20,6 @@ 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
(
from
...image_transforms
import
(
center_crop
,
get_resize_output_image_size
,
get_resize_output_image_size
,
resize
,
resize
,
to_channel_dimension_format
,
to_channel_dimension_format
,
...
@@ -144,30 +143,6 @@ class MobileNetV2ImageProcessor(BaseImageProcessor):
...
@@ -144,30 +143,6 @@ class MobileNetV2ImageProcessor(BaseImageProcessor):
output_size
=
get_resize_output_image_size
(
image
,
size
=
size
[
"shortest_edge"
],
default_to_square
=
False
)
output_size
=
get_resize_output_image_size
(
image
,
size
=
size
[
"shortest_edge"
],
default_to_square
=
False
)
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
center_crop
(
self
,
image
:
np
.
ndarray
,
size
:
Dict
[
str
,
int
],
data_format
:
Optional
[
Union
[
str
,
ChannelDimension
]]
=
None
,
**
kwargs
,
)
->
np
.
ndarray
:
"""
Center crop an image to (size["height"], size["width"]). If the input size is smaller than `size` along any
edge, the image is padded with 0's and then center cropped.
Args:
image (`np.ndarray`):
Image to center crop.
size (`Dict[str, int]`):
Size of the output 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.
"""
size
=
get_size_dict
(
size
)
if
"height"
not
in
size
or
"width"
not
in
size
:
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
preprocess
(
def
preprocess
(
self
,
self
,
images
:
ImageInput
,
images
:
ImageInput
,
...
...
src/transformers/models/mobilevit/image_processing_mobilevit.py
View file @
1689aea7
...
@@ -20,7 +20,6 @@ import numpy as np
...
@@ -20,7 +20,6 @@ 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
(
from
...image_transforms
import
(
center_crop
,
flip_channel_order
,
flip_channel_order
,
get_resize_output_image_size
,
get_resize_output_image_size
,
resize
,
resize
,
...
@@ -136,30 +135,6 @@ class MobileViTImageProcessor(BaseImageProcessor):
...
@@ -136,30 +135,6 @@ class MobileViTImageProcessor(BaseImageProcessor):
output_size
=
get_resize_output_image_size
(
image
,
size
=
size
[
"shortest_edge"
],
default_to_square
=
False
)
output_size
=
get_resize_output_image_size
(
image
,
size
=
size
[
"shortest_edge"
],
default_to_square
=
False
)
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
center_crop
(
self
,
image
:
np
.
ndarray
,
size
:
Dict
[
str
,
int
],
data_format
:
Optional
[
Union
[
str
,
ChannelDimension
]]
=
None
,
**
kwargs
,
)
->
np
.
ndarray
:
"""
Center crop an image to size `(size["height], size["width"])`. If the input size is smaller than `size` along
any edge, the image is padded with 0's and then center cropped.
Args:
image (`np.ndarray`):
Image to center crop.
size (`Dict[str, int]`):
Size of the output 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.
"""
size
=
get_size_dict
(
size
)
if
"height"
not
in
size
or
"width"
not
in
size
:
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
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
:
...
...
src/transformers/models/poolformer/image_processing_poolformer.py
View file @
1689aea7
...
@@ -20,7 +20,6 @@ import numpy as np
...
@@ -20,7 +20,6 @@ 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
(
from
...image_transforms
import
(
center_crop
,
get_resize_output_image_size
,
get_resize_output_image_size
,
resize
,
resize
,
to_channel_dimension_format
,
to_channel_dimension_format
,
...
@@ -193,30 +192,6 @@ class PoolFormerImageProcessor(BaseImageProcessor):
...
@@ -193,30 +192,6 @@ class PoolFormerImageProcessor(BaseImageProcessor):
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
center_crop
(
self
,
image
:
np
.
ndarray
,
size
:
Dict
[
str
,
int
],
data_format
:
Optional
[
Union
[
str
,
ChannelDimension
]]
=
None
,
**
kwargs
,
)
->
np
.
ndarray
:
"""
Center crop an image to (size["height"], size["width"]). If the input size is smaller than `crop_size` along
any edge, the image is padded with 0's and then center cropped.
Args:
image (`np.ndarray`):
Image to center crop.
size (`Dict[str, int]`):
Size of the output 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.
"""
size
=
get_size_dict
(
size
)
if
"height"
not
in
size
or
"width"
not
in
size
:
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
preprocess
(
def
preprocess
(
self
,
self
,
images
:
ImageInput
,
images
:
ImageInput
,
...
...
src/transformers/models/tvlt/image_processing_tvlt.py
View file @
1689aea7
...
@@ -19,7 +19,6 @@ import numpy as np
...
@@ -19,7 +19,6 @@ 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
(
from
...image_transforms
import
(
center_crop
,
get_resize_output_image_size
,
get_resize_output_image_size
,
resize
,
resize
,
to_channel_dimension_format
,
to_channel_dimension_format
,
...
@@ -182,30 +181,6 @@ class TvltImageProcessor(BaseImageProcessor):
...
@@ -182,30 +181,6 @@ class TvltImageProcessor(BaseImageProcessor):
raise
ValueError
(
f
"Size must have 'height' and 'width' or 'shortest_edge' as keys. Got
{
size
.
keys
()
}
"
)
raise
ValueError
(
f
"Size must have 'height' and 'width' or 'shortest_edge' as keys. Got
{
size
.
keys
()
}
"
)
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
center_crop
(
self
,
image
:
np
.
ndarray
,
size
:
Dict
[
str
,
int
],
data_format
:
Optional
[
Union
[
str
,
ChannelDimension
]]
=
None
,
**
kwargs
,
)
->
np
.
ndarray
:
"""
Center crop an image to `(size["height"], size["width"])`. If the input size is smaller than `size` along any
edge, the image is padded with 0's and then center cropped.
Args:
image (`np.ndarray`):
Image to center crop.
size (`Dict[str, int]`):
Size of the output 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.
"""
size
=
get_size_dict
(
size
)
if
"height"
not
in
size
or
"width"
not
in
size
:
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
_preprocess_image
(
def
_preprocess_image
(
self
,
self
,
image
:
ImageInput
,
image
:
ImageInput
,
...
...
src/transformers/models/videomae/image_processing_videomae.py
View file @
1689aea7
...
@@ -20,7 +20,6 @@ import numpy as np
...
@@ -20,7 +20,6 @@ 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
(
from
...image_transforms
import
(
center_crop
,
get_resize_output_image_size
,
get_resize_output_image_size
,
resize
,
resize
,
to_channel_dimension_format
,
to_channel_dimension_format
,
...
@@ -161,30 +160,6 @@ class VideoMAEImageProcessor(BaseImageProcessor):
...
@@ -161,30 +160,6 @@ class VideoMAEImageProcessor(BaseImageProcessor):
raise
ValueError
(
f
"Size must have 'height' and 'width' or 'shortest_edge' as keys. Got
{
size
.
keys
()
}
"
)
raise
ValueError
(
f
"Size must have 'height' and 'width' or 'shortest_edge' as keys. Got
{
size
.
keys
()
}
"
)
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
center_crop
(
self
,
image
:
np
.
ndarray
,
size
:
Dict
[
str
,
int
],
data_format
:
Optional
[
Union
[
str
,
ChannelDimension
]]
=
None
,
**
kwargs
,
)
->
np
.
ndarray
:
"""
Center crop an image to `(size["height"], size["width"])`. If the input size is smaller than `size` along any
edge, the image is padded with 0's and then center cropped.
Args:
image (`np.ndarray`):
Image to center crop.
size (`Dict[str, int]`):
Size of the output 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.
"""
size
=
get_size_dict
(
size
)
if
"height"
not
in
size
or
"width"
not
in
size
:
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
_preprocess_image
(
def
_preprocess_image
(
self
,
self
,
image
:
ImageInput
,
image
:
ImageInput
,
...
...
src/transformers/models/vit_hybrid/image_processing_vit_hybrid.py
View file @
1689aea7
...
@@ -20,7 +20,6 @@ import numpy as np
...
@@ -20,7 +20,6 @@ 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
(
from
...image_transforms
import
(
center_crop
,
convert_to_rgb
,
convert_to_rgb
,
get_resize_output_image_size
,
get_resize_output_image_size
,
resize
,
resize
,
...
@@ -147,30 +146,6 @@ class ViTHybridImageProcessor(BaseImageProcessor):
...
@@ -147,30 +146,6 @@ class ViTHybridImageProcessor(BaseImageProcessor):
output_size
=
get_resize_output_image_size
(
image
,
size
=
size
[
"shortest_edge"
],
default_to_square
=
False
)
output_size
=
get_resize_output_image_size
(
image
,
size
=
size
[
"shortest_edge"
],
default_to_square
=
False
)
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
center_crop
(
self
,
image
:
np
.
ndarray
,
size
:
Dict
[
str
,
int
],
data_format
:
Optional
[
Union
[
str
,
ChannelDimension
]]
=
None
,
**
kwargs
,
)
->
np
.
ndarray
:
"""
Center crop an image. If the image is too small to be cropped to the size given, it will be padded (so the
returned result will always be of size `size`).
Args:
image (`np.ndarray`):
Image to center crop.
size (`Dict[str, int]`):
Size of the output image in the form of a dictionary with keys `height` and `width`.
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.
"""
size
=
get_size_dict
(
size
)
if
"height"
not
in
size
or
"width"
not
in
size
:
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
preprocess
(
def
preprocess
(
self
,
self
,
images
:
ImageInput
,
images
:
ImageInput
,
...
...
src/transformers/models/vivit/image_processing_vivit.py
View file @
1689aea7
...
@@ -22,7 +22,6 @@ from transformers.utils.generic import TensorType
...
@@ -22,7 +22,6 @@ from transformers.utils.generic import TensorType
from
...image_processing_utils
import
BaseImageProcessor
,
BatchFeature
,
get_size_dict
from
...image_processing_utils
import
BaseImageProcessor
,
BatchFeature
,
get_size_dict
from
...image_transforms
import
(
from
...image_transforms
import
(
center_crop
,
get_resize_output_image_size
,
get_resize_output_image_size
,
rescale
,
rescale
,
resize
,
resize
,
...
@@ -168,30 +167,6 @@ class VivitImageProcessor(BaseImageProcessor):
...
@@ -168,30 +167,6 @@ class VivitImageProcessor(BaseImageProcessor):
raise
ValueError
(
f
"Size must have 'height' and 'width' or 'shortest_edge' as keys. Got
{
size
.
keys
()
}
"
)
raise
ValueError
(
f
"Size must have 'height' and 'width' or 'shortest_edge' as keys. Got
{
size
.
keys
()
}
"
)
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
center_crop
(
self
,
image
:
np
.
ndarray
,
size
:
Dict
[
str
,
int
],
data_format
:
Optional
[
Union
[
str
,
ChannelDimension
]]
=
None
,
**
kwargs
,
)
->
np
.
ndarray
:
"""
Center crop an image to `(size["height"], size["width"])`. If the input size is smaller than `size` along any
edge, the image is padded with 0's and then center cropped.
Args:
image (`np.ndarray`):
Image to center crop.
size (`Dict[str, int]`):
Size of the output 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.
"""
size
=
get_size_dict
(
size
)
if
"height"
not
in
size
or
"width"
not
in
size
:
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
(
def
rescale
(
self
,
self
,
image
:
np
.
ndarray
,
image
:
np
.
ndarray
,
...
...
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