<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 十、移動計算 在本章中,我們將討論在移動設備上使用深度學習的問題,并為以下內容提供一些方法: * 安裝適用于 macOS 和 Android 的 TensorFlow Mobile * 玩轉 TensorFlow 和 Android 示例 * 為 MacOS 和 iPhone 安裝 TensorFlow Mobile * 為移動設備優化 TensorFlow 圖 * 轉換移動設備的 TensorFlow 圖 # 介紹 在本節中,我們將介紹移動深度學習的一些用例。 這與臺式機或云深度學習的情況大不相同,在臺式機或云深度學習中,GPU 和電力通常可用。 實際上,在移動設備上,保存電池非常重要,并且 GPU 經常不可用。 但是,深度學習在許多情況下可能非常有用。 讓我們回顧一下: * **圖像識別**:現代手機具有功能強大的攝像頭,用戶熱衷于嘗試對圖像和圖片產生效果。 通常,了解圖片中的內容也很重要,并且有多種適用于此的預訓練模型,如專用于 CNN 的章節所述。 [這里給出了用于圖像識別的模型的一個很好的例子](https://github.com/tensorflow/models/tree/master/official/resnet)。 * **對象定位**:識別運動對象是一項關鍵操作,對于視頻和圖像處理是必需的。 例如,可以想象如果在圖像中識別出多個人,那么相機將使用多個對焦點。 [這里提供了對象本地化示例的集合](https://github.com/tensorflow/models/tree/master/research/object_detection)。 * **光學字符識別**:在許多活動(例如文本分類和推薦)中,識別手寫字符都是至關重要的。 深度學習可以為開展這些活動提供根本幫助。 在專用于 CNN 的章節中,我們研究了 MNIST 識別的一些示例。 關于 MNIST 的信息也可以在[這個頁面](https://github.com/TensorFlow/models/tree/master/official/mnist)中找到。 * **語音識別**:語音識別是訪問現代電話的常用界面。 因此,深度學習用于識別語音和口頭命令。 在過去的幾年中,這方面的進展令人印象深刻。 * **翻譯**:處理多種語言是現代多元文化世界的一部分。 手機在各種語言之間進行即時翻譯的準確率越來越高,深度學習幫助打破了障礙,而這在幾年前是無法想象的。 在專門針對 RNN 的一章中,我們研究了一些機器翻譯示例。 * **手勢識別**:電話開始使用手勢作為接收命令的界面。 當然,有一些模型。 * **壓縮**:壓縮是手機的關鍵方面。 可以想象,在通過網絡發送圖像或視頻之前減少空間是有益的。 同樣,在本地存儲在設備上之前壓縮數據可能會很方便。 在所有這些情況下,深度學習都可以提供幫助。 使用 RNNS 進行壓縮的模型位于[這里](https://github.com/tensorflow/models/tree/master/research/compression)。 # TensorFlow,移動和云 如上所述,電話通常沒有 GPU,因此節省電池電量非常重要。 為了減輕成本,需要將許多昂貴的計算卸載到云中。 當然,要折衷考慮各種因素,包括在移動設備上執行深度學習模型的成本,將數據移至云的成本,用于此傳輸的電池成本以及云計算的成本。 沒有單一的解決方案,最佳策略取決于您的具體情況。 # 安裝適用于 macOS 和 Android 的 TensorFlow Mobile 在本秘籍中,我們將學習如何為移動環境設置 TensorFlow。 我的環境是 macOS,我為 Android 開發。 但是,在以下秘籍中將描述其他配置。 # 準備 我們將使用 Android Studio,這是適用于 Google Android 操作系統的官方**集成開發環境**(**IDE**)。 # 操作步驟 我們繼續按以下步驟安裝適用于 macOS 和 Android 的 TensorFlow mobile: 1. 從[這里](https://developer.android.com/studio/install.html)安裝 Android Studio。 2. 創建一個新的項目名稱`AndroidExampleTensorflow`,如以下屏幕截圖所示: ![](https://img.kancloud.cn/f9/dc/f9dcd8cf4a175bd34b42b39f07584a21_2168x1300.png) 在 AndroidStudio 中創建 TensorFlow 移動應用的示例,第一步如下圖所示:選擇電話和表格選項: ![](https://img.kancloud.cn/0a/9b/0a9b3bbe869867078675ae60682de086_2162x1304.png) 在 AndroidStudio 中創建 TensorFlow 移動應用的示例,第二步并選擇一個空活動,如下圖所示: ![](https://img.kancloud.cn/7a/73/7a73d8724d4f3f2d980a53bb40730f0f_2162x1304.png) 在 AndroidStudio 中創建 TensorFlow 移動應用的示例,第三步然后自定義`MainActivity`,如下圖所示: ![](https://img.kancloud.cn/79/80/7980433eaa8ce38a01f74deda88b7592_2162x1308.png) 在 AndroidStudio 中創建 TensorFlow 移動應用的示例,第四步稱為“基礎神經節”,根據神經科學,它負責選擇動作,即幫助我們確定在任何給定時間執行幾個可能動作中的哪個。 3. 將以下行插入`build.gradle`應用中,如以下代碼所示: ```py // added for automatically connect to TensorFlow via maven repositories { jcenter() maven { url 'https://google.bintray.com/TensorFlow' } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:26.+' compile 'com.android.support.constraint:constraint-layout:1.0.2' // added for automatically compile TensorFlow compile 'org.TensorFlow:TensorFlow-android:+' testCompile 'junit:junit:4.12' } ``` 以下屏幕截圖顯示了插入的代碼: ![](https://img.kancloud.cn/aa/ed/aaed3b325872f9d200595b774c889580_2238x1630.png) 4. 運行項目并獲得結果: ![](https://img.kancloud.cn/7a/ef/7aef584dba984206e59e3a15927591b1_1264x950.png) 使用 AndroidStudio 進行編譯的示例,其中顯示了連接的設備。 ![](https://img.kancloud.cn/70/53/70535e0454e740aaa41dbb4319156d3f_914x1668.png) 在 AndroidStudio 中創建 TensorFlow 移動應用的示例。 一個簡單的`Hello World`應用 # 工作原理 使用 Android Studio 設置 Android TensorFlow 非常簡單。 您只需要在應用的`build.gradle`文件中添加一些配置行,Android Studio 就會代表您執行所有操作。 # 更多 如果要直接從 TensorFlow 源構建,則需要安裝 Bazel 和 TensorFlow。 Bazel 是一個快速,可擴展,多語言和可擴展的構建系統。 Google 內部使用了構建工具 Blaze,并將 Blaze 工具的開源部分稱為 Bazel。 名稱是 Blaze 的字謎。 [此頁面將指導您完成該過程](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/android/)。 如果您正在運行 macOS,則過程非常簡單: 1. 按照[這個頁面](https://docs.bazel.build/versions/master/install.html)上的說明安裝 Bazel。 對于 macOS,我們將使用 Homebrew: ```py /usr/bin/ruby -e "$(curl -fsSL \ https://raw.githubusercontent.com/Homebrew/install/master/install)" brew install bazel bazel version brew upgrade bazel ``` 2. 從 GitHub 克隆 TensorFlow 發行版。 ```py git clone https://github.com/TensorFlow/TensorFlow.git ``` # 玩轉 TensorFlow 和 Android 示例 在本秘籍中,我們將考慮 TensorFlow 發行版中提供的標準 Android 示例并將其安裝在我們的移動設備上。 # 準備 TensorFlow 移動 Android 應用[可在 GitHub 上的以下地址獲得](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/android)。 2017 年 10 月,該頁面包含以下示例: * **TF 分類**:使用 Google Inception 模型實時對相機幀進行分類,并在相機圖像上以重疊顯示頂部結果。 * **TF 檢測**:演示使用 TensorFlow 對象檢測 API 訓練的 SSD-Mobilenet 模型。 這是在現代卷積目標檢測器的速度/精度折衷中引入的,以實時定位和跟蹤攝像機預覽中的目標(來自 80 個類別)。 * **TF 風格化**:使用基于*藝術風格的學習表示*的模型將相機預覽圖像重新設置為許多不同藝術風格的圖像。 * **TF 語音**:運行在音頻訓練教程中構建的簡單語音識別模型。 監聽一小部分單詞,并在識別它們時在 UI 中突出顯示它們。 # 操作步驟 我們按以下步驟進行: 1. 安裝包的最佳方法是使用每晚創建的預構建 APK。 將瀏覽器指向[這里](https://ci.tensorflow.org/view/Nightly/job/nightly-android/)并下載`TensorFlow_demo.apk`,如以下屏幕截圖所示: ![](https://img.kancloud.cn/0f/1a/0f1a5a9429080a5488ad29bf54a6f24f_1534x912.png) 2. 在您的設備上安裝應用。 在我的示例中,我將使用 Android Studio 中可用的 Pixel XL 仿真設備。 這是直接從 Android Studio 內部模擬的終端設備。 命令`adb devices`列出所有連接的設備。 在這種情況下,我有一個 Pixel XL 模擬器,可以安裝`TensorFlow_demo apk`。 ```py adb devices List of devices attached emulator-5554 device adb install -r TensorFlow_demo.apk ``` 安裝后,仿真器將具有一組新的 TensorFlow 應用可供使用,如下圖所示。 ![](https://img.kancloud.cn/61/af/61af186952f4538e57157b2702d5057d_912x1628.png) 3. 運行您喜歡的應用。 例如,以下圖像是 TF Stylize 的示例,用于通過 Transfer Learning 將相機預覽圖像重新設置為多種不同藝術風格的圖像: ![](https://img.kancloud.cn/b2/43/b243f229a8f3f888e21056fb65e5c653_908x1634.png) 下圖是 TF 語音的示例(請記住為仿真器激活麥克風): ![](https://img.kancloud.cn/c8/55/c8557814197ae3c78b034c18b9c71ed3_2500x1505.png) # 工作原理 如果您使用夜間構建演示和`adb`工具在設備上安裝 APK,則安裝 Android 的 TensorFlow 示例非常容易。 # 為 MacOS 和 iPhone 安裝 TensorFlow Mobile 在本秘籍中,我們將學習如何在移動環境中設置 TensorFlow。 我的環境是 macOS,這里的想法是為 iOS 和 iPhone 開發。 # 準備 我們將使用 Xcode 開發環境和 CocoaPods 來預安裝 TensorFlow。 我將假定您的環境中已經安裝了 Xcode。 如果沒有,請從[這里](https://developer.apple.com/xcode/)下載。 # 操作步驟 我們將按照以下步驟進行操作: 1. 使用以下命令安裝 CocoaPods ```py sudo gem install cocoapods pod setup Setting up CocoaPods master repo $ /usr/local/git/current/bin/git clone https://github.com/CocoaPods/Specs.git master --progress Cloning into 'master'... remote: Counting objects: 1602077, done. remote: Compressing objects: 100% (243/243), done. remote: Total 1602077 (delta 125), reused 172 (delta 74), pack-reused 1601747 Receiving objects: 100% (1602077/1602077), 432.12 MiB | 1.83 MiB/s, done. Resolving deltas: 100% (849517/849517), done. Checking out files: 100% (188907/188907), done. ``` 2. 使用 CocoaPods 安裝 TensorFlow 發行版: ```py cd TensorFlow/TensorFlow/examples/ios/benchmark pod install Analyzing dependencies Downloading dependencies Installing TensorFlow-experimental (1.1.1) Generating Pods project Integrating client project [!] Please close any current Xcode sessions and use `tf_benchmark_example.xcworkspace` for this project from now on. Sending stats Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed. ``` 3. 從 Inception v1 下載一些樣本數據。 將標簽和圖文件提取到`simple`和`camera`文件夾內的數據文件夾中: ```py mkdir -p ~/graphs curl -o ~/graphs/inception5h.zip \ https://storage.googleapis.com/download.TensorFlow.org/models/inception5h.zip \ && unzip ~/graphs/inception5h.zip -d ~/graphs/inception5h cp ~/graphs/inception5h/* TensorFlow/examples/ios/benchmark/data/ cp ~/graphs/inception5h/* TensorFlow/examples/ios/camera/data/ cp ~/graphs/inception5h/* TensorFlow/examples/ios/simple/data/ ``` 4. 從中下載用作測試的圖像并將其復制到基準目錄: <https://upload.wikimedia.org/wikipedia/commons/5/55/Grace_Hopper.jpg> ```py cp grace_hopper.jpg ../../benchmark/data/ ``` ![](https://img.kancloud.cn/c5/b5/c5b50bbe149db1002b33989419348ae0_517x606.jpg) Grace Hopper 的圖片 5. 打開以前使用的示例項目。 以下命令將打開已經可用的 TensorFlow 的 Xcode 之后,運行編譯,如以下代碼和圖像所示: ```py open tf_benchmark_example.xcworkspace ``` ![](https://img.kancloud.cn/6c/31/6c31bd5a5980e626d8353850d742e609_2280x1018.png) 6. 在 iPhone 模擬器中查看結果。 根據 Inception v1 類別,將步驟 4 中使用的圖像識別為*軍服*的圖像: ![](https://img.kancloud.cn/9f/c7/9fc710efdbd033662011db7161fd495e_954x1868.png) ![](https://img.kancloud.cn/18/0a/180a448dd2e78c5f489fa3a6caeb9379_978x1866.png) 用于 Tensorflow 計算的 Iphone 應用示例 # 工作原理 Xcode 和 CocoaPods 用于編譯 TensorFlow 應用,該應用用于對不同 Inception 類別中的圖像進行分類。 結果使用 iPhone 模擬器可視化。 # 更多 您可以直接在應用中使用 TensorFlow。 [可在此處獲得更多信息](https://github.com/TensorFlow/TensorFlow/blob/master/TensorFlow/examples/ios/README.md)。 # 為移動設備優化 TensorFlow 圖 在本秘籍中,我們將考慮不同的選項來優化在移動設備上運行的 TensorFlow 代碼。 從減小模型的大小到量化,分析了不同的選項。 # 準備 我們將使用 Bazel 構建 TensorFlow 的不同組件。 因此,第一步是確保同時安裝了 Bazel 和 TensorFlow。 # 操作步驟 我們按以下步驟進行優化: 1. 從[這里](https://developer.android.com/studio/install.html)安裝 Android Studio。 2. 按照[這個頁面](https://docs.bazel.build/versions/master/install.html)上的說明安裝 Bazel。 對于 macOS,我們將使用 Homebrew: ```py /usr/bin/ruby -e "$(curl -fsSL \ https://raw.githubusercontent.com/Homebrew/install/master/install)" brew install bazel bazel version brew upgrade bazel ``` 3. 從 GitHub 克隆 TensorFlow 發行版: ```py git clone https://github.com/TensorFlow/TensorFlow.git ``` 4. 構建一個圖轉換器,匯總一下圖本身: ```py cd ~/TensorFlow/ bazel build TensorFlow/tools/graph_transforms:summarize_graph [2,326 / 2,531] Compiling TensorFlow/core/kernels/cwise_op_greater.cc INFO: From Linking TensorFlow/tools/graph_transforms/summarize_graph: clang: warning: argument unused during compilation: '-pthread' [-Wunused-command-line-argument] Target //TensorFlow/tools/graph_transforms:summarize_graph up-to-date: bazel-bin/TensorFlow/tools/graph_transforms/summarize_graph INFO: Elapsed time: 1521.260s, Critical Path: 103.87s ``` 7. 下載 TensorFlow 圖以用作示例。 在這種情況下,我們將使用 Inception v1 TensorFlow 圖: ```py mkdir -p ~/graphs curl -o ~/graphs/inception5h.zip \ https://storage.googleapis.com/download.TensorFlow.org/models/inception5h.zip \ && unzip ~/graphs/inception5h.zip -d ~/graphs/inception5h ``` 8. 匯總 Inception 圖并注意常參數的數量:1,346 萬。 它們每個都存儲有 32 位浮點數,這非常昂貴: ```py bazel-bin/TensorFlow/tools/graph_transforms/summarize_graph --in_graph=/Users/gulli/graphs/TensorFlow_inception_graph.pb Found 1 possible inputs: (name=input, type=float(1), shape=[]) No variables spotted. Found 3 possible outputs: (name=output, op=Identity) (name=output1, op=Identity) (name=output2, op=Identity) Found 13462015 (13.46M) const parameters, 0 (0) variable parameters, and 0 control_edges 370 nodes assigned to device '/cpu:0'Op types used: 142 Const, 64 BiasAdd, 61 Relu, 59 Conv2D, 13 MaxPool, 9 Concat, 5 Reshape, 5 MatMul, 3 Softmax, 3 Identity, 3 AvgPool, 2 LRN, 1 Placeholder To use with TensorFlow/tools/benchmark:benchmark_model try these arguments: bazel run TensorFlow/tools/benchmark:benchmark_model -- --graph=/Users/gulli/graphs/TensorFlow_inception_graph.pb --show_flops --input_layer=input --input_layer_type=float --input_layer_shape= --output_layer=output,output1,output2 ``` 9. 編譯該工具以將常量操作截斷至 8 位: ```py bazel build TensorFlow/tools/graph_transforms:transform_graph INFO: From Linking TensorFlow/tools/graph_transforms/transform_graph: clang: warning: argument unused during compilation: '-pthread' [-Wunused-command-line-argument] Target //TensorFlow/tools/graph_transforms:transform_graph up-to-date: bazel-bin/TensorFlow/tools/graph_transforms/transform_graph INFO: Elapsed time: 294.421s, Critical Path: 28.83s ``` 10. 運行該工具以量化 Inception V1 圖: ```py bazel-bin/TensorFlow/tools/graph_transforms/transform_graph --in_graph=/Users/gulli/graphs/inception5h/TensorFlow_inception_graph.pb --out_graph=/tmp/TensorFlow_inception_quantized.pb --inputs='Mul:0' --outputs='softmax:0' --transforms='quantize_weights' 2017-10-15 18:56:01.192498: I TensorFlow/tools/graph_transforms/transform_graph.cc:264] Applying quantize_weights ``` 11. 比較兩個模型: ```py ls -lah /Users/gulli/graphs/inception5h/TensorFlow_inception_graph.pb -rw-r----- 1 gulli 5001 51M Nov 19 2015 /Users/gulli/graphs/inception5h/TensorFlow_inception_graph.pb ls -lah /tmp/TensorFlow_inception_quantized.pb -rw-r--r-- 1 gulli wheel 13M Oct 15 18:56 /tmp/TensorFlow_inception_quantized.pb ``` # 工作原理 量化通過將常量操作從 32 位縮減為 8 位來幫助減小模型的大小。 通常,該模型不會遭受表現的顯著降低。 但是,這必須根據具體情況進行驗證。 # 為移動設備分析 TensorFlow 圖 在本秘籍中,我們將考慮不同的選項來優化 TensorFlow 代碼以在移動設備上運行。 從減小模型的大小到量化,分析了不同的選項。 # 準備 我們將使用 Bazel 構建 TensorFlow 的不同組件。 因此,第一步是確保同時安裝了 Bazel 和 TensorFlow。 # 操作步驟 我們進行如下分析: 1. 從[這里](https://developer.android.com/studio/install.html)安裝 Android Studio。 2. 按照[這個頁面](https://docs.bazel.build/versions/master/install.html)上的說明安裝 Bazel。 對于 macOS,我們將使用 Homebrew: ```py /usr/bin/ruby -e "$(curl -fsSL \ https://raw.githubusercontent.com/Homebrew/install/master/install)" brew install bazel bazel version brew upgrade bazel ``` 3. 從 GitHub 克隆 TensorFlow 發行版: ```py git clone https://github.com/TensorFlow/TensorFlow.git ``` 4. 構建圖變壓器,該圖變壓器對圖本身進行配置: ```py cd ~/TensorFlow/ bazel build -c opt TensorFlow/tools/benchmark:benchmark_model INFO: Found 1 target... Target //TensorFlow/tools/benchmark:benchmark_model up-to-date: bazel-bin/TensorFlow/tools/benchmark/benchmark_model INFO: Elapsed time: 0.493s, Critical Path: 0.01s ``` 5. 通過在桌面上運行以下命令來對模型進行基準測試: ```py bazel-bin/TensorFlow/tools/benchmark/benchmark_model --graph=/Users/gulli/graphs/TensorFlow_inception_graph.pb --show_run_order=false --show_time=false --show_memory=false --show_summary=true --show_flops=true Graph: [/Users/gulli/graphs/TensorFlow_inception_graph.pb] Input layers: [input:0] Input shapes: [1,224,224,3] Input types: [float] Output layers: [output:0] Num runs: [1000] Inter-inference delay (seconds): [-1.0] Inter-benchmark delay (seconds): [-1.0] Num threads: [-1] Benchmark name: [] Output prefix: [] Show sizes: [0] Warmup runs: [2] Loading TensorFlow. Got config, 0 devices Running benchmark for max 2 iterations, max -1 seconds without detailed stat logging, with -1s sleep between inferences count=2 first=279182 curr=41827 min=41827 max=279182 avg=160504 std=118677 Running benchmark for max 1000 iterations, max 10 seconds without detailed stat logging, with -1s sleep between inferences count=259 first=39945 curr=44189 min=36539 max=51743 avg=38651.1 std=1886 Running benchmark for max 1000 iterations, max 10 seconds with detailed stat logging, with -1s sleep between inferences count=241 first=40794 curr=39178 min=37634 max=153345 avg=41644.8 std=8092 Average inference timings in us: Warmup: 160504, no stats: 38651, with stats: 41644 ``` ![](https://img.kancloud.cn/e2/96/e29630a068fbc523f22e8b1dbea23206_1890x620.png) 6. 通過在運行 64 位 ARM 處理器的目標 android 設備上運行以下命令來對模型進行基準測試。 請注意,以下命令將初始圖推送到設備上并運行可在其中執行基準測試的外殼程序: ```py bazel build -c opt --config=android_arm64 \ TensorFlow/tools/benchmark:benchmark_model adb push bazel-bin/TensorFlow/tools/benchmark/benchmark_model \ /data/local/tmp adb push /tmp/TensorFlow_inception_graph.pb /data/local/tmp/ adb push ~gulli/graphs/inception5h/TensorFlow_inception_graph.pb /data/local/tmp/ /Users/gulli/graphs/inception5h/TensorFlow_inception_graph.pb: 1 file pushed. 83.2 MB/s (53884595 bytes in 0.618s) adb shell generic_x86:/ $ /data/local/tmp/benchmark_model --graph=/data/local/tmp/TensorFlow_inception_graph.pb --show_run_order=false --show_time=false --show_memory=false --show_summary=true ``` # 工作原理 正如預期的那樣,該模型在 Conv2D 操作上花費了大量時間。 總體而言,這大約占我臺式機平均時間的 77.5%。 如果在移動設備上運行此程序,那么花時間執行神經網絡中的每一層并確保它們處于受控狀態至關重要。 要考慮的另一個方面是內存占用。 在這種情況下,桌面執行約為 10 Mb。 # 轉換移動設備的 TensorFlow 圖 在本秘籍中,我們將學習如何轉換 TensorFlow 圖,以便刪除所有僅訓練節點。 這將減小圖的大小,使其更適合于移動設備。 **What is a graph transform tool?** According to https://github.com/tensorflow/tensorflow/blob/master/tensorflow/tools/graph_transforms/README.md "When you have finished training a model and want to deploy it in production, you'll often want to modify it to better run in its final environment. For example if you're targeting a phone you might want to shrink the file size by quantizing the weights, or optimize away batch normalization or other training-only features. The Graph Transform framework offers a suite of tools for modifying computational graphs, and a framework to make it easy to write your own modifications". # 準備 我們將使用 Bazel 構建 TensorFlow 的不同組件。 因此,第一步是確保同時安裝了 Bazel 和 TensorFlow。 # 操作步驟 這是我們如何轉換 TensorFlow 的方法: 1. 從[這里](https://developer.android.com/studio/install.html)安裝 Android Studio。 2. 按照[這個頁面](https://docs.bazel.build/versions/master/install.html)上的說明安裝 Bazel。 對于 macOS,我們將使用 Homebrew: ```py /usr/bin/ruby -e "$(curl -fsSL \ https://raw.githubusercontent.com/Homebrew/install/master/install)" brew install bazel bazel version brew upgrade bazel ``` 3. 從 GitHub 克隆 TensorFlow 發行版: ```py git clone https://github.com/TensorFlow/TensorFlow.git ``` 4. 構建一個圖轉換器,它匯總了圖本身: ```py bazel run TensorFlow/tools/graph_transforms:summarize_graph -- --in_graph=/Users/gulli/graphs/inception5h/TensorFlow_inception_graph.pb WARNING: /Users/gulli/TensorFlow/TensorFlow/core/BUILD:1783:1: in includes attribute of cc_library rule //TensorFlow/core:framework_headers_lib: '../../external/nsync/public' resolves to 'external/nsync/public' not below the relative path of its package 'TensorFlow/core'. This will be an error in the future. Since this rule was created by the macro 'cc_header_only_library', the error might have been caused by the macro implementation in /Users/gulli/TensorFlow/TensorFlow/TensorFlow.bzl:1054:30. INFO: Found 1 target... Target //TensorFlow/tools/graph_transforms:summarize_graph up-to-date: bazel-bin/TensorFlow/tools/graph_transforms/summarize_graph INFO: Elapsed time: 0.395s, Critical Path: 0.01s INFO: Running command line: bazel-bin/TensorFlow/tools/graph_transforms/summarize_graph '--in_graph=/Users/gulli/graphs/inception5h/TensorFlow_inception_graph.pb' Found 1 possible inputs: (name=input, type=float(1), shape=[]) No variables spotted. Found 3 possible outputs: (name=output, op=Identity) (name=output1, op=Identity) (name=output2, op=Identity) Found 13462015 (13.46M) const parameters, 0 (0) variable parameters, and 0 control_edges 370 nodes assigned to device '/cpu:0'Op types used: 142 Const, 64 BiasAdd, 61 Relu, 59 Conv2D, 13 MaxPool, 9 Concat, 5 Reshape, 5 MatMul, 3 Softmax, 3 Identity, 3 AvgPool, 2 LRN, 1 Placeholder To use with TensorFlow/tools/benchmark:benchmark_model try these arguments: bazel run TensorFlow/tools/benchmark:benchmark_model -- --graph=/Users/gulli/graphs/inception5h/TensorFlow_inception_graph.pb --show_flops --input_layer=input --input_layer_type=float --input_layer_shape= --output_layer=output,output1,output2 ``` 5. 剝去用于訓練的所有節點,當在移動設備上使用圖進行推理時,不需要這些節點: ```py bazel run TensorFlow/tools/graph_transforms:transform_graph -- --in_graph=/Users/gulli/graphs/inception5h/TensorFlow_inception_graph.pb --out_graph=/tmp/optimized_inception_graph.pb --transforms="strip_unused_nodes fold_constants(ignore_errors=true) fold_batch_norms fold_old_batch_norms" WARNING: /Users/gulli/TensorFlow/TensorFlow/core/BUILD:1783:1: in includes attribute of cc_library rule //TensorFlow/core:framework_headers_lib: '../../external/nsync/public' resolves to 'external/nsync/public' not below the relative path of its package 'TensorFlow/core'. This will be an error in the future. Since this rule was created by the macro 'cc_header_only_library', the error might have been caused by the macro implementation in /Users/gulli/TensorFlow/TensorFlow/TensorFlow.bzl:1054:30. INFO: Found 1 target... Target //TensorFlow/tools/graph_transforms:transform_graph up-to-date: bazel-bin/TensorFlow/tools/graph_transforms/transform_graph INFO: Elapsed time: 0.578s, Critical Path: 0.01s INFO: Running command line: bazel-bin/TensorFlow/tools/graph_transforms/transform_graph '--in_graph=/Users/gulli/graphs/inception5h/TensorFlow_inception_graph.pb' '--out_graph=/tmp/optimized_inception_graph.pb' '--transforms=strip_unused_nodes fold_constants(ignore_errors=true) fold_batch_norms fold_old_batch_norms' 2017-10-15 22:26:59.357129: I TensorFlow/tools/graph_transforms/transform_graph.cc:264] Applying strip_unused_nodes 2017-10-15 22:26:59.367997: I TensorFlow/tools/graph_transforms/transform_graph.cc:264] Applying fold_constants 2017-10-15 22:26:59.387800: I TensorFlow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.2 AVX AVX2 FMA 2017-10-15 22:26:59.388676: E TensorFlow/tools/graph_transforms/transform_graph.cc:279] fold_constants: Ignoring error Must specify at least one target to fetch or execute. 2017-10-15 22:26:59.388695: I TensorFlow/tools/graph_transforms/transform_graph.cc:264] Applying fold_batch_norms 2017-10-15 22:26:59.388721: I TensorFlow/tools/graph_transforms/transform_graph.cc:264] Applying fold_old_batch_norms ``` # 工作原理 為了創建可以在設備上加載的更輕的模型,我們使用了圖變換工具應用的`strip_unused_nodes`規則刪除了所有不需要的節點。 該操作將刪除用于學習的所有操作,并保留用于推理的操作。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看