-
Notifications
You must be signed in to change notification settings - Fork 4
/
pre_dataset.py
189 lines (148 loc) · 6.33 KB
/
pre_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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import os.path
import random
import torchvision.transforms as transforms
from PIL import Image
from PIL import ImageFile
from torchvision.datasets.folder import make_dataset
ImageFile.LOAD_TRUNCATED_IMAGES = True
import torch
from torchvision.transforms import functional as F
import torchvision.datasets as datasets
class AlignedConcDataset(datasets.DatasetFolder):
def __init__(self, data_dir=None, transform=None, labeled=True):
self.transform = transform
self.data_dir = data_dir
self.labeled = labeled
self.target_transform=None
#self.transforms = StandardTransform(transform, target_transform)
if labeled:
self.classes = [d.name for d in os.scandir(self.data_dir) if d.is_dir()]
self.classes.sort()
self.class_to_idx = {cls_name: i for i, cls_name in enumerate(self.classes)}
self.int_to_class = dict(zip(range(len(self.classes)), self.classes))
self.imgs = make_dataset(self.data_dir, self.class_to_idx, 'png')
self.samples=self.imgs
self.root=self.data_dir
self.targets = [s[1] for s in self.samples]
def __len__(self):
return len(self.imgs)
def __getitem__(self, index):
if self.labeled:
img_path, label = self.imgs[index]
else:
img_path = self.imgs[index]
img_name = os.path.basename(img_path)
AB_conc = Image.open(img_path).convert('RGB')
# split RGB and Depth as A and B
w, h = AB_conc.size
w2 = int(w / 2)
A = AB_conc.crop((0, 0, w2, h))
B = AB_conc.crop((w2, 0, w, h))
if self.labeled:
sample = {'A': A, 'B': B, 'img_name': img_name, 'label': label}
else:
sample = {'A': A, 'B': B, 'img_name': img_name}
if self.transform:
sample = self.transform(sample)
return sample
class RandomCrop(transforms.RandomCrop):
def __call__(self, sample):
w, h = sample.size
w2 = int(w / 2)
A = sample.crop((0, 0, w2, h))
B = sample.crop((w2, 0, w, h))
if self.padding :
A = F.pad(A, self.padding)
B = F.pad(B, self.padding)
# pad the width if needed
if self.pad_if_needed and A.size[0] < self.size[1]:
A = F.pad(A, (int((1 + self.size[1] - A.size[0]) / 2), 0))
B = F.pad(B, (int((1 + self.size[1] - B.size[0]) / 2), 0))
# pad the height if needed
if self.pad_if_needed and A.size[1] < self.size[0]:
A = F.pad(A, (0, int((1 + self.size[0] - A.size[1]) / 2)))
B = F.pad(B, (0, int((1 + self.size[0] - B.size[1]) / 2)))
i, j, h, w = self.get_params(A, self.size)
A = F.crop(A, i, j, h, w)
B = F.crop(B, i, j, h, w)
target_images = Image.new('RGB', (448, 224))
target_images.paste(A, (0, 0, 224, 224))
target_images.paste(B, (224, 0, 448, 224))
return target_images
class RandomErasing(transforms.RandomErasing):
def __call__(self, sample):
A=sample[:, :, :224]
B=sample[:, :, 224:]
x, y, h, w, v = self.get_params(A, scale=self.scale, ratio=self.ratio, value=self.value)
A=F.erase(A, x, y, h, w, v, self.inplace)
B= F.erase(B, x, y, h, w, v, self.inplace)
target_images=torch.cat((A, B), dim=2)
return target_images
class RandomResizedCrop(transforms.RandomResizedCrop):
def __call__(self, sample):
w, h = sample.size
w2 = int(w / 2)
A = sample.crop((0, 0, w2, h))
B = sample.crop((w2, 0, w, h))
i, j, h, w = self.get_params(A, self.scale, self.ratio)
A=F.resized_crop(A, i, j, h, w, self.size, self.interpolation)
B=F.resized_crop(B, i, j, h, w, self.size, self.interpolation)
target_images = Image.new('RGB', (448, 224))
target_images.paste(A, (0, 0, 224, 224))
target_images.paste(B, (224, 0, 448, 224))
return target_images
class ColorJitter(transforms.ColorJitter):
def __call__(self, sample):
w, h = sample.size
w2 = int(w / 2)
A = sample.crop((0, 0, w2, h))
B = sample.crop((w2, 0, w, h))
fn_idx = torch.randperm(4)
for fn_id in fn_idx:
if fn_id == 0 and self.brightness is not None:
brightness = self.brightness
brightness_factor = torch.tensor(1.0).uniform_(brightness[0], brightness[1]).item()
A = F.adjust_brightness(A, brightness_factor)
if fn_id == 1 and self.contrast is not None:
contrast = self.contrast
contrast_factor = torch.tensor(1.0).uniform_(contrast[0], contrast[1]).item()
A = F.adjust_contrast(A, contrast_factor)
if fn_id == 2 and self.saturation is not None:
saturation = self.saturation
saturation_factor = torch.tensor(1.0).uniform_(saturation[0], saturation[1]).item()
A = F.adjust_saturation(A, saturation_factor)
if fn_id == 3 and self.hue is not None:
hue = self.hue
hue_factor = torch.tensor(1.0).uniform_(hue[0], hue[1]).item()
A = F.adjust_hue(A, hue_factor)
target_images = Image.new('RGB', (448, 224))
target_images.paste(A, (0, 0, 224, 224))
target_images.paste(B, (224, 0, 448, 224))
return target_images
class CenterCrop(transforms.CenterCrop):
def __call__(self, sample):
A, B = sample['A'], sample['B']
sample['A'] = F.center_crop(A, self.size)
sample['B'] = F.center_crop(B, self.size)
return sample
class RandomHorizontalFlip(transforms.RandomHorizontalFlip):
def __call__(self, sample):
w, h = sample.size
w2 = int(w / 2)
A = sample.crop((0, 0, w2, h))
B = sample.crop((w2, 0, w, h))
if random.random() > 0.5:
A = F.hflip(A)
B = F.hflip(B)
target_images = Image.new('RGB', (448, 224))
target_images.paste(A, (0, 0, 224, 224))
target_images.paste(B, (224, 0, 448, 224))
return target_images
class Resize(transforms.Resize):
def __call__(self, sample):
A, B = sample['A'], sample['B']
h = self.size[0]
w = self.size[1]
sample['A'] = F.resize(A, (h, w))
sample['B'] = F.resize(B, (h, w))
return sample