-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_tf1.py
66 lines (55 loc) · 3.15 KB
/
test_tf1.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
import argparse
import random
import os
import cv2
import preprocess
import json
import numpy as np
import tensorflow as tf
import tensorflow.contrib.decent_q
with open(os.path.join("calibration_images","imagenet_class_index.json")) as fp:
classIndex = json.load(fp)
print(classIndex["66"])
GroudTruthFile = os.path.join("calibration_images","ILSVRC2012_validation_ground_truth.txt")
with open(GroudTruthFile, "r") as f:
mapping = f.readlines()
def getImageNumber(image_name):
return int(image_name.split("_")[2].split(".")[0])
# the image is a Snake on a beach/sand field
img = cv2.imread(os.path.join("./calibration_images/ILSVRC2012_img_val","ILSVRC2012_val_00000001.JPEG"))
# Converting BGR color to RGB color format
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# img = tf.keras.preprocessing.image.load_img(os.path.join("./calibration_images/ILSVRC2012_img_val","ILSVRC2012_val_00000001.JPEG"))
# img = tf.io.decode_jpeg(img, channels=0)
# img = tf.image.decode_png(tf.io.read_file(os.path.join("./calibration_images/ILSVRC2012_img_val","ILSVRC2012_val_00000001.JPEG")))
# img = preprocess.resize_shortest_edge(img, 224)
# img = preprocess.central_crop(img, 224, 224)
# img = preprocess.tf_imagenet_preprocess(img)
img = preprocess.finalPreprocess(image=img, height=224, width=224)
img = np.expand_dims(img, axis=0)
# img = preprocess.preprocess_for_eval(img,224,224)
# img = tf.train.batch(img,1)
groundTruth = mapping[getImageNumber("ILSVRC2012_val_00000001.JPEG")-1]
tf.compat.v1.reset_default_graph()
with tf.io.gfile.GFile(f"./tf_models/mobilenet_v1_1.0_224/mobilenet_v1_1.0_224_frozen.pb", "rb") as f:
graph = tf.compat.v1.GraphDef()
graph.ParseFromString(f.read()) # This upload the model graph in RAM
tf.import_graph_def(graph,name = '') # This is the model load operation
input_data = tf.compat.v1.get_default_graph().get_tensor_by_name('input'+':0')
labels = tf.compat.v1.placeholder(tf.int64, shape=[None,])
logits = tf.compat.v1.get_default_graph().get_tensor_by_name('MobilenetV1/Predictions/Reshape_1'+':0')
nn_output = tf.argmax(logits, 1)
# correct_prediction = tf.equal(nn_output, labels)
# accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # accuracy metric
with tf.compat.v1.Session() as sess:
sess.run(tf.compat.v1.global_variables_initializer()) # An Op that initializes global variables in the graph. This is just a shortcut for variables_initializer(global_variables())
sess.run(tf.compat.v1.initializers.local_variables()) # An Op that initializes all local variables in the graph. This is just a shortcut for variables_initializer(local_variables())
# t.eval() is a shortcut for calling tf.get_default_session().run(t)
# tf.tensor.eval(feed_dict=None, session=None)
# feed_dict := A dictionary that maps Tensor objects to feed values. See tf.Session.run for a description of the valid feed values.
# acc = accuracy.eval(feed_dict={input_data: img, labels: y_test})
# print("Average accuracy on test set: {}".format(acc))
predict = nn_output.eval(feed_dict={input_data:img})
print()
print("Predict",predict, "GroudTruth", groundTruth)
print("Predict",type(predict), "GroudTruth", type(groundTruth))