Reputation: 884
I have this model: https://github.com/williamyang1991/DualStyleGAN and try to convert it to CoreML. So far I create copy of original Colab notebook and append at the end two blocks:
!pip install coremltools
import coremltools as ct
and
#@title Convert inverted image.
inverted_latent = torch.Tensor(result_latents[0][4]).cuda().unsqueeze(0).unsqueeze(1)
with torch.no_grad():
net.eval()
[sampled_src, sampled_dst] = net(inverted_latent, input_is_latent=True)[0]
traced_model = torch.jit.trace(net, inverted_latent)
mlmodel = ct.convert(traced_model, inputs=[ct.ImageType(name="input", shape=inverted_latent.shape,bias=[-1,-1,-1],scale=2.0/255.0)])
mlmodel.save("modelsaved.mlmodel")
To run it, you should put any image with face to /content and in /usr/local/lib/python3.7/dist-packages/torchvision/transforms/functional.py replace round method at 545, 546 lines with np.round
But then it fails at
mlmodel = ct.convert(...
with:
RuntimeError: PyTorch convert function for op 'pythonop' not implemented.
I suggest that there the way to rewrite this module with methods that could be convert, am I right? But I can't to figure out how to find the source of this module.
So my question is: If I think in a right way, how I can find the source of module? And if I wrong, please advise me the right way to do it.
Upvotes: 1
Views: 1270
Reputation: 1
A bit late but it seems like you’re hitting a common snag with CoreML conversions. Some PyTorch operations, like pythonop, aren’t supported. A good next step is to trace the model’s forward pass and identify where things might be breaking. From there, tools like TorchScript can help make the conversion smoother.
I ran into similar challenges while converting the Wav2Lip model to CoreML and posted a guide with tips and steps that might help:
https://github.com/Ialzouby/Wav2Lip-PyTorch-To-CoreML/
Hope it helps, and good luck!
Upvotes: 0
Reputation: 50
The code starts by loading the model into PyTorch. The code then converts the model into CoreML format and saves it to a .mlmodel file.The code below will take the existing PyTorch model and convert it into a CoreML model with input and output features. The outputs are saved in the file example.mlmodel which can be opened in Xcode or any other development environment that supports CoreML models.
import torch
import coremltools
model = torch.load('MyPyTorchModel.pt')
coreml_model = coremltools.converters.pytorch.from_pytorch(model,
input_features=
['input'],
output_features=
['output'])
coreml_model.save('MyCoreMLModel.mlmodel')
Upvotes: -1