Skip to content

CoreML custom layer (GPU-accelerated) and converter for torchvision.ops.deform_conv2d

Notifications You must be signed in to change notification settings

dneprDroid/DeformConv2d-Metal

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DeformConv2d-Metal

CoreML custom layer (GPU-accelerated) and converter for torchvision.ops.deform_conv2d.

Demo

Convert the demo ml-model with torchvision.ops.deform_conv2d operation to CoreML format:

# Run in the root dir:

python3 -m converter.demo

It'll save the ml-model and example input/output tensors to the DemoApp/generated directory so the demo app can validate the CoreML output results and compare them with the PyTorch output.

The converted ML-model contains custom layers:

Screenshot 2024-05-27 at 12 55 43

iOS and macOS demo apps

Open DemoApp/DemoApp.xcodeproj in Xcode and run the demo app.

The Test-iOS target contains the demo for iOS.

The Test-macOS target contains the demo for macOS.

In MLModelTestWorker it loads the generated CoreML model and the example input tensor from the DemoApp/generated directory and compares the calculated CoreML output tensor with the PyTorch example output tensor from the DemoApp/generated directory:

Screenshot 2024-05-28 at 12 21 52

Custom layers

Custom layers are located in DeformConv2dMetal/Sources/CustomOps.

Use in your project

Install this pip package and import it in your converter script:

pip install git+https://github.com/dneprDroid/DeformConv2d-Metal.git
import DeformConv2dConvert

...

# register op so CoreML Tools can find the converter function  
DeformConv2dConvert.register_op()

# and convert your model...
...

NOTE: In the coremltools.convert function you need to set convert_to="neuralnetwork":

mlmodel = coremltools.convert(
    traced_model,
    inputs=...,
    outputs=...,
    convert_to="neuralnetwork"
)

iOS/macOS app

In your iOS/macOS app add the SwiftPM package from this repository:

https://github.com/dneprDroid/DeformConv2d-Metal.git

CoreML should find and load the custom layers from the DeformConv2dMetal module automatically, so you don't need to do anything.

About

CoreML custom layer (GPU-accelerated) and converter for torchvision.ops.deform_conv2d

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published