-
Notifications
You must be signed in to change notification settings - Fork 40
/
dataset.py
167 lines (136 loc) · 6.17 KB
/
dataset.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import os
import torch.utils.data as data
import torchvision.transforms as transforms
import numpy as np
from PIL import Image
class Resize(object):
def __init__(self, size):
self.size = size
def __call__(self, sample):
img, mask = sample['image'], sample['mask']
img, mask = img.resize((self.size, self.size), resample=Image.BILINEAR), mask.resize((self.size, self.size),
resample=Image.BILINEAR)
return {'image': img, 'mask': mask}
class RandomCrop(object):
def __init__(self, size):
self.size = size
def __call__(self, sample):
img, mask = sample['image'], sample['mask']
img, mask = img.resize((256, 256), resample=Image.BILINEAR), mask.resize((256, 256), resample=Image.BILINEAR)
h, w = img.size
new_h, new_w = self.size, self.size
top = np.random.randint(0, h - new_h)
left = np.random.randint(0, w - new_w)
img = img.crop((left, top, left + new_w, top + new_h))
mask = mask.crop((left, top, left + new_w, top + new_h))
return {'image': img, 'mask': mask}
class RandomFlip(object):
def __init__(self, prob):
self.prob = prob
self.flip = transforms.RandomHorizontalFlip(1.)
def __call__(self, sample):
if np.random.random_sample() < self.prob:
img, mask = sample['image'], sample['mask']
img = self.flip(img)
mask = self.flip(mask)
return {'image': img, 'mask': mask}
else:
return sample
class ToTensor(object):
def __init__(self):
self.tensor = transforms.ToTensor()
def __call__(self, sample):
img, mask = sample['image'], sample['mask']
img, mask = self.tensor(img), self.tensor(mask)
return {'image': img, 'mask': mask}
class DUTSDataset(data.Dataset):
def __init__(self, root_dir, train=True, data_augmentation=True):
self.root_dir = root_dir
self.train = train
self.image_list = sorted(os.listdir('{}/DUTS-{}-Image'.format(root_dir, 'TR' if train else 'TE')))
self.mask_list = sorted(os.listdir('{}/DUTS-{}-Mask'.format(root_dir, 'TR' if train else 'TE')))
self.transform = transforms.Compose(
[RandomFlip(0.5),
RandomCrop(224),
ToTensor()])
if not (train and data_augmentation):
self.transform = transforms.Compose([Resize(224), ToTensor()])
self.root_dir = root_dir
self.train = train
self.data_augmentation = data_augmentation
def arrange(self):
flag = True
if len(self.image_list) > len(self.mask_list):
for image in self.image_list:
for mask in self.mask_list:
if image.split("Image")[-1].split(".")[-2] == mask.split("Mask")[-1].split(".")[-2]:
print(image.split("Image")[-1].split(".")[-2])
flag = False
if flag:
print(image + ' Deleted')
os.remove('{}/DUTS-{}-Image/{}'.format(self.root_dir, 'TR' if self.train else 'TE', image))
else:
for mask in self.mask_list:
for image in self.image_list:
if image.split("Image")[-1].split(".")[-2] == mask.split("Mask")[-1].split(".")[-2]:
print(image.split("Image")[-1].split(".")[-2])
flag = False
if flag:
print(mask + ' Deleted')
os.remove('{}/DUTS-{}-Mask/{}'.format(self.root_dir, 'TR' if self.train else 'TE', mask))
self.image_list = sorted(os.listdir('{}/DUTS-{}-Image'.format(self.root_dir, 'TR' if self.train else 'TE')))
self.mask_list = sorted(os.listdir('{}/DUTS-{}-Mask'.format(self.root_dir, 'TR' if self.train else 'TE')))
def __len__(self):
return len(self.image_list)
def __getitem__(self, item):
img_name = '{}/DUTS-{}-Image/{}'.format(self.root_dir, 'TR' if self.train else 'TE', self.image_list[item])
mask_name = '{}/DUTS-{}-Mask/{}'.format(self.root_dir, 'TR' if self.train else 'TE', self.mask_list[item])
img = Image.open(img_name)
mask = Image.open(mask_name)
img = img.convert('RGB')
mask = mask.convert('L')
sample = {'image': img, 'mask': mask}
sample = self.transform(sample)
return sample
class PairDataset(data.Dataset):
def __init__(self, root_dir, train=True, data_augmentation=True):
self.root_dir = root_dir
self.train = train
self.image_list = sorted(os.listdir(os.path.join(root_dir, 'images')))
self.mask_list = sorted(os.listdir(os.path.join(root_dir, 'masks')))
self.transform = transforms.Compose(
[RandomFlip(0.5),
RandomCrop(224),
ToTensor()])
if not (train and data_augmentation):
self.transform = transforms.Compose([Resize(224), ToTensor()])
self.root_dir = root_dir
self.data_augmentation = data_augmentation
def __len__(self):
return len(self.image_list)
def __getitem__(self, item):
img_name = os.path.join(self.root_dir, 'images', self.image_list[item])
mask_name = os.path.join(self.root_dir, 'masks', self.mask_list[item])
img = Image.open(img_name)
mask = Image.open(mask_name)
img = img.convert('RGB')
mask = mask.convert('L')
sample = {'image': img, 'mask': mask}
sample = self.transform(sample)
return sample
class CustomDataset(data.Dataset):
def __init__(self, root_dir):
self.image_list = sorted(os.listdir(root_dir))
self.transform = transforms.Compose([transforms.Resize((224, 224)), transforms.ToTensor()])
self.root_dir = root_dir
def __len__(self):
return len(self.image_list)
def __getitem__(self, item):
img_name = '{}/{}'.format(self.root_dir, self.image_list[item])
img = Image.open(img_name)
sample = img.convert('RGB')
sample = self.transform(sample)
return sample
if __name__ == '__main__':
ds = DUTSDataset('../DUTS-TR')
ds.arrange()