Skip to content

Commit 0dab20c

Browse files
membriuxfacebook-github-bot
authored andcommitted
Return images same mode (initial commit) (#132)
Summary: ## Related Issue Fixes #{128} - [x] I have read CONTRIBUTING.md to understand how to contribute to this repository :) - [x] Add src_mode arg to ret_and_save_image() in image/utils/utils.py - [x] Pass src_mode arg into ret_and_save_image() from every augmentation except convert_color in image/functional.py (e.g. here for apply_lambda) - [x] In image test evaluate_class(), assert that the mode of self.img & dst are equal. - [x] Run image tests, make sure they all pass: python -m unittest discover -s augly/tests/image_tests/ -p "*" ## 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](url) ```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: n/a ## Other testing N/A Pull Request resolved: #132 Test Plan: - Added line to transforms unit test saving dst image in `/tmp/` - Ran transforms unit test ``` $ manifold rm ai_red_team/tree/augmentations/tests/image/dfdc_expected_output/test_compose.png && manifold put /tmp/test_compose.png ai_red_team/tree/augmentations/tests/image/dfdc_expected_output/test_compose.png $ manifold rm ai_red_team/tree/augmentations/tests/image/dfdc_expected_output/test_opacity.png && manifold put /tmp/test_opacity.png ai_red_team/tree/augmentations/tests/image/dfdc_expected_output/test_opacity.png $ manifold rm ai_red_team/tree/augmentations/tests/image/dfdc_expected_output/test_overlay_emoji.png && manifold put /tmp/test_overlay_emoji.png ai_red_team/tree/augmentations/tests/image/dfdc_expected_output/test_overlay_emoji.png $ manifold rm ai_red_team/tree/augmentations/tests/image/dfdc_expected_output/test_overlay_image.png && manifold put /tmp/test_overlay_image.png ai_red_team/tree/augmentations/tests/image/dfdc_expected_output/test_overlay_image.png $ manifold rm ai_red_team/tree/augmentations/tests/image/dfdc_expected_output/test_overlay_onto_background_image.png && manifold put /tmp/test_overlay_onto_background_image.png ai_red_team/tree/augmentations/tests/image/dfdc_expected_output/test_overlay_onto_background_image.png $ manifold rm ai_red_team/tree/augmentations/tests/image/dfdc_expected_output/test_overlay_onto_screenshot.png && manifold put /tmp/test_overlay_onto_screenshot.png ai_red_team/tree/augmentations/tests/image/dfdc_expected_output/test_overlay_onto_screenshot.png ``` ``` $ rm -rf /home/zoep/.torch/iopath_cache/manifold_cache $ buck test //aml/augly/tests/image_tests:transforms_unit_test Summary Pass: 40 Skip: 2 ↻ aml/augly/tests/image_tests:transforms_unit_test - test_RandomEmojiOverlay (augly.tests.image_tests.transforms_unit_test.TransformsImageUnitTest) ↻ aml/augly/tests/image_tests:transforms_unit_test - test_MemeFormat (augly.tests.image_tests.transforms_unit_test.TransformsImageUnitTest) Omit: 1 {emoji:2702} aml/augly/tests/image_tests:transforms_unit_test - test_OverlayText (augly.tests.image_tests.transforms_unit_test.TransformsImageUnitTest) ListingSuccess: 1 ``` ``` $ buck test //aml/augly/tests/image_tests:functional_unit_test Summary Pass: 34 Skip: 2 ↻ aml/augly/tests/image_tests:functional_unit_test - test_overlay_text (augly.tests.image_tests.functional_unit_test.FunctionalImageUnitTest) ↻ aml/augly/tests/image_tests:functional_unit_test - test_meme_format (augly.tests.image_tests.functional_unit_test.FunctionalImageUnitTest) ListingSuccess: 1 ``` Reviewed By: jbitton Differential Revision: D31503672 Pulled By: zpapakipos fbshipit-source-id: afcff9edaf791e2fe9ba3c45dbe9f7228b5f4d7b
1 parent 02e4cd5 commit 0dab20c

File tree

9 files changed

+85
-36
lines changed

9 files changed

+85
-36
lines changed
-114 KB
Loading
-70 KB
Loading
-70.1 KB
Loading
-70.1 KB
Loading
-633 KB
Loading

augly/image/functional.py

Lines changed: 70 additions & 34 deletions
Large diffs are not rendered by default.

augly/image/utils/utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ def validate_and_load_image(image: Union[str, Image.Image]) -> Image.Image:
3030
return image
3131

3232

33-
def ret_and_save_image(image: Image.Image, output_path: Optional[str]) -> Image.Image:
33+
def ret_and_save_image(
34+
image: Image.Image, output_path: Optional[str], src_mode: Optional[str] = None
35+
) -> Image.Image:
36+
if src_mode is not None:
37+
image = image.convert(src_mode)
38+
3439
if output_path is not None:
3540
if any(output_path.endswith(extension) for extension in JPEG_EXTENSIONS):
3641
image = image.convert("RGB")

augly/tests/image_tests/base_unit_test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ def evaluate_class(
9696
transform_class: Callable[..., Image.Image],
9797
fname: str,
9898
metadata_exclude_keys: Optional[List[str]] = None,
99+
check_mode: bool = True,
99100
):
100101
metadata = []
101102
bboxes, bbox_format = [(0.5, 0.5, 0.25, 0.75)], "yolo"
@@ -104,6 +105,9 @@ def evaluate_class(
104105
self.img, metadata=metadata, bboxes=bboxes, bbox_format=bbox_format
105106
)
106107

108+
if check_mode:
109+
self.assertTrue(self.img.mode == dst.mode)
110+
107111
self.assertTrue(
108112
are_equal_metadata(metadata, self.metadata[fname], metadata_exclude_keys)
109113
)

augly/tests/image_tests/transforms_unit_test.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ def test_Contrast(self):
6767
self.evaluate_class(imaugs.Contrast(), fname="contrast")
6868

6969
def test_ConvertColor(self):
70-
self.evaluate_class(imaugs.ConvertColor(mode="L"), fname="convert_color")
70+
self.evaluate_class(
71+
imaugs.ConvertColor(mode="L"),
72+
fname="convert_color",
73+
check_mode=False,
74+
)
7175

7276
def test_Crop(self):
7377
self.evaluate_class(imaugs.Crop(), fname="crop")

0 commit comments

Comments
 (0)