Skip to content

Commit

Permalink
Update resize() to be on par with torchvision speed (#144)
Browse files Browse the repository at this point in the history
Summary:
- [x] I have read CONTRIBUTING.md to understand how to contribute to this repository :)

**Summary:** Refactored `resize()` from image/functional.py to be on par with torchvision. However, I just have one minor failure in my code. Please advise on where I should look :)

**Test results**
The following results were acquired from my machine
- Augly original (without interpolation)= 0.04475s
- Augly revised (with interpolation) = 0.02873s
- torchvision (uses interpolation) = 0.02696s

**Test code** → https://colab.research.google.com/drive/14-KZdSGaOaz73OgIS0DZZY4RsS3cJ0rg#scrollTo=xVI_h-1v49lC

## Unit Tests
If your changes touch the `audio` module, please run all of the `audio` tests and paste the output here. Likewise for `image`, `text`, & `video`. If your changes could affect behavior in multiple modules, please run the tests for all potentially affected modules. If you are unsure of which modules might be affected by your changes, please just run all the unit tests.
```

### Image
```bash
python -m unittest discover -s augly/tests/image_tests/ -p "*_test.py"
# Or `python -m unittest discover -s augly/tests/image_tests/ -p "*.py"` to run pytorch test too (must install `torchvision` to run)
```

**TEST OUTPUT**
```
======================================================================
FAIL: test_Resize (transforms_unit_test.TransformsImageUnitTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/macbookpro/Desktop/Github/AugLy/augly/tests/image_tests/transforms_unit_test.py", line 187, in test_Resize
    self.evaluate_class(imaugs.Resize(), fname="resize")
  File "/Users/macbookpro/Desktop/Github/AugLy/augly/tests/image_tests/base_unit_test.py", line 111, in evaluate_class
    self.assertTrue(
AssertionError: False is not true

----------------------------------------------------------------------
Ran 82 tests in 52.735s

FAILED (failures=1, skipped=5)
```

Pull Request resolved: #144

Reviewed By: jbitton

Differential Revision: D32664565

Pulled By: zpapakipos

fbshipit-source-id: 871c1861fb146c98a9f69ede2ac4df82424ecb64
  • Loading branch information
membriux authored and facebook-github-bot committed Dec 3, 2021
1 parent 4238abd commit 5edf973
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
7 changes: 6 additions & 1 deletion augly/image/functional.py
Expand Up @@ -1985,6 +1985,7 @@ def resize(
output_path: Optional[str] = None,
width: Optional[int] = None,
height: Optional[int] = None,
resample: Any = Image.BILINEAR,
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: A 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
@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
13 changes: 11 additions & 2 deletions augly/image/transforms.py
Expand Up @@ -1761,7 +1761,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: Any = Image.BILINEAR,
p: float = 1.0,
):
"""
@param width: the desired width the image should be resized to have. If None,
Expand All @@ -1770,10 +1774,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: A 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
@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 +1813,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
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)

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
Expand Up @@ -12,6 +12,7 @@
IMAGE_METADATA_PATH,
IMG_MASK_PATH,
)
from PIL import Image


class TransformsImageUnitTest(BaseImageUnitTest):
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
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

0 comments on commit 5edf973

Please sign in to comment.