IOS平台构建TensorFlow应用教程详解(附源码)(二)
更多深度文章,请关注云计算频道:https://yq.aliyun.com/cloud
推特地址:https://twitter.com/mhollemans
邮件地址:mailto:matt@machinethink.net
github地址:https://github.com/hollance
个人博客:https://machinethink.net/
前面已经训练好模型,下面创建一个利用TensorFlow C++ 库和这个模型的app。坏消息是你不得不从源构建TensorFlow,还需要使用Java环境;好消息是这个过程相对简单。完整的指导在这里,但是下面几步很重要(测试环境为TensorFlow 1.0)。
sudo xcode-select -s /Applications/Xcode.app/Contents/Developer
brew cask install java
brew install bazel
brew install automake
brew install libtool
完成之后,你需要克隆TensorFlow GitHub仓库。注意,一定要保存在没有空格的路径下,否则bazel会拒绝构建。我是克隆到我的主目录下:
cd /Users/matthijs
git clone https://github.com/tensorflow/tensorflow -b r1.0
-b r1.0表明克隆的是r1.0分支。当然你也可以随时获取最新的分支或者主分支。
Note:在MacOS Sierra 上,运行下面的配置脚本报错了,我只能克隆主分支来代替。在OS X EI Caption 上使用r1.0分支就不会有任何问题。
一旦GitHub仓库克隆完毕,你就需要运行配置脚本(configure script):
cd tensorflow
./configure
Please specify the location of python. [Default is /usr/bin/python]:
我写的是/usr/local/bin/python3,因为我使用的是Python 3.6。如果你选择默认选项,就会使用Python 2.7来创建TensorFlow。
Please specify optimization flags to use during compilation [Default is
-march=native]:
tensorflow/contrib/makefile/build_all_ios.sh
这个脚本首先会下载一些依赖项,然后开始构建。一切顺利的话,它会创建三个链入你的app的静态库:libtensorflow-core.a, libprotobuf.a, libprotobuf-lite.a。
bazel build tensorflow/python/tools:freeze_graph
bazel build tensorflow/python/tools:optimize_for_inference
Note: 这个过程至少需要20分钟,因为它会从头开始构建TensorFlow(本次使用的是bazel)。如果遇到问题,请参考官方指导。
现在你就可以创建一个自定义的TensorFlow版本。例如,当运行train.py脚本时,如果出现“The TensorFlow library wasn’t compiled to use SSE4.1 instructions”提醒,你可以编译一个允许这些指令的TensorFlow版本。
bazel build --copt=-march=native -c opt //tensorflow/tools/pip_package:build_pip_package
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
pip3 uninstall tensorflow
sudo -H pip3 install /tmp/tensorflow_pkg/tensorflow-1.0.0-XXXXXX.whl
更多详细指令请参考TensorFlow网站。
我们将要创建的app会载入之前训练好的模型,并作出预测。之前在train.py中,我们将图保存到了 /tmp/voice/graph.pb文件中。但是你不能在IOS app中直接载入这个计算图,因为图中的部分操作是TensorFlow C++库并不支持。所以就需要用到上面我们构建的那两个工具。
freeze_graph将包含训练好的w和b的graph.pb和检查点文件合成为一个文件,并移除IOS不支持的操作。在终端运行TensorFlow目录下的这个工具:
bazel-bin/tensorflow/python/tools/freeze_graph \
--input_graph=/tmp/voice/graph.pb --input_checkpoint=/tmp/voice/model \
--output_node_names=model/y_pred,inference/inference --input_binary \
--output_graph=/tmp/voice/frozen.pb
最终输出/tmp/voice/frozen.pb文件,只包含得到y_pred和inference的节点,不包括用来训练的节点。freeze_graph也将权重保存进了文件,就不用再单独载入。
optimize_for_inference工具进一步简化了可计算图,它以frozen.pb作为输入,以/tmp/voice/inference.pb作为输出。这就是我们将嵌入IOS app中的文件,按如下方式运行这个工具:
bazel-bin/tensorflow/python/tools/optimize_for_inference \
--input=/tmp/voice/frozen.pb --output=/tmp/voice/inference.pb \
--input_names=inputs/x --output_names=model/y_pred,inference/inference \
--frozen_graph=True
你可以在VoiceTensorFlow 文件夹下找到这个app。用Xcode打开这个项目,有几处需要注意:
/Users/matthijs/tensorflow/tensorflow/contrib/makefile/gen/protobuf_ios/lib/
libprotobuf-lite.a
/Users/matthijs/tensorflow/tensorflow/contrib/makefile/gen/protobuf_ios/lib/
libprotobuf.a
-force_load /Users/matthijs/tensorflow/tensorflow/contrib/makefile/gen/lib/
libtensorflow-core.a
~/tensorflow
~/tensorflow/tensorflow/contrib/makefile/downloads
~/tensorflow/tensorflow/contrib/makefile/downloads/eigen
~/tensorflow/tensorflow/contrib/makefile/downloads/protobuf/src
~/tensorflow/tensorflow/contrib/makefile/gen/proto
1.从.pb文件中载入计算图和权重;
- (BOOL)loadGraphFromPath:(NSString *)path
{
auto status = ReadBinaryProto(tensorflow::Env::Default(),
path.fileSystemRepresentation, &graph);
if (!status.ok()) {
NSLog(@"Error reading graph: %s", status.error_message().c_str());
return NO;
}
return YES;
}
Xcode项目包含在 graph.pb上运行freeze_graph 和optimize_for_inference工具得到的inference.pb图。如果你试图载入graph.pb,会报错:
Error adding graph to session: No OpKernel was registered to support Op
'L2Loss' with these attrs. Registered devices: [CPU], Registered kernels:
<no registered kernels>
[[Node: loss-function/L2Loss = L2Loss[T=DT_FLOAT](model/W/read)]]
这个C++ API 支持的操作要比Python API少。这里他说的是损失函数节点中L2Loss操作在IOS上不支持。这就是为什么我们要使用freeze_graph简化图。
- (BOOL)createSession
{
tensorflow::SessionOptions options;
auto status = tensorflow::NewSession(options, &session);
if (!status.ok()) {
NSLog(@"Error creating session: %s",
status.error_message().c_str());
return NO;
}
status = session->Create(graph);
if (!status.ok()) {
NSLog(@"Error adding graph to session: %s",
status.error_message().c_str());
return NO;
}
return YES;
}
会话创建好之后,就可以进行预测了。predict:方法需要一个包含20个浮点数的元组,代表声学特征,然后传入图中,该方法如下所示:
- (void)predict:(float *)example {
tensorflow::Tensor x(tensorflow::DT_FLOAT,
tensorflow::TensorShape({ 1, 20 }));
auto input = x.tensor<float, 2>();
for (int i = 0; i < 20; ++i) {
input(0, i) = example[i];
}
首先定义张量x作为输入数据。这个张量维度为{1, 20},因为它一次接收一个样本,每个样本有20个特征。然后从float *数组将数据拷贝至张量中。
std::vector<std::pair<std::string, tensorflow::Tensor>> inputs = {
{"inputs/x-input", x}
};
std::vector<std::string> nodes = {
{"model/y_pred"},
{"inference/inference"}
};
std::vector<tensorflow::Tensor> outputs;
auto status = session->Run(inputs, nodes, {}, &outputs);
if (!status.ok()) {
NSLog(@"Error running model: %s", status.error_message().c_str());
return;
}
pred, inf = sess.run([y_pred, inference], feed_dict={x: example})
auto y_pred = outputs[0].tensor<float, 2>();
NSLog(@"Probability spoken by a male: %f%%", y_pred(0, 0));
auto isMale = outputs[1].tensor<float, 2>();
if (isMale(0, 0)) {
NSLog(@"Prediction: male");
} else {
NSLog(@"Prediction: female");
}
}
本来只需要运行inference节点就可以得到男性/女性的预测结果,但我还想看计算出来的概率,所以后面运行了y_pred节点。
Node count: 9
Node 0: Placeholder 'inputs/x-input'
Node 1: Const 'model/W'
Node 2: Const 'model/b'
Node 3: MatMul 'model/MatMul'
Node 4: Add 'model/add'
Node 5: Sigmoid 'model/y_pred'
Node 6: Const 'inference/Greater/y'
Node 7: Greater 'inference/Greater'
Node 8: Cast 'inference/inference'
Probability spoken by a male: 0.970405%
Prediction: male
Probability spoken by a male: 0.005632%
Prediction: female
1. 一个工具搞定所有事。你可以使用TensorFlow训练模型并进行预测。不需要将计算图移植到其他的API,如BNNS或者Metal。另一方面,你只需要将少量Python代码移植到C++代码;
2.TensorFlow有比BNNS和Metal更多的特性;
3.你可以在模拟器上运行。Metal总是要在设备上运行。
1.目前不支持GPU。TensorFlow使用 Accelerate 框架能够发挥CPU向量指令的优势,原始速度比不上Metal;
2.TensorFlow API使用C++写的,所以你不得不写一些C++代码,并不能直接使用Swift编写。
3.相比于Python API,C++ API有限。这意味着你不能在设备上进行训练,因为不支持反向传播中用到的自动梯度计算。
4.TensorFlow静态库增加了app包大概40MB的空间。通过减少支持操作的数量,可以减少这个额外空间,不过这很麻烦。而且,这还不包括模型的大小。
Note: 如果决定在你的IOS app中使用TensorFlow,那你必须知道别人很容易从app安装包中拷贝图的.pb文件窃取你的模型。由于固化的图文件包含模型参数和图定义,反编译简直轻而易举。如果你的模型具有竞争优势,你可能需要做出预案防止你的机密被窃取。
训练后,我们需要将学习到的参数w和b保存成Metal能够读取的格式。其实只要以二进制格式保存为浮点数列表就可以了。
下面的Python脚本export_weights.py和之前载入图定义和检查点的test.py很相似,如下:
W.eval().tofile("W.bin")
b.eval().tofile("b.bin")
W.eval()计算w目前的值,并以返回Numpy数组(和sess.run(W)作用是一样的)。然后使用tofile()将Numpy数组保存为二进制文件。
你可以在源码的VoiceMetal文件夹下发现Xcode项目,使用Swift编写的。
y_pred = sigmoid((W * x) + b)
这和神经网络中全连接层进行的计算相同,为了实现Metal版分类器,我们只需要使用MPSCNN Fully Connected 层。首先将W.bin和b.bin载入到Data对象:
let W_url = Bundle.main.url(forResource: "W", withExtension: "bin")
let b_url = Bundle.main.url(forResource: "b", withExtension: "bin")
let W_data = try! Data(contentsOf: W_url!)
let b_data = try! Data(contentsOf: b_url!)
let sigmoid = MPSCNNNeuronSigmoid(device: device)
let layerDesc = MPSCNNConvolutionDescriptor(
kernelWidth: 1, kernelHeight: 1,
inputFeatureChannels: 20, outputFeatureChannels: 1,
neuronFilter: sigmoid)
W_data.withUnsafeBytes { W in
b_data.withUnsafeBytes { b in
layer = MPSCNNFullyConnected(device: device,
convolutionDescriptor: layerDesc,
kernelWeights: W, biasTerms: b, flags: .none)
}
}
因为输入是20个数字,我设计了作用于一个1x1的有20个输入信道(input channels)的全连接层。预测结果y_pred是一个数字,所以全连接层只有一个输出信道。输入和输出数据放在MPSImage 中:
let inputImgDesc = MPSImageDescriptor(channelFormat: .float16,
width: 1, height: 1, featureChannels: 20)
let outputImgDesc = MPSImageDescriptor(channelFormat: .float16,
width: 1, height: 1, featureChannels: 1)
inputImage = MPSImage(device: device, imageDescriptor: inputImgDesc)
outputImage = MPSImage(device: device, imageDescriptor: outputImgDesc)
和app上的TensorFlow一样,这里也有一个predict 方法,这个方法以组成一条样本的20个浮点数作为输入。下面是完整的方法:
func predict(example: [Float]) {
convert(example: example, to: inputImage)
let commandBuffer = commandQueue.makeCommandBuffer()
layer.encode(commandBuffer: commandBuffer, sourceImage: inputImage,
destinationImage: outputImage)
commandBuffer.commit()
commandBuffer.waitUntilCompleted()
let y_pred = outputImage.toFloatArray()
print("Probability spoken by a male: \(y_pred[0])%")
if y_pred[0] > 0.5 {
print("Prediction: male")
} else {
print("Prediction: female")
}
}
和运行session的结果是一样的。convert(example:to:)和toFloatArray()方法加载和输出MPSImage 对象的辅助函数。
你需要在设备上运行这个app,因为模拟器不支持Metal。输出结果如下:
Probability spoken by a male: 0.970215%
Prediction: male
Probability spoken by a male: 0.00568771%
Prediction: female
注意到这些概率和用TensorFlow预测到的概率不完全相同,这是因为Metal使用16位浮点数,但结果相当接近。
本文所用的数据集是 Kory Becker制作的,在 Kaggle.com下载,也参考了Kory的博文和源码。其他人也写过IOS上TensorFlow相关的一些东西。从这些文章和代码中我受益匪浅:
1.Getting Started with Deep MNIST and TensorFlow on iOS by Matt Rajca
2.Speeding Up TensorFlow with Metal Performance Shaders also by Matt Rajca
3.tensorflow-cocoa-example by Aaron Hillegass
4.TensorFlow iOS Examples in the TensorFlow repository
以上为译文
本文由北邮@爱可可-爱生活 老师推荐,阿里云云栖社区组织翻译。
文章原标题《Getting started with TensorFlow on iOS》,由Matthijs Hollemans发布。
译者:李烽 ;审校:
文章为简译,更为详细的内容,请查看原文。中文译制文档见附件。
最后更新:2017-04-15 00:02:21