-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutil.py
More file actions
171 lines (124 loc) · 3.38 KB
/
util.py
File metadata and controls
171 lines (124 loc) · 3.38 KB
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
"""
Some utility functions
Author: Yuhuang Hu
Email: duguyue100@gmail.com
"""
import os;
import gzip;
import cPickle as pickle;
import numpy as np;
import numpy.linalg as LA;
### DATA LOADING FUCNTIONS ###
def load_mnist(dataset):
"""Load MNIST dataset
Parameters
----------
dataset : string
address of MNIST dataset
Returns
-------
rval : list
training, valid and testing dataset files
"""
# Load the dataset
f = gzip.open(dataset, 'rb');
train_set, valid_set, test_set = pickle.load(f);
f.close();
#mean_image=get_mean_image(train_set[0]);
return [train_set, valid_set, test_set];
def load_CIFAR_batch(filename):
"""
load single batch of cifar-10 dataset
code is adapted from CS231n assignment kit
@param filename: string of file name in cifar
@return: X, Y: data and labels of images in the cifar batch
"""
with open(filename, 'r') as f:
datadict=pickle.load(f);
X=datadict['data'];
Y=datadict['labels'];
X=X.reshape(10000, 3, 32, 32).transpose(0,2,3,1).astype("float");
Y=np.array(Y);
return X, Y;
def load_CIFAR10(ROOT):
"""
load entire CIFAR-10 dataset
code is adapted from CS231n assignment kit
@param ROOT: string of data folder
@return: Xtr, Ytr: training data and labels
@return: Xte, Yte: testing data and labels
"""
xs=[];
ys=[];
for b in range(1,6):
f=os.path.join(ROOT, "data_batch_%d" % (b, ));
X, Y=load_CIFAR_batch(f);
xs.append(X);
ys.append(Y);
Xtr=np.concatenate(xs);
Ytr=np.concatenate(ys);
del X, Y;
Xte, Yte=load_CIFAR_batch(os.path.join(ROOT, "test_batch"));
return Xtr, Ytr, Xte, Yte;
### UTILITIES FUNCTIONS ###
def sample_patches_mnist(data, N, d):
"""
Sample image patches from mnist
"""
X=np.array([]);
for i in xrange(N):
temp=data[0][i].reshape(28,28);
temp=temp[5:5+d, 5:5+d].reshape(-1);
if X.size is 0:
X=temp;
else:
X=np.vstack((X, temp));
return X.T;
def sample_patches_cifar10(data, N, d):
"""
Sample image patches from CIFAR-10
"""
X=np.array([]);
for i in xrange(N):
temp=data[i, :, :];
temp=temp[5:5+d, 5:5+d].reshape(-1);
if X.size is 0:
X=temp;
else:
X=np.vstack((X, temp));
return X.T;
def normalize_data(X):
"""
Normalize data's mean and variance,
X in (data_size x number_of_samples) manner
"""
X_mean=np.mean(X, axis=0);
X_var=np.var(X, axis=0)+10;
X=(X-X_mean)/np.sqrt(X_var);
return X;
def ZCA_whitening(X):
"""
Perform ZCA_whitening
X in (data_size x number_of_samples) manner
"""
U, S, _ = LA.svd(X.dot(X.T)/X.shape[1]);
return U.dot(np.diag(1/np.sqrt(S+0.01))).dot(U.T).dot(X);
def kmeans(X, D, num_iter):
"""
Perform kmeans algorithm
Parameters
----------
X : 2-D array with size of (data_size x num_of_samples);
image patches
D : 2-D array with size of (data_size x num_of_centroids);
dictioanry
num_iter : integer
number of iteration
"""
for i in xrange(num_iter):
S=D.T.dot(X);
S=S*(S>=np.max(S, axis=0));
D=X.dot(S.T)+D;
D=D/np.sqrt(np.sum(D**2, axis=0));
print "[MESSAGE] Iteration %i is done" % (i);
return D;