This repository was archived by the owner on Jul 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
181 lines (163 loc) · 6.3 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
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
from __future__ import division
from keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array
from keras.datasets import mnist, cifar10
from os import path as osp
from keras import backend as K
from keras.utils import np_utils
from scipy.misc import imresize
import os
import numpy as np
import pickle
import h5py
def save_cifar_to_hdf5():
K.set_image_data_format('channels_first')
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
print x_train.shape
h5f = h5py.File('data4torch.h5', 'w')
h5f.create_dataset(name='data', data=x_train, shape=(50000, 3, 32, 32))
h5f.create_dataset(name='labels', data=y_train.flatten(), shape=(50000,))
h5f.close()
def load(path, min_pixel=200):
img = load_img(path)
mat = img_to_array(img)
if mat.shape[0] < mat.shape[1]:
new_shape = (200, int(mat.shape[1] / mat.shape[0] * min_pixel))
else:
new_shape = (int(mat.shape[0] / mat.shape[1] * min_pixel), 200)
return imresize(mat, new_shape)
def mit_scene_67(pixel=200):
dir = '/home/x/Downloads/mit67'
x_train_file = osp.join(dir, 'x_train_%d.npy' % pixel)
y_train_file = osp.join(dir, 'y_train_%d.npy' % pixel)
x_test_file = osp.join(dir, 'x_test_%d.npy' % pixel)
y_test_file = osp.join(dir, 'y_test_%d.npy' % pixel)
if os.path.exists(x_train_file):
return (np.load(x_train_file), np.load(y_train_file)),\
(np.load(x_test_file), np.load(y_test_file))
names = []
train_list_file = osp.join(dir, 'TrainImages.txt')
test_list_file = osp.join(dir, 'TestImages.txt')
crop_size = 200
x_train = []
y_train = []
x_test = []
y_test = []
for line in open(train_list_file, 'r').readlines():
img = load(osp.join(dir, line.rstrip('\n')), crop_size)
name = line.split('/')[0]
if name not in names:
names.append(name)
label = names.index(name)
r = (img.shape[0] - crop_size) // 2
c = (img.shape[1] - crop_size) // 2
x_train.append(img[r:r+crop_size, c:c+crop_size])
if x_train[-1].shape != (200, 200, 3): print line, img.shape
y_train.append(label)
for line in open(test_list_file, 'r').readlines():
img = load(osp.join(dir, line.rstrip('\n')))
name = line.split('/')[0]
if name not in names:
names.append(name)
label = names.index(name)
r = (img.shape[0] - crop_size) / 2
c = (img.shape[1] - crop_size) / 2
x_test.append(img[r:r+crop_size, c:c+crop_size])
if x_train[-1].shape != (200, 200, 3): print line, img.shape
y_test.append(label)
x_train = np.stack(x_train)
y_train = np.stack(y_train)
x_test = np.stack(x_test)
y_test = np.stack(y_test)
np.save(x_train_file, x_train)
np.save(y_train_file, y_train)
np.save(x_test_file, x_test)
np.save(y_test_file, y_test)
return (x_train, y_train), (x_test, y_test)
def load_mnist(mode):
if mode == 'basic':
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255.
x_test /= 255.
elif mode == 'rot':
x_train, y_train = pickle.load(open('rotation_train.pkl', 'rb'))
x_test, y_test = pickle.load(open('rotation_test.pkl', 'rb'))
elif mode == 'rndback':
x_train, y_train = pickle.load(open('rndback_train.pkl', 'rb'))
x_test, y_test = pickle.load(open('rndback_test.pkl', 'rb'))
elif mode == 'imgback':
x_train, y_train = pickle.load(open('imgback_train.pkl', 'rb'))
x_test, y_test = pickle.load(open('imgback_test.pkl', 'rb'))
elif mode == 'imgback_rot':
x_train, y_train = pickle.load(open('imgback_rot_train.pkl', 'rb'))
x_test, y_test = pickle.load(open('imgback_rot_test.pkl', 'rb'))
else:
raise NotImplementedError("no this dataset")
print x_train.max(), x_test.max()
return (x_train, y_train), (x_test, y_test)
def clip_stich_augmentation(x, y, num):
if num == 0: return x, y
n = y.shape[0]
n_classes = y.shape[1]
images = []
labels = []
import cv2
h = 32
r = h // 2
s = int(0.5 * r)
for i in range(num):
idx = np.random.randint(low=0, high=n, size=2)
pathes = [x[i_] for i_ in idx]
img_new = np.zeros((h, h, 3), dtype='float32')
img_new[:, 0:r] = pathes[0][:, s:s + r]
img_new[:, r:2*r] = pathes[1][:, s:s + r]
# img_new[:, 2*r:3*r] = x[i, :, s:s + r]
# img_new[:, 3*r:4*r] = x[i, :, s:s + r]
images.append(img_new)
l = np.zeros((n_classes,), dtype='float32')
for j in idx:
t = np.argwhere(y[j] == 1)[0, 0]
l[t] += 0.5
labels.append(l)
return np.concatenate([x, np.asarray(images)]), np.concatenate([y, np.asarray(labels)])
def mix_augmentation(x, y, num):
if num == 0: return x, y
n = y.shape[0]
n_classes = y.shape[1]
images = []
labels = []
import cv2
h = 32
r = h // 2
for i in range(num):
idx = np.random.randint(low=0, high=n, size=4)
pathes = [cv2.resize(x[i], (r, r)) for i in idx]
img_new = np.zeros((h, h, 3), dtype='float32')
img_new[0:r, 0:r] = pathes[0]
img_new[0:r, r:h] = pathes[1]
img_new[r:h, 0:r] = pathes[2]
img_new[r:h, r:h] = pathes[3]
images.append(img_new)
l = np.zeros((n_classes,), dtype='float32')
for j in idx:
t = np.argwhere(y[j]==1)[0, 0]
l[t] = 1
labels.append(l)
return np.asarray(images), np.asarray(labels)
# return np.concatenate([x, np.asarray(images)]), np.concatenate([y, np.asarray(labels)])
def load_data(mode, batch_size):
(x_train, y_train), (x_test, y_test) = load_mnist(mode)
y_train = np_utils.to_categorical(y_train, 10)
y_test = np_utils.to_categorical(y_test, 10)
x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:])))
x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:])))
cut = int(batch_size * (x_train.shape[0] // batch_size))
x_train = x_train[:cut]
y_train = y_train[:cut]
cut = int(batch_size * (x_test.shape[0] // batch_size))
x_test = x_test[:cut]
y_test = y_test[:cut]
return (x_train, y_train), (x_test, y_test)
if __name__ == '__main__':
save_cifar_to_hdf5()