[Python] Convert PyTorch to CoreML

Discussão em 'Python' iniciado por Stack, Novembro 29, 2024.

  1. Stack

    Stack Membro Participativo

    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.

    Continue reading...

Compartilhe esta Página