Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

移动端优化 #37

Merged
merged 2 commits into from
Mar 29, 2018
Merged

移动端优化 #37

merged 2 commits into from
Mar 29, 2018

Conversation

changkun
Copy link
Member

resolve #27

@KeithMorning
Copy link

@leviding 校对认领

@leviding
Copy link
Member

@KeithMorning ok

Copy link

@KeithMorning KeithMorning left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@changkun @leviding 校对完成

There are some special issues that you have to deal with when you’re trying to
ship on mobile or embedded devices, and you’ll need to think about these as
you’re developing your model.
在移动或嵌入式设备中存在一些你需要去处理的问题,同时你还需要在开发模型时就思考这些问题

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

句子结尾缺少句号

smartphone might be able to run 10 GFLOPs per second, so the best you could hope
for from a 5 GFLOP model is two frames per second, though you may do worse
depending on what the exact computation patterns are.
运行包含基本功能的 TensorFlow 需要牺牲 1MB 的程序以及几兆的内存大小,因此 TensorFlow 不能运行在 DSP 或微控制器上。除此之外,限制 TensorFlow 的最大因素通常是设备的计算速度,以及你能否在足够低的计算延迟内运行你所需的模型。你可以在 [模型的分析](#模型的分析)中使用性能基准测试工具来了解至少需要多少 FLOP 才能运行模型。然后使用该工具对运行速度在不同设备上对计算消耗进行经验估计。例如,当今的智能手机可以美妙运行 10 GFLOP,所以对于 5 GFLOP 模型而言,可以得到的最佳效果是每秒两帧,根据设备实际的计算模式,你可能需要降低帧率。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

『你可以在 模型的分析中使用性能基准测试工具来了解至少需要多少 FLOP 才能运行模型。然后使用该工具对运行速度在不同设备上对计算消耗进行经验估计。』=>『你可以参考 模型的分析 用性能测基准测试工具来了解运行模型需要多少 FLOP,然后根据该值对不同设备的运行速度做经验估算。』

the latency budget and possibly within limited RAM too. For memory usage, you
mostly need to make sure that the intermediate buffers that TensorFlow creates
aren’t too large, which you can examine in the benchmark output too.
这种模型的要求使得只要你能在有限的内存中将神经网络优化得足够好以适应计算延迟, TensorFlow 就可以运行在非常古老或受到限制的智能手机上。考虑到内存的限制,你需要保证 TensorFlow 创建的中间缓存不能太大,这样你才能在性能基准输出中检查这些缓存的输出结果。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

『这种模型的要求使得只要你能在有限的内存中将神经网络优化得足够好以适应计算延迟, TensorFlow 就可以运行在非常古老或受到限制的智能手机上。』TensorFlow 与前面的『 ,』标点之间多一个空格

smartphone might be able to run 10 GFLOPs per second, so the best you could hope
for from a 5 GFLOP model is two frames per second, though you may do worse
depending on what the exact computation patterns are.
运行包含基本功能的 TensorFlow 需要牺牲 1MB 的程序以及几兆的内存大小,因此 TensorFlow 不能运行在 DSP 或微控制器上。除此之外,限制 TensorFlow 的最大因素通常是设备的计算速度,以及你能否在足够低的计算延迟内运行你所需的模型。你可以在 [模型的分析](#模型的分析)中使用性能基准测试工具来了解至少需要多少 FLOP 才能运行模型。然后使用该工具对运行速度在不同设备上对计算消耗进行经验估计。例如,当今的智能手机可以美妙运行 10 GFLOP,所以对于 5 GFLOP 模型而言,可以得到的最佳效果是每秒两帧,根据设备实际的计算模式,你可能需要降低帧率。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

『当今的智能手机可以美妙运行 10 GFLOP』存在错别字

the latency budget and possibly within limited RAM too. For memory usage, you
mostly need to make sure that the intermediate buffers that TensorFlow creates
aren’t too large, which you can examine in the benchmark output too.
这种模型的要求使得只要你能在有限的内存中将神经网络优化得足够好以适应计算延迟, TensorFlow 就可以运行在非常古老或受到限制的智能手机上。考虑到内存的限制,你需要保证 TensorFlow 创建的中间缓存不能太大,这样你才能在性能基准输出中检查这些缓存的输出结果。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

『考虑到内存的限制,你需要保证 TensorFlow 创建的中间缓存不能太大,这样你才能在性能基准输出中检查这些缓存的输出结果。』=>『考虑到内存的限制,你需要保证 TensorFlow 创建的中间缓存不能太大,你也可以通过性能基准输出做检查。』


* 加速加载时间
* 减少内存的分页调度(增加性能)
* 应用不需要考虑读取时的内存因素

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

『* 应用不需要考虑读取时的内存因素』=> 『* 不计入你的 APP 内存占用 』

* 减少内存的分页调度(增加性能)
* 应用不需要考虑读取时的内存因素

TensorFlow 支持由内存映射的方式来将你模型文件的大部分内容(如权重)一口气加载到内存中区。由于 `Protobuf` 序列化格式的限制,我们必须对模型的加载和处理代码进行一些修改。内存映射的工作原理是,我们有一个单独的文件,其中第一个部分是将一个普通的 `GraphDef` 序列化为 protobuf 格式,然后所有的权重就可以直接添加映射其后。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

『然后所有的权重就可以直接添加映射其后。』 => 『然后所有的权重就可以添加到一个直接映射的内存表后。』

One thing to notice here is that we’re also disabling automatic optimizations,
since in some cases these will fold constant sub-trees, and so create copies of
tensor values that we don’t want and use up more RAM.
值得注意的是,我们还禁用了自动优化,因为在某些情况下,这回折叠常量子树,从而创建我们不希望出现的张量的拷贝导致的额外内存占用。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

『这折叠常量子树,』这里应该改为 『会』

obfuscated by optimizations that the risk is similar to that of competitors
disassembling and reusing your code, but if you do want to make it tougher for
casual users to access your files it is possible to take some basic steps.
默认情况下,你的模型会在磁盘上以序列化后标准的 protobuf 格式存储。理论上来说,任何人都可以复制你的模型,但你可能并不希望别人这么做。然而,实际上大多数模型都是应用特定的,并且已经通过编译优化进行了混淆处理,其难度与反编译并服用你应用的代码一样难。但是,如果你确实希望让用户难以访问你的模型文件,可以采取下面这些基本步骤。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

『其难度与反编译并服用你应用的代码一样难。』这里应该修改为 『再调用』

and it should be easy to write an equivalent that can decrypt in memory. Here's
some code that shows how you can read and decrypt a protobuf using your own
decryption routine:
我们的大部分示例程序都使用 [ReadBinaryProto()](https://www.tensorflow.org/code/tensorflow/core/platform/env.cc?q=core/platform/env.cc&l=409) 从磁盘上加载一个 `GraphDef`。这的确需要使用一个未加密的 protobuf 文件。辛运的是,调用的实现非常简单,你可以编写一个在内存中进行解密的中间件来达到你的目的。下面的代码展示了如何使用自己的解密函数来读取和解密 protobuf:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

『辛运的是,』修改为 『幸运的是』

@leviding
Copy link
Member

@KeithMorning 很赞 👍

@changkun
Copy link
Member Author

@KeithMorning @leviding 修改完毕

sogou 真难用。。速拼错误连篇。。。

@leviding
Copy link
Member

@changkun 哈哈哈,很赞喔 👍

@leviding leviding mentioned this pull request Mar 29, 2018
@leviding leviding merged commit 5289095 into xitu:zh-hans Mar 29, 2018
@leviding
Copy link
Member

@KeithMorning 有空欢迎校对其他几篇哈

@KeithMorning
Copy link

@leviding 哈哈好的 没问题

@KeithMorning
Copy link

@changkun 哈哈 我在想你是不是被逼着通宵翻译的

@changkun
Copy link
Member Author

那天白天好像是在搬家 想着凌晨前要弄完所以打得比较快\捂脸 @KeithMorning

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

mobile/optimizing.md
3 participants