microsoft / MMdnn

MMdnn is a set of tools to help users inter-operate among different deep learning frameworks. E.g. model conversion and visualization. Convert models between Caffe, Keras, MXNet, Tensorflow, CNTK, PyTorch Onnx and CoreML.
MIT License
5.79k stars 964 forks source link

update for tensorflow v2 compatibility #917

Open vladmandic opened 3 years ago

vladmandic commented 3 years ago

This PR updates MMdnn for TensorFlow v2 compatibility by utilizing legacy support in TF2

Main reason is that TF1 requires Python 3.7 or lower and new systems come with Python 3.8 which I did not want to downgrade

Change is basically switching imports from tensorflow to tensorflow.compat.v1 and setting appropriate flags
Only additional change needed is definition of flatten layer as it has to be explicitly defined as legacy since contrib namespace no longer exists

Additional change in this PR is that emitted code does not use Inf constant as any constant may be undefined depending on the TF backend used

For example, Python or NodeJS with Tensorflow backend will resolve it fine, but GLSL code generated by TensorFlow/JS WebGL backend does not handle constants as constants in general do not exist in GLSL

I've tested this with converting Places365 ResNet152 model from Caffe to Tensorflow saved model and further using tensorflowjs_convert to convert this new TF saved model to TFJS graph model and then tested both saved model and graph model using NodeJS TensorFlow backend, WASM backend and WebGL backend

ghost commented 3 years ago

CLA assistant check
All CLA requirements met.

vladmandic commented 3 years ago

so it breaks the compatibility with older versions of the framework.

Handling both tensorflow v2 and v1 could be done with

try:
  import tensorflow.compat.v1 as tf
except ImportError:
  import tensorflow as tf

(I hate that concept, but it works)

However, it can still only be used with TensorFlow 1 models

Not really as tensorflow.compat.v1 has access to all ops from v2
so only v2 models that could not be converted are ones that use new execution flow - and that is really a) very rare, b) outside of the scope of this change

So, the name "update for TensorFlow v2 compatibility" is not so correct.

True. I'm ok with naming it anything else...

mwessley commented 3 years ago

Is there a reason why this is not merged yet? would be cool to have it in the master branch.

Doch88 commented 3 years ago

Is there a reason why this is not merged yet? would be cool to have it in the master branch.

Because this project is abandoned.