Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

Add semantic segmentation task #239

Merged
merged 58 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
e2f5f20
semantic segmentation skeleton
edgarriba Apr 22, 2021
f3ce4c7
expose and add smoke tests for preproces and datamodule
edgarriba Apr 23, 2021
1ef1b40
data module connections working
edgarriba Apr 23, 2021
7f17fb2
preprocess not crashing(wip)
edgarriba Apr 23, 2021
7d9d46c
implement segmentation sequential
edgarriba Apr 26, 2021
498e278
implement torchvision backbone model
edgarriba Apr 26, 2021
56fa4d5
model working
edgarriba Apr 26, 2021
950252e
implement labels mapping
edgarriba Apr 26, 2021
6a75245
add map labels tests
edgarriba Apr 26, 2021
7a7f855
from filepaths training test not crashing
edgarriba Apr 26, 2021
def1ea0
non working visualiser
edgarriba Apr 27, 2021
ed17eb0
fix visualiser
edgarriba Apr 27, 2021
3eb6417
training working
edgarriba Apr 27, 2021
d529d9e
training not crashing
edgarriba Apr 27, 2021
13095e6
cleanup example and move serializer to core
edgarriba Apr 28, 2021
2f9ede5
cleanup model code, tests and docs
edgarriba Apr 28, 2021
dc9b2b8
move transforms apart
edgarriba Apr 28, 2021
e767a53
implement ApplytransformsToKey augmentations
edgarriba Apr 28, 2021
f268b62
relative path
edgarriba Apr 28, 2021
99b99f0
fix load from pretrained and add resnet 101
edgarriba Apr 28, 2021
d1a91fd
create segmentation keys enum
edgarriba Apr 28, 2021
7343887
sync with master and fix val_split
edgarriba Apr 28, 2021
febe7f0
move apart segmentation backbones
edgarriba Apr 29, 2021
3891920
Merge branch 'master' into feat/segmentation
edgarriba Apr 29, 2021
248145b
fix tests
edgarriba Apr 29, 2021
6d635db
fix tests
edgarriba Apr 29, 2021
ca97034
fix tests
edgarriba Apr 29, 2021
da83251
fix memory leak issues
edgarriba Apr 29, 2021
f1e76f9
Merge branch 'master' into feat/segmentation
edgarriba Apr 29, 2021
2ef8c88
undo function filtering
edgarriba Apr 29, 2021
87a92f3
fix import
edgarriba Apr 29, 2021
73d462b
more fixes for memory leaks
edgarriba Apr 29, 2021
8b971a4
add segmentation to docs
edgarriba Apr 30, 2021
69358a6
add inference example
edgarriba Apr 30, 2021
caabfb6
add image to docs and update with AdamW
edgarriba Apr 30, 2021
78301ff
Merge branch 'master' into feat/segmentation
ethanwharris May 7, 2021
e8e92d1
Make pretrained arg kwarg
ethanwharris May 7, 2021
cf430f3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 7, 2021
74ce6dc
Data sources initial commit
ethanwharris May 7, 2021
df2b989
Update transforms
ethanwharris May 7, 2021
bb95b8f
Updates
ethanwharris May 7, 2021
3a8842d
Fixes
ethanwharris May 8, 2021
3596e16
Fix tests
ethanwharris May 8, 2021
859a0ef
Fixes
ethanwharris May 8, 2021
268b4c8
Fixes
ethanwharris May 8, 2021
0b67769
Merge branch 'master' into feat/segmentation
ethanwharris May 8, 2021
091e50d
Merge branch 'master' into feat/segmentation
ethanwharris May 10, 2021
4aa3716
Add tests
ethanwharris May 10, 2021
f50c200
Update docs/source/reference/semantic_segmentation.rst
ethanwharris May 10, 2021
11ed7c5
Update docs/source/reference/semantic_segmentation.rst
ethanwharris May 10, 2021
0fc3581
Add a check
ethanwharris May 10, 2021
1875967
Move KorniaParallelTransforms and add docstring
ethanwharris May 10, 2021
e9dee30
implement quick test for segmentation labels
edgarriba May 10, 2021
0049197
add small assertion tests
edgarriba May 10, 2021
4c75774
Rename test_serialisation.py to test_serialization.py
ethanwharris May 10, 2021
2d76f37
Switch to exception
ethanwharris May 10, 2021
5f254b6
Fix
ethanwharris May 10, 2021
8745191
Fixes
ethanwharris May 10, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions flash/vision/segmentation/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,20 @@ def load_data(self, data: Union[Tuple[str, str], Tuple[List[str], List[str]]]) -

