Package: onnx2keras-tl-fork
Tensorleap fork of onnx2kerasONNX to Keras deep neural network converter.RequirementsTensorFlow 2.0APIonnx_to_keras(onnx_model, input_names, input_shapes=None, name_policy=None, verbose=True, change_ordering=False) -> {Keras model}onnx_model: ONNX model to convertinput_names: list with graph input namesinput_shapes: override input shapes (experimental)name_policy: ['renumerate', 'short', 'default'] override layer names (experimental)verbose: detailed outputchange_ordering:change ordering to HWC (experimental)Getting startedONNX modelimportonnxfromonnx2kerasimportonnx_to_keras# Load ONNX modelonnx_model=onnx.load('resnet18.onnx')# Call the converter (input - is the main model input name, can be different for your model)k_model=onnx_to_keras(onnx_model,['input'])Keras model will be stored to thek_modelvariable. So simple, isn't it?PyTorch modelUsing ONNX as intermediate format, you can convert PyTorch model as well.importnumpyasnpimporttorchfromtorch.autogradimportVariablefrompytorch2keras.converterimportpytorch_to_kerasimporttorchvision.modelsasmodelsif__name__=='__main__':input_np=np.random.uniform(0,1,(1,3,224,224))input_var=Variable(torch.FloatTensor(input_np))model=models.resnet18()model.eval()k_model=\pytorch_to_keras(model,input_var,[(3,224,224,)],verbose=True,change_ordering=True)foriinrange(3):input_np=np.random.uniform(0,1,(1,3,224,224))input_var=Variable(torch.FloatTensor(input_np))output=model(input_var)pytorch_output=output.data.numpy()keras_output=k_model.predict(np.transpose(input_np,[0,2,3,1]))error=np.max(pytorch_output-keras_output)print('error -- ',error)# Around zero :)Deplying model as frozen graphYou can try using the snippet below to convert your onnx / PyTorch model to frozen graph. It may be useful for deploy for Tensorflow.js / for Tensorflow for Android / for Tensorflow C-API.importnumpyasnpimporttorchfrompytorch2keras.converterimportpytorch_to_kerasfromtorch.autogradimportVariableimporttensorflowastffromtensorflow.python.framework.convert_to_constantsimportconvert_variables_to_constants_v2# Create and load modelmodel=Model()model.load_state_dict(torch.load('model-checkpoint.pth'))model.eval()# Make dummy variables (and checking if the model works)input_np=np.random.uniform(0,1,(1,3,224,224))input_var=Variable(torch.FloatTensor(input_np))output=model(input_var)# Convert the model!k_model=\pytorch_to_keras(model,input_var,(3,224,224),verbose=True,name_policy='short',change_ordering=True)# Save model to SavedModel formattf.saved_model.save(k_model,"./models")# Convert Keras model to ConcreteFunctionfull_model=tf.function(lambdax:k_model(x))full_model=full_model.get_concrete_function(tf.TensorSpec(k_model.inputs[0].shape,k_model.inputs[0].dtype))# Get frozen ConcreteFunctionfrozen_func=convert_variables_to_constants_v2(full_model)frozen_func.graph.as_graph_def()print("-"*50)print("Frozen model layers: ")forlayerin[op.nameforopinfrozen_func.graph.get_operations()]:print(layer)print("-"*50)print("Frozen model inputs: ")print(frozen_func.inputs)print("Frozen model outputs: ")print(frozen_func.outputs)# Save frozen graph from frozen ConcreteFunction to hard drivetf.io.write_graph(graph_or_graph_def=frozen_func.graph,logdir="./frozen_models",name="frozen_graph.pb",as_text=False)LicenseThis software is covered by MIT License.