TensorFlow C API Class Wrapper in Server Side Swift.

Overview

Perfect TensorFlow 简体中文

Get Involved with Perfect!

Star Perfect On Github Stack Overflow Follow Perfect on Twitter Join the Perfect Slack

Swift 4.1 Platforms OS X | Linux License Apache PerfectlySoft Twitter Slack Status

This project is an experimental wrapper of TensorFlow C API which enables Machine Learning in Server Side Swift.

This package builds with Swift Package Manager and is part of the Perfect project but can also be used as an independent module.

Ensure you have installed and activated the latest Swift 4.1.1 / Xcode 9.3

Project Status

The framework conforms to TensorFlow v1.8.0 C API functionality.

Development Notes

These files are the key part of Perfect-TensorFlow:

Sources
├── PerfectTensorFlow
│   ├── APILoader.swift (1000+ lines, translated from tensorflow/c/c_api.h)
│   ├── PerfectTensorFlow.swift (2700+ lines)
└── TensorFlowAPI
    ├── TensorFlowAPI.c (72 lines)
    └── include
        └── TensorFlowAPI.h (138 lines)

All other Swift sources named as 'pb.*.swift', which is totally up to 45,000+ lines of code, are automatically generated by updateprotos.sh in the root directory. Unfortunately, if using such a script, you still need to manually edit the public typealias part listed in the PerfectTensorFlow.swift.

Up to now there is no such a plan to generate these protocol buffer files dynamically in the Swift Source since Perfect-TensorFlow is a part of Perfect, although it can run independently, all features of Perfect framework are built by Swift Package Manager for consistency consideration. However, since the project is also fast growing, all pull request, ideas, suggestions and comments are welcome!

API Guide

API programming topics can be found in Perfect TensorFlow Guide.

Also, there are many features that has already embedded in the testing script, such as TensorFlow event and summary for TensorBoard report and benchmarking. Please check the Perfect TensorFlow Testing Script for detail.

Quick Start

TensorFlow C API Library Installation

Perfect-TensorFlow is based on TensorFlow C API, i.e., libtensorflow.so and libtensorflow_framework.so on runtime. This project contains an express CPU version installation script for this module on both macOS / Ubuntu Linux, and will install both dynamic libraries into path /usr/local/lib. You can download & run install.sh. Before running this script, please make sure that curl has been installed onto your computer.

For more installation options, such as GPU/CPU and multiple versions on the same machine, please check TensorFlow website: Installing TensorFlow for C

Perfect TensorFlow Application

To use this library, add dependencies to your project's Package.swift with the LATEST TAG:

.package(url: "https://github.com/PerfectlySoft/Perfect-TensorFlow.git", from: "1.4.0")

and it also requires a dependency declaration in the same file, target section:

dependencies: ["PerfectTensorFlow"]

Then declare the library:

// TensorFlowAPI contains most API functions defined in libtensorflow.so
import TensorFlowAPI

// This is the Swift version of TensorFlow classes and objects
import PerfectTensorFlow

// To keep the naming consistency with TensorFlow in other languages such as
// Python or Java, making an alias of `TensorFlow` Class is a good idea:
public typealias TF = TensorFlow

Library Activation

⚠️ NOTE ⚠️ Prior to use ANY ACTUAL FUNCTIONS of Perfect TensorFlow framework, TF.Open() must be called first:

// this action will load all api functions defined
// in /usr/local/lib/libtensorflow.so
try TF.Open()

Please also note that you can active the library with a specific path, alternatively, especially in case of different versions or CPU/GPU library adjustment required:

// this action will load the library with the path
try TF.Open("/path/to/DLL/of/libtensorflow.so")

"Hello, Perfect TensorFlow!"

Here is the Swift version of "Hello, TensorFlow!":

// define a string tensor
let tensor = try TF.Tensor.Scalar("Hello, Perfect TensorFlow! 🇨🇳🇨🇦")

// declare a new graph
let g = try TF.Graph()

// turn the tensor into an operation
let op = try g.const(tensor: tensor, name: "hello")

// run a session
let o = try g.runner().fetch(op).addTarget(op).run()

// decode the result      
let decoded = try TF.Decode(strings: o[0].data, count: 1)

// check the result
let s2 = decoded[0].string
print(s2)

Matrix Operations

