{"id":14959100,"url":"https://github.com/patwie/tensorflow-cmake","last_synced_at":"2025-04-06T01:09:07.171Z","repository":{"id":55497397,"uuid":"120651602","full_name":"PatWie/tensorflow-cmake","owner":"PatWie","description":"TensorFlow examples in C, C++, Go and Python without bazel but with cmake and FindTensorFlow.cmake","archived":false,"fork":false,"pushed_at":"2019-08-18T07:16:15.000Z","size":777,"stargazers_count":439,"open_issues_count":8,"forks_count":91,"subscribers_count":16,"default_branch":"master","last_synced_at":"2025-04-06T01:08:44.283Z","etag":null,"topics":["c","cmake","cpp","cuda","deep-learning","golang","inference","opencv","tensorflow","tensorflow-cc","tensorflow-cmake","tensorflow-examples","tensorflow-gpu"],"latest_commit_sha":null,"homepage":"","language":"CMake","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PatWie.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-02-07T18:04:35.000Z","updated_at":"2024-11-07T13:17:01.000Z","dependencies_parsed_at":"2022-08-15T01:50:23.258Z","dependency_job_id":null,"html_url":"https://github.com/PatWie/tensorflow-cmake","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PatWie%2Ftensorflow-cmake","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PatWie%2Ftensorflow-cmake/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PatWie%2Ftensorflow-cmake/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PatWie%2Ftensorflow-cmake/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PatWie","download_url":"https://codeload.github.com/PatWie/tensorflow-cmake/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247419860,"owners_count":20936012,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","cmake","cpp","cuda","deep-learning","golang","inference","opencv","tensorflow","tensorflow-cc","tensorflow-cmake","tensorflow-examples","tensorflow-gpu"],"created_at":"2024-09-24T13:18:50.822Z","updated_at":"2025-04-06T01:09:07.150Z","avatar_url":"https://github.com/PatWie.png","language":"CMake","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TensorFlow CMake/C++ Collection\n\nLooking at the official docs: What do you see? The usual fare?\nNow, guess what: This is a bazel-free zone. We use CMake here!\n\nThis collection contains **reliable** and **dead-simple** examples to use TensorFlow in C, C++, Go and Python: load a pre-trained model or compile a custom operation with or without CUDA. All builds are tested against the most recent stable TensorFlow version and rely on CMake with a custom [FindTensorFlow.cmake](https://github.com/PatWie/tensorflow-cmake/blob/master/cmake/modules/FindTensorFlow.cmake). This cmake file includes common work arounds for bugs in specific TF versions.\n\n| TensorFlow | Status |\n| ------ | ------ |\n|   1.14.0 | [![Build Status TensorFlow](https://ci.patwie.com/api/badges/PatWie/tensorflow-cmake/TensorFlow%201.14.0/status.svg)](http://ci.patwie.com/PatWie/tensorflow-cmake) |\n|   1.13.1 | [![Build Status TensorFlow](https://ci.patwie.com/api/badges/PatWie/tensorflow-cmake/TensorFlow%201.13.1/status.svg)](http://ci.patwie.com/PatWie/tensorflow-cmake) |\n|   1.12.0 | [![Build Status TensorFlow](https://ci.patwie.com/api/badges/PatWie/tensorflow-cmake/TensorFlow%201.12.0/status.svg)](http://ci.patwie.com/PatWie/tensorflow-cmake) |\n|   1.11.0 | [![Build Status TensorFlow](https://ci.patwie.com/api/badges/PatWie/tensorflow-cmake/TensorFlow%201.11.0/status.svg)](http://ci.patwie.com/PatWie/tensorflow-cmake) |\n|   1.10.0 | [![Build Status TensorFlow](https://ci.patwie.com/api/badges/PatWie/tensorflow-cmake/TensorFlow%201.10.0/status.svg)](http://ci.patwie.com/PatWie/tensorflow-cmake) |\n|   1.9.0 | [![Build Status TensorFlow](https://ci.patwie.com/api/badges/PatWie/tensorflow-cmake/TensorFlow%201.9.0/status.svg)](http://ci.patwie.com/PatWie/tensorflow-cmake) |\n\n\n\nThe repository contains the following examples.\n\n| Example| Explanation |\n| ------ | ------ |\n| [custom operation](./custom_op)   | build a custom operation for TensorFLow in C++/CUDA (requires only pip) |\n| [inference  (C++)](./inference/cc) | run inference in C++ |\n| [inference  (C)](./inference/c) | run inference in C |\n| [inference  (Go)](./inference/go) | run inference in Go |\n| [event writer](./examples/event_writer)  | write event files for TensorBoard in C++ |\n| [keras cpp-inference example](./examples/keras)  | run a Keras-model in C++ |\n| [simple example](./examples/simple)  | create and run a TensorFlow graph in C++ |\n| [resize image example](./examples/resize)  | resize an image in TensorFlow with/without OpenCV |\n\n\n## Custom Operation\n\nThis example illustrates the process of creating a custom operation using C++/CUDA and CMake. It is *not* intended to show an implementation obtaining peak-performance. Instead, it is just a boilerplate-template.\n\n```console\nuser@host $ pip install tensorflow-gpu --user # solely the pip package is needed\nuser@host $ cd custom_op/user_ops\nuser@host $ cmake .\nuser@host $ make\nuser@host $ python test_matrix_add.py\nuser@host $ cd ..\nuser@host $ python example.py\n```\n\n## TensorFlow Graph within C++\n\nThis example illustrates the process of loading an image (using OpenCV or TensorFlow), resizing the image  saving the image as a JPG or PNG (using OpenCV or TensorFlow).\n\n```console\nuser@host $ cd examples/resize\nuser@host $ export TENSORFLOW_BUILD_DIR=...\nuser@host $ export TENSORFLOW_SOURCE_DIR=...\nuser@host $ cmake .\nuser@host $ make\n```\n\n\n## TensorFlow-Serving\n\nThere are two examples demonstrating the handling of TensorFlow-Serving: using a vector input and using an encoded image input.\n\n```console\nserver@host $ CHOOSE=basic # or image\nserver@host $ cd serving/${CHOOSE}/training\nserver@host $ python create.py # create some model\nserver@host $ cd serving/server/\nserver@host $ ./run.sh # start server\n\n# some some queries\n\nclient@host $ cd client/bash\nclient@host $ ./client.sh\nclient@host $ cd client/python\n# for the basic-example\nclient@host $ python client_rest.py\nclient@host $ python client_grpc.py\n# for the image-example\nclient@host $ python client_rest.py /path/to/img.[png,jpg]\nclient@host $ python client_grpc.py /path/to/img.[png,jpg]\n```\n\n## Inference\n\nCreate a model in Python, save the graph to disk and load it in C/C+/Go/Python to perform inference. As these examples are based on the TensorFlow C-API they require the `libtensorflow_cc.so` library which is *not* shipped in the pip-package (tensorfow-gpu). Hence, you will need to build TensorFlow from source beforehand, e.g.,\n\n```console\nuser@host $ ls ${TENSORFLOW_SOURCE_DIR}\n\nACKNOWLEDGMENTS     bazel-genfiles      configure          pip\nADOPTERS.md         bazel-out           configure.py       py.pynano\nANDROID_NDK_HOME    bazel-tensorflow    configure.py.bkp   README.md\n...\nuser@host $ cd ${TENSORFLOW_SOURCE_DIR}\nuser@host $  ./configure\nuser@host $  # ... or whatever options you used here\nuser@host $ bazel build -c opt --copt=-mfpmath=both --copt=-msse4.2 --config=cuda //tensorflow:libtensorflow.so\nuser@host $ bazel build -c opt --copt=-mfpmath=both --copt=-msse4.2 --config=cuda //tensorflow:libtensorflow_cc.so\n\nuser@host $ export TENSORFLOW_BUILD_DIR=/tensorflow_dist\nuser@host $ mkdir ${TENSORFLOW_BUILD_DIR}\nuser@host $ cp ${TENSORFLOW_SOURCE_DIR}/bazel-bin/tensorflow/*.so ${TENSORFLOW_BUILD_DIR}/\nuser@host $ cp ${TENSORFLOW_SOURCE_DIR}/bazel-genfiles/tensorflow/cc/ops/*.h ${TENSORFLOW_BUILD_DIR}/includes/tensorflow/cc/ops/\n```\n\n### 1. Save Model\n\nWe just run a very basic model\n\n```python\nx = tf.placeholder(tf.float32, shape=[1, 2], name='input')\noutput = tf.identity(tf.layers.dense(x, 1), name='output')\n```\n\nTherefore, save the model like you regularly do. This is done in `example.py` besides some outputs\n\n```console\nuser@host $ python example.py\n\n[\u003ctf.Variable 'dense/kernel:0' shape=(2, 1) dtype=float32_ref\u003e, \u003ctf.Variable 'dense/bias:0' shape=(1,) dtype=float32_ref\u003e]\ninput            [[1. 1.]]\noutput           [[2.1909506]]\ndense/kernel:0   [[0.9070684]\n [1.2838823]]\ndense/bias:0     [0.]\n```\n\n### 2. Run Inference\n\n#### Python\n\n```console\nuser@host $ python python/inference.py\n\n[\u003ctf.Variable 'dense/kernel:0' shape=(2, 1) dtype=float32_ref\u003e, \u003ctf.Variable 'dense/bias:0' shape=(1,) dtype=float32_ref\u003e]\ninput            [[1. 1.]]\noutput           [[2.1909506]]\ndense/kernel:0   [[0.9070684]\n [1.2838823]]\ndense/bias:0     [0.]\n```\n\n#### C++\n\n```console\nuser@host $ cd cc\nuser@host $ cmake .\nuser@host $ make\nuser@host $ cd ..\nuser@host $ ./cc/inference_cc\n\ninput           Tensor\u003ctype: float shape: [1,2] values: [1 1]\u003e\noutput          Tensor\u003ctype: float shape: [1,1] values: [2.19095063]\u003e\ndense/kernel:0  Tensor\u003ctype: float shape: [2,1] values: [0.907068372][1.28388226]\u003e\ndense/bias:0    Tensor\u003ctype: float shape: [1] values: 0\u003e\n```\n\n#### C\n\n```console\nuser@host $ cd c\nuser@host $ cmake .\nuser@host $ make\nuser@host $ cd ..\nuser@host $ ./c/inference_c\n\n2.190951\n\n```\n\n\n#### Go\n\n```console\nuser@host $ go get github.com/tensorflow/tensorflow/tensorflow/go\nuser@host $ cd go\nuser@host $ ./build.sh\nuser@host $ cd ../\nuser@host $ ./inference_go\n\ninput           [[1 1]]\noutput          [[2.1909506]]\ndense/kernel:0  [[0.9070684] [1.2838823]]\ndense/bias:0    [0]\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpatwie%2Ftensorflow-cmake","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpatwie%2Ftensorflow-cmake","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpatwie%2Ftensorflow-cmake/lists"}