From 4e59ab889c169b7a36d34d1fe11107768239b730 Mon Sep 17 00:00:00 2001 From: izikgo Date: Sun, 8 Jan 2023 21:32:42 +0000 Subject: [PATCH 1/6] fix channel dim selection on segmentation target --- flash/image/segmentation/input.py | 2 +- tests/image/segmentation/test_data.py | 76 +++++++++++++++++++++++++-- 2 files changed, 73 insertions(+), 5 deletions(-) diff --git a/flash/image/segmentation/input.py b/flash/image/segmentation/input.py index 7d8455ae63..cad5f93730 100644 --- a/flash/image/segmentation/input.py +++ b/flash/image/segmentation/input.py @@ -94,7 +94,7 @@ def load_data( def load_sample(self, sample: Dict[str, Any]) -> Dict[str, Any]: if DataKeys.TARGET in sample: - sample[DataKeys.TARGET] = np.array(load_image(sample[DataKeys.TARGET])).transpose((2, 0, 1))[:, :, 0] + sample[DataKeys.TARGET] = np.array(load_image(sample[DataKeys.TARGET])).transpose((2, 0, 1))[0, :, :] return super().load_sample(sample) diff --git a/tests/image/segmentation/test_data.py b/tests/image/segmentation/test_data.py index 81d6e55a32..ecdd6550fa 100644 --- a/tests/image/segmentation/test_data.py +++ b/tests/image/segmentation/test_data.py @@ -1,6 +1,6 @@ import os from pathlib import Path -from typing import Dict, List, Tuple +from typing import Callable, Dict, List, Tuple import numpy as np import pytest @@ -8,6 +8,9 @@ from flash import Trainer from flash.core.data.io.input import DataKeys +from flash.core.data.io.input_transform import InputTransform +from flash.core.data.transforms import ApplyToKeys +from flash.core.data.utilities.loading import load_image from flash.core.utilities.imports import ( _FIFTYONE_AVAILABLE, _IMAGE_AVAILABLE, @@ -16,6 +19,7 @@ _PIL_AVAILABLE, ) from flash.image import SemanticSegmentation, SemanticSegmentationData +from flash.image.segmentation.input import SemanticSegmentationFilesInput if _PIL_AVAILABLE: from PIL import Image @@ -43,12 +47,22 @@ def _rand_labels(size: Tuple[int, int], num_classes: int): return Image.fromarray(data.astype(np.uint8)) -def create_random_data(image_files: List[str], label_files: List[str], size: Tuple[int, int], num_classes: int): +def create_random_data( + image_files: List[str], label_files: List[str], size: Tuple[int, int], num_classes: int +) -> Tuple[List[Image.Image], List[Image.Image]]: + imgs = [] for img_file in image_files: - _rand_image(size).save(img_file) + img = _rand_image(size) + img.save(img_file) + imgs.append(img) + labels = [] for label_file in label_files: - _rand_labels(size, num_classes).save(label_file) + label = _rand_labels(size, num_classes) + label.save(label_file) + labels.append(label) + + return imgs, labels class TestSemanticSegmentationData: @@ -58,6 +72,60 @@ def test_smoke(): dm = SemanticSegmentationData(batch_size=1) assert dm is not None + @staticmethod + @pytest.mark.skipif(not _IMAGE_TESTING, reason="image libraries aren't installed.") + def test_identity(tmpdir): + class IdentityTransform(InputTransform): + def per_sample_transform(self) -> Callable: + return ApplyToKeys( + DataKeys.INPUT, + np.array, + ) + + def per_batch_transform(self) -> Callable: + return lambda x: x + + tmp_dir = Path(tmpdir) + + # create random dummy data + + os.makedirs(str(tmp_dir / "images")) + os.makedirs(str(tmp_dir / "targets")) + + images = [ + str(tmp_dir / "images" / "img1.png"), + ] + + targets = [ + str(tmp_dir / "targets" / "img1.png"), + ] + + num_classes: int = 2 + img_size: Tuple[int, int] = (128, 128) + images_data, targets_data = create_random_data(images, targets, img_size, num_classes) + + # instantiate the data module + + dm = SemanticSegmentationData.from_files( + test_files=images, + test_targets=targets, + batch_size=1, + num_workers=0, + num_classes=num_classes, + transform=IdentityTransform(), + ) + + assert dm is not None + assert dm.test_dataloader() is not None + + # check test data + data = next(iter(dm.test_dataloader())) + imgs, labels = data[DataKeys.INPUT], data[DataKeys.TARGET] + assert imgs.shape == (1, 128, 128, 3) + assert labels.shape == (1, 128, 128) + assert torch.allclose(imgs, torch.from_numpy(np.array(images_data[0]))) + assert torch.allclose(labels, torch.from_numpy(np.array(targets_data[0]))[:, :, 0]) + @staticmethod @pytest.mark.skipif(not _IMAGE_TESTING, reason="image libraries aren't installed.") def test_from_folders(tmpdir): From ee211bd871751232f4161c38ccef93d5e27771f5 Mon Sep 17 00:00:00 2001 From: Jirka Borovec <6035284+Borda@users.noreply.github.com> Date: Mon, 9 Jan 2023 07:28:26 +0100 Subject: [PATCH 2/6] Apply suggestions from code review --- tests/image/segmentation/test_data.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/image/segmentation/test_data.py b/tests/image/segmentation/test_data.py index ecdd6550fa..3bc3d8ffbf 100644 --- a/tests/image/segmentation/test_data.py +++ b/tests/image/segmentation/test_data.py @@ -93,11 +93,11 @@ def per_batch_transform(self) -> Callable: os.makedirs(str(tmp_dir / "targets")) images = [ - str(tmp_dir / "images" / "img1.png"), + str(tmp_dir / "images" / "img1.png") ] targets = [ - str(tmp_dir / "targets" / "img1.png"), + str(tmp_dir / "targets" / "img1.png") ] num_classes: int = 2 From cf65734aa7cecd696104d9e4223f4f5075f2c205 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 9 Jan 2023 06:28:55 +0000 Subject: [PATCH 3/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/image/segmentation/test_data.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/tests/image/segmentation/test_data.py b/tests/image/segmentation/test_data.py index 3bc3d8ffbf..7d5cc5093c 100644 --- a/tests/image/segmentation/test_data.py +++ b/tests/image/segmentation/test_data.py @@ -92,13 +92,9 @@ def per_batch_transform(self) -> Callable: os.makedirs(str(tmp_dir / "images")) os.makedirs(str(tmp_dir / "targets")) - images = [ - str(tmp_dir / "images" / "img1.png") - ] + images = [str(tmp_dir / "images" / "img1.png")] - targets = [ - str(tmp_dir / "targets" / "img1.png") - ] + targets = [str(tmp_dir / "targets" / "img1.png")] num_classes: int = 2 img_size: Tuple[int, int] = (128, 128) From f515ed7e4ff64f83e5c62df14e3a4e4687f2b1b3 Mon Sep 17 00:00:00 2001 From: izikgo Date: Mon, 9 Jan 2023 07:49:46 +0000 Subject: [PATCH 4/6] fix pep8 unused imports --- tests/image/segmentation/test_data.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/image/segmentation/test_data.py b/tests/image/segmentation/test_data.py index 7d5cc5093c..74f1d75a02 100644 --- a/tests/image/segmentation/test_data.py +++ b/tests/image/segmentation/test_data.py @@ -10,7 +10,6 @@ from flash.core.data.io.input import DataKeys from flash.core.data.io.input_transform import InputTransform from flash.core.data.transforms import ApplyToKeys -from flash.core.data.utilities.loading import load_image from flash.core.utilities.imports import ( _FIFTYONE_AVAILABLE, _IMAGE_AVAILABLE, @@ -19,7 +18,6 @@ _PIL_AVAILABLE, ) from flash.image import SemanticSegmentation, SemanticSegmentationData -from flash.image.segmentation.input import SemanticSegmentationFilesInput if _PIL_AVAILABLE: from PIL import Image From e38ddf0876ebc65d589111f9a4ad01767f4e954f Mon Sep 17 00:00:00 2001 From: Jirka Date: Thu, 11 May 2023 14:51:49 +0200 Subject: [PATCH 5/6] update dependabot --- .github/dependabot.yml | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 0cef51216f..6a316f6219 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -1,14 +1,38 @@ -# https://docs.github.com/en/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file +# Basic dependabot.yml file with minimum configuration for two package managers + version: 2 updates: + # Enable version updates for python + - package-ecosystem: "pip" + # Look for a `requirements` in the `root` directory + directory: "/" + # Check for updates once a week + schedule: + interval: "monthly" + # Labels on pull requests for version updates only + labels: ["enhancement"] + pull-request-branch-name: + # Separate sections of the branch name with a hyphen + # for example, `dependabot-npm_and_yarn-next_js-acorn-6.4.1` + separator: "-" + # Allow up to 5 open pull requests for pip dependencies + open-pull-requests-limit: 10 + reviewers: + - "Lightning-Universe/engs" + + # Enable version updates for GitHub Actions - package-ecosystem: "github-actions" directory: "/" + # Check for updates once a week schedule: - interval: "weekly" - labels: - - "tests / CI" + interval: "monthly" + # Labels on pull requests for version updates only + labels: ["tests / CI"] pull-request-branch-name: + # Separate sections of the branch name with a hyphen + # for example, `dependabot-npm_and_yarn-next_js-acorn-6.4.1` separator: "-" + # Allow up to 5 open pull requests for GitHub Actions open-pull-requests-limit: 5 reviewers: - - "Lightning-AI/core-flash" + - "Lightning-Universe/engs" \ No newline at end of file From e4695a14b5dd84663da27cc7e11d3ead00a7a7fd Mon Sep 17 00:00:00 2001 From: Jirka Date: Thu, 11 May 2023 14:57:36 +0200 Subject: [PATCH 6/6] ... --- tests/image/semantic_segm/test_data.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/image/semantic_segm/test_data.py b/tests/image/semantic_segm/test_data.py index 83032914fd..aae5129c90 100644 --- a/tests/image/semantic_segm/test_data.py +++ b/tests/image/semantic_segm/test_data.py @@ -71,7 +71,7 @@ def test_smoke(): assert dm is not None @staticmethod - @pytest.mark.skipif(not _IMAGE_TESTING, reason="image libraries aren't installed.") + @pytest.mark.skipif(not _TOPIC_IMAGE_AVAILABLE, reason="image libraries aren't installed.") def test_identity(tmpdir): class IdentityTransform(InputTransform): def per_sample_transform(self) -> Callable: