pablorocg commited on
Commit
e7c75bd
·
1 Parent(s): 040765c

Upload 11 files

Browse files
Files changed (11) hide show
  1. .gitattributes +1 -0
  2. README.md +94 -5
  3. app.py +48 -0
  4. flow +6 -0
  5. inference.py +105 -0
  6. labels.txt +3 -0
  7. model_card.md +83 -0
  8. requirements.txt +5 -0
  9. setup.py +74 -0
  10. test.Dockerfile +20 -0
  11. utils.py +15 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ weights/* filter=lfs diff=lfs merge=lfs -text
README.md CHANGED
@@ -1,10 +1,99 @@
1
  ---
2
- title: Blood Cell Detector
3
- emoji: 🏢
4
- colorFrom: gray
5
- colorTo: yellow
6
  sdk: docker
7
  pinned: false
 
 
 
 
 
 
 
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: Demo Cell Detection Gradio
3
+ emoji: 📈
4
+ colorFrom: indigo
5
+ colorTo: indigo
6
  sdk: docker
7
  pinned: false
8
+ license: gpl-3.0
9
+ metrics:
10
+ - accuracy
11
+ - map
12
+ tags:
13
+ - yolo
14
+ - cell detection
15
  ---
16
 
17
+ # Model Card for Automatic Identification and Counting of Blood Cells
18
+
19
+ This model is designed for the automatic identification and counting of blood cells from smear images, using a machine learning approach.
20
+
21
+ ## Model Details
22
+
23
+ ### Model Description
24
+
25
+ - **Developed by:** [Mahmudul Alam](https://github.com/MahmudulAlam)
26
+ - **Shared by:** [Pablo Rocamora-Garcia](https://github.com/pablorocg)
27
+ - **Model type:** Object Detection and Classification (YOLO)
28
+ - **License:** GNU General Public License v3.0
29
+
30
+ ### Model Sources
31
+
32
+ - **Repository:** https://github.com/MahmudulAlam/Automatic-Identification-and-Counting-of-Blood-Cells
33
+ - **Paper:** [Machine learning approach of automatic identification and counting of blood cells](https://doi.org/10.1049/htl.2018.5098)
34
+ - **Demo:** (Demo link or more information needed)
35
+
36
+ ## Uses
37
+
38
+ ### Direct Use
39
+
40
+ This model is intended for use in medical diagnosis to evaluate overall health condition through the automatic identification and counting of red blood cells, white blood cells, and platelets from blood smear images.
41
+
42
+ ### Out-of-Scope Use
43
+
44
+ The model is not intended for use outside of the scope of medical imaging and blood cell analysis. Misuse or application in other domains may result in inaccurate or irrelevant results.
45
+
46
+ ## How to Get Started with the Model
47
+
48
+ To get started with the model, download the trained weights, set up the environment with TensorFlow and TF-slim, and run `detect.py`. Detailed instructions can be found in the [repository](https://github.com/MahmudulAlam/Automatic-Identification-and-Counting-of-Blood-Cells).
49
+
50
+ ## Training Details
51
+
52
+ ### Training Data
53
+
54
+ The model was trained using the [Complete Blood Count (CBC) Dataset](https://github.com/MahmudulAlam/Complete-Blood-Cell-Count-Dataset), which contains images of blood smears for red blood cells, white blood cells, and platelets.
55
+
56
+ ### Results
57
+
58
+ ### Performance on Blood Cell Detection
59
+
60
+ The performance of different CNN architectures with the YOLO algorithm for detecting different types of blood cells is summarized in the following table:
61
+
62
+ | Model | RBC Accuracy (%) | WBC Accuracy (%) | Platelet Accuracy (%) | mAP | Execution Time (ms) |
63
+ |-------------|------------------|------------------|-----------------------|-------|---------------------|
64
+ | Tiny YOLO | 96.09 | 86.89 | 96.36 | 0.623 | 660 |
65
+ | VGG-16 | 72.98 | 100.00 | 90.91 | 0.713 | 3106 |
66
+ | ResNet50 | 79.80 | 95.08 | 87.27 | 0.743 | 3711 |
67
+ | InceptionV3 | 87.75 | 100.00 | 96.36 | 0.682 | 2630 |
68
+ | MobileNet | 74.24 | 93.44 | 83.64 | 0.520 | 784 |
69
+
70
+ ### Accuracy of Counting Blood Cells
71
+
72
+ The accuracy of counting Red Blood Cells (RBCs), White Blood Cells (WBCs), and platelets using the proposed method is detailed in the table below:
73
+
74
+ | Cell Type | Ground Truths | Estimated Count | Accuracy (%) |
75
+ |-------------|---------------|-----------------|--------------|
76
+ | RBCs | 792 | 823 | 96.09 |
77
+ | WBCs | 65 | 53 | 86.89 |
78
+ | Platelets | 155 | 353 | 96.36 |
79
+
80
+ ## Citation
81
+
82
+ **BibTeX:**
83
+
84
+ ```bibtex
85
+ @article{https://doi.org/10.1049/htl.2018.5098,
86
+ title={Machine learning approach of automatic identification and counting of blood cells},
87
+ author={Alam, Mohammad Mahmudul and Islam, Mohammad Tariqul},
88
+ journal={Healthcare Technology Letters},
89
+ volume={6},
90
+ number={4},
91
+ pages={103-108},
92
+ year={2019},
93
+ doi={https://doi.org/10.1049/htl.2018.5098},
94
+ url={https://ietresearch.onlinelibrary.wiley.com/doi/abs/10.1049/htl.2018.5098}
95
+ }
96
+ ```
97
+
98
+ ## Model Card Authors
99
+ - [Pablo Rocamora-Garcia](https://github.com/pablorocg)
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import os
4
+ import cv2
5
+ # from inference import inference
6
+ from PIL import Image, ImageFilter
7
+
8
+ def prueba_inferencia(img):
9
+
10
+ df = pd.DataFrame({"Cell type": ["rbc", "wbc", "platelets"], "Count": [28, 55, 43]})
11
+
12
+ # Aplicar filtro gaussiano
13
+ img = cv2.imread(img, cv2.IMREAD_COLOR)
14
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
15
+
16
+
17
+ return [img, df]
18
+
19
+
20
+ def run():
21
+ # Crear la interfaz de Gradio
22
+ interface = gr.Interface(
23
+ fn=prueba_inferencia,#inference
24
+ inputs=gr.Image(type="filepath"),
25
+ outputs=[gr.Image(type="pil"),
26
+ gr.BarPlot(
27
+ x="Cell type",
28
+ y="Count",
29
+ x_title="Cell type",
30
+ y_title="Count",
31
+ title="Cell Count",
32
+ tooltip=["Cell type", "Count"],
33
+ vertical=False,
34
+ y_lim=[0, 100],
35
+ )],
36
+
37
+ examples=[os.path.join("./data", img) for img in os.listdir("./data")],
38
+ title="Detección de Células Sanguíneas",
39
+ description="Sube una imagen de microscopio de células sanguíneas para detectar RBC, WBC y platelets."
40
+ )
41
+
42
+ # Ejecutar la aplicación
43
+ interface.launch(server_name="0.0.0.0", server_port=7860) # Se encuentra en http://127.0.0.1:7860/
44
+
45
+
46
+
47
+ if __name__ == "__main__":
48
+ run()
flow ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ #! /usr/bin/env python
2
+
3
+ import sys
4
+ from darkflow.cli import cliHandler
5
+
6
+ cliHandler(sys.argv)
inference.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ from utils import iou
3
+ from scipy import spatial
4
+ from darkflow.net.build import TFNet
5
+ import pandas as pd
6
+
7
+ def inference(file_name:str):
8
+ options = {'model': 'cfg/tiny-yolo-voc-3c.cfg',
9
+ 'load': 3750,
10
+ 'threshold': 0.1,
11
+ 'gpu': 0.7}
12
+
13
+ tfnet = TFNet(options)
14
+
15
+ pred_bb = [] # predicted bounding box
16
+ pred_cls = [] # predicted class
17
+ pred_conf = [] # predicted class confidence
18
+
19
+ rbc = 0
20
+ wbc = 0
21
+ platelets = 0
22
+
23
+ cell = []
24
+ cls = []
25
+ conf = []
26
+
27
+ record = []
28
+ tl_ = []
29
+ br_ = []
30
+ iou_ = []
31
+ iou_value = 0
32
+
33
+ # tic = time.time()
34
+ image = cv2.imread(file_name)
35
+ output = tfnet.return_predict(image)
36
+
37
+ for prediction in output:
38
+ label = prediction['label']
39
+ confidence = prediction['confidence']
40
+ tl = (prediction['topleft']['x'], prediction['topleft']['y'])
41
+ br = (prediction['bottomright']['x'], prediction['bottomright']['y'])
42
+
43
+ if label == 'RBC' and confidence < .5:
44
+ continue
45
+ if label == 'WBC' and confidence < .25:
46
+ continue
47
+ if label == 'Platelets' and confidence < .25:
48
+ continue
49
+
50
+ # clearing up overlapped same platelets
51
+ if label == 'Platelets':
52
+ if record:
53
+ tree = spatial.cKDTree(record)
54
+ index = tree.query(tl)[1]
55
+ iou_value = iou(tl + br, tl_[index] + br_[index])
56
+ iou_.append(iou_value)
57
+
58
+ if iou_value > 0.1:
59
+ continue
60
+
61
+ record.append(tl)
62
+ tl_.append(tl)
63
+ br_.append(br)
64
+
65
+ center_x = int((tl[0] + br[0]) / 2)
66
+ center_y = int((tl[1] + br[1]) / 2)
67
+ center = (center_x, center_y)
68
+
69
+ if label == 'RBC':
70
+ color = (255, 0, 0)
71
+ rbc = rbc + 1
72
+ if label == 'WBC':
73
+ color = (0, 255, 0)
74
+ wbc = wbc + 1
75
+ if label == 'Platelets':
76
+ color = (0, 0, 255)
77
+ platelets = platelets + 1
78
+
79
+ radius = int((br[0] - tl[0]) / 2)
80
+ image = cv2.circle(image, center, radius, color, 2)
81
+ font = cv2.FONT_HERSHEY_COMPLEX
82
+ image = cv2.putText(image, label, (center_x - 15, center_y + 5), font, .5, color, 1)
83
+ cell.append([tl[0], tl[1], br[0], br[1]])
84
+
85
+ if label == 'RBC':
86
+ cls.append(0)
87
+ if label == 'WBC':
88
+ cls.append(1)
89
+ if label == 'Platelets':
90
+ cls.append(2)
91
+
92
+ conf.append(confidence)
93
+
94
+ # toc = time.time()
95
+ pred_bb.append(cell)
96
+ pred_cls.append(cls)
97
+ pred_conf.append(conf)
98
+
99
+ df = pd.DataFrame({"Cell type": ["rbc", "wbc", "platelets"], "Count": [rbc, wbc, platelets]})
100
+
101
+ return [image, df]
102
+
103
+
104
+
105
+
labels.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ WBC
2
+ RBC
3
+ Platelets
model_card.md ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Model Card for Automatic Identification and Counting of Blood Cells
2
+
3
+ This model is designed for the automatic identification and counting of blood cells from smear images, using a machine learning approach.
4
+
5
+ ## Model Details
6
+
7
+ ### Model Description
8
+
9
+ - **Developed by:** Mahmudul Alam (https://github.com/MahmudulAlam)
10
+ - **Shared by:** Pablo Rocamora-Garcia (https://github.com/pablorocg)
11
+ - **Model type:** Object Detection and Classification (YOLO)
12
+ - **License:** GNU General Public License v3.0
13
+
14
+ ### Model Sources
15
+
16
+ - **Repository:** https://github.com/MahmudulAlam/Automatic-Identification-and-Counting-of-Blood-Cells
17
+ - **Paper:** [Machine learning approach of automatic identification and counting of blood cells](https://doi.org/10.1049/htl.2018.5098)
18
+ - **Demo:** (Demo link or more information needed)
19
+
20
+ ## Uses
21
+
22
+ ### Direct Use
23
+
24
+ This model is intended for use in medical diagnosis to evaluate overall health condition through the automatic identification and counting of red blood cells, white blood cells, and platelets from blood smear images.
25
+
26
+ ### Out-of-Scope Use
27
+
28
+ The model is not intended for use outside of the scope of medical imaging and blood cell analysis. Misuse or application in other domains may result in inaccurate or irrelevant results.
29
+
30
+ ## How to Get Started with the Model
31
+
32
+ To get started with the model, download the trained weights, set up the environment with TensorFlow and TF-slim, and run `detect.py`. Detailed instructions can be found in the [repository](https://github.com/MahmudulAlam/Automatic-Identification-and-Counting-of-Blood-Cells).
33
+
34
+ ## Training Details
35
+
36
+ ### Training Data
37
+
38
+ The model was trained using the [Complete Blood Count (CBC) Dataset](https://github.com/MahmudulAlam/Complete-Blood-Cell-Count-Dataset), which contains images of blood smears for red blood cells, white blood cells, and platelets.
39
+
40
+ ### Results
41
+
42
+ ### Performance on Blood Cell Detection
43
+
44
+ The performance of different CNN architectures with the YOLO algorithm for detecting different types of blood cells is summarized in the following table:
45
+
46
+ | Model | RBC Accuracy (%) | WBC Accuracy (%) | Platelet Accuracy (%) | mAP | Execution Time (ms) |
47
+ |-------------|------------------|------------------|-----------------------|-------|---------------------|
48
+ | Tiny YOLO | 96.09 | 86.89 | 96.36 | 0.623 | 660 |
49
+ | VGG-16 | 72.98 | 100.00 | 90.91 | 0.713 | 3106 |
50
+ | ResNet50 | 79.80 | 95.08 | 87.27 | 0.743 | 3711 |
51
+ | InceptionV3 | 87.75 | 100.00 | 96.36 | 0.682 | 2630 |
52
+ | MobileNet | 74.24 | 93.44 | 83.64 | 0.520 | 784 |
53
+
54
+ ### Accuracy of Counting Blood Cells
55
+
56
+ The accuracy of counting Red Blood Cells (RBCs), White Blood Cells (WBCs), and platelets using the proposed method is detailed in the table below:
57
+
58
+ | Cell Type | Ground Truths | Estimated Count | Accuracy (%) |
59
+ |-------------|---------------|-----------------|--------------|
60
+ | RBCs | 792 | 823 | 96.09 |
61
+ | WBCs | 65 | 53 | 86.89 |
62
+ | Platelets | 155 | 353 | 96.36 |
63
+
64
+ ## Citation
65
+
66
+ **BibTeX:**
67
+
68
+ ```bibtex
69
+ @article{https://doi.org/10.1049/htl.2018.5098,
70
+ title={Machine learning approach of automatic identification and counting of blood cells},
71
+ author={Alam, Mohammad Mahmudul and Islam, Mohammad Tariqul},
72
+ journal={Healthcare Technology Letters},
73
+ volume={6},
74
+ number={4},
75
+ pages={103-108},
76
+ year={2019},
77
+ doi={https://doi.org/10.1049/htl.2018.5098},
78
+ url={https://ietresearch.onlinelibrary.wiley.com/doi/abs/10.1049/htl.2018.5098}
79
+ }
80
+ ```
81
+
82
+ ## Model Card Authors
83
+ - [Pablo Rocamora-Garcia](https://github.com/pablorocg)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ opencv-python-headless==4.5.5.64
2
+ tensorflow-gpu==2.2.0
3
+ tf-slim==1.1.0
4
+ Cython==0.29.2
5
+ gradio==2.9.4
setup.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from setuptools import setup, find_packages
2
+ from setuptools.extension import Extension
3
+ from Cython.Build import cythonize
4
+ import numpy
5
+ import os
6
+ import imp
7
+
8
+ VERSION = imp.load_source('version', os.path.join('.', 'darkflow', 'version.py'))
9
+ VERSION = VERSION.__version__
10
+
11
+ if os.name == 'nt':
12
+ ext_modules = [
13
+ Extension("darkflow.cython_utils.nms",
14
+ sources=["darkflow/cython_utils/nms.pyx"],
15
+ # libraries=["m"] # Unix-like specific
16
+ include_dirs=[numpy.get_include()]
17
+ ),
18
+ Extension("darkflow.cython_utils.cy_yolo2_findboxes",
19
+ sources=["darkflow/cython_utils/cy_yolo2_findboxes.pyx"],
20
+ # libraries=["m"] # Unix-like specific
21
+ include_dirs=[numpy.get_include()]
22
+ ),
23
+ Extension("darkflow.cython_utils.cy_yolo_findboxes",
24
+ sources=["darkflow/cython_utils/cy_yolo_findboxes.pyx"],
25
+ # libraries=["m"] # Unix-like specific
26
+ include_dirs=[numpy.get_include()]
27
+ )
28
+ ]
29
+
30
+ elif os.name == 'posix':
31
+ ext_modules = [
32
+ Extension("darkflow.cython_utils.nms",
33
+ sources=["darkflow/cython_utils/nms.pyx"],
34
+ libraries=["m"], # Unix-like specific
35
+ include_dirs=[numpy.get_include()]
36
+ ),
37
+ Extension("darkflow.cython_utils.cy_yolo2_findboxes",
38
+ sources=["darkflow/cython_utils/cy_yolo2_findboxes.pyx"],
39
+ libraries=["m"], # Unix-like specific
40
+ include_dirs=[numpy.get_include()]
41
+ ),
42
+ Extension("darkflow.cython_utils.cy_yolo_findboxes",
43
+ sources=["darkflow/cython_utils/cy_yolo_findboxes.pyx"],
44
+ libraries=["m"], # Unix-like specific
45
+ include_dirs=[numpy.get_include()]
46
+ )
47
+ ]
48
+
49
+ else:
50
+ ext_modules = [
51
+ Extension("darkflow.cython_utils.nms",
52
+ sources=["darkflow/cython_utils/nms.pyx"],
53
+ libraries=["m"] # Unix-like specific
54
+ ),
55
+ Extension("darkflow.cython_utils.cy_yolo2_findboxes",
56
+ sources=["darkflow/cython_utils/cy_yolo2_findboxes.pyx"],
57
+ libraries=["m"] # Unix-like specific
58
+ ),
59
+ Extension("darkflow.cython_utils.cy_yolo_findboxes",
60
+ sources=["darkflow/cython_utils/cy_yolo_findboxes.pyx"],
61
+ libraries=["m"] # Unix-like specific
62
+ )
63
+ ]
64
+
65
+ setup(
66
+ version=VERSION,
67
+ name='darkflow',
68
+ description='Darkflow',
69
+ license='GPLv3',
70
+ url='https://github.com/thtrieu/darkflow',
71
+ packages=find_packages(),
72
+ scripts=['flow'],
73
+ ext_modules=cythonize(ext_modules)
74
+ )
test.Dockerfile ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.6
2
+
3
+ WORKDIR /code
4
+
5
+ COPY ./requirements.txt /code/requirements.txt
6
+
7
+ # RUN pip install --no-cache-dir -r /code/requirements.txt
8
+ RUN pip install gradio==2.9.4
9
+ RUN pip install pandas
10
+ RUN pip install Cython==0.29.2
11
+ RUN pip install tf-slim==1.1.0
12
+ RUN pip install opencv-python-headless==4.5.5.64
13
+ RUN pip install tensorflow-gpu==2.2.0
14
+
15
+
16
+ COPY . .
17
+
18
+ EXPOSE 7860
19
+
20
+ CMD ["python", "app.py"]
utils.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def iou(boxA, boxB):
2
+ xA = max(boxA[0], boxB[0])
3
+ yA = max(boxA[1], boxB[1])
4
+ xB = min(boxA[2], boxB[2])
5
+ yB = min(boxA[3], boxB[3])
6
+
7
+ interArea = (xB - xA + 1) * (yB - yA + 1)
8
+
9
+ if interArea < 0:
10
+ interArea = 0
11
+
12
+ boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)
13
+ boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)
14
+
15
+ return interArea / float(boxAArea + boxBArea - interArea)