-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcifar10_predict_once.py
166 lines (135 loc) · 5.73 KB
/
cifar10_predict_once.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
153
154
155
156
157
158
159
160
161
162
163
164
165
"""Prediction for CIFAR-10."""
import numpy as np
from PIL import Image
import matplotlib.image as mpimg
import cifar10
learning_rate = 0.01 # Step length of gradient descent
num_iterations = 1 # Number of iterations
height = 32; width = 32; channels = 3 # Size of CIFAR-10 image
location = '/Users/apple/desktop/car/image_0001.jpg'
saved_location = '/Users/apple/desktop/1.jpg'
def resize_image(location):
"""Resize image to 32*32.
Args:
location: Saved location of image.
"""
im = Image.open(location)
im_resize = im.resize((height, width))
im_resize.save(saved_location)
def preprocessing(location, saved_location):
"""Resize image and load image.
Args:
location: Saved location of image.
saved_location: New location which new image want to save in.
Returns:
image: image of shape[height, width, channels].
"""
resize_image(location)
image = mpimg.imread(saved_location)
image = image.reshape(height*width, channels).T
image = image.reshape(1, channels, height, width)
return image
def predict_once():
"""Predict class of new image."""
# Preprocessing image
image = preprocessing(location, saved_location)
# Get training set
mini_batches = cifar10.preprocessing_inputs()
# Training
parameters, costs, bn_param = cifar10.inference(mini_batches, learning_rate=learning_rate,
num_iterations=num_iterations)
# 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_2 = bn_param['bn_param_conv_2']
bn_param_local_3 = bn_param['bn_param_local_3']
bn_param_local_4 = bn_param['bn_param_local_4']
bn_param_local_5 = bn_param['bn_param_local_5']
# conv_1
conv_param = {}
conv_param['stride'] = 1
conv_param['pad'] = 1
conv_1, cache_conv_1 = cifar10.conv_forward_naive(image, 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(1, -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)
dict = {0: "airplane",
1: "automobile",
2: "bird",
3: "cat",
4: 'deer',
5: 'dog',
6: 'frog',
7: 'horse',
8: 'sheep',
9: 'truck'}
for i in range(10):
if logits[:, 0][i] == np.max(logits[:, 0]):
print "class of image is " + dict[i]
if __name__ == '__main__':
predict_once()