Skip to content

Commit c7565ed

Browse files
committed
initial commit
0 parents  commit c7565ed

13 files changed

+2947
-0
lines changed

.dockerignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
**/target

.gitignore

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# MacOS cache
2+
.DS_Store
3+
4+
# Python cache
5+
__pycache__
6+
7+
# Editor settings
8+
.vscode
9+
.ipynb_checkpoints
10+
11+
# Rust compilation targets
12+
server/target
13+
14+
# Downloaded data
15+
data/
16+

Dockerfile

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
FROM rust:latest AS build
2+
3+
WORKDIR /app
4+
COPY server/Cargo.toml server/Cargo.lock ./
5+
COPY server/src/ src/
6+
RUN cargo build --release
7+
RUN find . -type d | grep "libtorch/lib$" | xargs -I{} mv {} libtorch
8+
9+
FROM debian:buster-slim
10+
11+
RUN apt-get update \
12+
&& apt-get install -y \
13+
ca-certificates \
14+
libgomp1 \
15+
&& apt-get autoremove \
16+
&& apt-get clean \
17+
&& rm -rf /var/lib/apt/lists/*
18+
19+
WORKDIR /app
20+
COPY --from=build /app/target/release/actix-torch-server /usr/local/bin/actix-torch-server
21+
copy --from=build /app/libtorch/* /usr/lib/
22+
COPY saved_model/ /app/saved_model/
23+
24+
RUN useradd -mU -s /bin/bash actix
25+
USER actix
26+
27+
EXPOSE 8080
28+
ENTRYPOINT ["actix-torch-server", "--model-path=/app/saved_model/model.pt"]

README.md

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Serving PyTorch with Actix-Web
2+
3+
This repository gives an example of training a machine learning model using PyTorch, exporting that model, then serving inference over a RESTful API using Actix-Web in rust.
4+
5+
Implemented by request based on the original [TensorFlow example](https://github.com/kykosic/actix-tensorflow-example).
6+
7+
8+
For more information on the tools used, check out the following repositories:
9+
* [PyTorch](https://pytorch.org/)
10+
* [Torch Rust (tch-rs)](https://github.com/LaurentMazare/tch-rs)
11+
* [Actix Web](https://github.com/actix/actix-web)
12+
13+
14+
## Overview
15+
The repository has 3 sections:
16+
* `./training` – Contains the script which trains a neural network to recognize digits.
17+
* `./server` – Contains the RESTful API webserver rust code.
18+
* `./client` – Contains a sample script that demonstrates making a request to the server.
19+
20+
The training script will output a saved neural network model in compiled TorchScript format. The server then loads this into memory on startup. The server accepts a JSON payload at the `/mnist` endpoint with a single key "image" that is a base64 encoded image (PNG or JPG). This image is decoded, rescaled to the correct input dimensions, converted to grayscale, normalized (matching the training data normalization), and finally submitted to the model for inference. Predictions are returned with a "label" integer value and a "confidence" float between 0 and 1.
21+
22+
## Setup
23+
This example assumes you have [rust installed](https://www.rust-lang.org/tools/install) and python 3.6+ setup. To install the needed python dependencies:
24+
```
25+
pip install -r requirements.txt
26+
```
27+
28+
## Training
29+
The model used is a simple convolutional neural network trained on the [MNIST dataset](http://yann.lecun.com/exdb/mnist/). The data is automatically downloaded using the `torchvision` library. To train the model:
30+
```
31+
python training/train.py
32+
```
33+
This will output a saved model to `./saved_model/model.pt`. A pre-trained model is included in this repository. The model isn't too large and can be trained without any GPU.
34+
35+
## Serving
36+
The server code is a rust crate located in `./server`. In order to run, the server requires the saved model directory location specified with the `--model-path` command line argument. you can try running the server with:
37+
```
38+
cd server
39+
cargo run -- --model-dir ../saved_model/model.pt
40+
```
41+
42+
## Serving in Docker
43+
For actual deployments, you probably would want to build a release in a container to serve the API. To build the docker image:
44+
```
45+
docker build -t actix-torch .
46+
```
47+
Then to run the image locally for testing:
48+
```
49+
docker run --rm -it -p 8080:8080 actix-torch
50+
```
51+
52+
## Client Testing
53+
With the server running locally, you can test inference using `./client/client.py`. Included is a PNG file with a handwritten "3" that is base64-encoded and submitted to the server.
54+
55+
To test:
56+
```
57+
python client/client.py
58+
```
59+
60+
Input:
61+
62+
<img src="client/test_image_3.png" width="80">
63+
64+
Expected output:
65+
```
66+
POST to http://127.0.0.1:8080/mnist
67+
Response (200)
68+
Content: {
69+
"label": 3,
70+
"confidence": 0.9999999
71+
}
72+
```

client/client.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python
2+
"""
3+
Example python client for testing the prediction server
4+
"""
5+
import base64
6+
import json
7+
import os
8+
9+
import requests
10+
11+
12+
if __name__ == '__main__':
13+
url = "http://127.0.0.1:8080/mnist"
14+
this_dir = os.path.dirname(os.path.abspath(__file__))
15+
image_path = os.path.join(this_dir, 'test_image_3.png')
16+
17+
print(f"Reading image from {image_path}")
18+
with open(image_path, 'rb') as f:
19+
image = f.read()
20+
21+
data = {
22+
'image': base64.b64encode(image).decode()
23+
}
24+
print(f"POST to {url}")
25+
res = requests.post(url,
26+
json=data,
27+
headers={'content-type': 'application/json'})
28+
print(f"Response ({res.status_code})")
29+
if res.status_code == 200:
30+
print(f"Content: {json.dumps(res.json(), indent=4)}")
31+
else:
32+
print(f"Body: {res.text}")

client/test_image_3.png

14.4 KB
Loading

requirements.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
requests~=2.26.0
2+
torch~=1.9.0
3+
torchvision~=0.10.0

saved_model/model.pt

362 KB
Binary file not shown.

0 commit comments

Comments
 (0)