-
Notifications
You must be signed in to change notification settings - Fork 6
/
datasets.py
149 lines (111 loc) · 4.45 KB
/
datasets.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
import glob
import json
import random
import numpy as np
import torch
from PIL import Image
def norm(image):
return (image / 127.5) - 1.0
def denorm(image):
return (image + 1.0) * 127.5
def augment(dt_im, eh_im):
# Random interpolation
a = random.random()
dt_im = dt_im * a + eh_im * (1 - a)
# Random flip left right
if random.random() < 0.25:
dt_im = np.fliplr(dt_im)
eh_im = np.fliplr(eh_im)
# Random flip up down
if random.random() < 0.25:
dt_im = np.flipud(dt_im)
eh_im = np.flipud(eh_im)
return dt_im, eh_im
class PairDataset(torch.utils.data.Dataset):
def __init__(self, data_root, im_size, split):
super(PairDataset, self).__init__()
self.data_root = data_root
self.im_size = im_size
self.split = split
# Load JSON of splits
names = json.load(open(f"{self.data_root}/splits.json", "r"))[self.split]
# Build image paths
self.dt_ims = [f"{self.data_root}/trainA/{n}" for n in names]
self.eh_ims = [f"{self.data_root}/trainB/{n}" for n in names]
print(f"Total {len(self.dt_ims)} data")
def __getitem__(self, index):
# Read and resize image pair
dt_im = Image.open(self.dt_ims[index]).convert("RGB")
eh_im = Image.open(self.eh_ims[index]).convert("RGB")
dt_im = dt_im.resize(self.im_size)
eh_im = eh_im.resize(self.im_size)
# Transfrom image pair to float32 np.ndarray
dt_im = np.array(dt_im, dtype=np.float32)
eh_im = np.array(eh_im, dtype=np.float32)
# Augment image pair
if self.split == "train":
dt_im, eh_im = augment(dt_im, eh_im)
# Transfrom image pair to (C, H, W) torch.Tensor
dt_im = torch.Tensor(norm(dt_im)).permute(2, 0, 1)
eh_im = torch.Tensor(norm(eh_im)).permute(2, 0, 1)
return dt_im, eh_im
def __len__(self):
return len(self.dt_ims)
class UnpairDataset(torch.utils.data.Dataset):
def __init__(self, data_root, im_size, split):
super(UnpairDataset, self).__init__()
self.data_root = data_root
self.im_size = im_size
self.split = split
# Load JSON of splits
names = json.load(open(f"{self.data_root}/splits.json", "r"))[self.split]
# Build image paths
self.dt_ims = [f"{self.data_root}/{n}" for n in names if "trainA" in n]
self.eh_ims = [f"{self.data_root}/{n}" for n in names if "trainB" in n]
print(f"Total {len(self.dt_ims)} poor quality data")
print(f"Total {len(self.eh_ims)} good quality data")
# Force # of images to the least amount
num = min(len(self.dt_ims), len(self.eh_ims))
self.dt_ims = self.dt_ims[:num]
self.eh_ims = self.eh_ims[:num]
print(f"Total {len(self.eh_ims)} data used")
def __getitem__(self, index):
# Read and resize image pair
dt_im = Image.open(self.dt_ims[index]).convert("RGB")
eh_im = Image.open(self.eh_ims[index]).convert("RGB")
dt_im = dt_im.resize(self.im_size)
eh_im = eh_im.resize(self.im_size)
# Transfrom image pair to float32 np.ndarray
dt_im = np.array(dt_im, dtype=np.float32)
eh_im = np.array(eh_im, dtype=np.float32)
# Augment image pair
if self.split == "train":
dt_im, eh_im = augment(dt_im, eh_im)
# Transfrom image pair to (C, H, W) torch.Tensor
dt_im = torch.Tensor(norm(dt_im)).permute(2, 0, 1)
eh_im = torch.Tensor(norm(eh_im)).permute(2, 0, 1)
return dt_im, eh_im
def __len__(self):
return len(self.dt_ims)
class TestDataset(torch.utils.data.Dataset):
def __init__(self, data_root, im_size):
super(TestDataset, self).__init__()
self.data_root = data_root
self.im_size = im_size
self.ims = glob.glob(f"{self.data_root}/*")
def __getitem__(self, index):
# Read and resize image
path = self.ims[index]
im = Image.open(path).convert("RGB")
im = im.resize(self.im_size)
# Transfrom image to float32 np.ndarray
im = np.array(im, dtype=np.float32)
# Transfrom image to (C, H, W) torch.Tensor
im = torch.Tensor(norm(im)).permute(2, 0, 1)
return path, im
def __len__(self):
return len(self.ims)
if __name__ == "__main__":
dataset = PairDataset(
data_root="../data/EUVP Dataset/Paired/underwater_dark", im_size=(256, 256))
image, target = dataset[0]