tensorflow cmake
r1.13
查看官方文檔:您看到了什麼?通常的票價?現在,猜猜是什麼:這是一個無巴澤爾的區域。我們在這裡使用Cmake!
該集合包含可靠且死去的簡單示例,用於在C,C ++,GO和Python中使用TensorFlow:加載預訓練的模型或使用或不帶有CUDA的自定義操作。所有構建均針對最新的穩定張量流版本進行了測試,並依靠CMAKE使用Custom FindTensorFlow.cmake進行測試。該CMAKE文件包括特定TF版本中錯誤的常見工作。
| 張量 | 地位 |
|---|---|
| 1.14.0 | |
| 1.13.1 | |
| 1.12.0 | |
| 1.11.0 | |
| 1.10.0 | |
| 1.9.0 |
存儲庫包含以下示例。
| 例子 | 解釋 |
|---|---|
| 自定義操作 | 在C ++/CUDA中構建定制操作(僅需要PIP) |
| 推理(C ++) | 在C ++中進行推斷 |
| 推理(c) | 在C中進行推斷 |
| 推理(GO) | 在GO中進行推斷 |
| 活動作家 | 在C ++中寫入張板的事件文件 |
| KERAS CPP-inference示例 | 在C ++中運行KERAS模型 |
| 簡單示例 | 在C ++中創建並運行TensorFlow圖 |
| 調整圖像示例 | 通過/不使用OPENCV,在TensorFlow中調整圖像大小 |
此示例說明了使用C ++/CUDA和CMAKE創建自定義操作的過程。它不打算顯示獲得峰值績效的實現。相反,它只是一個樣板模板。
user@host $ pip install tensorflow-gpu --user # solely the pip package is needed
user@host $ cd custom_op/user_ops
user@host $ cmake .
user@host $ make
user@host $ python test_matrix_add.py
user@host $ cd ..
user@host $ python example.py 此示例說明了加載圖像的過程(使用OpenCV或TensorFlow),調整圖像將圖像保存為JPG或PNG(使用OpenCV或TensorFlow)。
user@host $ cd examples/resize
user@host $ export TENSORFLOW_BUILD_DIR=...
user@host $ export TENSORFLOW_SOURCE_DIR=...
user@host $ cmake .
user@host $ make 有兩個示例證明了張量流服務的處理:使用向量輸入並使用編碼的圖像輸入。
server@host $ CHOOSE=basic # or image
server@host $ cd serving/ ${CHOOSE} /training
server@host $ python create.py # create some model
server@host $ cd serving/server/
server@host $ ./run.sh # start server
# some some queries
client@host $ cd client/bash
client@host $ ./client.sh
client@host $ cd client/python
# for the basic-example
client@host $ python client_rest.py
client@host $ python client_grpc.py
# for the image-example
client@host $ python client_rest.py /path/to/img.[png,jpg]
client@host $ python client_grpc.py /path/to/img.[png,jpg] 在Python中創建一個模型,將圖形保存到磁盤並將其加載到C/C+/GO/Python中以執行推理。由於這些示例是基於Tensorflow C-API的,因此他們需要libtensorflow_cc.so庫,該庫未在PIP包裝(TensorFow-gpu)中發貨。因此,您需要事先從源構建張力流,例如
user@host $ ls ${TENSORFLOW_SOURCE_DIR}
ACKNOWLEDGMENTS bazel-genfiles configure pip
ADOPTERS.md bazel-out configure.py py.pynano
ANDROID_NDK_HOME bazel-tensorflow configure.py.bkp README.md
...
user@host $ cd ${TENSORFLOW_SOURCE_DIR}
user@host $ ./configure
user@host $ # ... or whatever options you used here
user@host $ bazel build -c opt --copt=-mfpmath=both --copt=-msse4.2 --config=cuda //tensorflow:libtensorflow.so
user@host $ bazel build -c opt --copt=-mfpmath=both --copt=-msse4.2 --config=cuda //tensorflow:libtensorflow_cc.so
user@host $ export TENSORFLOW_BUILD_DIR=/tensorflow_dist
user@host $ mkdir ${TENSORFLOW_BUILD_DIR}
user@host $ cp ${TENSORFLOW_SOURCE_DIR} /bazel-bin/tensorflow/ * .so ${TENSORFLOW_BUILD_DIR} /
user@host $ cp ${TENSORFLOW_SOURCE_DIR} /bazel-genfiles/tensorflow/cc/ops/ * .h ${TENSORFLOW_BUILD_DIR} /includes/tensorflow/cc/ops/我們只是運行一個非常基本的模型
x = tf . placeholder ( tf . float32 , shape = [ 1 , 2 ], name = 'input' )
output = tf . identity ( tf . layers . dense ( x , 1 ), name = 'output' )因此,像定期這樣做一樣保存模型。這是在example.py中完成的。除了一些輸出以外
user@host $ python example.py
[<tf.Variable 'dense/kernel:0' shape=(2, 1) dtype=float32_ref>, <tf.Variable 'dense/bias:0' shape=(1,) dtype=float32_ref>]
input [[1. 1.]]
output [[2.1909506]]
dense/kernel:0 [[0.9070684]
[1.2838823]]
dense/bias:0 [0.] user@host $ python python/inference.py
[<tf.Variable 'dense/kernel:0' shape=(2, 1) dtype=float32_ref>, <tf.Variable 'dense/bias:0' shape=(1,) dtype=float32_ref>]
input [[1. 1.]]
output [[2.1909506]]
dense/kernel:0 [[0.9070684]
[1.2838823]]
dense/bias:0 [0.] user@host $ cd cc
user@host $ cmake .
user@host $ make
user@host $ cd ..
user@host $ ./cc/inference_cc
input Tensor<type: float shape: [1,2] values: [1 1]>
output Tensor<type: float shape: [1,1] values: [2.19095063]>
dense/kernel:0 Tensor<type: float shape: [2,1] values: [0.907068372][1.28388226]>
dense/bias:0 Tensor<type: float shape: [1] values: 0> user@host $ cd c
user@host $ cmake .
user@host $ make
user@host $ cd ..
user@host $ ./c/inference_c
2.190951
user@host $ go get github.com/tensorflow/tensorflow/tensorflow/go
user@host $ cd go
user@host $ ./build.sh
user@host $ cd ../
user@host $ ./inference_go
input [[1 1]]
output [[2.1909506]]
dense/kernel:0 [[0.9070684] [1.2838823]]
dense/bias:0 [0]