-
Notifications
You must be signed in to change notification settings - Fork 0
/
datahandler.py
135 lines (118 loc) · 4.49 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
from pathlib import Path
import numpy as np
from torch.utils.data import DataLoader
from torchvision import transforms
import torch
from segdataset import SegmentationDataset
class normalize():
def __init__(self, mean = (0.485, 0.456, 0.406), std = (0.229, 0.224, 0.225)):
self.mean = mean
self.std = std
def __call__(self, img):
imgarr = np.asarray(img)
proc_img = np.empty_like(imgarr, np.float32)
proc_img[..., 0] = (imgarr[..., 0] / 255. - self.mean[0]) / self.std[0]
proc_img[..., 1] = (imgarr[..., 1] / 255. - self.mean[1]) / self.std[1]
proc_img[..., 2] = (imgarr[..., 2] / 255. - self.mean[2]) / self.std[2]
return proc_img
def HWC_to_CHW(tensor, sal=False):
print(np.shape(tensor))
if sal:
tensor = np.expand_dims(tensor, axis=0)
else:
tensor = tensor.transpose(0, 1).transpose(0, 2).contiguous()
return tensor
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.
"""
tf_list = []
tf_list.append(normalize())
tf_list.append(HWC_to_CHW)
tf_list.append(torch.from_numpy)
data_transforms = transforms.Compose(tf_list)
image_datasets = {
x: SegmentationDataset(root=Path(data_dir) / x,
transforms=data_transforms,
image_folder=image_folder,
mask_folder=mask_folder,
coating="all")
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,
coating: str,
image_folder: str = 'Images',
mask_folder: str = 'Masks',
fraction: float = 0.1,
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.1.
batch_size (int, optional): Dataloader batch size. Defaults to 4.
Returns:
dataloaders: Returns dataloaders dictionary containing the
Train and Test dataloaders.
"""
# tf_list = []
# tf_list.append(HWC_to_CHW)
# tf_list.append(normalize())
# tf_list.append(torch.from_numpy)
data_transforms = transforms.Compose([transforms.ToTensor()])
image_datasets = {
x: SegmentationDataset(data_dir,
image_folder=image_folder,
mask_folder=mask_folder,
coating=coating,
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