最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Pytorch模型轉(zhuǎn)onnx模型實例

 更新時間:2020年01月15日 11:37:43   作者:joey.lei  
今天小編就為大家分享一篇Pytorch模型轉(zhuǎn)onnx模型實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

如下所示:

import io
import torch
import torch.onnx
from models.C3AEModel import PlainC3AENetCBAM

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

def test():
  model = PlainC3AENetCBAM()
 
  pthfile = r'/home/joy/Projects/models/emotion/PlainC3AENet.pth'
  loaded_model = torch.load(pthfile, map_location='cpu')
  # try:
  #   loaded_model.eval()
  # except AttributeError as error:
  #   print(error)

  model.load_state_dict(loaded_model['state_dict'])
  # model = model.to(device)

  #data type nchw
  dummy_input1 = torch.randn(1, 3, 64, 64)
  # dummy_input2 = torch.randn(1, 3, 64, 64)
  # dummy_input3 = torch.randn(1, 3, 64, 64)
  input_names = [ "actual_input_1"]
  output_names = [ "output1" ]
  # torch.onnx.export(model, (dummy_input1, dummy_input2, dummy_input3), "C3AE.onnx", verbose=True, input_names=input_names, output_names=output_names)
  torch.onnx.export(model, dummy_input1, "C3AE_emotion.onnx", verbose=True, input_names=input_names, output_names=output_names)

if __name__ == "__main__":
 test()

直接將PlainC3AENetCBAM替換成需要轉(zhuǎn)換的模型,然后修改pthfile,輸入和onnx模型名字然后執(zhí)行即可。

注意:上面代碼中注釋的dummy_input2,dummy_input3,torch.onnx.export對應(yīng)的是多個輸入的例子。

在轉(zhuǎn)換過程中遇到的問題匯總

RuntimeError: Failed to export an ONNX attribute, since it's not constant, please try to make things (e.g., kernel size) static if possible

在轉(zhuǎn)換過程中遇到RuntimeError: Failed to export an ONNX attribute, since it's not constant, please try to make things (e.g., kernel size) static if possible的錯誤。

根據(jù)報的錯誤日志信息打開/home/joy/.tensorflow/venv/lib/python3.6/site-packages/torch/onnx/symbolic_helper.py,在相應(yīng)位置添加print之后,可以定位到具體哪個op出問題。

例如:

在相應(yīng)位置添加

print(v.node())

輸出信息如下:

%124 : Long() = onnx::Gather[axis=0](%122, %121), scope: PlainC3AENetCBAM/Bottleneck[cbam]/CBAM[cbam]/ChannelGate[ChannelGate] # /home/joy/Projects/models/emotion/WhatsTheemotion/models/cbam.py:46:0

原因是pytorch中的tensor.size(1)方式onnx識別不了,需要修改成常量。

以上這篇Pytorch模型轉(zhuǎn)onnx模型實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

孝昌县| 宜昌市| 铅山县| 山东| 青岛市| 怀仁县| 临夏市| 永和县| 皮山县| 惠东县| 建瓯市| 峨眉山市| 门源| 那坡县| 青龙| 商南县| 丹阳市| 牙克石市| 天津市| 苏州市| 庄河市| 靖州| 洛隆县| 威海市| 商河县| 观塘区| 河南省| 长宁区| 南岸区| 宁安市| 阿拉善盟| 阿瓦提县| 格尔木市| 中方县| 岑巩县| 贵南县| 正定县| 元氏县| 安国市| 泾源县| 青田县|