1 | initial version |
I threw zideks graph into tensorflow and Opencv with the following scripts (some test functions i copy pasted and changed ) basically you only need to look at OpenCVTest and TensorflowResult.
import sys
import tensorflow as tf
import numpy as np
import cv2
import time
import locale
import argparse
def load_graph(model_file):
graph = tf.Graph()
graph_def = tf.GraphDef()
with open(model_file, "rb") as f:
graph_def.ParseFromString(f.read())
with graph.as_default():
tf.import_graph_def(graph_def)
return graph
def OpenCVTest(picture,graphToLoad, labels):
load_graph(graphToLoad)
image = cv2.imread(picture)
print("Size of Picture " + str(sys.getsizeof(image)))
blob= cv2.dnn.blobFromImage(image,1,(299,299))
print("size of blob " + str(sys.getsizeof(blob)) )
Net= cv2.dnn.readNetFromTensorflow(graphToLoad)
print("size of Net " + str(sys.getsizeof(Net)) )
Net.setInput(blob)
start = time.time()
preds = Net.forward()
end = time.time()
print("[INFO] classification took {:.5} seconds".format(end - start))
top = np.argsort(preds[0])[::-1][:5]
print("OpenCV: ")
for i in top:
print(i)
print(labels[i],preds[0][i])
def TensorflowResult(pic, graphName, labels):
print("Tensorflow:")
file_name = pic
model_file = graphName
#label_file = "jan_mobile_min.txt"
input_height = 299
input_width = 299
input_mean = 128
input_std = 128
input_layer = "Mul"
output_layer = "final_result"
parser = argparse.ArgumentParser()
parser.add_argument("--image", help="image to be processed")
parser.add_argument("--graph", help="graph/model to be executed")
parser.add_argument("--labels", help="name of file containing labels")
parser.add_argument("--input_height", type=int, help="input height")
parser.add_argument("--input_width", type=int, help="input width")
parser.add_argument("--input_mean", type=int, help="input mean")
parser.add_argument("--input_std", type=int, help="input std")
parser.add_argument("--input_layer", help="name of input layer")
parser.add_argument("--output_layer", help="name of output layer")
args = parser.parse_args()
if args.graph:
model_file = args.graph
if args.image:
file_name = args.image
if args.labels:
label_file = args.labels
if args.input_height:
input_height = args.input_height
if args.input_width:
input_width = args.input_width
if args.input_mean:
input_mean = args.input_mean
if args.input_std:
input_std = args.input_std
if args.input_layer:
input_layer = args.input_layer
if args.output_layer:
output_layer = args.output_layer
graph = load_graph(model_file)
t = read_tensor_from_image_file(file_name,
input_height=input_height,
input_width=input_width,
input_mean=input_mean,
input_std=input_std)
input_name = "import/" + input_layer
output_name = "import/" + output_layer
input_operation = graph.get_operation_by_name(input_name);
output_operation = graph.get_operation_by_name(output_name);
with tf.Session(graph=graph) as sess:
results = sess.run(output_operation.outputs[0],
{input_operation.outputs[0]: t})
results = np.squeeze(results)
top_k = results.argsort()[-5:][::-1]
#labels = load_labels(label_file)
for i in top_k:
print(i)
print(labels[i], results[i])
print("prefferred encoding: "+ locale.getpreferredencoding(False))
print("Tensorflow Version: "+tf.__version__)
print("Opencv Version: "+cv2.__version__)
labels = ["daisy","dandelion","roses","sunflower","tulip"]
graphToLoad = "final_graph.pb"
picture = "sun.jpg"
OpenCVTest(picture,graphToLoad, labels)
TensorflowResult(picture, graphToLoad, labels)
The following picture was used and is one of the training pictures of that Tutorial. The positions of the labels i have from his printout in his c++ script. .C:\fakepath\sun.jpg
Could be that i made a horrible mistake or something but it seems Opencv puts out the same numbers on different indexes.
the Ouptut:
prefferred encoding: cp1252 Tensorflow Version: 1.5.0 Opencv Version: 3.4.1 Size of Picture 562628 size of blob 1072956 size of Net 32 [INFO] classification took 0.27402 seconds OpenCV:
2 roses 0.98177
1 dandelion 0.0157247
4 tulip 0.00239711
0 daisy 8.73592e-05
3 sunflower 2.08934e-05
Tensorflow:
3 sunflower 0.984398
1 dandelion 0.00944433
4 tulip 0.00270193
0 daisy 0.00195503
2 roses 0.0015011
2 | No.2 Revision |
I threw zideks graph into tensorflow and Opencv with the following scripts (some test functions i copy pasted and changed ) basically you only need to look at OpenCVTest and TensorflowResult.
import sys
import tensorflow as tf
import numpy as np
import cv2
import time
import locale
import argparse
def load_graph(model_file):
graph = tf.Graph()
graph_def = tf.GraphDef()
with open(model_file, "rb") as f:
graph_def.ParseFromString(f.read())
with graph.as_default():
tf.import_graph_def(graph_def)
return graph
def OpenCVTest(picture,graphToLoad, labels):
labels):
load_graph(graphToLoad)
image = cv2.imread(picture)
print("Size of Picture " + str(sys.getsizeof(image)))
str(sys.getsizeof(image)))
blob= cv2.dnn.blobFromImage(image,1,(299,299))
print("size of blob " + str(sys.getsizeof(blob)) )
Net= cv2.dnn.readNetFromTensorflow(graphToLoad)
print("size of Net " + str(sys.getsizeof(Net)) )
Net.setInput(blob)
start = time.time()
preds = Net.forward()
end = time.time()
print("[INFO] classification took {:.5} seconds".format(end - start))
top = np.argsort(preds[0])[::-1][:5]
print("OpenCV: ")
for i in top:
print(i)
print(labels[i],preds[0][i])
def TensorflowResult(pic, graphName, labels):
print("Tensorflow:")
file_name = pic
model_file = graphName
#label_file = "jan_mobile_min.txt"
input_height = 299
input_width = 299
input_mean = 128
input_std = 128
input_layer = "Mul"
output_layer = "final_result"
parser = argparse.ArgumentParser()
parser.add_argument("--image", help="image to be processed")
parser.add_argument("--graph", help="graph/model to be executed")
parser.add_argument("--labels", help="name of file containing labels")
parser.add_argument("--input_height", type=int, help="input height")
parser.add_argument("--input_width", type=int, help="input width")
parser.add_argument("--input_mean", type=int, help="input mean")
parser.add_argument("--input_std", type=int, help="input std")
parser.add_argument("--input_layer", help="name of input layer")
parser.add_argument("--output_layer", help="name of output layer")
args = parser.parse_args()
if args.graph:
model_file = args.graph
if args.image:
file_name = args.image
if args.labels:
label_file = args.labels
if args.input_height:
input_height = args.input_height
if args.input_width:
input_width = args.input_width
if args.input_mean:
input_mean = args.input_mean
if args.input_std:
input_std = args.input_std
if args.input_layer:
input_layer = args.input_layer
if args.output_layer:
output_layer = args.output_layer
graph = load_graph(model_file)
t = read_tensor_from_image_file(file_name,
input_height=input_height,
input_width=input_width,
input_mean=input_mean,
input_std=input_std)
input_name = "import/" + input_layer
output_name = "import/" + output_layer
input_operation = graph.get_operation_by_name(input_name);
output_operation = graph.get_operation_by_name(output_name);
with tf.Session(graph=graph) as sess:
results = sess.run(output_operation.outputs[0],
{input_operation.outputs[0]: t})
results = np.squeeze(results)
top_k = results.argsort()[-5:][::-1]
#labels = load_labels(label_file)
for i in top_k:
print(i)
print(labels[i], results[i])
print("prefferred encoding: "+ locale.getpreferredencoding(False))
print("Tensorflow Version: "+tf.__version__)
print("Opencv Version: "+cv2.__version__)
labels = ["daisy","dandelion","roses","sunflower","tulip"]
graphToLoad = "final_graph.pb"
picture = "sun.jpg"
OpenCVTest(picture,graphToLoad, labels)
TensorflowResult(picture, graphToLoad, labels)
The following picture was used and is one of the training pictures of that Tutorial. The positions of the labels i have from his printout in his c++ script. .C:\fakepath\sun.jpg
Could be that i made a horrible mistake or something but it seems Opencv puts out the same numbers on different indexes.
the Ouptut:
prefferred encoding: cp1252 Tensorflow Version: 1.5.0 Opencv Version: 3.4.1 Size of Picture 562628 size of blob 1072956 size of Net 32 [INFO] classification took 0.27402 seconds OpenCV:
2 roses 0.98177
1 dandelion 0.0157247
4 tulip 0.00239711
0 daisy 8.73592e-05
3 sunflower 2.08934e-05
Tensorflow:
3 sunflower 0.984398
1 dandelion 0.00944433
4 tulip 0.00270193
0 daisy 0.00195503
2 roses 0.0015011
3 | No.3 Revision |
I threw zideks graph into tensorflow and Opencv with the following scripts (some test functions i copy pasted and changed ) basically you only need to look at OpenCVTest and TensorflowResult.
import sys
import tensorflow as tf
import numpy as np
import cv2
import time
import locale
import argparse
def load_graph(model_file):
graph = tf.Graph()
graph_def = tf.GraphDef()
with open(model_file, "rb") as f:
graph_def.ParseFromString(f.read())
with graph.as_default():
tf.import_graph_def(graph_def)
return graph
def OpenCVTest(picture,graphToLoad, labels):
load_graph(graphToLoad)
image = cv2.imread(picture)
print("Size of Picture " + str(sys.getsizeof(image)))
blob= cv2.dnn.blobFromImage(image,1,(299,299))
print("size of blob " + str(sys.getsizeof(blob)) )
Net= cv2.dnn.readNetFromTensorflow(graphToLoad)
print("size of Net " + str(sys.getsizeof(Net)) )
Net.setInput(blob)
start = time.time()
preds = Net.forward()
end = time.time()
print("[INFO] classification took {:.5} seconds".format(end - start))
top = np.argsort(preds[0])[::-1][:5]
print("OpenCV: ")
for i in top:
print(i)
print(labels[i],preds[0][i])
def TensorflowResult(pic, graphName, labels):
print("Tensorflow:")
file_name = pic
model_file = graphName
input_height = 299
input_width = 299
input_mean = 128
input_std = 128
input_layer = "Mul"
output_layer = "final_result"
parser = argparse.ArgumentParser()
parser.add_argument("--image", help="image to be processed")
parser.add_argument("--graph", help="graph/model to be executed")
parser.add_argument("--labels", help="name of file containing labels")
parser.add_argument("--input_height", type=int, help="input height")
parser.add_argument("--input_width", type=int, help="input width")
parser.add_argument("--input_mean", type=int, help="input mean")
parser.add_argument("--input_std", type=int, help="input std")
parser.add_argument("--input_layer", help="name of input layer")
parser.add_argument("--output_layer", help="name of output layer")
args = parser.parse_args()
if args.graph:
model_file = args.graph
if args.image:
file_name = args.image
if args.labels:
label_file = args.labels
if args.input_height:
input_height = args.input_height
if args.input_width:
input_width = args.input_width
if args.input_mean:
input_mean = args.input_mean
if args.input_std:
input_std = args.input_std
if args.input_layer:
input_layer = args.input_layer
if args.output_layer:
output_layer = args.output_layer
graph = load_graph(model_file)
t = read_tensor_from_image_file(file_name,
input_height=input_height,
input_width=input_width,
input_mean=input_mean,
input_std=input_std)
input_name = "import/" + input_layer
output_name = "import/" + output_layer
input_operation = graph.get_operation_by_name(input_name);
output_operation = graph.get_operation_by_name(output_name);
with tf.Session(graph=graph) as sess:
results = sess.run(output_operation.outputs[0],
{input_operation.outputs[0]: t})
results = np.squeeze(results)
top_k = results.argsort()[-5:][::-1]
#labels = load_labels(label_file)
for i in top_k:
print(i)
print(labels[i], results[i])
print("prefferred encoding: "+ locale.getpreferredencoding(False))
print("Tensorflow Version: "+tf.__version__)
print("Opencv Version: "+cv2.__version__)
labels = ["daisy","dandelion","roses","sunflower","tulip"]
graphToLoad = "final_graph.pb"
picture = "sun.jpg"
OpenCVTest(picture,graphToLoad, labels)
TensorflowResult(picture, graphToLoad, labels)
The following picture was used and is one of the training pictures of that Tutorial. The positions of the labels i have from his printout in his c++ script. .C:\fakepath\sun.jpg
Could be that i made a horrible mistake or something but it seems Opencv puts out the same numbers on different indexes.
the Ouptut:
prefferred encoding: cp1252
Tensorflow Version: 1.5.0
Opencv Version: 3.4.1
Size of Picture 562628
size of blob 1072956
size of Net 32
[INFO] classification took 0.27402 seconds
OpenCV:
seconds
OpenCV:
2 roses 0.98177
1 dandelion 0.0157247
4 tulip 0.00239711
0 daisy 8.73592e-05
3 sunflower 2.08934e-05
Tensorflow:
3 sunflower 0.984398
1 dandelion 0.00944433
4 tulip 0.00270193
0 daisy 0.00195503
2 roses 0.0015011
4 | No.4 Revision |
edit: Fixed it it was my error (but the lines below could have some use for others) it was the wrong blob: blob= cv2.dnn.blobFromImage(image,1.0/127.5,(299,299),(127.5,127.5,127.5), True, False)
I threw zideks graph into tensorflow and Opencv with the following scripts (some test functions i copy pasted and changed ) basically you only need to look at OpenCVTest and TensorflowResult.
import sys
import tensorflow as tf
import numpy as np
import cv2
import time
import locale
import argparse
def load_graph(model_file):
graph = tf.Graph()
graph_def = tf.GraphDef()
with open(model_file, "rb") as f:
graph_def.ParseFromString(f.read())
with graph.as_default():
tf.import_graph_def(graph_def)
return graph
def OpenCVTest(picture,graphToLoad, labels):
load_graph(graphToLoad)
image = cv2.imread(picture)
print("Size of Picture " + str(sys.getsizeof(image)))
blob= cv2.dnn.blobFromImage(image,1,(299,299))
print("size of blob " + str(sys.getsizeof(blob)) )
Net= cv2.dnn.readNetFromTensorflow(graphToLoad)
print("size of Net " + str(sys.getsizeof(Net)) )
Net.setInput(blob)
start = time.time()
preds = Net.forward()
end = time.time()
print("[INFO] classification took {:.5} seconds".format(end - start))
top = np.argsort(preds[0])[::-1][:5]
print("OpenCV: ")
for i in top:
print(i)
print(labels[i],preds[0][i])
def TensorflowResult(pic, graphName, labels):
print("Tensorflow:")
file_name = pic
model_file = graphName
input_height = 299
input_width = 299
input_mean = 128
input_std = 128
input_layer = "Mul"
output_layer = "final_result"
parser = argparse.ArgumentParser()
parser.add_argument("--image", help="image to be processed")
parser.add_argument("--graph", help="graph/model to be executed")
parser.add_argument("--labels", help="name of file containing labels")
parser.add_argument("--input_height", type=int, help="input height")
parser.add_argument("--input_width", type=int, help="input width")
parser.add_argument("--input_mean", type=int, help="input mean")
parser.add_argument("--input_std", type=int, help="input std")
parser.add_argument("--input_layer", help="name of input layer")
parser.add_argument("--output_layer", help="name of output layer")
args = parser.parse_args()
if args.graph:
model_file = args.graph
if args.image:
file_name = args.image
if args.labels:
label_file = args.labels
if args.input_height:
input_height = args.input_height
if args.input_width:
input_width = args.input_width
if args.input_mean:
input_mean = args.input_mean
if args.input_std:
input_std = args.input_std
if args.input_layer:
input_layer = args.input_layer
if args.output_layer:
output_layer = args.output_layer
graph = load_graph(model_file)
t = read_tensor_from_image_file(file_name,
input_height=input_height,
input_width=input_width,
input_mean=input_mean,
input_std=input_std)
input_name = "import/" + input_layer
output_name = "import/" + output_layer
input_operation = graph.get_operation_by_name(input_name);
output_operation = graph.get_operation_by_name(output_name);
with tf.Session(graph=graph) as sess:
results = sess.run(output_operation.outputs[0],
{input_operation.outputs[0]: t})
results = np.squeeze(results)
top_k = results.argsort()[-5:][::-1]
#labels = load_labels(label_file)
for i in top_k:
print(i)
print(labels[i], results[i])
print("prefferred encoding: "+ locale.getpreferredencoding(False))
print("Tensorflow Version: "+tf.__version__)
print("Opencv Version: "+cv2.__version__)
labels = ["daisy","dandelion","roses","sunflower","tulip"]
graphToLoad = "final_graph.pb"
picture = "sun.jpg"
OpenCVTest(picture,graphToLoad, labels)
TensorflowResult(picture, graphToLoad, labels)
The following picture was used and is one of the training pictures of that Tutorial. The positions of the labels i have from his printout in his c++ script. .C:\fakepath\sun.jpg
Could be that i made a horrible mistake or something but it seems Opencv puts out the same numbers on different indexes.
the Ouptut:
prefferred encoding: cp1252 Tensorflow Version: 1.5.0 Opencv Version: 3.4.1 Size of Picture 562628 size of blob 1072956 size of Net 32 [INFO] classification took 0.27402 seconds
OpenCV:
2 roses 0.98177
1 dandelion 0.0157247
4 tulip 0.00239711
0 daisy 8.73592e-05
3 sunflower 2.08934e-05
Tensorflow:
3 sunflower 0.984398
1 dandelion 0.00944433
4 tulip 0.00270193
0 daisy 0.00195503
2 roses 0.0015011