input_data = input_files
target_data = target_files
else:
if not isinstance(input_data, list) and not isinstance(target_data, list):
input_data = [input_data]
target_data = [target_data]

if not isinstance(input_data, list) and not isinstance(target_data, list):
input_data = [input_data]
target_data = [target_data]
if len(input_data) != len(target_data):
rank_zero_warn(
ethanwharris marked this conversation as resolved.
Show resolved Hide resolved
f"The number of input files ({len(input_data)}) and number of target files ({len(target_data)}) are"
" different. Some files have been dropped.",
UserWarning,
)
length = min(len(input_data), len(target_data))
input_data = input_data[:length]
target_data = target_data[:length]

data = filter(
lambda sample: (
Expand Down
57 changes: 39 additions & 18 deletions tests/vision/segmentation/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,36 +140,18 @@ def test_from_folders_warning(self, tmpdir):
dm = SemanticSegmentationData.from_folders(
train_folder=str(tmp_dir / "images"),
train_target_folder=str(tmp_dir / "targets"),
val_folder=str(tmp_dir / "images"),
val_target_folder=str(tmp_dir / "targets"),
test_folder=str(tmp_dir / "images"),
test_target_folder=str(tmp_dir / "targets"),
batch_size=1,
num_workers=0,
)
assert dm is not None
assert dm.train_dataloader() is not None
assert dm.val_dataloader() is not None
assert dm.test_dataloader() is not None

# check training data
data = next(iter(dm.train_dataloader()))
imgs, labels = data[DefaultDataKeys.INPUT], data[DefaultDataKeys.TARGET]
assert imgs.shape == (1, 3, 196, 196)
assert labels.shape == (1, 196, 196)

# check val data
data = next(iter(dm.val_dataloader()))
imgs, labels = data[DefaultDataKeys.INPUT], data[DefaultDataKeys.TARGET]
assert imgs.shape == (1, 3, 196, 196)
assert labels.shape == (1, 196, 196)

# check test data
data = next(iter(dm.test_dataloader()))
imgs, labels = data[DefaultDataKeys.INPUT], data[DefaultDataKeys.TARGET]
assert imgs.shape == (1, 3, 196, 196)
assert labels.shape == (1, 196, 196)

def test_from_files(self, tmpdir):
tmp_dir = Path(tmpdir)

Expand Down Expand Up @@ -226,6 +208,45 @@ def test_from_files(self, tmpdir):
assert imgs.shape == (2, 3, 196, 196)
assert labels.shape == (2, 196, 196)

def test_from_files_warning(self, tmpdir):
tmp_dir = Path(tmpdir)

# create random dummy data

images = [
str(tmp_dir / "img1.png"),
str(tmp_dir / "img2.png"),
str(tmp_dir / "img3.png"),
]

targets = [
str(tmp_dir / "labels_img1.png"),
str(tmp_dir / "labels_img2.png"),
str(tmp_dir / "labels_img3.png"),
]

num_classes: int = 2
img_size: Tuple[int, int] = (196, 196)
create_random_data(images, targets, img_size, num_classes)

# instantiate the data module

with pytest.warns(UserWarning, match="The number of input files"):
dm = SemanticSegmentationData.from_files(
train_files=images,
train_targets=targets + [str(tmp_dir / "labels_img4.png")],
batch_size=2,
num_workers=0,
)
assert dm is not None
assert dm.train_dataloader() is not None

# check training data
data = next(iter(dm.train_dataloader()))
imgs, labels = data[DefaultDataKeys.INPUT], data[DefaultDataKeys.TARGET]
assert imgs.shape == (2, 3, 196, 196)
assert labels.shape == (2, 196, 196)

def test_map_labels(self, tmpdir):
tmp_dir = Path(tmpdir)

Expand Down