How to convert model format from PyTorch to tflite?

I want to convert trained model from PyTorch to tflite.
I saved trained model bellow.
torch.save(net.to(“cpu”).state_dict(), ‘mobilenet_v2.pth’)

How to convert model format from PyTorch to tflite?

python 3.5.6
pytorch 1.3.1
torch 1.4.0
torchvision 0.4.2
tensorflow 2.0.0

1 Like

We don’t officially support this. It might be possible by using ONNX.

Yes, first export to ONNX, then onward to the format of your choosing.

I tried this, but get the problem of pytorch and onnx working witch NCHW Tensor order, while tensorflow / tflite expects NHWC.
Has anyone been able to solve this?
I was trying to convert a vanilla mobilenet V2 model and ran into this issue as explained here on stackoverflow

My hunch is that Mobilenet V2 is too new for onnx…
Here is my Configuration:

torch                1.6.0.dev20200508 (needs pytorch-nightly to work with mobilenet V2 from torch.hub)
tensorflow-gpu       1.14.0
onnx                 1.6.0              
onnx-tf              1.5.0 
1 Like

Try https://github.com/nerox8664/pytorch2keras Then, export it to tflite. Six months ago, I managed to export it. However, I recommend you to try pytorch mobile. It’s almost as fast as tflite but completely flawless.