- AI Model using FastAPI and Docker
- Python Environment
- Create the Network Weights
- Run FastAPI inside of Container
- Create digit image
- Testing API
- Extra
Application of an AI model to predict digits built in Fast API and Docker.
As usual, the dependencies for the running the project is on requeriments.txt:
- Create a new environment using your prefer environment manager and activate it:
$ python3 -m venv .venv
$ source .venv/bin/activate # unix
- Install the dependencies:
$ pip install -r requirements.txt
You need to train the network if you want to generate the weights to be used on the API after. Run the script train.py
and will download the MNIST Dataset locally and execute the training of the network. In the end, will be reach something about 98% of accuracy when evaluating the model (The network is small, can be trained on modern CPUs quickly)
$ python train.py
There's now a directory called weights/
with the weights mnist_net.pth
.
I'll use here Podman, feel free to use Docker with some adjust to the commands.
- Build the image (this will take a time because of torch framework)
$ podman build -t aimodel:v1 .
- Run a container with the image created
$ podman run -p 8000:8000 aimodel:v1
Now, you can be able to see the API docs opening http://0.0.0.0:8000/docs
in your browser. We'll use the post-HTTP method.
But before, we need an image with a digit to test our network. I'll use the Pixil Art for creating the digit image (.png). Click on new
and adjust the size to 28x28
(because it's a size of our network)
- Select
Bucket - B
and the colorblack - RGB (0, 0, 0)
and paint all the background. - Optional: You can create a
new layer
with you want to draw above the background, and you can erase without changing the background layer. - Draw a digit and click on
file
→export/download
and choose a directory to save the image (doesn't need to be in the same directory as the model)
Open the docs and click on Try it out
→ Browse...
and select the digit image saved before → Execute
- Something interesting is if you swap the colors
black <-> white
as shown in the picture below:
- If you try to predict, this image will result in the prediction of "4" instead of "5".
The answer because this happens:
The trained data the background is black, and we're try predicting a image that is out of distribution. You can read more here: 4.7. Environment and Distribution ShiftCreated by BrenoAV