Pneumonia Classification
This is a simple REST api that is served to classify pneumonia given an X-ray image of a chest of a human being. The following are expected results when the model does it's classification.
- pneumonia bacteria
- pneumonia virus
- normal
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.pyfile and run
python app.py
Model
We are using a simple Multi Layer Perceptron (MLP) achitecture to do the categorical image classification on chest-x-ray images which looks simply as follows:
class MLP(nn.Module):
def __init__(self, input_dim, output_dim, dropout=.5):
super(MLP, self).__init__()
self.input_fc = nn.Linear(input_dim, 250)
self.hidden_fc = nn.Linear(250, 100)
self.output_fc = nn.Linear(100, output_dim)
self.dropout = nn.Dropout(dropout)
def forward(self, x):
batch_size = x.shape[0]
x = x.view(batch_size, -1)
x = F.relu(self.input_fc(x))
x = self.dropout(x)
x = F.relu(self.hidden_fc(x))
x = self.dropout(x)
outputs = self.output_fc(x)
return outputs, x
All images are transformed to
grayscale.
Model Metrics
The following table shows all the metrics summary we get after training the model for few 10 epochs.
| model name | model description | test accuracy | validation accuracy | train accuracy | test loss | validation loss | train loss |
|---|---|---|---|---|---|---|---|
| chest-x-ray.pt | pneumonia classification using Multi Layer Perceprton (MLP) | 73.73% | 73.73% | 72.47% | 0.621 | 0.621 | 0.639 |
Classification report
This classification report is based on the first batch of the test dataset i used which consist of 64 images in a batch.
| # | precision | recall | f1-score | support |
|---|---|---|---|---|
| micro avg | 100% | 81% | 90% | 4096 |
| macro avg | 100% | 81% | 90% | 4096 |
| weighted avg | 100% | 81% | 90% | 4096 |
Confusion matrix
The following image represents a confusion matrix for the first batch in the validation set which contains 64 images in a batch:
Pneumonia classification
If you hit the server at http://localhost:3001/api/pneumonia 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/pneumonia with a file image of the right format will yield the following json response to the client.
{
"predictions": {
"class_label": "PNEUMONIA VIRAL",
"label": 2,
"meta": {
"description": "given a medical chest-x-ray image of a human being we are going to classify weather a person have pneumonia virus, pneumonia bacteria or none of those(normal).",
"language": "python",
"library": "pytorch",
"main": "computer vision (cv)",
"programmer": "@crispengari"
},
"predictions": [
{
"class_label": "NORMAL",
"label": 0,
"probability": 0.15000000596046448
},
{
"class_label": "PNEUMONIA BACTERIA",
"label": 1,
"probability": 0.10000000149011612
},
{ "class_label": "PNEUMONIA VIRAL", "label": 2, "probability": 0.75 }
],
"probability": 0.75
},
"success": true
}
Using curl
Make sure that you have the image named normal.jpeg 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
curlPOSTrequest athttp://localhost:3001/api/pneumoniawith the filenormal.jpegwe run the following command.
curl -X POST -F [email protected] http://127.0.0.1:3001/api/pneumonia
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
fileon theKEYattribute - For the
KEYtypeimageand 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_label": "NORMAL",
"label": 0,
"meta": {
"description": "given a medical chest-x-ray image of a human being we are going to classify weather a person have pneumonia virus, pneumonia bacteria or none of those(normal).",
"language": "python",
"library": "pytorch",
"main": "computer vision (cv)",
"programmer": "@crispengari"
},
"predictions": [
{
"class_label": "NORMAL",
"label": 0,
"probability": 0.8500000238418579
},
{
"class_label": "PNEUMONIA BACTERIA",
"label": 1,
"probability": 0.07000000029802322
},
{
"class_label": "PNEUMONIA VIRAL",
"label": 2,
"probability": 0.07999999821186066
}
],
"probability": 0.8500000238418579
},
"success": true
}
Using JavaScript fetch api.
- First you need to get the input from
html - Create a
formDataobject - make a POST requests
const input = document.getElementById("input").files[0];
let formData = new FormData();
formData.append("image", input);
fetch("http://127.0.0.1:3001/api/pneumonia", {
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_label": "PNEUMONIA VIRAL",
"label": 2,
"meta": {
"description": "given a medical chest-x-ray image of a human being we are going to classify weather a person have pneumonia virus, pneumonia bacteria or none of those(normal).",
"language": "python",
"library": "pytorch",
"main": "computer vision (cv)",
"programmer": "@crispengari"
},
"predictions": [
{
"class_label": "NORMAL",
"label": 0,
"probability": 0.15000000596046448
},
{
"class_label": "PNEUMONIA BACTERIA",
"label": 1,
"probability": 0.10000000149011612
},
{ "class_label": "PNEUMONIA VIRAL", "label": 2, "probability": 0.75 }
],
"probability": 0.75
},
"success": true
}
Notebooks
The ipynb notebook that i used for training the model and saving an .pt file was can be found:

