【问题标题】:Pytorch: Convert 2D-CNN model to tflitePytorch:将 2D-CNN 模型转换为 tflite
【发布时间】:2020-08-24 01:18:59
【问题描述】:

我想将模型(例如 Mobilenet V2)从 pytorch 转换为 tflite,以便在移动设备上运行。

有人做到了吗?

我发现的只是一种使用 ONNX 将模型转换为中间状态的方法。但是,这似乎无法正常工作,因为 Tensorflow 需要 NHWC 通道顺序,而 onnx 和 pytorch 使用 NCHW 通道顺序。

有一个讨论on github,但是在我的情况下,转换工作没有抱怨,直到“冻结的张量流图模型”,在尝试将模型进一步转换为 tflite 后,它抱怨通道顺序错误......

到目前为止,这是我的代码:

import torch
import torch.onnx
import onnx
from onnx_tf.backend import prepare

# Create random input
input_data = torch.randn(1,3,224,224)

# Create network
model = torch.hub.load('pytorch/vision:v0.6.0', 'mobilenet_v2', pretrained=True)
model.eval()

# Forward Pass
output = model(input_data)

# Export model to onnx
filename_onnx = "mobilenet_v2.onnx"
filename_tf = "mobilenet_v2.pb"

torch.onnx.export(model, input_data, filename_onnx)

# Export model to tensorflow
onnx_model = onnx.load(filename_onnx)
tf_rep = prepare(onnx_model)
tf_rep.export_graph(filename_tf) 

直到这里所有工作都没有错误(忽略许多 tf 警告)。然后我使用netron(“input.1”和“473”)查找输入和输出张量的名称。

最后我将我常用的 tf-graph 从 bash 应用到 tf-lite 转换脚本:

tflite_convert \
    --output_file=mobilenet_v2.tflite \
    --graph_def_file=mobilenet_v2.pb \
    --input_arrays=input.1 \
    --output_arrays=473

我的配置:

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 

这是我从tflite 收到的确切错误消息:

Unexpected value for attribute 'data_format'. Expected 'NHWC'
Fatal Python error: Aborted

更新
更新我的配置:

torch                1.6.0.dev20200508 
tensorflow-gpu       2.2.0
onnx                 1.7.0              
onnx-tf              1.5.0 

使用

tflite_convert \
    --output_file=mobilenet_v2.tflite \
    --graph_def_file=mobilenet_v2.pb \
    --input_arrays=input.1 \
    --output_arrays=473 \
    --enable_v1_converter  # <-- needed for conversion of frozen graphs

导致另一个错误:

Exception: <unknown>:0: error: loc("convolution"): 'tf.Conv2D' op is neither a custom op nor a flex op

更新
这是通过 netron 加载的 mobilenet v2 的 onnx 模型:

Here 是我转换后的 onnx 和 pb 文件的 gdrive 链接

【问题讨论】:

  • 你可能想升级你的 tensorflow 版本,1.14 使用的是旧转换器,它不支持 2.2 那么多的模型。
  • 升级到 tensorflow 2.2 会导致另一个错误,同时转换为 tflite:&lt;unknown&gt;:0: error: loc("convolution_7012"): 'tf.Conv2D' op is neither a custom op nor a flex op。我想我需要兼容版本的精确组合。这是一个完全依赖地狱....
  • 很抱歉让您感到沮丧——这应该可以,但如果不知道 pb 中有什么就很难说。您可以在某处发布 Netron 或 graphdef 本身的屏幕截图吗?
  • @daverim 我添加了一张 netron 的图片和模型的链接(正如我所说:这些是“未受影响的”mobilenet v2 模型,所以我想它们至少应该使用一些配置

标签: pytorch onnx tensorflow-lite


【解决方案1】:

https://github.com/alibaba/TinyNeuralNetwork

你可以试试这个项目将 pytorch 模型转换为 tflite。支持torchvision中的所有模型,并且可以消除冗余算子,基本没有性能损失

【讨论】:

    【解决方案2】:

    @Ahwar 使用 Google Colab 笔记本为此发布了一个不错的 solution

    它使用

    torch                    1.5.0+cu101    
    torchsummary             1.5.1          
    torchtext                0.3.1          
    torchvision              0.6.0+cu101
    
    tensorflow               1.15.2         
    tensorflow-addons        0.8.3                  
    tensorflow-estimator     1.15.1         
    
    onnx                     1.7.0
    onnx-tf                  1.5.0
    

    转换工作正常,模型可以在我的电脑上测试。然而,当将模型推送到手机时,它仅在 CPU 模式下工作,并且比直接在 tensorflow 中创建的相应模型慢得多(几乎 10 倍)。 GPU模式在我的手机上不起作用(与直接在tensorflow中创建的相应模型相反)

    更新:
    显然,在转换 mobilenet v2 模型后,tensorflow 冻结图包含比原始 pytorch 模型更多的卷积操作(~38 000 vs ~180),如this github issue 中所述。

    【讨论】:

    • 感谢@mcExchange 支持我的回答和传播。
    猜你喜欢
    • 2022-01-02
    • 2020-05-27
    • 2021-09-27
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    • 2019-05-06
    • 2021-02-13
    相关资源
    最近更新 更多