-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
63 lines (49 loc) · 1.42 KB
/
util.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
import numpy as np
import pandas as pd
import matplotlib as mpl
mpl.use('TkAgg')
import matplotlib.pyplot as plt
def init_weight_and_bias(M1, M2):
W = np.random.randn(M1, M2) / (np.sqrt(M1 + M2) * 2)
b = np.zeros(M2)
return W.astype(np.float32), b.astype(np.float32)
def init_filter(shape, poolsz):
w = np.random.randn(*shape) /np.sqrt(np.prod(shape[:-1]) + shape[-1]*np.prod(shape[:-2] / np.prod(poolsz)))
return w.astype(np.float32)
def error_rate(p, t):
return np.mean(p != t)
def y2indicator(y):
N = len(y)
K = len(set(y))
ind = np.zeros((N, K))
for i in range(N):
ind[i, int(y[i])] = 1
return ind
def getData(balance_ones = True):
#images are 48x48 = 2304 size vectors
# N = 35887
Y = []
X = []
first = True
for line in open('fer2013/fer2013.csv'):
if first:
first = False
else:
row = line.split(',')
Y.append(int(row[0]))
X.append([int(p) for p in row[1].split()])
X, Y = np.array(X) / 255.0, np.array(Y)
if balance_ones:
# repeat class 1 to balance classes
X0, Y0 = X[Y!=1, :], Y[Y!= 1]
X1 = X[Y==1, :]
X1 = np.repeat(X1, 9, axis= 0)
X = np.vstack([X0, X1])
Y = np.concatenate((Y0, [1]*len(X1)))
return X, Y
def getImageData():
X, Y = getData()
N, D = X.shape
d = int(np.sqrt(D))
X = X.reshape(N, d, d, 1)
return X, Y