Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/perfectlysoft/perfect-tensorflow
TensorFlow C API Class Wrapper in Server Side Swift.
https://github.com/perfectlysoft/perfect-tensorflow
artificial-intelligence artificial-neural-networks deep-learning machine-learning matrix-library perfect swift tensorflow
Last synced: 1 day ago
JSON representation
TensorFlow C API Class Wrapper in Server Side Swift.
- Host: GitHub
- URL: https://github.com/perfectlysoft/perfect-tensorflow
- Owner: PerfectlySoft
- License: apache-2.0
- Created: 2017-06-14T02:06:53.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-07-07T21:00:46.000Z (over 4 years ago)
- Last Synced: 2024-10-30T17:17:25.461Z (15 days ago)
- Topics: artificial-intelligence, artificial-neural-networks, deep-learning, machine-learning, matrix-library, perfect, swift, tensorflow
- Language: Swift
- Homepage:
- Size: 627 KB
- Stars: 168
- Watchers: 14
- Forks: 15
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Perfect TensorFlow [简体中文](README.zh_CN.md)
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](https://github.com/PerfectlySoft/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](GUIDE.md).
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](Tests/PerfectTensorFlowTests/PerfectTensorFlowTests.swift) 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`](https://github.com/PerfectlySoft/Perfect-TensorFlow/blob/master/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](https://www.tensorflow.org/install/install_c)
### Perfect TensorFlow Application
To use this library, add dependencies to your project's Package.swift with the **LATEST TAG**:
``` swift
.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:
``` swift
dependencies: ["PerfectTensorFlow"]
```Then declare the library:
``` swift
// 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:
``` swift
// 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:
``` swift
// 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!":
``` swift
// 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:
``` swift
/* 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:
``` swift
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](https://github.com/PerfectExamples/Perfect-TensorFlow-Demo-Vision), 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](http://perfect.org).## Now WeChat Subscription is Available (Chinese)