onnx / onnx-tensorflow

Tensorflow Backend for ONNX
Other
1.29k stars 296 forks source link

Export model containing dropout. #877

Open poor1017 opened 3 years ago

poor1017 commented 3 years ago

Describe the bug

I need export a torch model which contains dropout into tensorflow. For this purpose I use PyTorch —> ONNX —> Tensorflow approach. But the generated pb has no dropout.

To Reproduce

import onnx from onnx_tf.backend import prepare onnx_model = onnx.load('ln.onnx') pb_model = prepare(onnx_model)

ONNX model file

https://www.dropbox.com/s/bx3b1ml2xwhx3v8/ln.onnx?dl=0

Python, ONNX, ONNX-TF, Tensorflow version

This section can be obtained by running get_version.py from util folder.

chinhuang007 commented 3 years ago

Your dropout has only one input 'data'. According to the spec, https://github.com/onnx/onnx/blob/master/docs/Operators.md#Dropout, 'If this input (ratio) was not set, or if it was set to 0, the output would be a simple copy of the input.', that is why the converted model has no dropout.

poor1017 commented 3 years ago

Thank you for quickly reply.

You mean that I need modify code for exporting onnx model?

The following is correct? torch.onnx.export(ln_model, data_input, 'ln.onnx', opset_version=10, verbose=True, training_mode=True, do_constant_folding=True, input_names=['hs'], output_names=['logit'], dynamic_axes={'hs': {1:'sequence'}} )

chinhuang007 commented 3 years ago

I am not sure you need to modify code since dropout is supposed to be a no-op (doing nothing) in inference. I am not familiar with Pytorch exporter. The bottom line is, the generated ONNX dropout node must have 1. training_mode is True 2. ratio is provided as non-zero for dropout to be in the converted Tensorflow model.

poor1017 commented 3 years ago

Thank you for your advice. Could you show me a example ONNX model which the dropout node have 1 and 2. Thank you.

chinhuang007 commented 3 years ago

I don't have such an example. Perhaps you open an issue in ONNX core repo, https://github.com/onnx/onnx/issues