Dhaval Trivedi
Dhaval Trivedi

Reputation: 63

TensorFlow for Swift - iOS

I want to use TensorFlow in my swift application I referred this page https://www.tensorflow.org/swift/tutorials/model_training_walkthrough. Still i have few queries from the guide tutorial as below.

  1. Is neural network calculation required in coding? (I know about concepts and working of neural network)
  2. Will it work for iOS? (I read somewhere that it only works on macOS)
  3. Is Python coding knowledge required for implementing in iOS app?
  4. Can I make a Python bridge to Swift, like Objective-C and Swift? (does iOS allow that?)
  5. Can I use TensorFlow in an existing app? If yes, then how can I implement this? I mean, is there anything like a CocoaPod or do I have to manually insert files?

In addition, please suggest some best tutorial for TensorFlow implementation in Swift.

Upvotes: 0

Views: 1121

Answers (1)

rusito23
rusito23

Reputation: 1003

DISCLAIMER

This answer is not about TensorFlow Swift, but about other ways to perform predictions using a mobile device.

ANSWER

 TL;DR

The answers for each of your questions depends on a very specific detail, which is the purpose of using Tensorflow within an iOS app. You can either be trying to train a model using this framework, or trying to perform predictions in an iOS app.

I will assume Nº 2: you are trying to perform predictions in an iOS app.

If your purpose is Nº3: I want to train my models using my computer with Swift and I don't care about running my model in a mobile app. You can still use TensorFlow Swift, but I would not know how to help.

If you want to perform predictions on a model in an iOS device (i.e use it!), you have some alternatives:

  • Tensorflow Lite (here is a quickstart)

    It's really good If you need to share the code between platforms (iOS - Android - Mac - Windows ...), as all the applications will use the same library.

  • CoreML (it's from Apple!)

    A CoreML model will only run in iOS or Mac, but you rest assured that Apple will perform great and give you some really good numbers, as I suppose this Framework makes the most out of the devices hardware.

Answering your questions:

  • Is neural network calculation required in coding?

If you are dealing with one of the alternatives I suggested above, the only contact you'll have with your model is passing the correct input, and retrieving the correct output. Of course you need to have a trained model to load inside the app, there are several tools to convert different models to CoreML or Tensorflow Lite from Keras, PyTorch, Tensorflow, etc.

  • Will it work for iOS?

Both alternatives suggested above work on iOS for model inference.

  • Is Python coding knowledge required for implementing in iOS app?

Not really, the python part is used mostly to build, train and evaluate the model. One thing that can be difficult to handle from within the app is the input and output handling. For example, if you are working with Computer Vision models, the input/output processing can be very problematic using Swift or Objective C only and I would recommend to use OpenCV with C++ for image processing.

  • Can I make a Python bridge to Swift, like Objective-C and Swift? (does iOS allow that?)

There is, but I would not recommend to use this to run your model in a mobile device. Both alternatives I suggested work very good, and in the case you need to use extra tools like OpenCV in C++, the bridge between C++ and Objective-C is really simple (use a .mm file).

  • Can I use TensorFlow in an existing app? If yes, then how can I implement this? I mean, is there anything like a CocoaPod or do I have to manually insert files?

To use CoreML you don't have to download anything extra, the framework is already available. If you are trying to use TensorFlow Lite you need to add the dependency to your carthage / Podfile / SPM (here).

To sum up, these are very good alternatives to run ML models on mobile devices which have very good results. I would recommend to read more about these and check if you can make a use of them. If you wanted to know about how to use Swift to train a model, I'm sorry but I don't have much information about that.

Here are some sample apps for each of the alternatives I suggested:

Let me know if this helps!

Upvotes: 1

Related Questions