onnx-tensorflow icon indicating copy to clipboard operation
onnx-tensorflow copied to clipboard

how to use onnx to tensorflow converted model

Open farazBhatti opened this issue 3 years ago • 5 comments

I am trying to convert pytorch model to tflite. For this I've been able to convert to TensorFlow model and I want to get inference for it. I am using the following codce to load the converted saved model

with tf.gfile.FastGFile(PB_PATH, "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="")
 
input_tensor = graph.get_tensor_by_name(INPUT_TENSOR_NAME)
output_tensor = graph.get_tensor_by_name(OUTPUT_TENSOR_NAME)
 
with tf.Session(graph=graph) as sess:
    output_vals = sess.run(output_tensor, feed_dict={input_tensor: img})

but i am getting the following error

 graph_def.ParseFromString(f.read())
google.protobuf.message.DecodeError: Error parsing message

ive gone through this #892 and #498issue but no use. i am using TensorFlow 2.4.1

farazBhatti avatar Apr 15 '21 08:04 farazBhatti

Use the saved_model API to load and run the converted model such as

model_path = 'mnist_savedmodel'
m = tf.saved_model.load(model_path)
my_output=m(Input3=my_input)

where Input3 is the input name and my_input is the input data

chinhuang007 avatar Apr 16 '21 22:04 chinhuang007

screenshot of my tf model(converted from pytorch->onnx-->tf) so ive updated my code to


import json
import sys
import os
# import tensorflow as tf
import tensorflow.compat.v1 as tf


import time
import numpy as np
import cv2
import onnx
import onnxruntime
import PIL.Image as Image
from onnx import numpy_helper



def preprocess(img_path, dim):
    img = Image.open(img_path)
    img = img.resize(dim, Image.BILINEAR)
    img_data = np.array(img)
    img_data = np.transpose(img_data, [2, 0, 1])
    img_data = np.expand_dims(img_data, 0)
    mean_vec = np.array([0.485, 0.456, 0.406])
    stddev_vec = np.array([0.229, 0.224, 0.225])
    norm_img_data = np.zeros(img_data.shape).astype('float32')
    for i in range(img_data.shape[1]):
        norm_img_data[:,i,:,:] = (img_data[:,i,:,:]/255 - mean_vec[i]) / stddev_vec[i]
    return norm_img_data


w = 224
h = 224
dim = (w,h)
img_path = 'boat.jpg'
PB_PATH = "output/saved_model.pb"
img = preprocess(img_path, dim)

from google.protobuf import text_format

with tf.Session() as sess:
    print(sess)

    tf.saved_model.loader.load(sess=sess, tags=None, export_dir = "output" )
    # output_vals = sess.run(output_tensor, feed_dict={input_tensor: img})  #

pred = pred[0].reshape(1,w,h)

im = Image.fromarray(pred[0]*255).convert('RGB')

now getting following error

TypeError: 'NoneType' object is not iterable at line tf.saved_model.loader.load(sess=sess, tags=None, export_dir = "output" )

Screenshot from 2021-04-19 18-37-56

farazBhatti avatar Apr 20 '21 06:04 farazBhatti

The saved model is not used in a session in Tensorflow 2.x. Just run it directly as examples here,https://www.tensorflow.org/api_docs/python/tf/saved_model/load

chinhuang007 avatar Apr 26 '21 16:04 chinhuang007

@chinhuang007 using above mentioned method in the link I get an error of undefined session. It seems TensorFlow needs sess to run the saved model at line
tf.saved_model.load( export_dir = PB_PATH, tags=None, options=None )

Error File "tfinference.py", line 40, in <module> tf.saved_model.load(see=sess, NameError: name 'sess' is not defined


import json
import sys
import os
import tensorflow.compat.v1 as tf
import time
import numpy as np
import cv2
import PIL.Image as Image




def preprocess(img_path, dim):
    img = Image.open(img_path)
    img = img.resize(dim, Image.BILINEAR)
    img_data = np.array(img)
    img_data = np.transpose(img_data, [2, 0, 1])
    img_data = np.expand_dims(img_data, 0)
    mean_vec = np.array([0.485, 0.456, 0.406])
    stddev_vec = np.array([0.229, 0.224, 0.225])
    norm_img_data = np.zeros(img_data.shape).astype('float32')
    for i in range(img_data.shape[1]):
        norm_img_data[:,i,:,:] = (img_data[:,i,:,:]/255 - mean_vec[i]) / stddev_vec[i]
    return norm_img_data


w = 224
h = 224
dim = (w,h)
img_path = 'boat.jpg'
PB_PATH = "output"
img = preprocess(img_path, dim)

from google.protobuf import text_format

# with tf.Session() as sess:
#     print(sess)


tf.saved_model.load(
    export_dir = PB_PATH, tags=None, options=None
)


    # output_vals = sess.run(output_tensor, feed_dict={input_tensor: img})  #

pred = pred[0].reshape(1,w,h)

im = Image.fromarray(pred[0]*255).convert('RGB')

farazBhatti avatar Apr 27 '21 08:04 farazBhatti

I have the same problem. Use follow code:

tf_model = tf.saved_model.load(onnx2pb_path)
out = tf_model(x=inputdata)

I got callback pyfunc_3 is not found error.

se7enXF avatar Dec 21 '21 12:12 se7enXF