Mirando los documentos oficiales: ¿Qué ves? ¿La tarifa habitual? Ahora, adivina qué: esta es una zona libre de bazel. ¡Usamos cmake aquí!
Esta colección contiene ejemplos confiables e simples muertos para usar TensorFlow en C, C ++, GO y Python: Cargue un modelo previamente capacitado o compilen una operación personalizada con o sin CUDA. Todas las compilaciones se prueban con la versión estable de TensorFlow más reciente y confían en CMake con un FindTensorFlow.cmake personalizado. Este archivo CMake incluye trabajos comunes para errores en versiones específicas de TF.
| Flujo tensor | Estado |
|---|---|
| 1.14.0 | |
| 1.13.1 | |
| 1.12.0 | |
| 1.11.0 | |
| 1.10.0 | |
| 1.9.0 |
El repositorio contiene los siguientes ejemplos.
| Ejemplo | Explicación |
|---|---|
| operación personalizada | Cree una operación personalizada para TensorFlow en C ++/CUDA (solo requiere PIP) |
| inferencia (C ++) | ejecutar inferencia en c ++ |
| inferencia (c) | ejecutar inferencia en c |
| inferencia (ir) | Corre la inferencia en Go |
| escritor de eventos | Escriba archivos de eventos para TensorBoard en C ++ |
| Ejemplo de Keras CPP-Inferencia | Ejecute un modelo keras en c ++ |
| ejemplo simple | Crear y ejecutar un gráfico TensorFlow en C ++ |
| Ejemplo de imagen de tamaño de tamaño | cambiar el tamaño de una imagen en tensorflow con/sin opencv |
Este ejemplo ilustra el proceso de crear una operación personalizada utilizando C ++/CUDA y CMake. No está destinado a mostrar una implementación que obtenga un rendimiento máximo. En cambio, es solo una plantilla de planta.
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 Este ejemplo ilustra el proceso de carga de una imagen (usando OpenCV o TensorFlow), cambia el tamaño de la imagen guardando la imagen como JPG o PNG (usando OpenCV o TensorFlow).
user@host $ cd examples/resize
user@host $ export TENSORFLOW_BUILD_DIR=...
user@host $ export TENSORFLOW_SOURCE_DIR=...
user@host $ cmake .
user@host $ make Hay dos ejemplos que demuestran el manejo de TensorFlow-Flow: usando una entrada vectorial y utilizando una entrada de imagen codificada.
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] Cree un modelo en Python, guarde el gráfico en el disco y cargue en C/C+/Go/Python para realizar una inferencia. Como estos ejemplos se basan en el TensorFlow C-API, requieren la biblioteca libtensorflow_cc.so que no se envía en el paquete PIP (tensorFow-GPU). Por lo tanto, necesitará construir TensorFlow a partir de la fuente de antemano, por ejemplo,
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/Acabamos de ejecutar un modelo muy básico
x = tf . placeholder ( tf . float32 , shape = [ 1 , 2 ], name = 'input' )
output = tf . identity ( tf . layers . dense ( x , 1 ), name = 'output' ) Por lo tanto, guarde el modelo como lo hace regularmente. Esto se hace en example.py además de algunas salidas
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]