|
1 | | -# TensorFlow Serving + Java + Kafka Streams + gRCP |
2 | | -This project contains a demo to do model inference with Apache Kafka, Kafka Streams and a TensorFlow model deployed using [TensorFlow Serving](https://www.tensorflow.org/serving/) (leveraging [Google Cloud ML Engine](https://cloud.google.com/ml-engine/docs/tensorflow/deploying-models) in this example). The concepts are very similar for other ML frameworks and Cloud Providers, e.g. you could also use Apache MXNet and [AWS model server](https://github.com/awslabs/mxnet-model-server). |
| 1 | +# TensorFlow Serving + gRPC + Java + Kafka Streams |
| 2 | +This project contains a demo to do **model inference with Apache Kafka, Kafka Streams and a TensorFlow model deployed using [TensorFlow Serving](https://www.tensorflow.org/serving/)**. The concepts are very similar for other ML frameworks and Cloud Providers, e.g. you could also use [Google Cloud ML Engine](https://cloud.google.com/ml-engine/docs/tensorflow/deploying-models) for TensorFlow (which uses TensorFlow Serving under the hood) or Apache MXNet and [AWS model server](https://github.com/awslabs/mxnet-model-server). |
| 3 | + |
| 4 | +Most ML servers for model serving are also extendible to serve other types of models and data, e.g. you could also deploy non-TensorFlow models to TensorFlow Serving. Many ML servers are available as cloud service and for local deployment. |
3 | 5 |
|
4 | 6 | ## Model Serving: Stream Processing vs. Request Response |
5 | | -Machine Learning / Deep Learning models can be used in different way to do predictions. The preferred way is to deploy an analytic model directly into a Kafka Streams application. You could e.g. use the [TensorFlow for Java API](https://www.tensorflow.org/install/install_java). Examples here: [Model Inference within Kafka Streams Microservices](https://github.com/kaiwaehner/kafka-streams-machine-learning-examples). |
| 7 | +Some background on model serving alternatives: |
| 8 | + |
| 9 | +Machine Learning / Deep Learning models can be used in different ways to do predictions. The preferred way is to deploy an analytic model directly into a stream processing application (like [Kafka Streams](https://kafka.apache.org/documentation/streams/)). You could e.g. use the [TensorFlow for Java API](https://www.tensorflow.org/install/install_java). This allows best latency and independence of external services. Examples here: [Model Inference within Kafka Streams Microservices](https://github.com/kaiwaehner/kafka-streams-machine-learning-examples). |
6 | 10 |
|
7 | | -However, it is not always a feasible approach. Sometimes it makes sense or is needed to deploy a model in another serving infrastructure like TF-Serving for TensorFlow models. This project shows how access such an infrastructure via Apache Kafka and Kafka Streams. |
| 11 | +However, direct deployment of models is not always a feasible approach. Sometimes it makes sense or is needed to deploy a model in another serving infrastructure like TensorFlow Serving for TensorFlow models. Organisational or technical reasons might force this approach. This Github project shows an **example for how to access a model serving infrastructure from a stream processing microservice leveraging Apache Kafka and Kafka Streams**. |
8 | 12 |
|
9 | 13 |  |
10 | 14 |
|
11 | | -*Pros of an external model serving infrastructure like TensorFlow Serving:* |
12 | | -- Simple integration with existing systems and technologies |
| 15 | +**Pros of an external model serving infrastructure like TensorFlow Serving:** |
| 16 | +- Simple integration with existing technologies and organizational processes |
13 | 17 | - Easier to understand if you come from non-streaming world |
14 | 18 | - Later migration to real streaming is also possible |
15 | 19 |
|
16 | | -*Cons:* |
17 | | -- Framework-specific Deployment (e.g. only TensorFlow models) |
18 | | -- Coupling the availability, scalability, and latency/throughput of your Kafka Streams application with the SLAs of the RPC interface |
| 20 | + |
| 21 | +**Cons:** |
| 22 | +- Worse latency as remote call instead of local inference |
| 23 | +- No offline inference (devices, edge processing, etc.) |
| 24 | +- Coupling the availability, scalability, and latency / throughput of your Kafka Streams application with the SLAs of the RPC interface |
19 | 25 | - Side-effects (e.g. in case of failure) not covered by Kafka processing (e.g. Exactly Once) |
20 | | -- Worse latency as communication over internet required |
21 | | -- No local inference (offline, devices, edge processing, etc.) |
22 | 26 |
|
23 | | -## TensorFlow Serving (using Google Cloud ML Engine) |
24 | | -The blog post "[How to deploy TensorFlow models to production using TF Serving](https://medium.freecodecamp.org/how-to-deploy-tensorflow-models-to-production-using-tf-serving-4b4b78d41700)" is a great explanation of how to export and deploy trained TensorFlow models to a TensorFlow Serving infrastructure. You can either deploy your own infrastructure anywhere or leverage a cloud service like Google Cloud ML Engine. A [SavedModel](https://www.tensorflow.org/programmers_guide/saved_model#build_and_load_a_savedmodel) is TensorFlow's recommended format for saving models, and it is the required format for deploying trained TensorFlow models using TensorFlow Serving or deploying on Goodle Cloud ML Engine |
25 | 27 |
|
26 | | -Things to do: |
27 | | -1. Create Cloud ML Engine |
28 | | -2. Deploy prebuild TensorFlow Model |
29 | | -3. Create Kafka Cluster |
30 | | -4. Implement Kafka Streams application |
31 | | -5. Deploy Kafka Streams application (e.g. to a Kubernetes cluster) |
32 | | -6. Generate streaming data to test the combination of Kafka Streams and TensorFlow Serving |
| 28 | +### TensorFlow Serving |
| 29 | +The blog post "[How to deploy TensorFlow models to production using TF Serving](https://medium.freecodecamp.org/how-to-deploy-tensorflow-models-to-production-using-tf-serving-4b4b78d41700)" is a great explanation of how to export and deploy trained TensorFlow models to a TensorFlow Serving infrastructure. You can either deploy your own infrastructure anywhere or leverage a cloud service like Google Cloud ML Engine. A [SavedModel](https://www.tensorflow.org/programmers_guide/saved_model#build_and_load_a_savedmodel) is TensorFlow's recommended format for saving models, and it is the required format for deploying trained TensorFlow models using TensorFlow Serving or deploying on Goodle Cloud ML Engine. |
33 | 30 |
|
34 | 31 |
|
35 | | -### Step 1: Create a TensorFlow model and export it to 'SavedModel' format. |
36 | | -I simply added an existing pretrained Image Recognition model built with TensorFlow (Inception V1). |
| 32 | +## Demo: Mixing Stream Processing with RPC: TensorFlow Serving + Kafka Streams |
37 | 33 |
|
38 | | -I also created a new model for predictions of census using the "[ML Engine getting started guide](https://cloud.google.com/ml-engine/docs/tensorflow/getting-started-training-prediction)". The data for training is in 'data' folder. |
| 34 | +### Requirements |
| 35 | +- Java 8 |
| 36 | +- Docker |
| 37 | + |
| 38 | +### Things to do |
| 39 | +1. Install and start a ML Serving Engine |
| 40 | +2. Deploy prebuilt TensorFlow Model |
| 41 | +3. Create Kafka Cluster |
| 42 | +4. Implement Kafka Streams application |
| 43 | +5. Deploy Kafka Streams application (e.g. locally on laptop or to a Kubernetes cluster) |
| 44 | +6. Generate streaming data to test the combination of Kafka Streams and TensorFlow Serving |
39 | 45 |
|
40 | | -### Step 2: Deploy model to Google ML Engine |
41 | | -[Getting Started with Google ML Engine](https://cloud.google.com/ml-engine/docs/tensorflow/deploying-models) |
| 46 | +### Step 1: Create a TensorFlow model and export it to 'SavedModel' format |
| 47 | +I simply added an existing pretrained Image Recognition model built with TensorFlow. You just need to export a model using TensorFlow's API and then use the exported folder. TensorFlow uses Protobuf to store the model graph and adds variables for the weights of the neural network. |
42 | 48 |
|
43 | | -### Step 3: Create Kafka Cluster using GCP Confluent Cloud |
44 | | -[Confluent Cloud - Apache Kafka as a Service](https://www.confluent.io/confluent-cloud/) |
| 49 | +Google ML Engine shows how to create a simple TensorFlow model for predictions of census using the "[ML Engine getting started guide](https://cloud.google.com/ml-engine/docs/tensorflow/getting-started-training-prediction)". In a second step, you can build a more advanced example for image recognition using Transfer Learning folling the guide "[Image Classification using Flowers dataset](https://cloud.google.com/ml-engine/docs/tensorflow/flowers-tutorial)". |
45 | 50 |
|
46 | | -### TODO Implement and deploy Streams app |
| 51 | +You can also combine cloud and local services, e.g. build the analytic model with Google ML Engine and then deploy it locally using TensorFlow Serving as we do. |
47 | 52 |
|
48 | | -### Example 4 - Census Prediction with TensorFlow Serving |
49 | | -This example shows how do use TensorFlow Serving to deploy a model. The Kafka Streams app can access it via HTTP or gRPC to do the inference. You could also use e.g. Google Cloud ML Engine to deploy the TensorFlow model in a public cloud the same way. |
| 53 | +### Step 2: Install and start TensorFlow Serving server + deploy model |
| 54 | +Different options are available. Installing TensforFlow Serving on a Mac is still a pain in mid of 2018. apt-get works much easier on Linux operating systems. Unforunately there is nothing like a 'brew' command or simple zip file you can use on Mac. Alternatives: |
50 | 55 |
|
51 | | -TODO more details discussed in another github project. |
| 56 | +- You can **build the project and compile everything using [Bazel build system]**(https://bazel.build/) - which literaly takes forever (on my laptop), i.e. many hours. |
52 | 57 |
|
53 | | -Steps: |
54 | | -- Install and run TensorFlow Serving locally (e.g. in [Docker container](https://www.tensorflow.org/serving/docker)) |
| 58 | +- **Install and run TensorFlow Serving via a [Docker container](https://www.tensorflow.org/serving/docker)** |
| 59 | + |
55 | 60 | docker build --pull -t tensorflow-serving-devel -f Dockerfile.devel . |
| 61 | + |
56 | 62 | docker run -it tensorflow-serving-devel |
57 | 63 |
|
58 | 64 | git clone --recurse-submodules https://github.com/tensorflow/serving |
59 | 65 | cd serving/tensorflow |
60 | 66 | ./configure |
61 | 67 | cd .. |
62 | 68 | bazel test tensorflow_serving/... |
| 69 | +Also requires building the project. In addition, documentation is not very good and outdated. |
63 | 70 |
|
64 | | -=> Takes long time... Better use a prebuilt container like below |
65 | | -- [Deploy TensorFlow model to TensorFlow serving](https://www.tensorflow.org/programmers_guide/saved_model#load_and_serve_a_savedmodel_in_tensorflow_serving) |
| 71 | +- **Preferred option for beginners => Use a prebuilt Docker container with TensorFlow Serving**. I used an [example from Thamme Gowda](https://github.com/thammegowda/tensorflow-grpc-java). Kudos to him for building a project which not just contains the TensorFlow Serving Docker image, but also shows an example of how to do gRPC communication between a Java application and TensorFlow Serving. |
66 | 72 |
|
67 | | -- mvn clean package istall |
| 73 | +**Pull and start container with TensorFlow Serving preinstalled (forward port 9000)** |
| 74 | + |
| 75 | + docker run -it -p 9000:9000 tgowda/inception_serving_tika |
| 76 | + |
| 77 | +**Inside the container, start the Tensorflow Serving server - this deploys the TensorFlow model for Image Recognition** |
| 78 | + |
| 79 | + root@8311ea4e8074:/# /serving/server.sh |
| 80 | + |
| 81 | +If you want to your own model, read the guide "[Deploy TensorFlow model to TensorFlow serving](https://www.tensorflow.org/programmers_guide/saved_model#load_and_serve_a_savedmodel_in_tensorflow_serving)". Or to use a cloud service, e.g. take a look at "[Getting Started with Google ML Engine](https://cloud.google.com/ml-engine/docs/tensorflow/deploying-models)". |
| 82 | + |
| 83 | +### Step 3: Create Kafka Cluster and Kafka topics |
| 84 | +Create a local Kafka environment (Apache Kafka broker + Zookeeper). The easiest way is the open source [Confluent CLI](https://github.com/confluentinc/confluent-cli) - which is also part of Confluent Open Source and Confluent Enteprise Platform: |
68 | 85 |
|
69 | | -- Start Kafka and create topics |
70 | 86 | confluent start kafka |
71 | 87 | |
| 88 | +You can also create a cluster using Kafka as a Service. Best option is [Confluent Cloud - Apache Kafka as a Service](https://www.confluent.io/confluent-cloud/). You can choose between Confluent Cloud Professional for "playing around" or Confluent Cloud Enterprise on AWS, GCP or Azure for mission-critical deployments including 99.95% SLA and very large scale up to 2 GBbyte/second throughput. The third option is to connect to your existing Kafka cluster on premise or in cloud (note that you need to change the broker URL and port in the Kafka Streams Java code before building the project). |
| 89 | + |
| 90 | +Next create the two Kafka topics for this example ('ImageInputTopic' for URLs to the image and 'ImageOutputTopic' for the prediction result): |
| 91 | + |
72 | 92 | kafka-topics --zookeeper localhost:2181 --create --topic ImageInputTopic --partitions 3 --replication-factor 1 |
73 | 93 |
|
74 | 94 | kafka-topics --zookeeper localhost:2181 --create --topic ImageOutputTopic --partitions 3 --replication-factor 1 |
75 | | - |
76 | | - java -cp target/kafka-streams-machine-learning-examples-1.0-SNAPSHOT-jar-with-dependencies.jar com.github.megachucky.kafka.streams.machinelearning.Kafka_Streams_TensorFlow_Serving_gRPC_Image_Recognition_Example |
77 | 95 |
|
| 96 | +### Step 4 Build and deploy Kafka Streams app + send test messages |
| 97 | +The Kafka Streams microservice [Kafka_Streams_TensorFlow_Serving_gRPC_Example](https://github.com/kaiwaehner/tensorflow-serving-java-grpc-kafka-streams/blob/master/src/main/java/com/github/megachucky/kafka/streams/machinelearning/Kafka_Streams_TensorFlow_Serving_gRPC_Example.java) is the Kafka Streams Java client. The microservice uses gRPC and Protobuf for request-response communication with the TensorFlow Serving server to do model inference to predict the contant of the image. Note that the Java client does not need any TensorFlow APIs, but just gRPC interfaces. |
78 | 98 |
|
79 | | - java -cp target/kafka-streams-machine-learning-examples-1.0-SNAPSHOT-jar-with-dependencies.jar com.github.megachucky.kafka.streams.machinelearning.Main |
80 | | - |
81 | | - |
82 | | -- TODO Start Streams App |
83 | | -- TODO Start Kafka and create topic |
84 | | -- TODO Send test message |
| 99 | +Let's build the project: |
85 | 100 |
|
86 | | -- Send messages, e.g. with kafkacat: |
87 | | - echo -e "src/main/resources/TensorFlow_Images/dog.jpg" | kafkacat -b localhost:9092 -P -t ImageInputTopic |
| 101 | + mvn clean package |
88 | 102 | |
89 | | -- Consume predictions: |
90 | | - kafka-console-consumer --bootstrap-server localhost:9092 --topic ImageOutputTopic --from-beginning |
91 | | -- Find more details in the unit test... |
92 | | - |
93 | | - |
94 | | -https://github.com/gameofdimension/inception-java-client |
95 | | -pull and start the prebuilt container, forward port 9000 |
| 103 | +This example executes a Java main method, i.e. it starts a local Java process running the Kafka Streams microservice. It waits continuously for new events arriving at 'ImageInputTopic' to do a model inference (via gRCP call to TensorFlow Serving) and then sending the prediction to 'ImageOutputTopic' - all in real time within milliseconds. |
96 | 104 |
|
97 | | -# pull and start the prebuilt container, forward port 9000 |
98 | | -docker run -it -p 9000:9000 tgowda/inception_serving_tika |
99 | | - |
100 | | -# Inside the container, start tensorflow service |
101 | | -root@8311ea4e8074:/# /serving/server.sh |
102 | | -This is hosting the model. The client just uses gRPC and Protobuf. It does not include any TensorFlow APIs. |
103 | | - |
104 | | -mvn clean compile exec:java -Dexec.args="localhost:9000 example.jpg" |
| 105 | + java -cp target/kafka-streams-machine-learning-examples-1.0-SNAPSHOT-jar-with-dependencies.jar com.github.megachucky.kafka.streams.machinelearning.Kafka_Streams_TensorFlow_Serving_gRPC_Image_Recognition_Example |
105 | 106 |
|
106 | | -https://github.com/thammegowda/tensorflow-grpc-java/blob/master/src/main/java/edu/usc/irds/tensorflow/grpc/TensorflowObjectRecogniser.java |
| 107 | +In the same way, you could deploy this Kafka Streams microservice anywhere - including Kubernetes (e.g. on premise OpenShift cluster or Google Kubernetes Engine), Mesosphere, Amazon ECS or even in a Java EE app - and scale it up and down dynamically. |
| 108 | + |
| 109 | +Now send messages, e.g. with kafkacat... |
107 | 110 |
|
108 | | -java -cp target/kafka-streams-machine-learning-examples-1.0-SNAPSHOT-jar-with-dependencies.jar com.github.megachucky.kafka.streams.machinelearning.Main localhost:9000 src/main/resources/TensorFlow_Images/dog.jpg |
| 111 | + echo -e "src/main/resources/TensorFlow_Images/dog.jpg" | kafkacat -b localhost:9092 -P -t ImageInputTopic |
| 112 | + |
| 113 | +... and consume predictions: |
109 | 114 |
|
110 | | -java -cp target/kafka-streams-machine-learning-examples-1.0-SNAPSHOT-jar-with-dependencies.jar com.github.megachucky.kafka.streams.machinelearning.Kafka_Streams_TensorFlow_Serving_gRPC_Image_Recognition_Example localhost:9000 src/main/resources/TensorFlow_Images/dog.jpg |
| 115 | + kafka-console-consumer --bootstrap-server localhost:9092 --topic ImageOutputTopic --from-beginning |
0 commit comments