-
Notifications
You must be signed in to change notification settings - Fork 7
/
ten_predict.py
192 lines (161 loc) · 5.29 KB
/
ten_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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# Imports
import numpy as np
import tensorflow as tf
from tensorflow.contrib import learn
from tensorflow.contrib.learn.python.learn.estimators import model_fn as model_fn_lib
from skimage.io import imread, imshow, imsave
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
def cnn_model_fn(features, labels, mode):
# Input layer
input_layer = tf.reshape(features, [-1, 227, 227, 3])
# Conv layer 1
conv1 = tf.layers.conv2d(
inputs=input_layer,
filters=24,
strides=4,
kernel_size=[11, 11],
activation=tf.nn.relu
)
# Pooling layer 1
pool1 = tf.layers.max_pooling2d(
inputs=conv1,
pool_size=[3, 3],
strides=2)
# Nomorlization layer 1
norm1 = tf.nn.local_response_normalization(
pool1,
depth_radius = 2,
alpha = 2e-05,
beta = 0.75,
bias = 1.0,
name = 'norm1')
# Conv layer 2
conv2 = tf.layers.conv2d(
inputs=norm1,
filters=64,
kernel_size=[5, 5],
padding='SAME',
activation=tf.nn.relu)
# Pooling layer 2
pool2 = tf.layers.max_pooling2d(
inputs=conv2,
pool_size=[3, 3],
strides=2)
# Nomorlization layer 2
norm2 = tf.nn.local_response_normalization(
pool2,
depth_radius = 2,
alpha = 2e-05,
beta = 0.75,
bias = 1.0,
name = 'norm2')
# Conv layer 3
conv3 = tf.layers.conv2d(
inputs=norm2,
filters=256,
kernel_size=[3, 3],
padding='SAME',
activation=tf.nn.relu)
# Conv layer 4
conv4 = tf.layers.conv2d(
inputs=conv3,
filters=256,
kernel_size=[3, 3],
padding='SAME',
activation=tf.nn.relu)
# Conv layer 5
conv5 = tf.layers.conv2d(
inputs=conv4,
filters=128,
kernel_size=[3, 3],
padding='SAME',
activation=tf.nn.relu)
# Pooling layer 5
pool5 = tf.layers.max_pooling2d(
inputs=conv5,
pool_size=[3, 3],
strides=2)
# Dense layer(full-connected layer)
flat = tf.reshape(pool5, [-1, 6*6*128])
fc6 = fc(flat, 6*6*128, 4096, name='fc6')
dropout6 = tf.nn.dropout(fc6, keep_prob=0.5)
fc7 = fc(dropout6, 4096, 4096, name = 'fc7')
dropout7 = tf.nn.dropout(fc7, keep_prob=0.5)
logits = tf.layers.dense(inputs=dropout7, units=10)
loss = None
train_op = None
# Calculate Loss
if mode != learn.ModeKeys.INFER:
onehot_labels = tf.one_hot(indices=tf.cast(labels, tf.int32), depth=10)
loss = tf.losses.softmax_cross_entropy(
onehot_labels=onehot_labels, logits=logits)
# Configure the Training Op
if mode == learn.ModeKeys.TRAIN:
train_op = tf.contrib.layers.optimize_loss(
loss=loss,
global_step=tf.contrib.framework.get_global_step(),
learning_rate=0.001,
optimizer='SGD')
# Generate Predictions
predictions = {
"classes": tf.argmax(input=logits, axis=1),
"probabilities": tf.nn.softmax(logits, name='softmax_tensor')
}
# Return a ModelFnOps objects
return model_fn_lib.ModelFnOps(
mode=mode, predictions=predictions, loss=loss, train_op=train_op)
def fc(x, num_in, num_out, name, relu = True):
with tf.variable_scope(name) as scope:
# Create tf variables for the weights and biases
weights = tf.get_variable('weights', shape=[num_in, num_out], trainable=True)
biases = tf.get_variable('biases', [num_out], trainable=True)
# Matrix multiply weights and inputs and add bias
act = tf.nn.xw_plus_b(x, weights, biases, name=scope.name)
if relu == True:
# Apply ReLu non linearity
relu = tf.nn.relu(act)
return relu
else:
return act
def main(unused_argv):
# Load classifier
print('Begin to load')
ten_classifier = learn.Estimator(
model_fn=cnn_model_fn, model_dir="/tmp/ten_alex_convnet_model")
print('Loading finished')
# Load region proposals
regions = np.load("C:/Users/eric/selectivesearch/cut_photo.npy")
regions = regions[1:,:]
regions = np.array(regions, dtype = np.float32)
p1 = ten_classifier.predict(x=regions, as_iterable=True)
max_p = 0;
for i, p in enumerate(p1):
#for k in p:
#print(k, p[k])
print("Prediction %s: %s" % (i+1, p['classes']))
print("Probabilities: %s " % p['probabilities'].max())
if p['probabilities'].max() > max_p:
max_p = p['probabilities'].max()
index = i
predict = p['classes']
print("Region: %s" % str(index+1))
print("Predict: %s" % predict)
print("Probabilities: %s" % max_p)
# read 原图
img = imread('C:/Users/eric/selectivesearch/example/dog (3).JPEG')
endpoint = np.load("C:/Users/eric/selectivesearch/endpoint_4.npy")
a = index+1
name = ['airplane', 'bird', 'car', 'cat', 'deer',
'dog', 'frog', 'horse', 'ship', 'truck']
# draw rectangles on the original image
fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6))
ax.imshow(img)
rect = mpatches.Rectangle(
(endpoint[a][0], endpoint[a][1]), endpoint[a][2], endpoint[a][3], fill=False, edgecolor='red', linewidth=1)
ax.add_patch(rect)
ax.annotate(name[predict]+' %.2f'%max_p, xy = (endpoint[a][0], endpoint[a][1]), fontsize=16, color='red')
plt.show()
# Draw
if __name__ == "__main__":
tf.app.run()