Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support Swift for ncnn #2642

Open
zhuzilin opened this issue Jan 29, 2021 · 2 comments
Open

support Swift for ncnn #2642

zhuzilin opened this issue Jan 29, 2021 · 2 comments

Comments

@zhuzilin
Copy link

Hi there! I noticed that ncnn has no support for Swift at the moment and wonder if you have interest in creating a cocoapod pod or SwiftPM package for ncnn? It will be a great help to iOS developers as Swift has grown to be the major pl for iOS and apple is promoting their new UI framework SwiftUI, which is purely in Swift.

I have tried to create a tiny wrapper for ncnn as a prove of concept in zhuzilin/ncnn-swift, where I used squeezenet to create a image classification app in swift.
image

The roadmap between the C++ code of ncnn to Swift I used is:

  • Create Objective-C++ code to use ncnn's C++ code.
  • Create Objective-C header to use the Objective-C++ code. (a technique from stackoverflow).
  • Use Bridge-Header to generate the interface between Objective-C and Swift.

A snippet of the swift code is:

class Squeezenet {
    let net: NcnnNet
    ...
    init?() {
        // MARK: initialize net
        net = NcnnNet()
        let paramBinPath = Bundle.main.path(forResource: "squeezenet_v1.1.param", ofType: "bin")
        guard net.loadParamBin(paramBinPath) == 0 else {
            return nil
        }
        let modelPath = Bundle.main.path(forResource: "squeezenet_v1.1", ofType: "bin")
        guard net.loadModel(modelPath) == 0 else {
            return nil
        }
        ...
    }

    func predict(for image: UIImage) -> [(Int, Float)] {
        let rgba = Image.toRgbaUInt8Array()

        let inputData: Data = Data(copyingBufferOf: rgba)
        let input: NcnnMat = NcnnMat.init(fromPixels: inputData, 65540, 227, 227)
        let mean: [NSNumber] = ...
        input.substractMeanNormalize(mean, nil)

        let output: [NSNumber: NcnnMat] = net.run([0: input], [82])
        let outputData: Data = output[82]!.toData(1000 * 4)!
        let outputProb: [Float] = outputData.toArray(type: Float.self)
        ...
    }

The run method will do all the net.input and net.extract at this same time.

I believe we could expand this idea to create a cocoapod pod for those who do not need very delicate performance optimziation, like what tflite provides. We don't need to ship the swift code to this main repo. Instead, we could create a SIG named "Swift for ncnn" for example and discuss the swift relevant issues there.

If you have interest in this, I'd love to help~

Thank you for your time on this long issue and wish you happy New Year!

Gently ping @nihui

@nihui
Copy link
Member

nihui commented Jan 29, 2021

Hello
Thank you very much for getting your hands wet in ncnn-swift.
I know very little about modern ios development.

It is appreciated that you can create some fancy stuff and help all users accessing ncnn's power more easily.

Therefore, please do what you think is useful, and if necessary, we can also make some changes to the ncnn code base for ease of use.

@zhuzilin
Copy link
Author

@nihui
Thank you for your support!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants