-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdataset.py
146 lines (101 loc) · 3.6 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
import os
import numpy as np
import torch
from skimage import transform
import matplotlib.pyplot as plt
class Dataset(torch.utils.data.Dataset):
def __init__(self, data_dir, data_type='float32', nch=1, transform=[]):
self.data_dir = data_dir
self.transform = transform
self.nch = nch
self.data_type = data_type
lst_data = os.listdir(data_dir)
self.names = lst_data
def __getitem__(self, index):
data = plt.imread(os.path.join(self.data_dir, self.names[index]))[:, :, :self.nch]
if data.dtype == np.uint8:
data = data / 255.0
if self.transform:
data = self.transform(data)
return data
def __len__(self):
return len(self.names)
class ToTensor(object):
def __call__(self, data):
if data.ndim == 3:
data = data.transpose((2, 0, 1)).astype(np.float32)
elif data.ndim == 4:
data = data.transpose((0, 3, 1, 2)).astype(np.float32)
return torch.from_numpy(data)
class Normalize(object):
def __init__(self, mean=0.5, std=0.5):
self.mean = mean
self.std = std
def __call__(self, data):
data = (data - self.mean)/self.std
return data
class RandomFlip(object):
def __call__(self, data):
if np.random.rand() > 0.5:
data = np.fliplr(data)
return data
class Rescale(object):
def __init__(self, output_size):
assert isinstance(output_size, (int, tuple))
self.output_size = output_size
def __call__(self, data):
ch, h, w = data.shape
if isinstance(self.output_size, int):
if h > w:
new_h, new_w = self.output_size * h / w, self.output_size
else:
new_h, new_w = self.output_size, self.output_size * w / h
else:
new_h, new_w = self.output_size
new_h, new_w = int(new_h), int(new_w)
data = transform.resize(data, (ch, new_h, new_w))
return data
class CenterCrop(object):
def __init__(self, output_size):
assert isinstance(output_size, (int, tuple))
if isinstance(output_size, int):
self.output_size = (output_size, output_size)
else:
assert len(output_size) == 2
self.output_size = output_size
def __call__(self, data):
h, w = data.shape[:2]
new_h, new_w = self.output_size
top = int(abs(h - new_h) / 2)
left = int(abs(w - new_w) / 2)
data = data[top: top + new_h, left: left + new_w]
return data
class RandomCrop(object):
def __init__(self, output_size):
assert isinstance(output_size, (int, tuple))
if isinstance(output_size, int):
self.output_size = (output_size, output_size)
else:
assert len(output_size) == 2
self.output_size = output_size
def __call__(self, data):
h, w = data.shape[:2]
new_h, new_w = self.output_size
top = np.random.randint(0, h - new_h)
left = np.random.randint(0, w - new_w)
data = data[top: top + new_h, left: left + new_w]
return data
class ToNumpy(object):
def __call__(self, data):
if data.ndim == 3:
data = data.to('cpu').detach().numpy().transpose((1, 2, 0))
elif data.ndim == 4:
data = data.to('cpu').detach().numpy().transpose((0, 2, 3, 1))
return data
class Denormalize(object):
def __init__(self, mean=0.5, std=0.5):
self.mean = mean
self.std = std
def __call__(self, data):
data = self.std * data + self.mean
return data