-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcifar10_eval.py
126 lines (106 loc) · 4.92 KB
/
cifar10_eval.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
"""Evaluation for CIFAR-10."""
import numpy as np
import cifar10
def predict(parameters, minibatches, bn_param):
"""Predict whether the label is 0 or 1 using parameters.
Args:
parameters: All the parameters through full training.
image_test: Test images.
bn_param: A python dictionary of all the bn_params.
Returns:
Y_prediction: All predictions (0/1) for the examples in image_test.
"""
# Get parameters
w_conv1 = parameters['w_conv1']
b_conv1 = parameters['b_conv1']
w_conv2 = parameters['w_conv2']
b_conv2 = parameters['b_conv2']
w1 = parameters['w1']
b1 = parameters['b1']
w2 = parameters['w2']
b2 = parameters['b2']
w3 = parameters['w3']
b3 = parameters['b3']
gamma_conv1 = parameters['gamma_conv1']
gamma_conv2 = parameters['gamma_conv2']
gamma1 = parameters['gamma1']
gamma2 = parameters['gamma2']
gamma3 = parameters['gamma3']
beta_conv1 = parameters['beta_conv1']
beta_conv2 = parameters['beta_conv2']
beta1 = parameters['beta1']
beta2 = parameters['beta2']
beta3 = parameters['beta3']
# Get bn_params
bn_param_conv_1 = bn_param['bn_param_conv_1']
# bn_param_conv_1['mode'] = 'test'
bn_param_conv_2 = bn_param['bn_param_conv_2']
# bn_param_conv_2['mode'] = 'test'
bn_param_local_3 = bn_param['bn_param_local_3']
# bn_param_local_3['mode'] = 'test'
bn_param_local_4 = bn_param['bn_param_local_4']
# bn_param_local_4['mode'] = 'test'
bn_param_local_5 = bn_param['bn_param_local_5']
# bn_param_local_5['mode'] = 'test'
# Number of mini_batches
num_complete_minibatches = len(minibatches)
Y_prediction = np.zeros((num_complete_minibatches, 128))
for i in range(1):
# conv_1
conv_param = {}
conv_param['stride'] = 1
conv_param['pad'] = 1
conv_1, cache_conv_1 = cifar10.conv_forward_naive(minibatches[i][0], w_conv1,
b_conv1, conv_param)
conv_1, cache_batchnorm_conv_1 = cifar10.spatial_batchnorm_forward(conv_1,
gamma_conv1,
beta_conv1,
bn_param_conv_1)
conv_1, cache_activation_conv_1 = cifar10.relu(conv_1)
# pool_1
pool_param = {}
pool_param['pool_height'] = 2
pool_param['pool_width'] = 2
pool_param['stride'] = 2
pool_1, cache_pool_1 = cifar10.max_pool_forward_naive(conv_1, pool_param)
# conv_2
conv_param = {}
conv_param['stride'] = 1
conv_param['pad'] = 1
conv_2, cache_conv_2 = cifar10.conv_forward_naive(pool_1, w_conv2, b_conv2,
conv_param)
conv_2, cache_batchnorm_conv_2 = cifar10.spatial_batchnorm_forward(conv_2,
gamma_conv2,
beta_conv2,
bn_param_conv_2)
conv_2, cache_activation_conv_2 = cifar10.relu(conv_2)
# pool_2
pool_param = {}
pool_param['pool_height'] = 2
pool_param['pool_width'] = 2
pool_param['stride'] = 2
pool_2, cache_pool_2 = cifar10.max_pool_forward_naive(conv_2, pool_param)
# local_3_relu
reshape = pool_2.reshape(128, -1).T
local_3, cache_local_3 = cifar10.linear_activation_forward(reshape, w1, b1,
activation='relu',
gamma=gamma1,
beta=beta1,
bn_param=bn_param_local_3)
# local_4_relu
local_4, cache_local_4 = cifar10.linear_activation_forward(local_3, w2, b2,
activation='relu',
gamma=gamma2,
beta=beta2,
bn_param=bn_param_local_4)
# local_5_softmax
logits, cache_local_5 = cifar10.linear_activation_forward(local_4, w3, b3,
activation='softmax',
gamma=gamma3,
beta=beta3,
bn_param=bn_param_local_5)
for j in range(logits.shape[1]):
for k in range(logits.shape[0]):
if logits[k][j] == np.max(logits, axis=0)[j]:
Y_prediction[i][j] = k
return Y_prediction