Upload processor
Browse files- image_processing_spice_cnn.py +3 -69
- preprocessor_config.json +1 -2
image_processing_spice_cnn.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from typing import Dict, List, Optional, Union
|
2 |
|
3 |
import numpy as np
|
4 |
|
@@ -9,7 +9,6 @@ from transformers.image_processing_utils import (
|
|
9 |
)
|
10 |
from transformers.image_transforms import (
|
11 |
normalize,
|
12 |
-
pad,
|
13 |
rescale,
|
14 |
resize,
|
15 |
to_channel_dimension_format,
|
@@ -20,23 +19,11 @@ from transformers.image_utils import (
|
|
20 |
ChannelDimension,
|
21 |
ImageInput,
|
22 |
PILImageResampling,
|
23 |
-
infer_channel_dimension_format,
|
24 |
make_list_of_images,
|
25 |
to_numpy_array,
|
26 |
valid_images,
|
27 |
)
|
28 |
-
from transformers.utils import
|
29 |
-
|
30 |
-
|
31 |
-
class PaddingMode(ExplicitEnum):
|
32 |
-
"""
|
33 |
-
Enum class for the different padding modes to use when padding images.
|
34 |
-
"""
|
35 |
-
|
36 |
-
CONSTANT = "constant"
|
37 |
-
REFLECT = "reflect"
|
38 |
-
REPLICATE = "replicate"
|
39 |
-
SYMMETRIC = "symmetric"
|
40 |
|
41 |
|
42 |
class SpiceCNNImageProcessor(BaseImageProcessor):
|
@@ -80,8 +67,7 @@ class SpiceCNNImageProcessor(BaseImageProcessor):
|
|
80 |
do_normalize: bool = True,
|
81 |
image_mean: Optional[Union[float, List[float]]] = None,
|
82 |
image_std: Optional[Union[float, List[float]]] = None,
|
83 |
-
|
84 |
-
padding: int = 0,
|
85 |
**kwargs,
|
86 |
) -> None:
|
87 |
super().__init__(**kwargs)
|
@@ -90,7 +76,6 @@ class SpiceCNNImageProcessor(BaseImageProcessor):
|
|
90 |
self.do_resize = do_resize
|
91 |
self.do_rescale = do_rescale
|
92 |
self.do_normalize = do_normalize
|
93 |
-
self.do_padding = do_padding
|
94 |
self.size = size
|
95 |
self.resample = resample
|
96 |
self.rescale_factor = rescale_factor
|
@@ -100,51 +85,6 @@ class SpiceCNNImageProcessor(BaseImageProcessor):
|
|
100 |
self.image_std = image_std if image_std is not None else IMAGENET_STANDARD_STD
|
101 |
self.padding = padding
|
102 |
|
103 |
-
def pad(
|
104 |
-
image: np.ndarray,
|
105 |
-
padding: Union[int, Tuple[int, int], Iterable[Tuple[int, int]]],
|
106 |
-
mode: PaddingMode = PaddingMode.CONSTANT,
|
107 |
-
constant_values: Union[float, Iterable[float]] = 0.0,
|
108 |
-
data_format: Optional[Union[str, ChannelDimension]] = None,
|
109 |
-
input_data_format: Optional[Union[str, ChannelDimension]] = None,
|
110 |
-
) -> np.ndarray:
|
111 |
-
"""
|
112 |
-
Pads the `image` with the specified (height, width) `padding` and `mode`.
|
113 |
-
|
114 |
-
Args:
|
115 |
-
image (`np.ndarray`):
|
116 |
-
The image to pad.
|
117 |
-
padding (`int` or `Tuple[int, int]` or `Iterable[Tuple[int, int]]`):
|
118 |
-
Padding to apply to the edges of the height, width axes. Can be one of three formats:
|
119 |
-
- `((before_height, after_height), (before_width, after_width))` unique pad widths for each axis.
|
120 |
-
- `((before, after),)` yields same before and after pad for height and width.
|
121 |
-
- `(pad,)` or int is a shortcut for before = after = pad width for all axes.
|
122 |
-
mode (`PaddingMode`):
|
123 |
-
The padding mode to use. Can be one of:
|
124 |
-
- `"constant"`: pads with a constant value.
|
125 |
-
- `"reflect"`: pads with the reflection of the vector mirrored on the first and last values of the
|
126 |
-
vector along each axis.
|
127 |
-
- `"replicate"`: pads with the replication of the last value on the edge of the array along each axis.
|
128 |
-
- `"symmetric"`: pads with the reflection of the vector mirrored along the edge of the array.
|
129 |
-
constant_values (`float` or `Iterable[float]`, *optional*):
|
130 |
-
The value to use for the padding if `mode` is `"constant"`.
|
131 |
-
data_format (`str` or `ChannelDimension`, *optional*):
|
132 |
-
The channel dimension format for the output image. Can be one of:
|
133 |
-
- `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
|
134 |
-
- `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
|
135 |
-
If unset, will use same as the input image.
|
136 |
-
input_data_format (`str` or `ChannelDimension`, *optional*):
|
137 |
-
The channel dimension format for the input image. Can be one of:
|
138 |
-
- `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
|
139 |
-
- `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
|
140 |
-
If unset, will use the inferred format of the input image.
|
141 |
-
|
142 |
-
Returns:
|
143 |
-
`np.ndarray`: The padded image.
|
144 |
-
|
145 |
-
"""
|
146 |
-
return pad(image, padding=padding)
|
147 |
-
|
148 |
def resize(
|
149 |
self,
|
150 |
image: np.ndarray,
|
@@ -251,8 +191,6 @@ class SpiceCNNImageProcessor(BaseImageProcessor):
|
|
251 |
do_normalize: Optional[bool] = None,
|
252 |
image_mean: Optional[Union[float, List[float]]] = None,
|
253 |
image_std: Optional[Union[float, List[float]]] = None,
|
254 |
-
do_padding: Optional[bool] = None,
|
255 |
-
padding: Optional[int] = None,
|
256 |
return_tensors: Optional[Union[str, TensorType]] = None,
|
257 |
data_format: Union[str, ChannelDimension] = ChannelDimension.FIRST,
|
258 |
**kwargs,
|
@@ -303,7 +241,6 @@ class SpiceCNNImageProcessor(BaseImageProcessor):
|
|
303 |
)
|
304 |
image_mean = image_mean if image_mean is not None else self.image_mean
|
305 |
image_std = image_std if image_std is not None else self.image_std
|
306 |
-
padding = padding if padding is not None else self.padding
|
307 |
|
308 |
size = size if size is not None else self.size
|
309 |
size_dict = get_size_dict(size)
|
@@ -342,9 +279,6 @@ class SpiceCNNImageProcessor(BaseImageProcessor):
|
|
342 |
for image in images
|
343 |
]
|
344 |
|
345 |
-
if do_padding:
|
346 |
-
images = [self.pad(image=image, padding=padding) for image in images]
|
347 |
-
|
348 |
images = [to_channel_dimension_format(image, data_format) for image in images]
|
349 |
|
350 |
data = {"pixel_values": images}
|
|
|
1 |
+
from typing import Dict, List, Optional, Union
|
2 |
|
3 |
import numpy as np
|
4 |
|
|
|
9 |
)
|
10 |
from transformers.image_transforms import (
|
11 |
normalize,
|
|
|
12 |
rescale,
|
13 |
resize,
|
14 |
to_channel_dimension_format,
|
|
|
19 |
ChannelDimension,
|
20 |
ImageInput,
|
21 |
PILImageResampling,
|
|
|
22 |
make_list_of_images,
|
23 |
to_numpy_array,
|
24 |
valid_images,
|
25 |
)
|
26 |
+
from transformers.utils import TensorType
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
|
29 |
class SpiceCNNImageProcessor(BaseImageProcessor):
|
|
|
67 |
do_normalize: bool = True,
|
68 |
image_mean: Optional[Union[float, List[float]]] = None,
|
69 |
image_std: Optional[Union[float, List[float]]] = None,
|
70 |
+
padding: bool = False,
|
|
|
71 |
**kwargs,
|
72 |
) -> None:
|
73 |
super().__init__(**kwargs)
|
|
|
76 |
self.do_resize = do_resize
|
77 |
self.do_rescale = do_rescale
|
78 |
self.do_normalize = do_normalize
|
|
|
79 |
self.size = size
|
80 |
self.resample = resample
|
81 |
self.rescale_factor = rescale_factor
|
|
|
85 |
self.image_std = image_std if image_std is not None else IMAGENET_STANDARD_STD
|
86 |
self.padding = padding
|
87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
def resize(
|
89 |
self,
|
90 |
image: np.ndarray,
|
|
|
191 |
do_normalize: Optional[bool] = None,
|
192 |
image_mean: Optional[Union[float, List[float]]] = None,
|
193 |
image_std: Optional[Union[float, List[float]]] = None,
|
|
|
|
|
194 |
return_tensors: Optional[Union[str, TensorType]] = None,
|
195 |
data_format: Union[str, ChannelDimension] = ChannelDimension.FIRST,
|
196 |
**kwargs,
|
|
|
241 |
)
|
242 |
image_mean = image_mean if image_mean is not None else self.image_mean
|
243 |
image_std = image_std if image_std is not None else self.image_std
|
|
|
244 |
|
245 |
size = size if size is not None else self.size
|
246 |
size_dict = get_size_dict(size)
|
|
|
279 |
for image in images
|
280 |
]
|
281 |
|
|
|
|
|
|
|
282 |
images = [to_channel_dimension_format(image, data_format) for image in images]
|
283 |
|
284 |
data = {"pixel_values": images}
|
preprocessor_config.json
CHANGED
@@ -3,13 +3,12 @@
|
|
3 |
"AutoImageProcessor": "image_processing_spice_cnn.SpiceCNNImageProcessor"
|
4 |
},
|
5 |
"do_normalize": false,
|
6 |
-
"do_padding": false,
|
7 |
"do_rescale": false,
|
8 |
"do_resize": true,
|
9 |
"image_mean": 0.5,
|
10 |
"image_processor_type": "SpiceCNNImageProcessor",
|
11 |
"image_std": 0.5,
|
12 |
-
"padding":
|
13 |
"resample": 2,
|
14 |
"rescale_factor": 0.00392156862745098,
|
15 |
"size": {
|
|
|
3 |
"AutoImageProcessor": "image_processing_spice_cnn.SpiceCNNImageProcessor"
|
4 |
},
|
5 |
"do_normalize": false,
|
|
|
6 |
"do_rescale": false,
|
7 |
"do_resize": true,
|
8 |
"image_mean": 0.5,
|
9 |
"image_processor_type": "SpiceCNNImageProcessor",
|
10 |
"image_std": 0.5,
|
11 |
+
"padding": false,
|
12 |
"resample": 2,
|
13 |
"rescale_factor": 0.00392156862745098,
|
14 |
"size": {
|