So I am trying to run an image through a frozen graph of a tensorflow model in OpenCV. Here is my OpenCV code. I have an image of Size (250, 250):
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>
#include <string>
#include <vector>
#include <dirent.h>
#include <string>
using namespace cv;
using namespace std;
int main(int argc, char** argv )
{
Mat image = imread("0_1_125_131_203.png", CV_8UC1);
Mat blob = cv::dnn::blobFromImage(image);
cv::dnn::Net net = cv::dnn::readNetFromTensorflow("output_graph.pb");
net.setInput(blob, "input_node");
Mat result = net.forward("Dense4/output_node");
cout << result << endl;
return 0;
}
Here is the tensorflow code:
import cv2
import numpy as np
import tensorflow as tf
import argparse
def load_graph(frozen_graph_filename):
with tf.gfile.GFile(frozen_graph_filename, "rb") as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
with tf.Graph().as_default() as graph:
tf.import_graph_def(graph_def, name="prefix")
return graph
if __name__ == '__main__':
image = cv2.imread("0_1_125_131_203.png", cv2.CV_8UC1)
a = np.asarray(image[:,:])
a = np.reshape(a, [1,250,250,1])
parser = argparse.ArgumentParser()
parser.add_argument("--frozen_model_filename", default="output_graph.pb", type=str, help="Frozen model file to import")
args = parser.parse_args()
graph = load_graph(args.frozen_model_filename)
x = graph.get_tensor_by_name('prefix/input_node:0')
y = graph.get_tensor_by_name('prefix/Dense4/fully_connected6/BiasAdd:0')
# We launch a Session
with tf.Session(graph=graph) as sess:
y_out = sess.run( y, feed_dict={
x: a
})
print("Result", y_out)
The only difference between the two is that OpenCV creates a blob of shape ( 1, 1, 250, 250 ) from the image, whereas I pass the image as reshaped np array of shape ( 1, 250, 250, 1 ) in tensorflow. I get the correct answer in tensorflow whereas I get a random answer in through OpenCV. Now I want to know where am I going wrong ??. Or if anyone can suggest some other C++ api to port a tensorflow frozen graph.
Aucun commentaire:
Enregistrer un commentaire