I use these code to generate a transformer pb file.
import torch
import torch.nn as nn
src = torch.rand((10,32,10))
class Former(nn.Module):
def __init__(self):
super(Former, self).__init__()
self.linear1 = nn.Linear(10,512)
self.linear2 = nn.Linear(10,512)
self.transformer = nn.Transformer()
def forward(self,input):
input1 = self.linear1(input)
input2 = self.linear2(input)
output = self.transformer(input1,input2)
return output
src = torch.rand(1,1,10)
model = Former()
torch.onnx.export(model,src,"linear.onnx",verbose = True,input_names=["input"], opset_version= 11)
import onnx
from onnx_tf.backend import prepare
onnx_model = onnx.load("./transformer.onnx") # load onnx model
tf_rep = prepare(onnx_model) # prepare tf representation
tf_rep.export_graph("./transformer") # export the model
After this I want to convert the model to h5 model so I can use profile to cout its FLOPs.
from onnx2keras import onnx_to_keras
import keras
import onnx
onnx_model = onnx.load('linear.onnx')
k_model = onnx_to_keras(onnx_model, ['input'])
keras.models.save_model(k_model,'./kerasModel.h5',overwrite=True,include_optimizer=True)
But the trouble is
File "/home/kh/anaconda3/envs/3.8/lib/python3.8/site-packages/tensorflow/python/framework/constant_op.py", line 288, in _constant_impl
tensor_util.make_tensor_proto(
File "/home/kh/anaconda3/envs/3.8/lib/python3.8/site-packages/tensorflow/python/framework/tensor_util.py", line 564, in make_tensor_proto
append_fn(tensor_proto, proto_values)
File "tensorflow/python/framework/fast_tensor_util.pyx", line 127, in tensorflow.python.framework.fast_tensor_util.AppendObjectArrayToTensorProto
File "/home/kh/anaconda3/envs/3.8/lib/python3.8/site-packages/tensorflow/python/util/compat.py", line 86, in as_bytes
raise TypeError('Expected binary or unicode string, got %r' %
TypeError: Expected binary or unicode string, got 1536