[Python] How to Convert TensorFlow Arbitrary Image Stylization Model to Core ML for Swift?

Discussão em 'Python' iniciado por Stack, Fevereiro 5, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to implement arbitrary image stylization in Swift using TensorFlow's pre-trained model. After downloading the model, I got a saved_model.pb file. However, iOS does not support this format directly, so I tried converting it to Core ML using coremltools.

    Python Code for Conversion:

    import coremltools as ct

    # Load the TensorFlow model
    model = ct.convert(
    "/Users/kirit/Downloads/saved_model", # e.g., a SavedModel directory
    inputs=[
    ct.ImageType(shape=(1, 256, 256, 3), name="contentImage", scale=1/255),
    ct.ImageType(shape=(1, 256, 256, 3), name="styleImage", scale=1/255)
    ],
    outputs=[ct.TensorType(name="stylizedImage")],
    source="tensorflow"
    )

    # Save the Core ML model
    model.save("DualInputStylization.mlmodel")



    Issue:

    • The conversion process always fails with various errors.
    • Common errors include:
    • Failed to load _MLModelProxy: No module named 'coremltools.libcoremlpython'
    • Converter was called with source="tensorflow", but missing tensorflow package
    • TensorFlow import errors due to missing imp module in Python 3.13

    System Information: Python Version: 3.13.1 TensorFlow Version: (Latest) coremltools Version: (Latest) OS: macOS (15.1.1 (24B91))

    Questions:

    • How can I successfully convert a TensorFlow saved_model.pb for arbitrary image stylization to a Core ML mlmodel format?
    • Is there an alternative approach for using this model in Swift?
    • Is there a specific TensorFlow version that works best for Core ML conversion? Any guidance would be appreciated! Thanks.

    Continue reading...

Compartilhe esta Página