Skip to content

Commit 0defe1c

Browse files
committed
Change from skimage.transform.resize to cv2.resize
To make the process of cropping and resizing images go faster, switch from the resize function in skimage.transform to the resize function in cv2 if cv2 is available on the system, otherwise stick with skimage.transform.resize.
1 parent 356f107 commit 0defe1c

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ The performance of each model variant using the pre-trained weights converted fr
9292

9393
* `Keras >= 2.2.0` / `TensorFlow >= 1.12.0`
9494
* `keras_applications >= 1.0.7`
95-
* `scikit-image`
95+
* `opencv >= 3.4.2` or `scikit-image` (for resizing images; `opencv` seems to be faster)
9696

9797
### Installing from the source
9898

efficientnet/preprocessing.py

+36-14
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,33 @@
1313
# limitations under the License.
1414
# ==============================================================================
1515
import numpy as np
16-
from skimage.transform import resize
16+
try:
17+
import cv2
18+
except ModuleNotFoundError:
19+
import skimage.transform
1720

18-
MAP_INTERPOLATION_TO_ORDER = {
19-
"nearest": 0,
20-
"bilinear": 1,
21-
"biquadratic": 2,
22-
"bicubic": 3,
23-
}
21+
22+
try:
23+
# OpenCV: Map interpolation string to OpenCV interpolation enum value
24+
INTERPOLATION_DICT = {
25+
"nearest": cv2.INTER_NEAREST,
26+
"bilinear": cv2.INTER_LINEAR,
27+
"bicubic": cv2.INTER_CUBIC,
28+
"laconzos": cv2.INTER_LANCZOS4,
29+
}
30+
except NameError:
31+
# scikit-image: Map interpolation string to interpolation order
32+
INTERPOLATION_DICT = {
33+
"nearest": 0,
34+
"bilinear": 1,
35+
"biquadratic": 2,
36+
"bicubic": 3,
37+
}
2438

2539

2640
def center_crop_and_resize(image, image_size, crop_padding=32, interpolation="bicubic"):
2741
assert image.ndim in {2, 3}
28-
assert interpolation in MAP_INTERPOLATION_TO_ORDER.keys()
42+
assert interpolation in INTERPOLATION_DICT.keys()
2943

3044
in_h, in_w = image.shape[:2]
3145

@@ -55,11 +69,19 @@ def center_crop_and_resize(image, image_size, crop_padding=32, interpolation="bi
5569
offset_h : unpadded_center_crop_size_pre_scaling[0] + offset_h,
5670
offset_w : unpadded_center_crop_size_pre_scaling[1] + offset_w,
5771
]
58-
resized_image = resize(
59-
image_crop,
60-
(out_h, out_w),
61-
order=MAP_INTERPOLATION_TO_ORDER[interpolation],
62-
preserve_range=True,
63-
)
72+
73+
try:
74+
resized_image = cv2.resize(
75+
image_crop,
76+
(out_w, out_h),
77+
interpolation=INTERPOLATION_DICT[interpolation] if inv_scale < 1 else cv2.INTER_AREA,
78+
)
79+
except NameError:
80+
resized_image = skimage.transform.resize(
81+
image_crop,
82+
(out_h, out_w),
83+
order=INTERPOLATION_DICT[interpolation],
84+
preserve_range=True,
85+
)
6486

6587
return resized_image

0 commit comments

Comments
 (0)