Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update resize() to be on par with torchvision speed #144

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion augly/image/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -1985,6 +1985,7 @@ def resize(
output_path: Optional[str] = None,
width: Optional[int] = None,
height: Optional[int] = None,
resample: Optional[int] = Image.BILINEAR,
zpapakipos marked this conversation as resolved.
Show resolved Hide resolved
metadata: Optional[List[Dict[str, Any]]] = None,
bboxes: Optional[List[Tuple]] = None,
bbox_format: Optional[str] = None,
Expand All @@ -2004,6 +2005,10 @@ def resize(
@param height: the desired height the image should be resized to have. If
None, the original image height will be used

@param resample: An optional resampling filter. This can be one of
PIL.Image.NEAREST, PIL.Image.BOX, PIL.Image.BILINEAR, PIL.Image.HAMMING,
PIL.Image.BICUBIC or PIL.Image.LANCZOS
zpapakipos marked this conversation as resolved.
Show resolved Hide resolved

@param metadata: if set to be a list, metadata about the function execution
including its name, the source & dest width, height, etc. will be appended
to the inputted list. If set to None, no metadata will be appended or returned
Expand All @@ -2027,7 +2032,7 @@ def resize(
src_mode = image.mode

im_w, im_h = image.size
aug_image = image.resize((width or im_w, height or im_h))
aug_image = image.resize((width or im_w, height or im_h), resample)

imutils.get_metadata(
metadata=metadata,
Expand Down
14 changes: 12 additions & 2 deletions augly/image/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import os
import random
from PIL import Image
zpapakipos marked this conversation as resolved.
Show resolved Hide resolved
from typing import Any, Callable, Dict, List, Optional, Tuple, Union

import augly.image.functional as F
Expand Down Expand Up @@ -1761,7 +1762,11 @@ def apply_transform(

class Resize(BaseTransform):
def __init__(
self, width: Optional[int] = None, height: Optional[int] = None, p: float = 1.0
self,
width: Optional[int] = None,
height: Optional[int] = None,
resample: Optional[int] = Image.BILINEAR,
zpapakipos marked this conversation as resolved.
Show resolved Hide resolved
p: float = 1.0,
):
"""
@param width: the desired width the image should be resized to have. If None,
Expand All @@ -1770,10 +1775,14 @@ def __init__(
@param height: the desired height the image should be resized to have. If None,
the original image height will be used

@param resample: An optional resampling filter. This can be one of
PIL.Image.NEAREST, PIL.Image.BOX, PIL.Image.BILINEAR, PIL.Image.HAMMING,
PIL.Image.BICUBIC or PIL.Image.LANCZOS
zpapakipos marked this conversation as resolved.
Show resolved Hide resolved

@param p: the probability of the transform being applied; default value is 1.0
"""
super().__init__(p)
self.width, self.height = width, height
self.width, self.height, self.resample = width, height, resample

def apply_transform(
self,
Expand Down Expand Up @@ -1805,6 +1814,7 @@ def apply_transform(
image,
width=self.width,
height=self.height,
resample=self.resample,
metadata=metadata,
bboxes=bboxes,
bbox_format=bbox_format,
Expand Down
3 changes: 2 additions & 1 deletion augly/tests/image_tests/functional_unit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import augly.image as imaugs
from augly.tests.image_tests.base_unit_test import BaseImageUnitTest
from augly.utils import EMOJI_PATH, IMG_MASK_PATH
from PIL import Image


class FunctionalImageUnitTest(BaseImageUnitTest):
Expand Down Expand Up @@ -107,7 +108,7 @@ def test_random_noise(self):
self.evaluate_function(imaugs.random_noise)

def test_resize(self):
self.evaluate_function(imaugs.resize)
self.evaluate_function(imaugs.resize, resample=Image.BICUBIC)
membriux marked this conversation as resolved.
Show resolved Hide resolved

def test_rotate(self):
self.evaluate_function(imaugs.rotate)
Expand Down
3 changes: 2 additions & 1 deletion augly/tests/image_tests/transforms_unit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import unittest

import augly.image as imaugs
from PIL import Image
from augly.tests.image_tests.base_unit_test import BaseImageUnitTest
from augly.utils import (
EMOJI_PATH,
Expand Down Expand Up @@ -184,7 +185,7 @@ def test_RandomRotation(self):
self.evaluate_class(imaugs.RandomRotation(), fname="RandomRotation")

def test_Resize(self):
self.evaluate_class(imaugs.Resize(), fname="resize")
self.evaluate_class(imaugs.Resize(resample=Image.BICUBIC), fname="resize")

def test_Rotate(self):
self.evaluate_class(imaugs.Rotate(), fname="rotate")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@
"intensity": 0.0,
"name": "resize",
"output_path": null,
"resample": 3,
"src_bboxes": [[0.5, 0.5, 0.25, 0.75]],
"src_height": 1080,
"src_width": 1920,
Expand Down