Skip to content

Commit bd08126

Browse files
Initial Commit
1 parent d648e56 commit bd08126

7 files changed

Lines changed: 172 additions & 0 deletions

File tree

README.md

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,42 @@ The Tensorflow version used is 1.13.1. The inference REST API works on CPU and d
77

88
Models trained using our training tensorflow repository can be deployed in this API. Several object detection models can be loaded and used at the same time.
99

10+
This repo can be deployed using either **docker** or **docker swarm**.
11+
12+
Please use **docker swarm** only if you need to:
13+
14+
* Provide redundancy in terms of API containers: In case a container went down, the incoming requests will be redirected to another running instance.
15+
16+
* Coordinate between the containers: Swarm will orchestrate between the APIs and choose one of them to listen to the incoming request.
17+
18+
* Scale up the Inference service in order to get a faster prediction especially if there's traffic on the service.
19+
20+
If none of the aforementioned requirements are needed, simply use **docker**.
21+
1022
![predict image](./docs/4.gif)
1123

24+
## Contents
25+
26+
```sh
27+
Tensorflow CPU Inference API For Windows and Linux/
28+
├── Prerequisites
29+
│ ├── Check for prerequisites
30+
│ └── Install prerequisites
31+
├── Build The Docker Image
32+
├── Run the docker container
33+
│ ├── Docker
34+
│ └── Docker swarm
35+
│ ├── Docker swarm setup
36+
│ ├── With one host
37+
│ ├── With multiple hosts
38+
│ └── Useful Commands
39+
├── API Endpoints
40+
├── Model structure
41+
└── Benchmarking
42+
├── Docker
43+
└── Docker swarm
44+
```
45+
1246
## Prerequisites
1347

1448
- OS:
@@ -56,6 +90,8 @@ sudo docker build --build-arg http_proxy='' --build-arg https_proxy='' -t tensor
5690

5791
## Run the docker container
5892

93+
### Docker
94+
5995
To run the API, go the to the API's directory and run the following:
6096

6197
#### Using Linux based docker:
@@ -74,6 +110,104 @@ The <docker_host_port> can be any unique port of your choice.
74110

75111
The API file will be run automatically, and the service will listen to http requests on the chosen port.
76112

113+
114+
115+
In case you are deploying your API without **docker swarm**, please skip the next section and directly proceed to *API endpoints section*.
116+
117+
### Docker swarm
118+
119+
Docker swarm can scale up the API into multiple replicas and can be used on one or multiple hosts(Linux users only). In both cases, a docker swarm setup is required for all hosts.
120+
121+
#### Docker swarm setup
122+
123+
1- Initialize Swarm:
124+
125+
```sh
126+
docker swarm init
127+
```
128+
129+
2- On the manager host, open the cpu-inference.yaml file and specify the number of replicas needed. In case you are using multiple hosts (With multiple hosts section), the number of replicas will be divided across all hosts.
130+
131+
```yaml
132+
version: "3"
133+
134+
services:
135+
api:
136+
ports:
137+
- "4343:4343"
138+
image: tensorflow_inference_api_cpu
139+
volumes:
140+
- "/mnt/models:/models"
141+
deploy:
142+
replicas: 1
143+
update_config:
144+
parallelism: 2
145+
delay: 10s
146+
restart_policy:
147+
condition: on-failure
148+
```
149+
150+
**Notes about cpu-inference.yaml:**
151+
152+
* the volumes field on the left of ":" should be an absolute path, can be changeable by the user, and represents the models directory on your Operating System
153+
* the following volume's field ":/models" should never be changed
154+
155+
#### With one host
156+
157+
Deploy the API:
158+
159+
```sh
160+
docker stack deploy -c cpu-inference.yaml tensorflow-cpu
161+
```
162+
163+
![onehost](./docs/tcpu.png)
164+
165+
#### With multiple hosts (Linux users only)
166+
167+
1- **Make sure hosts are reachable on the same network**.
168+
169+
2- Choose a host to be the manager and run the following command on the chosen host to generate a token so the other hosts can join:
170+
171+
```sh
172+
docker swarm join-token worker
173+
```
174+
175+
A command will appear on your terminal, copy and paste it on the other hosts, as seen in the below image
176+
177+
3- Deploy your application using:
178+
179+
```sh
180+
docker stack deploy -c cpu-inference.yaml tensorflow-cpu
181+
```
182+
183+
![multhost](./docs/tcpu2.png)
184+
185+
#### Useful Commands
186+
187+
1- In order to scale up the service to 4 replicas for example use this command:
188+
189+
```sh
190+
docker service scale tensorflow-cpu_api=4
191+
```
192+
193+
2- To check the available workers:
194+
195+
```sh
196+
docker node ls
197+
```
198+
199+
3- To check on which node the container is running:
200+
201+
```sh
202+
docker service ps tensorflow-cpu_api
203+
```
204+
205+
4- To check the number of replicas:
206+
207+
```sh
208+
docker service ls
209+
```
210+
77211
## API Endpoints
78212

79213
To see all available endpoints, open your favorite browser and navigate to:
@@ -167,6 +301,8 @@ Inside each subfolder there should be a:
167301

168302
## Benchmarking
169303

304+
### Docker
305+
170306
<table>
171307
<thead align="center">
172308
<tr>
@@ -230,6 +366,26 @@ Inside each subfolder there should be a:
230366
</tbody>
231367
</table>
232368

369+
### Docker swarm
370+
371+
Here are two graphs showing time of prediction for different number of requests at the same time.
372+
373+
374+
![CPU 20 req](./docs/TCPU20req.png)
375+
376+
377+
![CPU 40 req](./docs/TCPU40req.png)
378+
379+
380+
We can see that both graphs got the same result no matter what is the number of received requests at the same time. When we increase the number of workers (hosts) we are able to speed up the inference by at least 2 times. For example we can see in the last column we were able to process 40 requests in:
381+
382+
- 17.5 seconds with 20 replicas in 1 machine
383+
- 8.8 seconds with 20 replicas in each of the 2 machines
384+
385+
Moreover, in case one of the machines is down the others are always ready to receive requests.
386+
387+
Finally since we are predicting on CPU scaling more replicas doesn't mean a faster prediction, 4 containers was faster than 20.
388+
233389
## Acknowledgment
234390

235391
[inmind.ai](https://inmind.ai)

cpu-inference.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
version: "3"
2+
3+
services:
4+
api:
5+
ports:
6+
- "4343:4343"
7+
image: tensorflow_inference_api_cpu
8+
volumes:
9+
- "/mnt/models:/models"
10+
deploy:
11+
replicas: 1
12+
update_config:
13+
parallelism: 2
14+
delay: 10s
15+
restart_policy:
16+
condition: on-failure

docs/TCPU20req.png

20.8 KB
Loading

docs/TCPU40req.png

21.2 KB
Loading

docs/nvidia-smi.gif

21.1 KB
Loading

docs/tcpu.png

14.4 KB
Loading

docs/tcpu2.png

30.8 KB
Loading

0 commit comments

Comments
 (0)