transformers icon indicating copy to clipboard operation
transformers copied to clipboard

Converting Transformers model to Tensorflow model

Open hakanbogan opened this issue 6 years ago • 6 comments

I am trying to convert dbmdz/bert-base-turkish-uncased to tensorflow model (.pb). It contains tf_model.h5 file.

I tried to convert from tf_model.h5 to tensorflow model. However, I couldn't handle it. Is tf_model.h5 file keras model, isn’t it?

Is there any instruction to convert huggingface non-tf models to tensorflow model(.pb)?

hakanbogan avatar Oct 26 '20 09:10 hakanbogan

I don't believe we have a way of converting keras models from .h5 to .pb. What is your use case, so that we can see if we may help further?

LysandreJik avatar Oct 26 '20 12:10 LysandreJik

I want to improve the inference performance. I fine-tuned the pretrained model (dbmdz/bert-base-turkish-uncased). TensorRT can be helpful to reduce inference time. Use case is converting Keras model to TensorRT model. How can I achieve this?

P.S.: I am open to any advice. Tensorflow Lite or other things can be used. I just need some guidance.

hakanbogan avatar Oct 26 '20 13:10 hakanbogan

@mfuntowicz I believe you have experience with using TensorRT with the transformers models. Do you have any idea of how to enable this?

LysandreJik avatar Oct 26 '20 15:10 LysandreJik

You can try a couple of ways :

  1. Keras -> Onnx -> TensorRT : You can use keras2onnx . You might have some operations that are not directly supported in onnx so you'll have to remove/edit those operations to convertible terms. Converting onnx to TRT can be done with a tool called "trtexec" or there are readymade scripts to do that. Do check model graph and accuracy at every step.
  2. Keras -> Frozen graph(.pb) -> onnx -> TensorRT

You can also use TF-TRT which again optimizes but less optimized than TensorRT.

There is another way you can explore Keras -> UFF -> TensorRT .

zerocool95 avatar Oct 26 '20 16:10 zerocool95

Thank you for leading @zerocool95

hakanbogan avatar Nov 03 '20 07:11 hakanbogan

i also face this problem, and i find a exits onnx file on onnx github, and it said it was transfered from huggingface, but when i use trtexec to covert onnx ---> trt . it say it don;'t support some ops like 'NoZero'

Light-Reflection avatar Sep 17 '21 06:09 Light-Reflection