-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataset.py
125 lines (92 loc) · 3.85 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
import torch.utils.data as data
import os
from os import listdir
from os.path import join
from PIL import Image, ImageOps
import random
def is_image_file(filename):
return any(filename.endswith(extension) for extension in [".png", ".jpg", ".bmp"])
def load_img(filepath):
img = Image.open(filepath).convert('RGB')
return img
def rescale_img(img_in, scale):
size_in = img_in.size
new_size_in = tuple([int(x * scale) for x in size_in])
img_in = img_in.resize(new_size_in, resample=Image.BICUBIC)
return img_in
def get_patch(img_in, img_tar, patch_size, scale=1, ix=-1, iy=-1):
(ih, iw) = img_in.size
patch_mult = scale
tp = patch_mult * patch_size
ip = tp // scale
if ix == -1:
ix = random.randrange(0, iw - ip + 1)
if iy == -1:
iy = random.randrange(0, ih - ip + 1)
(tx, ty) = (scale * ix, scale * iy)
img_in = img_in.crop((iy, ix, iy + ip, ix + ip))
img_tar = img_tar.crop((ty, tx, ty + tp, tx + tp))
info_patch = {
'ix': ix, 'iy': iy, 'ip': ip, 'tx': tx, 'ty': ty, 'tp': tp}
return img_in, img_tar, info_patch
def augment(img_in, img_tar, flip_h=True, rot=True):
info_aug = {'flip_h': False, 'flip_v': False, 'trans': False}
if random.random() < 0.5 and flip_h:
img_in = ImageOps.flip(img_in)
img_tar = ImageOps.flip(img_tar)
info_aug['flip_h'] = True
if rot:
if random.random() < 0.5:
img_in = ImageOps.mirror(img_in)
img_tar = ImageOps.mirror(img_tar)
info_aug['flip_v'] = True
if random.random() < 0.5:
img_in = img_in.rotate(180)
img_tar = img_tar.rotate(180)
info_aug['trans'] = True
return img_in, img_tar, info_aug
class DatasetFromFolder(data.Dataset):
def __init__(self, data_dir, label_dir, patch_size, data_augmentation, transform=None):
super(DatasetFromFolder, self).__init__()
data_filenames = [join(data_dir, x) for x in listdir(data_dir) if is_image_file(x)]
data_filenames.sort()
self.data_filenames = data_filenames
label_filenames = [join(label_dir, x) for x in listdir(label_dir) if is_image_file(x)]
label_filenames.sort()
self.label_filenames = label_filenames
self.patch_size = patch_size
self.transform = transform
self.data_augmentation = data_augmentation
def __getitem__(self, index):
target = load_img(self.label_filenames[index])
input = load_img(self.data_filenames[index])
_, file = os.path.split(self.label_filenames[index])
input, target, _ = get_patch(input, target, self.patch_size)
if self.data_augmentation:
input, target, _ = augment(input, target)
if self.transform:
input = self.transform(input)
target = self.transform(target)
return input, target, file
def __len__(self):
return len(self.label_filenames)
class DatasetFromFolderEval(data.Dataset):
def __init__(self, data_dir, label_dir, transform=None):
super(DatasetFromFolderEval, self).__init__()
data_filenames = [join(data_dir, x) for x in listdir(data_dir) if is_image_file(x)]
data_filenames.sort()
self.data_filenames = data_filenames
label_filenames = [join(label_dir, x) for x in listdir(label_dir) if is_image_file(x)]
label_filenames.sort()
self.label_filenames = label_filenames
self.transform = transform
def __getitem__(self, index):
target = load_img(self.label_filenames[index])
input = load_img(self.data_filenames[index])
_, file = os.path.split(self.label_filenames[index])
if self.transform:
input = self.transform(input)
target = self.transform(target)
return input, target, file
def __len__(self):
return len(self.label_filenames)