Baishali commited on
Commit
804f2f7
β€’
1 Parent(s): 5233acd

Added Pneumonia Detection Gradio App

Browse files
.gitattributes CHANGED
@@ -1,27 +1,2 @@
1
- *.7z filter=lfs diff=lfs merge=lfs -text
2
- *.arrow filter=lfs diff=lfs merge=lfs -text
3
- *.bin filter=lfs diff=lfs merge=lfs -text
4
- *.bin.* filter=lfs diff=lfs merge=lfs -text
5
- *.bz2 filter=lfs diff=lfs merge=lfs -text
6
- *.ftz filter=lfs diff=lfs merge=lfs -text
7
- *.gz filter=lfs diff=lfs merge=lfs -text
8
  *.h5 filter=lfs diff=lfs merge=lfs -text
9
- *.joblib filter=lfs diff=lfs merge=lfs -text
10
- *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
- *.model filter=lfs diff=lfs merge=lfs -text
12
- *.msgpack filter=lfs diff=lfs merge=lfs -text
13
- *.onnx filter=lfs diff=lfs merge=lfs -text
14
- *.ot filter=lfs diff=lfs merge=lfs -text
15
- *.parquet filter=lfs diff=lfs merge=lfs -text
16
- *.pb filter=lfs diff=lfs merge=lfs -text
17
- *.pt filter=lfs diff=lfs merge=lfs -text
18
- *.pth filter=lfs diff=lfs merge=lfs -text
19
- *.rar filter=lfs diff=lfs merge=lfs -text
20
- saved_model/**/* filter=lfs diff=lfs merge=lfs -text
21
- *.tar.* filter=lfs diff=lfs merge=lfs -text
22
- *.tflite filter=lfs diff=lfs merge=lfs -text
23
- *.tgz filter=lfs diff=lfs merge=lfs -text
24
- *.xz filter=lfs diff=lfs merge=lfs -text
25
- *.zip filter=lfs diff=lfs merge=lfs -text
26
- *.zstandard filter=lfs diff=lfs merge=lfs -text
27
- *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
1
  *.h5 filter=lfs diff=lfs merge=lfs -text
2
+ pneumonia_detection_cnn_model.h5 filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
README.md CHANGED
@@ -1,37 +1,99 @@
1
- ---
2
- title: Pneumonia Detection
3
- emoji: πŸ“Š
4
- colorFrom: green
5
- colorTo: yellow
6
- sdk: gradio
7
- app_file: app.py
8
- pinned: false
9
- ---
10
 
11
- # Configuration
12
 
13
- `title`: _string_
14
- Display title for the Space
15
 
16
- `emoji`: _string_
17
- Space emoji (emoji-only character allowed)
18
 
19
- `colorFrom`: _string_
20
- Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
21
 
22
- `colorTo`: _string_
23
- Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
24
 
25
- `sdk`: _string_
26
- Can be either `gradio` or `streamlit`
27
 
