-
Notifications
You must be signed in to change notification settings - Fork 3
/
dataset.py
126 lines (117 loc) · 3.66 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
import os
import os.path
import numpy as np
import random
import h5py
import cv2
import glob
import torch.utils.data as udata
import torch
def normalize(data):
return data/255.
def data_rotate(image, mode):
out = np.transpose(image, (1,2,0))
if mode == 0:
# original
out = out
elif mode == 1:
# flip up and down
out = np.flipud(out)
elif mode == 2:
# rotate counterwise 90 degree
out = np.rot90(out)
elif mode == 3:
# rotate 90 degree and flip up and down
out = np.rot90(out)
out = np.flipud(out)
elif mode == 4:
# rotate 180 degree
out = np.rot90(out, k=2)
elif mode == 5:
# rotate 180 degree and flip
out = np.rot90(out, k=2)
out = np.flipud(out)
elif mode == 6:
# rotate 270 degree
out = np.rot90(out, k=3)
elif mode == 7:
# rotate 270 degree and flip
out = np.rot90(out, k=3)
out = np.flipud(out)
return np.transpose(out, (2, 0, 1))
def prepare_data(data_path='data', train_filename='train_data.h5'):
# train
print('process training data')
files = glob.glob(os.path.join(data_path, 'pristine_images_color', '*'))
files.sort()
h5f = h5py.File(train_filename, 'w')
train_num = 0
for i in range(len(files)):
img = cv2.imread(files[i])
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
Img = cv2.resize(img, (256, 256), interpolation=cv2.INTER_LINEAR)
Img = torch.tensor(Img)
Img = Img.permute(2,0,1)
Img = Img.numpy()
Img = np.float32(normalize(Img))
# rorate image
for j in range(8):
Img = data_rotate(Img, j)
h5f.create_dataset(str(train_num), data=Img)
train_num += 1
print(train_num)
h5f.close()
print('training set, # samples %d\n' % train_num)
# val
print('\nprocess validation data')
files = glob.glob(os.path.join(data_path, 'CBSD68', '*.bmp'))
files.sort()
h5f = h5py.File('val_data.h5', 'w')
val_num = 0
for i in range(len(files)):
print("file: %s" % files[i])
img = cv2.imread(files[i])
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = torch.tensor(img)
img = img.permute(2, 0, 1)
img = img.numpy()
img = np.float32(normalize(img))
h5f.create_dataset(str(val_num), data=img)
val_num += 1
h5f.close()
print('training set, # samples %d\n' % train_num)
print('val set, # samples %d\n' % val_num)
class Dataset(udata.Dataset):
def __init__(self, train=True, data_root='train_data.h5'):
super(Dataset, self).__init__()
self.train = train
self.data_root = data_root
if self.train:
h5f = h5py.File(data_root, 'r')
else:
h5f = h5py.File('val_data.h5', 'r')
self.keys = list(h5f.keys())
random.shuffle(self.keys)
h5f.close()
def __len__(self):
return len(self.keys)
def __getitem__(self, index):
if self.train:
h5f = h5py.File(self.data_root, 'r')
else:
h5f = h5py.File('val_data.h5', 'r')
key = self.keys[index]
data = np.array(h5f[key])
h5f.close()
return torch.Tensor(data)
if __name__ == '__main__':
prepare_data(data_path='data', train_filename='train_data.h5')
# import visdom
#
# viz = visdom.Visdom()
# dataset = Dataset(train=True, data_root="train_data.h5")
# loader = udata.DataLoader(dataset, batch_size=8, num_workers=8, drop_last=True)
# print(len(loader.dataset))
# for x in loader:
# print(x.size())
# viz.images(x, nrow=2, win='image')