Gender Classification
This is a simple REST
api that is served to classify gender on an image given based on faces.
Starting the server
To run this server and make prediction on your own images follow the following steps
- create a virtual environment and activate it
- run the following command to install packages
pip install -r requirements.txt
- navigate to the
app.py
file and run
python app.py
Model Metrics
The following table shows all the metrics summary we get after training the model for few 6
epochs.
model name | model description | test accuracy | validation accuracy | train accuracy | test loss | validation loss | train loss |
---|---|---|---|---|---|---|---|
gender-classification | classification of gender using (vgg16 and python flask) | 95.04% | 91.59% | 91.59% | 0.1273 | 0.2593 | 0.2593 |
Classification report
This classification report is based on the first batch of the validation dataset i used which consist of 32 images.
precision recall f1-score support
# | precision | recall | f1-score | support |
---|---|---|---|---|
accuracy | 100% | 512 | ||
macro avg | 100% | 100% | 100% | 512 |
weighted avg | 100% | 100% | 100% | 512 |
Confusion matrix
The following image represents a confusion matrix for the first batch in the validation set which contains 32
images:
Gender classification
If you hit the server at http://localhost:3001/api/gender
you will be able to get the following expected response that is if the request method is POST
and you provide the file expected by the server.
Expected Response
The expected response at http://localhost:3001/api/gender
with a file image
of the right format will yield the following json
response to the client.
{
"predictions": {
"class": "male",
"label": 1,
"meta": {
"description": "classifying gender based on the face of a human being, (vgg16).",
"language": "python",
"library": "tensforflow: v2.*",
"main": "computer vision (cv)",
"programmer": "@crispengari"
},
"predictions": [
{
"class": "female",
"label": 0,
"probability": 0.019999999552965164
},
{
"class": "male",
"label": 1,
"probability": 0.9800000190734863
}
],
"probability": 0.9800000190734863
},
"success": true
}
curl
Using Make sure that you have the image named female.jpg
in the current folder that you are running your cmd
otherwise you have to provide an absolute or relative path to the image.
To make a
curl
POST
request athttp://localhost:3001/api/gender
with the filefemale.jpg
we run the following command.
curl -X POST -F [email protected] http://127.0.0.1:3001/api/gender
Using Postman client
To make this request with postman we do it as follows:
- Change the request method to
POST
- Click on
form-data
- Select type to be
file
on theKEY
attribute - For the
KEY
typeimage
and select the image you want to predict undervalue
- Click send
If everything went well you will get the following response depending on the face you have selected:
{
"predictions": {
"class": "male",
"label": 1,
"meta": {
"description": "classifying gender based on the face of a human being, (vgg16).",
"language": "python",
"library": "tensforflow: v2.*",
"main": "computer vision (cv)",
"programmer": "@crispengari"
},
"predictions": [
{
"class": "female",
"label": 0,
"probability": 0.019999999552965164
},
{
"class": "male",
"label": 1,
"probability": 0.9800000190734863
}
],
"probability": 0.9800000190734863
},
"success": true
}
fetch
api.
Using JavaScript - First you need to get the input from
html
- Create a
formData
object - make a POST requests
const input = document.getElementById("input").files[0];
let formData = new FormData();
formData.append("image", input);
fetch("http://localhost:3001/predict", {
method: "POST",
body: formData,
})
.then((res) => res.json())
.then((data) => console.log(data));
If everything went well you will be able to get expected response.
{
"predictions": {
"class": "male",
"label": 1,
"meta": {
"description": "classifying gender based on the face of a human being, (vgg16).",
"language": "python",
"library": "tensforflow: v2.*",
"main": "computer vision (cv)",
"programmer": "@crispengari"
},
"predictions": [
{
"class": "female",
"label": 0,
"probability": 0.019999999552965164
},
{
"class": "male",
"label": 1,
"probability": 0.9800000190734863
}
],
"probability": 0.9800000190734863
},
"success": true
}
Notebooks
The ipynb
notebook that i used for training the model and saving an .h5
file was can be found: