-
Notifications
You must be signed in to change notification settings - Fork 100
/
datahandler.py
99 lines (87 loc) · 3.37 KB
/
datahandler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from pathlib import Path
from torch.utils.data import DataLoader
from torchvision import transforms
from segdataset import SegmentationDataset
def get_dataloader_sep_folder(data_dir: str,
image_folder: str = 'Image',
mask_folder: str = 'Mask',
batch_size: int = 4):
""" Create Train and Test dataloaders from two
separate Train and Test folders.
The directory structure should be as follows.
data_dir
--Train
------Image
---------Image1
---------ImageN
------Mask
---------Mask1
---------MaskN
--Test
------Image
---------Image1
---------ImageM
------Mask
---------Mask1
---------MaskM
Args:
data_dir (str): The data directory or root.
image_folder (str, optional): Image folder name. Defaults to 'Image'.
mask_folder (str, optional): Mask folder name. Defaults to 'Mask'.
batch_size (int, optional): Batch size of the dataloader. Defaults to 4.
Returns:
dataloaders: Returns dataloaders dictionary containing the
Train and Test dataloaders.
"""
data_transforms = transforms.Compose([transforms.ToTensor()])
image_datasets = {
x: SegmentationDataset(root=Path(data_dir) / x,
transforms=data_transforms,
image_folder=image_folder,
mask_folder=mask_folder)
for x in ['Train', 'Test']
}
dataloaders = {
x: DataLoader(image_datasets[x],
batch_size=batch_size,
shuffle=True,
num_workers=8)
for x in ['Train', 'Test']
}
return dataloaders
def get_dataloader_single_folder(data_dir: str,
image_folder: str = 'Images',
mask_folder: str = 'Masks',
fraction: float = 0.2,
batch_size: int = 4):
"""Create train and test dataloader from a single directory containing
the image and mask folders.
Args:
data_dir (str): Data directory path or root
image_folder (str, optional): Image folder name. Defaults to 'Images'.
mask_folder (str, optional): Mask folder name. Defaults to 'Masks'.
fraction (float, optional): Fraction of Test set. Defaults to 0.2.
batch_size (int, optional): Dataloader batch size. Defaults to 4.
Returns:
dataloaders: Returns dataloaders dictionary containing the
Train and Test dataloaders.
"""
data_transforms = transforms.Compose([transforms.ToTensor()])
image_datasets = {
x: SegmentationDataset(data_dir,
image_folder=image_folder,
mask_folder=mask_folder,
seed=100,
fraction=fraction,
subset=x,
transforms=data_transforms)
for x in ['Train', 'Test']
}
dataloaders = {
x: DataLoader(image_datasets[x],
batch_size=batch_size,
shuffle=True,
num_workers=8)
for x in ['Train', 'Test']
}
return dataloaders