-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpredict_TensorFlowCNN.py
91 lines (58 loc) · 2.31 KB
/
predict_TensorFlowCNN.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
'''
Created on Oct 2, 2017
@author: inayat
'''
import numpy as np
import argparse
import imutils
import time
import cv2
# for shutting down the tensor flow warinings
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import tensorflow as tf
import sys
if __name__ == '__main__':
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
help="path to input image file")
args = vars(ap.parse_args())
imageSize = 150
numChannels = 3
# Reading the image using OpenCV
image = cv2.imread(args["image"])
if image is None:
print("can not load image: ", args["image"])
sys.exit()
imageOrig = image.copy()
# resize and preprocess the test image exactly in the same manner
# as done in training of the network
image = cv2.resize(image, (imageSize, imageSize),
cv2.INTER_LINEAR)
image = image.astype('float32')
image = np.multiply(image , 1.0 / 255.0)
# The input to the network is of shape [None imageSize imageSize numChannels]. Hence we reshape
image = image.reshape(1, imageSize, imageSize, numChannels)
# now restore the trained model
sess = tf.Session()
# S-1 recreate the network graph
datasetName = "flowers" # use the same name from flowers.json file
cnnModelname="./model/" + datasetName + ".meta"
saver = tf.train.import_meta_graph(cnnModelname)
# S-2 load the network weights saved
saver.restore(sess, tf.train.latest_checkpoint("./model/"))
# access the default graph which we have restored
graph = tf.get_default_graph()
# in the trained network y_pred is the tensor that is the prediction of network
y_pred = graph.get_tensor_by_name("y_pred:0")
# feed the test image to the input placeholder
x = graph.get_tensor_by_name("x:0")
y_true = graph.get_tensor_by_name("y_true:0")
y_test_images = np.zeros((1,2))
## creat feed_dic which is required to be feed inorder to calculat y_pred
feed_dic_testing = {x:image, y_true:y_test_images}
result = sess.run(y_pred, feed_dict=feed_dic_testing)
print(result)
print(tf.argmax(result, dimension=1))
cv2.imshow("prediction", imageOrig)
cv2.waitKey(0)