Args:
model: the tf.keras model we want to convert
input_signature: a tf.TensorSpec or a numpy array defining the shape/dtype of the input
opset: the opset to be used for the ONNX model, default is the latest
custom_ops: if a model contains ops not recognized by onnx runtime,
you can tag these ops with a custom op domain so that the
runtime can still open the model. Type is a dictionary `{op name: domain}`.
target: list of workarounds applied to help certain platforms
custom_op_handlers: dictionary of custom ops handlers
custom_rewriter: list of custom graph rewriters
extra_opset: list of extra opset's, for example the opset's used by custom ops
shape_override: dict with inputs that override the shapes given by tensorflow
inputs_as_nchw: transpose inputs in list from nchw to nhwc
large_model: use the ONNX external tensor storage format
output_path: save model to output_path
Returns:
An ONNX model_proto and an external_tensor_storage dict.
from keras.models import load_model import tensorflow as tf import os from keras import backend as K from tensorflow.python.framework import graph_util, graph_io import tensorflow as tf from tensorflow.python.platform import gfile
import cv2 from keras.models import load_model import numpy as np import onnxruntime as rt from keras.preprocessing.image import img_to_array import time
#keras推理 img = cv2.imread('1.jpg',0) img = cv2.resize(img,(28,28)) img = np.array([img_to_array(img)],dtype='float')/255.0 model = load_model('model.h5') predict = model.predict(img) p = [round(i,5) for i in predict[0]] print('\n keras :') print(p)
#opencv dnn pb img = cv2.imread('1.jpg',0) img = cv2.resize(img,(28,28)) img = np.array([img_to_array(img)],dtype='float')/255.0 img = img.transpose((0,3,1,2)) net = cv2.dnn.readNetFromTensorflow("model.pb") net.setInput(img) out = net.forward() print('\n opencv dnn pb:') p = [round(i,5) for i in out[0]] print(p)