-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathpredict.py
64 lines (52 loc) · 1.78 KB
/
predict.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
import numpy as np
import os
import argparse
## Sigmoid activation Function
def sigmoid(X):
return 1.0 / (1.0 + np.exp(-X))
## Different Activaion Function
def h(theta,X,func='sig'):
a = theta.dot(X.T)
if(func== 'tanh'):
return np.tanh(a)
if func == 'none':
return a
if func == 'softplus':
return np.log(1 + np.exp(a))
if func == 'relu':
return np.maximum(0.01*a, a)
if func == 'softmax':
a1 = np.exp(a)
a1 = a1 / np.sum(a1, axis = 0, keepdims = True)
return a1
return sigmoid(a)
def validate(theta1, theta2, X, act = 'sig'):
aa1 = h(theta1,X,act)
aa1 = np.insert(aa1, 0, 1, axis=0)
aa2 = h(theta2,aa1.T,'softmax')
accu_matrix = np.argmax(aa2,axis=0)
return accu_matrix
## construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-model_dir", "--model_dir", required=False, help="path to model directory")
args = vars(ap.parse_args())
## Retrieving the Temp Folder for Result
if args["model_dir"]:
filename = args["model_dir"]+str("/")
else:
filename = "/tmp/blur_clear/"
if not os.path.exists(os.path.dirname(filename)):
print "No dir exists" + filename
exit()
## Extracting the Info from Training Set Results from tmp dir
params = np.load(filename + str("/result.npy"))
test_images = np.load(filename + str("/test_images.npy"))
test_labels = np.load(filename + str("/test_labels.npy"))
## Rescalling the Inputs
test_images = test_images/255.0
test_images = np.insert(test_images, 0, 1, axis=1)
## Predicting the Labels , Accuracy Score
pred_y = validate(params[()]['Theta1'], params[()]['Theta2'], test_images)
test_labels = test_labels.flatten()
accuracy = np.mean(pred_y == test_labels)*100
print 'Accuracy : ' + str(accuracy) + ' %'