As you can see, Swift version of TensorFlow keeps the same principals of the original one, i.e., create tensors, save tensors into graph, define the operations and then run the session & check the result.

Here is an other simple example of matrix operations in Perfect TensorFlow:

/* Matrix Multiply:
| 1 2 |   |0 1|   |0 1|
| 3 4 | * |0 0| = |0 3|
*/
// input the matrix.
let tA = try TF.Tensor.Matrix([[1, 2], [3, 4]])
let tB = try TF.Tensor.Matrix([[0, 0], [1, 0]])

// adding tensors to graph
let g = try TF.Graph()
let A = try g.const(tensor: tA, name: "Const_0")
let B = try g.const(tensor: tB, name: "Const_1")

// define matrix multiply operation
let v = try g.matMul(l: A, r: B, name: "v", transposeB: true)

// run the session
let o = try g.runner().fetch(v).addTarget(v).run()
let m:[Float] = try o[0].asArray()
print(m)
// m shall be [0, 1, 0, 3]

Load a Saved Artificial Neural Network Model

Besides building graph & sessions in code, Perfect TensorFlow also provides a handy method to load models into runtime, i.e, generate a new session by loading a model file:

let g = try TF.Graph()

// the meta signature info defined in a saved model
let metaBuf = try TF.Buffer()

// load the session
let session = try g.load(
	exportDir: "/path/to/saved/model",
	tags: ["tag1", "tag2", ...],
	metaGraphDef: metaBuf)

Computer Vision Demo

A detailed example of Perfect TensorFlow for Computer Vision can be found in this repo: Perfect TensorFlow Demo, where you can upload any local images or draw a scribble online to test if the server can recognize the picture content:

Further Information

For more information on the Perfect project, please visit perfect.org.

Now WeChat Subscription is Available (Chinese)

You might also like...
Tiny YOLO for iOS implemented using CoreML but also using the new MPS graph API.
Tiny YOLO for iOS implemented using CoreML but also using the new MPS graph API.

YOLO with Core ML and MPSNNGraph This is the source code for my blog post YOLO: Core ML versus MPSNNGraph. YOLO is an object detection network. It can

A Swift library for creating and exporting CoreML Models in Swift

SwiftCoreMLTools A Swift Library for creating CoreML models in Swift. Work in progress This library expose a (function builder based) DSL as well as a

Accelerated tensor operations and dynamic neural networks based on reverse mode automatic differentiation for every device that can run Swift - from watchOS to Linux
Accelerated tensor operations and dynamic neural networks based on reverse mode automatic differentiation for every device that can run Swift - from watchOS to Linux

DL4S provides a high-level API for many accelerated operations common in neural networks and deep learning. It furthermore has automatic differentiati

The Swift machine learning library.
The Swift machine learning library.

Swift AI is a high-performance deep learning library written entirely in Swift. We currently offer support for all Apple platforms, with Linux support

BrainCore is a simple but fast neural network framework written in Swift.

BrainCore is a simple but fast neural network framework written in Swift. It uses Metal which makes it screamin' fast. If you want to see it

MLKit is a simple machine learning framework written in Swift.
MLKit is a simple machine learning framework written in Swift.

MLKit (a.k.a Machine Learning Kit) 🤖 MLKit is a simple machine learning framework written in Swift. Currently MLKit features machine learning algorit

Matft is Numpy-like library in Swift. Function name and usage is similar to Numpy.
Matft is Numpy-like library in Swift. Function name and usage is similar to Numpy.

Numpy-like library in swift. (Multi-dimensional Array, ndarray, matrix and vector library)

Artificial intelligence/machine learning data structures and Swift algorithms for future iOS development. bayes theorem, neural networks, and more AI.

Swift Brain The first neural network / machine learning library written in Swift. This is a project for AI algorithms in Swift for iOS and OS X develo

A toolbox of AI modules written in Swift:  Graphs/Trees, Support Vector Machines, Neural Networks, PCA, K-Means, Genetic Algorithms
A toolbox of AI modules written in Swift: Graphs/Trees, Support Vector Machines, Neural Networks, PCA, K-Means, Genetic Algorithms

AIToolbox A toolbox of AI modules written in Swift: Graphs/Trees, Linear Regression, Support Vector Machines, Neural Networks, PCA, KMeans, Genetic Al

