Newbie question. I've been trying to convert this PyTorch model into CoreML model. I've followed the guide here but couldn't make it work. I tried tracing and scripting but faced errors which hint that there might be an operation not supported in TorchScript:

Error on torch.jit.trace: RuntimeError: PyTorch convert function for op 'pythonop' not implemented

Error on torch.jit.script: RuntimeError: Python builtin <built-in method apply of FunctionMeta object at 0x7fa37e2ad600> is currently not supported in Torchscript

I suspect that it just might not be possible to convert any PyTorch model into CoreML one. Is this the case? Can I somehow overcome the errors without diving deep into PyTorch operations and layers?

My python script just in case (model is loaded locally):

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, 3, 64, 64))],
)

# Model scripting
scripted_model = torch.jit.script(model)
mlmodel2 = ct.convert(
    scripted_model,
    inputs=[ct.TensorType(name="input", shape=(1, 3, 64, 64))],
)
0

There are 0 best solutions below