The Swift Package Index logo.Swift Package Index

跟踪 Swift 6 严格并发检查对数据竞争安全性的采用情况。有多少个 Package 已为 Swift 6 做好准备?

当使用 Xcode 项目时

当使用 Swift Package Manager 清单时

选择一个 Package 版本

1.8.1

master


服务器端 Swift 中的 TensorFlow C API 类包装器。




完美的 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

本项目是 TensorFlow C API 的一个实验性封装,允许在服务器端 Swift 中进行机器学习。

此软件包使用 Swift Package Manager 构建,是 Perfect 项目的一部分,但也可以作为独立模块使用。

请确保您已安装并激活最新的 Swift 4.1.1 / Xcode 9.3

项目状态

该框架符合 TensorFlow v1.8.0 C API 的功能。

开发说明

以下文件是 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)

所有其他名为 'pb.*.swift' 的 Swift 源文件,总计超过 45,000 行代码,均由根目录下的 updateprotos.sh 自动生成。 遗憾的是,如果使用此类脚本,您仍然需要手动编辑 PerfectTensorFlow.swift 中列出的 public typealias 部分。

到目前为止,没有计划在 Swift 源代码中动态生成这些协议缓冲区文件,因为 Perfect-TensorFlow 是 Perfect 的一部分,尽管它可以独立运行,但 Perfect 框架的所有功能都由 Swift Package Manager 构建,以保持一致性。 但是,由于该项目也在快速发展,因此欢迎所有 pull 请求、想法、建议和评论!

API 指南

API 编程主题可以在 Perfect TensorFlow 指南中找到。

此外,许多功能已经嵌入到测试脚本中,例如用于 TensorBoard 报告和基准测试的 TensorFlow 事件摘要。 请查看 Perfect TensorFlow 测试脚本 获取详细信息。

快速开始

TensorFlow C API 库安装

Perfect-TensorFlow 基于 TensorFlow C API,即运行时需要的 libtensorflow.solibtensorflow_framework.so。 本项目包含一个快速安装 CPU 版本的脚本,适用于 macOS / Ubuntu Linux,并将两个动态库安装到 /usr/local/lib 路径下。 您可以下载并运行 install.sh。 在运行此脚本之前,请确保已在您的计算机上安装了 curl

有关更多安装选项,例如 GPU/CPU 以及同一台机器上的多个版本,请查看 TensorFlow 网站:安装 TensorFlow for C

Perfect TensorFlow 应用程序

要使用此库,请将依赖项添加到您项目的 Package.swift 中,并使用**最新标签**

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

并且还需要在同一文件的 target 部分中声明依赖项

dependencies: ["PerfectTensorFlow"]

然后声明库

// 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

库激活

⚠️注意⚠️在使用 Perfect TensorFlow 框架的**任何实际功能**之前,必须首先调用 TF.Open()

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

另请注意,您可以激活带有特定路径的库,尤其是在需要不同版本或 CPU/GPU 库调整的情况下

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

"Hello, Perfect TensorFlow!"

这是 Swift 版本的 "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)

矩阵运算

正如您所看到的,Swift 版本的 TensorFlow 保持了与原始版本相同的原则,即创建张量,将张量保存到图中,定义操作,然后运行会话并检查结果。

这是 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]

加载已保存的人工神经网络模型

除了在代码中构建图和会话之外,Perfect TensorFlow 还提供了一个方便的方法来将模型加载到运行时,即通过加载模型文件来生成新的会话

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)

计算机视觉演示

可以在此仓库中找到 Perfect TensorFlow 用于计算机视觉的详细示例:Perfect TensorFlow Demo,您可以在其中上传任何本地图像或在线绘制涂鸦,以测试服务器是否可以识别图片内容

更多信息

有关 Perfect 项目的更多信息,请访问 perfect.org

现在提供微信订阅(中文)