Comments
  • macOS system sandbox blocked open() of \'/usr/local/lib/libtensorflow.so\'

    macOS system sandbox blocked open() of \'/usr/local/lib/libtensorflow.so\'

    So I'm trying to run this on macOS Big Sur 11.6.1 and I'm getting

    Fatal error: 'try!' expression unexpectedly raised an error: 
    PerfectTensorFlow.TFLib.Panic.DLL(reason: "dlopen(/usr/local/lib/libtensorflow.so, 2): no suitable image found.  
    Did find:\n\tfile system sandbox blocked open() of \'/usr/local/lib/libtensorflow.so\'")
    

    I tried giving the program full disk access, by dragging the binary generated in the Products folder in xcode into the the program list in System Preferences -> Security & Privacy -> Privacy -> Full Disk Access. But, that did not allow my program to access outside the sandbox. Any ideas?

    opened by luca992 1
  • fault: DLL(reason:

    fault: DLL(reason: "dlopen(/usr/local/lib/libtensorflow.so, 2): image not found")

    Issues

    git clone https://github.com/PerfectExamples/Perfect-TensorFlow-Demo-Vision.git cd Perfect-TensorFlow-Demo-Vision ./install.sh ./.build/debug/PerfectTensorFlowDemo

    I get error as below: fault: DLL(reason: "dlopen(/usr/local/lib/libtensorflow.so, 2): image not found")

    opened by lunnersword 1
Releases(1.8.1)
Owner
PerfectlySoft Inc.
Server-side Swift
PerfectlySoft Inc.
Swift for TensorFlow

Swift for TensorFlow (Archived) Swift for TensorFlow was an experiment in the next-generation platform for machine learning, incorporating the latest

null 6.1k Dec 31, 2022
BetterMood is an iOS app that uses Tensorflow to recognize user’s emotions

BetterMood is an iOS app that uses Tensorflow to recognize user’s emotions, convert it into categories then send via our api along with the user’s date of birth and name, to end up with a emotion analyse and horoscope prediction.

Yosri 2 Sep 30, 2021
Easily craft fast Neural Networks on iOS! Use TensorFlow models. Metal under the hood.

Bender Bender is an abstraction layer over MetalPerformanceShaders useful for working with neural networks. Contents Introduction Why did we need Bend

xmartlabs 1.7k Dec 24, 2022
Flutter Piano Audio Detection implemented with Tensorflow Lite Model (Google Magenta)

FlutterPianoAudioDetection Plugin Flutter Piano Audio Detection implemented with Tensorflow Lite Model (Google Magenta) Android Implementation iOS/iPa

WonyJeong 27 Dec 29, 2022
[yolov5] + [ios] + [tensorflow lite]

YOLOv5 - TensorFlow Lite Object Detection iOS Example Application iOS Versions Supported: iOS 12.0 and above. Xcode Version Required: 10.0 and above O

Inpyo Hong 14 Dec 12, 2022
Models and examples built with TensorFlow

Welcome to the Model Garden for TensorFlow The TensorFlow Model Garden is a repository with a number of different implementations of state-of-the-art

null 74.9k Dec 29, 2022
🤗 Transformers: State-of-the-art Machine Learning for Pytorch, TensorFlow, and JAX.

English | 简体中文 | 繁體中文 | 한국어 State-of-the-art Machine Learning for JAX, PyTorch and TensorFlow ?? Transformers provides thousands of pretrained models

Hugging Face 77.1k Dec 31, 2022
Pose Estimation on iOS with TensorFlow Lite

This project is Pose Estimation on iOS with TensorFlow Lite. If you are interested in iOS + Machine Learning, visit here you can see various DEMOs. 2D

tucan9389 125 Nov 28, 2022
A simple Swift package which acts as an OpenAI client for the GPT-3 API.

SwiftyGPT3 A simple Swift package which acts as an OpenAI client for GPT-3 brought to you by the Airgift Crew. Supports GPT-3 Codex! Requirements iOS

Airgift 23 Dec 25, 2022
DL4S provides a high-level API for many accelerated operations common in neural networks and deep learning.

DL4S provides a high-level API for many accelerated operations common in neural networks and deep learning. It furthermore has automatic differentiati

DL4S Team 2 Dec 5, 2021