-
Notifications
You must be signed in to change notification settings - Fork 21
/
da_dataload.py
404 lines (302 loc) · 16.9 KB
/
da_dataload.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 25 17:30:35 2017
@author: damodara
"""
import DatasetLoad
import numpy as np
def mnist_to_usps():
from keras.datasets import mnist
(source_traindata, source_trainlabel), (source_testdata, source_testlabel) = mnist.load_data()
source_size = source_traindata.shape
resize = False
resize_size =16
from preprocess import zero_mean_unitvarince,resize_data
if resize == True:
source_traindata = resize_data(source_traindata, resize_size=resize_size)
source_testdata = resize_data(source_testdata, resize_size=resize_size)
source_size = source_traindata.shape
source_traindata = zero_mean_unitvarince(source_traindata,scaling=True)
source_testdata = zero_mean_unitvarince(source_testdata,scaling=True)
source_traindata = source_traindata.reshape(-1,source_size[1],source_size[2],1)
source_testdata =source_testdata.reshape(-1,source_size[1],source_size[2],1)
#%%
from DatasetLoad import usps_digit_dataload
target_traindata, target_trainlabel, target_testdata, target_testlabel = usps_digit_dataload()
target_trainlabel =target_trainlabel-1
target_testlabel =target_testlabel-1
target_traindata = target_traindata.reshape(-1, 16, 16,1)
target_testdata = target_testdata.reshape(-1, 16, 16,1)
print(target_traindata.shape)
resize =True
resize_size =28
if resize:
npad = ((0,0),(6,6),(6,6),(0,0))
target_traindata = np.pad(target_traindata,pad_width=npad, mode='constant')
target_testdata = np.pad(target_testdata, pad_width=npad, mode='constant')
# target_traindata = resize_data(target_traindata, resize_size=resize_size)
# target_testdata = resize_data(target_testdata, resize_size=resize_size)
target_traindata = target_traindata.reshape(-1, 28, 28, 1)
target_testdata = target_testdata.reshape(-1, 28, 28, 1)
target_traindata = zero_mean_unitvarince(target_traindata, scaling=True)
target_testdata = zero_mean_unitvarince(target_testdata, scaling=True)
return (source_traindata, source_trainlabel, source_testdata, source_testlabel), (target_traindata, target_trainlabel, target_testdata, target_testlabel)
#%%
def usps_to_mnist():
from DatasetLoad import usps_digit_dataload
source_traindata, source_trainlabel, source_testdata, source_testlabel = usps_digit_dataload()
source_trainlabel =source_trainlabel-1
source_testlabel =source_testlabel-1
# 2d to 3d for CNN
source_traindata = source_traindata.reshape(-1, 16, 16,1)
source_testdata = source_testdata.reshape(-1,16, 16,1)
from preprocess import zero_mean_unitvarince, resize_data
source_traindata = zero_mean_unitvarince(source_traindata, scaling=True)
source_testdata = zero_mean_unitvarince(source_testdata, scaling=True)
#
from keras.datasets import mnist
(target_traindata, target_trainlabel), (target_testdata, target_testlabel) = mnist.load_data()
target_size = target_traindata.shape
resize = True
resize_size =16
if resize == True:
target_traindata = resize_data(target_traindata, resize_size=resize_size)
target_testdata = resize_data(target_testdata, resize_size=resize_size)
target_size = target_traindata.shape
target_traindata = zero_mean_unitvarince(target_traindata,scaling=True)
target_testdata = zero_mean_unitvarince(target_testdata,scaling=True)
target_traindata = target_traindata.reshape(-1,target_size[1],target_size[2],1)
target_testdata =target_testdata.reshape(-1,target_size[1],target_size[2],1)
return (source_traindata, source_trainlabel, source_testdata, source_testlabel), (target_traindata, target_trainlabel, target_testdata, target_testlabel)
#%% MNIST MNISTM
def mnist_to_mnistm():
from keras.datasets import mnist
(source_traindata, source_trainlabel), (source_testdata, source_testlabel) = mnist.load_data()
source_size = source_traindata.shape
resize = False
resize_size =32
from preprocess import zero_mean_unitvarince,resize_data
if resize == True:
source_traindata = resize_data(source_traindata, resize_size=resize_size)
source_testdata = resize_data(source_testdata, resize_size=resize_size)
source_size = source_traindata.shape
source_traindata = zero_mean_unitvarince(source_traindata,scaling=True)
source_testdata = zero_mean_unitvarince(source_testdata,scaling=True)
convert_rgb=1
if convert_rgb:
source_traindata = np.stack((source_traindata,source_traindata,source_traindata), axis=3)
source_testdata = np.stack((source_testdata,source_testdata,source_testdata), axis=3)
from DatasetLoad import mnist_m_dataload
from skimage.color import rgb2gray
target_traindata, target_trainlabel, target_testdata, target_testlabel= mnist_m_dataload()
target_size = target_traindata.shape
resize = False
resize_size =28
if resize == True:
target_traindata = resize_data(target_traindata, resize_size=resize_size)
target_testdata = resize_data(target_testdata, resize_size=resize_size)
target_size = target_traindata.shape
target_traindata = zero_mean_unitvarince(target_traindata,scaling=True)
target_testdata = zero_mean_unitvarince(target_testdata,scaling=True)
return (source_traindata, source_trainlabel, source_testdata, source_testlabel), (target_traindata, target_trainlabel, target_testdata, target_testlabel)
#%%
def mnistm_to_mnist():
from DatasetLoad import mnist_m_dataload
from skimage.color import rgb2gray
source_traindata, source_trainlabel, source_testdata, source_testlabel= mnist_m_dataload()
source_size = source_traindata.shape
resize = True
resize_size =28
from preprocess import zero_mean_unitvarince,resize_data
if resize == True:
source_traindata = resize_data(source_traindata, resize_size=resize_size)
source_testdata = resize_data(source_testdata, resize_size=resize_size)
source_size = source_traindata.shape
source_traindata = zero_mean_unitvarince(source_traindata,scaling=True)
source_testdata = zero_mean_unitvarince(source_testdata,scaling=True)
from keras.datasets import mnist
(target_traindata, target_trainlabel), (target_testdata, target_testlabel) = mnist.load_data()
target_size = target_traindata.shape
resize = False
resize_size =32
from preprocess import zero_mean_unitvarince,resize_data
if resize == True:
target_traindata = resize_data(target_traindata, resize_size=resize_size)
target_testdata = resize_data(target_testdata, resize_size=resize_size)
target_size = target_traindata.shape
target_traindata = zero_mean_unitvarince(target_traindata,scaling=True)
target_testdata = zero_mean_unitvarince(target_testdata,scaling=True)
convert_rgb=1
if convert_rgb:
target_traindata = np.stack((target_traindata,target_traindata,target_traindata), axis=3)
target_testdata = np.stack((target_testdata,target_testdata,target_testdata), axis=3)
return source_traindata, source_trainlabel, source_testdata, source_testlabel, target_traindata, target_trainlabel, target_testdata, target_testlabel
#%% SVHNN MNIST
def svhnn_to_mnist(method = 'zero_mean_unitvarince', **params):
from skimage.color import rgb2gray
from scipy.misc import imresize
from DatasetLoad import SVHN_dataload
source_traindata, source_trainlabel, source_testdata, source_testlabel = SVHN_dataload()
source_size = source_traindata.shape
from preprocess import zero_mean_unitvarince, instance_zero_mean_unitvar, min_max_scaling
if method =='instance_zero_mean_unitvar':
source_traindata = instance_zero_mean_unitvar(source_traindata, scaling=True)
source_testdata = instance_zero_mean_unitvar(source_testdata, scaling=True)
elif method =='min_max':
source_traindata = min_max_scaling(source_traindata, **params)
source_testdata = min_max_scaling(source_testdata, **params)
else:
source_traindata = zero_mean_unitvarince(source_traindata, scaling=True)
source_testdata = zero_mean_unitvarince(source_testdata, scaling=True)
source_trainlabel = source_trainlabel*(source_trainlabel!=10)
source_testlabel = source_testlabel*(source_testlabel!=10)
from keras.datasets import mnist
(target_traindata, target_trainlabel), (target_testdata, target_testlabel) = mnist.load_data()
target_size = target_traindata.shape
resize = True
resize_size =32
from preprocess import zero_mean_unitvarince,resize_data
if resize == True:
target_traindata = resize_data(target_traindata, resize_size=resize_size)
target_testdata = resize_data(target_testdata, resize_size=resize_size)
if method =='instance_zero_mean_unitvar':
target_traindata = instance_zero_mean_unitvar(target_traindata, scaling=True)
target_testdata = instance_zero_mean_unitvar(target_testdata, scaling=True)
elif method =='min_max':
target_traindata = min_max_scaling(target_traindata, **params)
target_testdata = min_max_scaling(target_testdata, **params)
else:
target_traindata = zero_mean_unitvarince(target_traindata,scaling=True)
target_testdata = zero_mean_unitvarince(target_testdata,scaling=True)
convert_rgb=1
if convert_rgb:
target_traindata = np.stack((target_traindata,target_traindata,target_traindata), axis=3)
target_testdata = np.stack((target_testdata,target_testdata,target_testdata), axis=3)
return (source_traindata, source_trainlabel,source_testdata, source_testlabel), (target_traindata, target_trainlabel, target_testdata, target_testlabel)
def mnist_to_svhnn():
from keras.datasets import mnist
(source_traindata, source_trainlabel), (source_testdata, source_testlabel) = mnist.load_data()
source_size = source_traindata.shape
resize = False
resize_size =32
from preprocess import zero_mean_unitvarince,resize_data
if resize == True:
source_traindata = resize_data(source_traindata, resize_size=resize_size)
source_testdata = resize_data(source_testdata, resize_size=resize_size)
source_size = source_traindata.shape
source_traindata = zero_mean_unitvarince(source_traindata,scaling=True)
source_testdata = zero_mean_unitvarince(source_testdata,scaling=True)
convert_rgb=1
if convert_rgb:
source_traindata = np.stack((source_traindata,source_traindata,source_traindata), axis=3)
source_testdata = np.stack((source_testdata,source_testdata,source_testdata), axis=3)
#########################################
from skimage.color import rgb2gray
from scipy.misc import imresize
from DatasetLoad import SVHN_dataload
target_traindata, label = SVHN_dataload()
target_size = target_traindata.shape
from preprocess import zero_mean_unitvarince
target_traindata = zero_mean_unitvarince(target_traindata, scaling=True)
target_trainlabel = label*(label!=10)
target_size = target_traindata.shape
return source_traindata, source_trainlabel, source_testdata, source_testlabel, target_traindata, target_trainlabel
def syndigit_to_svhn(method = 'zero_mean_unitvarince'):
from DatasetLoad import synthetic_digits_dataload
source_traindata, source_trainlabel, source_testdata, source_testlabel = synthetic_digits_dataload()
from preprocess import zero_mean_unitvarince, instance_zero_mean_unitvar, min_max_scaling
if method == 'instance_zero_mean_unitvar':
source_traindata = instance_zero_mean_unitvar(source_traindata, scaling=True)
source_testdata = instance_zero_mean_unitvar(source_testdata, scaling=True)
elif method == 'min_max':
source_traindata = min_max_scaling(source_traindata)
source_testdata = min_max_scaling(source_testdata)
else:
source_traindata = zero_mean_unitvarince(source_traindata, scaling=True)
source_testdata = zero_mean_unitvarince(source_testdata, scaling=True)
from DatasetLoad import SVHN_dataload
target_traindata, target_trainlabel, target_testdata, target_testlabel = SVHN_dataload()
target_size = target_traindata.shape
from preprocess import zero_mean_unitvarince, instance_zero_mean_unitvar, min_max_scaling
if method == 'instance_zero_mean_unitvar':
source_traindata = instance_zero_mean_unitvar(source_traindata, scaling=True)
source_testdata = instance_zero_mean_unitvar(source_testdata, scaling=True)
elif method == 'min_max':
source_traindata = min_max_scaling(source_traindata)
source_testdata = min_max_scaling(source_testdata)
else:
source_traindata = zero_mean_unitvarince(source_traindata, scaling=True)
source_testdata = zero_mean_unitvarince(source_testdata, scaling=True)
target_trainlabel = target_trainlabel*(target_trainlabel!=10)
target_testlabel = target_testlabel*(target_testlabel!=10)
return (source_traindata, source_trainlabel,source_testdata, source_testlabel), (target_traindata, target_trainlabel, target_testdata, target_testlabel)
# %% stl10 to cifar10
def cifar_to_stl(resize_mode='i',normalize=True):
import numpy as np
from keras.datasets import cifar10
(source_traindata, source_trainlabel), (source_testdata, source_testlabel) = cifar10.load_data()
# remove the class 'frog' label = '6'
def remove(data, label, lind):
ind1 = (label < lind) + (label > lind)
ind1 = ind1.ravel()
data = data[ind1]
label = label[ind1]
ind2 = label > lind
label[ind2] = label[ind2] - 1
return data, label
source_traindata, source_trainlabel = remove(source_traindata, source_trainlabel, 6)
source_testdata, source_testlabel = remove(source_testdata, source_testlabel, 6)
source_size = source_traindata.shape
if resize_mode=='imagenet':
resize =True
resize_size = 224
else:
resize =False
resize_size =32
from preprocess import zero_mean_unitvarince, resize_data
if resize == True:
source_traindata = resize_data(source_traindata, resize_size=resize_size)
source_testdata = resize_data(source_testdata, resize_size=resize_size)
source_size = source_traindata.shape
if normalize == True:
source_traindata = zero_mean_unitvarince(source_traindata, scaling=True)
source_testdata = zero_mean_unitvarince(source_testdata, scaling=True)
from DatasetLoad import stl10_dataload
target_traindata, target_trainlabel, target_testdata, target_testlabel = stl10_dataload()
# remove the class name 'monkey', label = '7'
target_traindata, target_trainlabel = remove(target_traindata, target_trainlabel, 7)
target_testdata, target_testlabel = remove(target_testdata, target_testlabel, 7)
if resize_mode=='imagenet':
resize =True
resize_size = 224
else:
resize =True
resize_size =32
from preprocess import zero_mean_unitvarince, resize_data
if resize == True:
target_traindata = resize_data(target_traindata, resize_size=resize_size)
target_testdata = resize_data(target_testdata, resize_size=resize_size)
from preprocess import zero_mean_unitvarince
target_traindata = zero_mean_unitvarince(target_traindata, scaling=True)
target_testdata = zero_mean_unitvarince(target_testdata, scaling=True)
return (source_traindata, source_trainlabel,source_testdata, source_testlabel), (target_traindata, target_trainlabel, target_testdata, target_testlabel)
def generate_rotated_image(image, lower_angle=-90, upper_angle=90):
"""Generate a rotated image with a random rotation angle"""
import imutils
percent = np.random.random()
percent_to_angle = lambda x: x * (upper_angle-lower_angle) + lower_angle
#percent_to_scale = lambda x: x * 0.5 + 0.5
angle = percent_to_angle(percent)
rotated = imutils.rotate(image, angle, scale=1)
return rotated, percent
def generate_rotated_images(images, lower_angle=-90, upper_angle=90):
"""Generate rotated images from 4D array, returning rotated images and 2D angle labels"""
new_images = np.empty_like(images)
labels = np.empty(images.shape[0])
for i in range(images.shape[0]):
# if i % 2500 == 0:
# print("Generating image", i)
img, angle = generate_rotated_image(images[i], lower_angle, upper_angle)
new_images[i] = img
labels[i] = angle
return new_images, labels[..., np.newaxis]