如何解决“运行时错误:未实现用于 op 'pythonop' 的 PyTorch 转换函数”和“Torchscript 目前不支持 Python 内置”
新手问题。我一直在尝试将 this PyTorch model 转换为 CoreML 模型。我已按照指南 here 进行操作,但无法使其正常工作。我尝试了跟踪和脚本编写,但遇到了一些错误,提示可能存在 TorchScript 不支持的操作:
torch.jit.trace
上的错误:RuntimeError: PyTorch convert function for op 'pythonop' not implemented
torch.jit.script
上的错误:RuntimeError: Python builtin <built-in method apply of FunctionMeta object at 0x7fa37e2ad600> is currently not supported in Torchscript
我怀疑可能无法将任何 PyTorch 模型转换为 CoreML 模型。是这种情况吗?我能否在不深入研究 PyTorch 操作和层的情况下以某种方式克服这些错误?
我的python脚本以防万一(模型在本地加载):
import warnings
import torch
import torch.nn as nn
import coremltools as ct
from efficientnet_pytorch import EfficientNet
from torchvision import datasets,models,transforms
from PIL import Image
# Simple loading the model
# model = torch.load('food308_efnetb2_91.31.pth',map_location=torch.device('cpu'))
# ends up with RuntimeError("Could not get name of python class object")
# Load the model
model = EfficientNet.from_pretrained('efficientnet-b2')
num_ftrs = model._fc.in_features
model._fc = nn.Linear(num_ftrs,308)
prev_state = torch.load('food308_efnetb2_91.31.pth',map_location=torch.device('cpu'))
model.load_state_dict(prev_state)
model.eval()
# Model tracing
example_input = torch.rand(1,3,224,224)
traced_model = torch.jit.trace(model,example_input)
mlmodel = ct.convert(
traced_model,inputs=[ct.TensorType(name="input",shape=(1,64,64))],)
# Model scripting
scripted_model = torch.jit.script(model)
mlmodel2 = ct.convert(
scripted_model,)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。