This project consists of building a convolutional neural network model that classifies the emotion that a face shows in a given photo. Once the model is created, save it to W&B and create an API hosted on Heroku that makes the created model available for production.
The convolutional neural network consists of multiple layers: convolucional, dense(ReLU), Batch Normalization, Max Pooling, Dropout, Flatten and a Dense(Softmax) layer to classify the emotion.
The data consists of 48x48 pixels grayscale images of faces. The faces have been automatically registered. The task is to categorize each face based on the emotion shown in the facial expression into one of six categories (0=Angry, 1=Fear, 2=Happy, 3=Sad, 4=Surprise, 5=Neutral). The training set consists of 35,342 examples
The model was deployed to the web using the FastAPI package and API tests were created. The API tests will be embedded in a CI/CD framework using GitHub Actions. After we built our API locally and tested it, we deployed it to Heroku and tested it again live. Weights and Biases were used to manage the model.
So, in general, the notebook used is divided into 7 parts:
- Import library
- Loggin in W&B
- Import data
- Splitting the data between training and testing
- Create model
- Training
- Test
Create a conda environment with environment.yml
:
conda env create --file environment.yml
To remove an environment in your terminal window run:
conda remove --name myenv --all
To list all available environments run:
conda env list
To activate the environment, use
conda activate myenv
The API is implemented in the source/api/main.py
whereas tests are on source/api/test_main.py
.
For the sake of understanding and during the development, the API was constanly tested using:
uvicorn source.api.main:app --reload
and using these addresses:
http://127.0.0.1:8000/
http://127.0.0.1:8000/docs
The screenshot below show a view of the API docs.
For test the API, please run:
pytest source/api -vv -s
- Sign up for free and experience Heroku.
- Now, it's time to create a new app. It is very important to connect the APP to our Github repository and enable the automatic deploys.
- Install the Heroku CLI following the instructions.
- Sign in to heroku using terminal
heroku login
- In the root folder of the project check the heroku projects already created.
heroku apps
- Check buildpack is correct:
heroku buildpacks --app faces-emotions
- Update the buildpack if necessary:
heroku buildpacks:set heroku/python --app faces-emotions
- When you're running a script in an automated environment, you can control Wandb with environment variables set before the script runs or within the script. Set up access to Wandb on Heroku, if using the CLI:
heroku config:set WANDB_API_KEY=xxx --app faces-emotions
- The instructions for launching an app are contained in a
Procfile
file that resides in the highest level of your project directory. Create theProcfile
file with:
web: uvicorn source.api.main:app --host=0.0.0.0 --port=${PORT:-5000}
- Configure the remote repository for Heroku:
heroku git:remote --app faces-emotions
- Push all files to remote repository in Heroku. The command below will install all packages indicated in
requirements.txt
to Heroku VM.
git push heroku main
- Check the remote files run:
heroku run bash --app faces-emotions
- If all previous steps were done with successful you will see the message below after open:
https://faces-emotions.herokuapp.com/
. - For debug purposes whenever you can fetch your app’s most recent logs, use the heroku logs command:
heroku logs
Main reference - Ivanovitch's git repo