How to Use Bender: An Abstraction Layer for Neural Networks on iOS

Jan 12, 2023 | Data Science

Bender is a powerful tool that simplifies the process of utilizing neural networks on iOS devices using MetalPerformanceShaders. If you’re looking to leverage deep learning capabilities without the complexity of lower-level implementations, you’ve come to the right place. This guide will walk you through the essentials of getting started with Bender.

Introduction to Bender

Bender acts as a bridge between sophisticated neural network architectures and the intricacies of hardware-specific implementations like MetalPerformanceShaders. It enables developers to define and run common neural network layers, such as Convolutions and Pooling, while abstracting away much of the complexity involved.

Why Did We Need Bender?

At **[Xmartlabs](http://xmartlabs.com)**, we found MetalPerformanceShaders powerful but not very user-friendly. We often duplicated code when trying to implement neural networks in our iOS projects. Bender was born out of a need to streamline this process, allowing quick translations of TensorFlow models to formats understood by Metal, thus eliminating redundancy.

Basic Usage: Importing Your Model

Using Bender to define or run your neural network is straightforward. You can load a pre-trained model exported from TensorFlow or create one from scratch. Let’s break it down with an analogy:

Imagine Bender as a pizza restaurant where you can either choose a pre-made pizza (TensorFlow model) or customize your own (custom network). If you pick a pre-made pizza, you simply place an order (load the model), and the chef will prepare it for you. If you’re in the mood to create a pizza, you dish out your preferred toppings (layers) and let the chef know how you like it (input parameters).

Here’s how you would load a TensorFlow model:

swift
import MetalBender

let url = Bundle.main.url(forResource: myModel, withExtension: "pb")!
let network = Network.load(url: url, inputSize: LayerSize(h: 256, w: 256, f: 3))
network.run(input: /* your input */)  // output to get results

Defining Your Own Network

If you’d rather create a custom network, here’s how:

swift
let network = Network(inputSize: LayerSize(h: 256, w: 256, f: 3))
network.start
    - Convolution(convSize: ConvSize(outputChannels: 16, kernelSize: 3, stride: 2))
    - InstanceNorm()
    - Convolution(convSize: ConvSize(outputChannels: 32, kernelSize: 3, stride: 2), neuronType: .relu)
    - InstanceNorm()
    - FullyConnected(neurons: 128)
    - Neuron(type: .tanh)
    - FullyConnected(neurons: 10)
    - Softmax()
network.initialize()

Requirements

  • Xcode 9
  • iOS 11.0+ (still compatible with iOS 10.0)

Installation

To get Bender running, you can install it using CocoaPods or Carthage.

CocoaPods

Add this line to your Podfile:

ruby
pod 'MetalBender', '~> 0.5'

Carthage

Add the following line to your Cartfile:

ogdl
github "xmartlabs/Bender" ~> 0.5

Then run:

bash
carthage update --platform iOS

Troubleshooting

While working with Bender, if you encounter issues, here are some tips:

  • Ensure you have all the required dependencies installed, such as Xcode and compatible iOS versions.
  • If you face issues loading a model, verify that the model file path and its extension are correct.
  • For custom networks, check if all layers are properly defined and initialized.
  • If you’re still stuck, consult the Stack Overflow community or check older issues.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

Bender opens up a plethora of opportunities for mobile developers to harness the power of neural networks with ease. At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox