Giter VIP home page Giter VIP logo

ai-model-fastapi-docker's Introduction

AI Model using FastAPI and Docker

Application of an AI model to predict digits built in Fast API and Docker.

Python Environment

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

Create the Network Weights

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.

Run FastAPI inside of Container

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.

Create digit image

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)

New draw window

  • Select Bucket - B and the color black - 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 fileexport/download and choose a directory to save the image (doesn't need to be in the same directory as the model)

Example of digit

Testing API

Open the docs and click on Try it outBrowse... and select the digit image saved before → Execute

Final result test on API

Extra

  • Something interesting is if you swap the colors black <-> white as shown in the picture below:

Swapping colors

  • If you try to predict, this image will result in the prediction of "4" instead of "5".

Predicting swapped colors image

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 Shift

"PEOPLE'S DREAMS WILL NEVER END" - OP

Created by BrenoAV

ai-model-fastapi-docker's People

Contributors

brenoav avatar tobiasweede avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.