-
Notifications
You must be signed in to change notification settings - Fork 83
/
utils.py
executable file
·152 lines (108 loc) · 4.63 KB
/
utils.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
import os
import numpy as np
import scipy
import scipy.misc
import matplotlib.pyplot as plt
class Mnist(object):
def __init__(self):
self.dataname = "Mnist"
self.dims = 28*28
self.shape = [28 , 28 , 1]
self.image_size = 28
self.data, self.data_y = self.load_mnist()
def load_mnist(self):
data_dir = os.path.join("./data", "mnist")
fd = open(os.path.join(data_dir, 'train-images-idx3-ubyte'))
loaded = np.fromfile(file=fd , dtype=np.uint8)
trX = loaded[16:].reshape((60000, 28 , 28 , 1)).astype(np.float)
fd = open(os.path.join(data_dir, 'train-labels-idx1-ubyte'))
loaded = np.fromfile(file=fd, dtype=np.uint8)
trY = loaded[8:].reshape((60000)).astype(np.float)
fd = open(os.path.join(data_dir, 't10k-images-idx3-ubyte'))
loaded = np.fromfile(file=fd, dtype=np.uint8)
teX = loaded[16:].reshape((10000, 28 , 28 , 1)).astype(np.float)
fd = open(os.path.join(data_dir, 't10k-labels-idx1-ubyte'))
loaded = np.fromfile(file=fd, dtype=np.uint8)
teY = loaded[8:].reshape((10000)).astype(np.float)
trY = np.asarray(trY)
teY = np.asarray(teY)
X = np.concatenate((trX, teX), axis=0)
y = np.concatenate((trY, teY), axis=0)
seed = 547
np.random.seed(seed)
np.random.shuffle(X)
np.random.seed(seed)
np.random.shuffle(y)
#convert label to one-hot
y_vec = np.zeros((len(y), 10), dtype=np.float)
for i, label in enumerate(y):
y_vec[i, int(y[i])] = 1.0
return X / 255., y_vec
def getNext_batch(self, iter_num=0, batch_size=64):
ro_num = len(self.data) / batch_size - 1
if iter_num % ro_num == 0:
length = len(self.data)
perm = np.arange(length)
np.random.shuffle(perm)
self.data = np.array(self.data)
self.data = self.data[perm]
self.data_y = np.array(self.data_y)
self.data_y = self.data_y[perm]
return self.data[int(iter_num % ro_num) * batch_size: int(iter_num% ro_num + 1) * batch_size] \
, self.data_y[int(iter_num % ro_num) * batch_size: int(iter_num%ro_num + 1) * batch_size]
def get_image(image_path , is_grayscale = False):
return np.array(inverse_transform(imread(image_path, is_grayscale)))
def save_images(images , size , image_path):
return imsave(inverse_transform(images) , size , image_path)
def imread(path, is_grayscale = False):
if (is_grayscale):
return scipy.misc.imread(path, flatten = True).astype(np.float)
else:
return scipy.misc.imread(path).astype(np.float)
def imsave(images , size , path):
return scipy.misc.imsave(path , merge(images , size))
def merge(images , size):
h , w = images.shape[1] , images.shape[2]
img = np.zeros((h*size[0] , w*size[1] , 3))
for idx , image in enumerate(images):
i = idx % size[1]
j = idx // size[1]
img[j*h:j*h +h , i*w : i*w+w , :] = image
return img
def inverse_transform(image):
return (image + 1.)/2.
def read_image_list(category):
filenames = []
print("list file")
list = os.listdir(category)
for file in list:
filenames.append(category + "/" + file)
print("list file ending!")
return filenames
##from caffe
def vis_square(visu_path , data , type):
"""Take an array of shape (n, height, width) or (n, height, width , 3)
and visualize each (height, width) thing in a grid of size approx. sqrt(n) by sqrt(n)"""
# normalize data for display
data = (data - data.min()) / (data.max() - data.min())
# force the number of filters to be square
n = int(np.ceil(np.sqrt(data.shape[0])))
padding = (((0, n ** 2 - data.shape[0]) ,
(0, 1), (0, 1)) # add some space between filters
+ ((0, 0),) * (data.ndim - 3)) # don't pad the last dimension (if there is one)
data = np.pad(data , padding, mode='constant' , constant_values=1) # pad with ones (white)
# tilethe filters into an im age
data = data.reshape((n , n) + data.shape[1:]).transpose((0 , 2 , 1 , 3) + tuple(range(4 , data.ndim + 1)))
data = data.reshape((n * data.shape[1] , n * data.shape[3]) + data.shape[4:])
plt.imshow(data[:,:,0])
plt.axis('off')
if type:
plt.savefig('./{}/weights.png'.format(visu_path) , format='png')
else:
plt.savefig('./{}/activation.png'.format(visu_path) , format='png')
def sample_label():
num = 64
label_vector = np.zeros((num , 10), dtype=np.float)
for i in range(0 , num):
label_vector[i , int(i/8)] = 1.0
return label_vector