28
- `sdk_version` : _string_
29
- Only applicable for `streamlit` SDK.
30
- See [doc](https://hf.co/docs/hub/spaces) for more info on supported versions.
 
 
 
 
31
 
32
- `app_file`: _string_
33
- Path to your main application file (which contains either `gradio` or `streamlit` Python code).
34
- Path is relative to the root of the repository.
35
 
36
- `pinned`: _boolean_
37
- Whether the Space stays on top of your list.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ## Motivation
 
 
 
 
 
 
 
 
2
 
3
+ Pneumonia is a lung infection (🫁) that inflames the air sacs in one or both lungs. This infection arises when the air sacs get filled with fluid or pus (purulent material). It can be a bacterial or viral infection. The main symptoms are - cough with phlegm or pus, fever, chills, and breathing difficulty.
4
 
5
+ This disease is responsible for over 15% of all deaths of children under five years old worldwide. This proves the severity of this disease and the need for accurate detection.
 
6
 
7
+ The most commonly used method to diagnose pneumonia is through chest radiograph or chest X-ray, which depicts the infection as an increased opacity in the lungs' specific area(s).
 
8
 
9
+ To increase the diagnosis procedure's efficacy and reach, we can leverage machine learning algorithms to identify abnormalities in the chest X-ray images. In this model, many chest X-ray images (both normal and pneumonia) are fed to build `Convolutional Neural Network (CNN)` model for fulfilling the purpose.
 
10
 
 
 
11
 
12
+ ## Requirements
 
13
 
14
+ - Python 3.7.x
15
+ - Tensorflow 2.4.1+
16
+ - Keras 2.4.3+
17
+ - scikit-learn 0.24.1+
18
+ - matplotlib 3.3.3+
19
+ - texttable 1.6.3+
20
+ - gradio 1.5.3+
21
 
22
+ ## Dataset
 
 
23
 
24
+ You can download the dataset from [kaggle](https://www.kaggle.com/paultimothymooney/chest-xray-pneumonia/). Use the underlying download link to download the dataset.
25
+
26
+ ### Instructions to follow
27
+
28
+ * Extract the archive
29
+ * You will find several directories in it
30
+ * Copy the `chest-xray` directory contents (`train`, `test` and `val` subdirectories) to the `data` folder
31
+
32
+ The number of images belonging to both classes (`Normal` and `Pneumonia`) in the `train`, `test` and `val` datasets are -
33
+
34
+ <img width="326" alt="Screenshot 2021-02-07 at 16 40 00" src="https://user-images.githubusercontent.com/76659596/107151515-4083f280-6963-11eb-84c7-f2a23cc24134.png">
35
+
36
+
37
+ ## Installation
38
+
39
+ * Clone the repository
40
+
41
+ `git clone https://github.com/baishalidutta/Pneumonia-Detection.git`
42
+
43
+ * Install the required libraries
44
+
45
+ `pip3 install -r requirements.txt`
46
+
47
+ ## Usage
48
+
49
+ Enter into the `source` directory to execute the following source codes.
50
+
51
+ * To generate the model on your own, run
52
+
53
+ `python3 cnn_training_model.py`
54
+
55
+ * To evaluate any dataset using the pre-trained model (in the `model` directory), run
56
+
57
+ `python3 cnn_model_evaluation.py`
58
+
59
+ Note that, for evaluation, `cnn_model_evaluation.py` will use all the images contained inside both `test` and `val` subdirectories (inside `data` directory).
60
+
61
+ Alternatively, you can find the whole analysis in the notebook inside the `notebook` directory. To open the notebook, use either `jupyter notebook` or `google colab` or any other IDE that supports notebook feature such as `PyCharm Professional`.
62
+
63
+ ## Evaluation
64
+
65
+ Our model is trained with 96% accuracy on the training dataset. The model's accuracy on the `test` and `val` datasets are 91% and 88% respectively. In both cases, the `f1-score` and `ROC_AUC Score` are relatively high, as shown below.
66
+
67
+ ### On Test Dataset (624 images, 234 `Normal` and 390 `Pneumonia`)
68
+
69
+ <p align="center">
70
+ <img width="960" alt="Screenshot 2021-02-07 at 17 07 23" src="https://user-images.githubusercontent.com/76659596/107152321-93f83f80-6967-11eb-95b4-0bfb3ccae6d7.png">
71
+ </p>
72
+
73
+ ### On Validation Dataset (16 images, 8 `Normal` and 8 `Pneumonia`)
74
+
75
+ <p align="center">
76
+ <img width="960" alt="Screenshot 2021-02-07 at 17 10 07" src="https://user-images.githubusercontent.com/76659596/107152360-ba1ddf80-6967-11eb-90cb-dfaeca31f275.png">
77
+ </p>
78
+
79
+ ## Web Application
80
+
81
+ To run the web application locally, go to the `webapp` directory and execute:
82
+
83
+ `python3 web_app.py`
84
+
85
+ This will start a local server that you can access in your browser. You can either upload/drag a new X-ray image or select any test X-ray images from the examples below.
86
+
87
+ You can, alternatively, try out the hosted web application [here](https://gradio.app/g/baishalidutta/Pneumonia-Detection).
88
+
89
+ ## Developer
90
+
91
+ Baishali Dutta (<a href='mailto:me@itsbaishali.com'>me@itsbaishali.com</a>)
92
+
93
+ ## Contribution [![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/baishalidutta/Pneumonia-Detection/issues)
94
+
95
+ If you would like to contribute and improve the model further, check out the [Contribution Guide](https://github.com/baishalidutta/Pneumonia-Detection/blob/main/CONTRIBUTING.md)
96
+
97
+ ## License [![License](http://img.shields.io/badge/license-Apache-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0)
98
+
99
+ This project is licensed under Apache License Version 2.0
app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ __author__ = "Baishali Dutta"
2
+ __copyright__ = "Copyright (C) 2021 Baishali Dutta"
3
+ __license__ = "Apache License 2.0"
4
+ __version__ = "0.1"
5
+
6
+ # -------------------------------------------------------------------------
7
+ # Importing the libraries
8
+ # -------------------------------------------------------------------------
9
+ import gradio as gr
10
+ import numpy as np
11
+ from tensorflow.keras.models import load_model
12
+ from tensorflow.keras.preprocessing import image
13
+
14
+ # -------------------------------------------------------------------------
15
+ # Configurations
16
+ # -------------------------------------------------------------------------
17
+ MODEL_LOC = 'pneumonia_detection_cnn_model.h5'
18
+
19
+ # load the trained CNN model
20
+ cnn_model = load_model(MODEL_LOC)
21
+
22
+
23
+ def make_prediction(test_image):
24
+ test_image = test_image.name
25
+ test_image = image.load_img(test_image, target_size=(224, 224))
26
+ test_image = image.img_to_array(test_image) / 255.
27
+ test_image = np.expand_dims(test_image, axis=0)
28
+ result = cnn_model.predict(test_image)
29
+ return {"Normal": str(result[0][0]), "Pneumonia": str(result[0][1])}
30
+
31
+
32
+ image_input = gr.inputs.Image(type="file")
33
+
34
+ title = "Pneumonia Detection"
35
+ description = "This application uses a Convolutional Neural Network (CNN) model to predict whether a chosen X-ray shows if " \
36
+ "the person has pneumonia disease or not. To check the model prediction, here are the true labels of the " \
37
+ "provided examples below: the first 4 images belong to normal whereas the last 4 images are of pneumonia " \
38
+ "category. More specifically, the 5th and 6th images are viral pneumonia infection in nature whereas " \
39
+ "the last 2 images are bacterial infection in nature."
40
+
41
+ gr.Interface(fn=make_prediction,
42
+ inputs=image_input,
43
+ outputs="label",
44
+ examples=[["image1_normal.jpeg"],
45
+ ["image2_normal.jpeg"],
46
+ ["image3_normal.jpeg"],
47
+ ["image4_normal.jpeg"],
48
+ ["image1_pneumonia_virus.jpeg"],
49
+ ["image2_pneumonia_virus.jpeg"],
50
+ ["image1_pneumonia_bacteria.jpeg"],
51
+ ["image2_pneumonia_bacteria.jpeg"]],
52
+ title=title,
53
+ description=description,
54
+ article="http://raw.githubusercontent.com/baishalidutta/Pneumonia-Detection/gradio/README.md") \
55
+ .launch(share=True)
image1_normal.jpeg ADDED
image1_pneumonia_bacteria.jpeg ADDED
image1_pneumonia_virus.jpeg ADDED
image2_normal.jpeg ADDED
image2_pneumonia_bacteria.jpeg ADDED
image2_pneumonia_virus.jpeg ADDED
image3_normal.jpeg ADDED
image4_normal.jpeg ADDED
pneumonia_detection_cnn_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:48eb1a78829ddccc7bef7a1cfd01271200862b3aeb0fa6dbec310720274f54c4
3
+ size 79865784
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ tensorflow>=2.4.1
2
+ Keras>=2.4.3
3
+ gradio>=1.5.3
4
+ numpy>=1.19.5