-
Notifications
You must be signed in to change notification settings - Fork 0
/
Datasets.py
164 lines (139 loc) · 5.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import numpy as np
from glob import glob
import scipy.io as sio
from keras.datasets import mnist
from skimage.io import imread, imsave
# ---------------------- CONSTANTS -------------------------------
BLUR_PATH = './data/blur/blur_data.mat' # BLUR DATA PATH
SVHN_PATH = './data/svhn/*.mat' # SVHN DATA PATH
SVHN_TEST_SIZE = 30000
#----------------------- BLUR DATASET ----------------------------
class BlurDataSet():
def __init__(self, path = BLUR_PATH):
self.path = path
self.Train = None
self.Test = None
def LoadData(self):
Blur = sio.loadmat(self.path)
Blur_Train_tmp = Blur['X_Train'][0]
Blur_Test_tmp = Blur['X_Test'][0]
Blur_Train = []
for i in range(Blur_Train_tmp.shape[0]):
Blur_Train.append(Blur_Train_tmp[i])
Blur_Test = []
for i in range(Blur_Test_tmp.shape[0]):
Blur_Test.append(Blur_Test_tmp[i])
del Blur_Test_tmp, Blur_Train_tmp
Blur_Train = np.array(Blur_Train)
Blur_Test = np.array(Blur_Test)
Blur_Train = np.expand_dims(Blur_Train, axis = -1)
Blur_Test = np.expand_dims(Blur_Test, axis = -1)
self.Train = Blur_Train
self.Test = Blur_Test
def GetData(self):
return self.Train, self.Test
def GetBlur(self, idx = 'random', return_test = True):
# TEST SET
if return_test == True:
if idx == 'all':
return self.Test
elif idx=='random':
i = np.random.randint(self.Test.shape[0])
return self.Test[i].reshape(28,28)
else:
return self.Test[idx].reshape(28,28)
# TRAIN SET
else:
if idx == 'all':
return self.Train
elif idx=='random':
i = np.random.randint(self.Train.shape[0])
return self.Train[i].reshape(28,28)
else:
return self.Train[idx].reshape(28,28)
#----------------------- SVHN DATASET ----------------------------
SVHN_ORIG_IMAGES_RESULTS = './results/SVHN/Original Images/*.png'
class SVHNDataSet():
def __init__(self, path = SVHN_PATH, TestSize = SVHN_TEST_SIZE):
self.paths = glob(path)
self.Train = None
self.Test = None
self.TestSize = TestSize
self.IMAGES_IN_RESULT_FOLDER = None
def LoadData(self):
Data = np.zeros((531131,32,32,3))
i = 0
for path in self.paths:
X = sio.loadmat(path)['X_BATCH']
X = np.transpose(X, [3,0,1,2])
Data[i:i+X.shape[0]] = X/255
i = i + X.shape[0]
self.Train = Data[:Data.shape[0]-self.TestSize]
self.Test = Data[Data.shape[0]-self.TestSize:]
def GetImage(self, idx = 'random', return_test = True):
# TEST SET
if return_test == True:
if idx == 'all':
return self.Test
elif idx=='random':
i = np.random.randint(self.Test.shape[0])
return self.Test[i].reshape(32,32,3)
else:
return self.Test[idx].reshape(32,32,3)
# TRAIN SET
else:
if idx == 'all':
return self.Train
elif idx=='random':
i = np.random.randint(self.Train.shape[0])
return self.Train[i].reshape(32,32,3)
else:
return self.Train[idx].reshape(32,32,3)
# -------------------------- MNSIT DATA ----------------------------
class MNISTDataSet():
def __init__(self):
self.Train = None
self.Test = None
def LoadData(self):
(train, _ ), (test, _) = mnist.load_data()
self.Train = train.reshape(60000,28,28)
self.Test = test.reshape(10000,28,28)
def GetImage(self, idx = 'random', return_test = True):
# TEST SET
if return_test == True:
if idx == 'all':
return self.Test
elif idx=='random':
i = np.random.randint(self.Test.shape[0])
return self.Test[i].reshape(28,28)
else:
return self.Test[idx].reshape(28,28)
# TRAIN SET
else:
if idx == 'all':
return self.Train
elif idx=='random':
i = np.random.randint(self.Train.shape[0])
return self.Train[i].reshape(28,28)
else:
return self.Train[idx].reshape(28,28)
# ---------------------- EXPERIMENTS -------------------------------
Orig_Images_Path = glob('./results/svhn/Original Images/*.png')
Range_Images_Path = glob('/results/svhn/Range Images/*.png')
class EXPERIMENT_RESULTS():
def __init__(self,path_recov_test, path_recov_range,
path_orig = Orig_Images_Path,
path_range = Range_Images_Path):
# LOADING ORIGINAL IMAGES
path_orig = path_orig + '*.png'
self.Orig = np.array([imread(path) for path in path_orig])
# LOADING RANGE IMAGES
path_range = path_range + '*.png'
self.Range = np.array([imread(path) for path in path_range])
# LOADING RANGE IMAGES
path_recov_test = path_recov_test + '*.png'
self.RecovFromTest = np.array([imread(path) for path in path_recov_test])
# LOADING RANGE IMAGES
path_recov_range = path_recov_range + '*.png'
self.RecovFromRange = np.array([imread(path) for path in path_recov_range])
# ANALYSIS VARIABLES