血細胞計數(shù)是醫(yī)學(xué)上一種重要的檢測手段,用于評估患者的健康狀況,診斷疾病,以及監(jiān)測治療效果。而目標檢測是一種計算機視覺技術(shù),用于在圖像中識別和定位特定的目標。在血細胞計數(shù)中,目標檢測技術(shù)可以發(fā)揮重要作用。
首先,血細胞計數(shù)通常需要處理大量的血液樣本,手動計數(shù)每個細胞既耗時又容易出錯。使用目標檢測算法,可以自動識別和計數(shù)圖像中的血細胞,大大提高了計數(shù)的準確性和效率。
其次,不同的血細胞(如紅細胞、白細胞和血小板)具有不同的形態(tài)和大小,這使得使用傳統(tǒng)的圖像處理方法進行區(qū)分和計數(shù)變得困難。目標檢測算法可以通過訓(xùn)練識別不同血細胞的特征,準確地區(qū)分和計數(shù)各種血細胞。
此外,目標檢測算法還可以處理一些特殊情況,如細胞重疊、不規(guī)則形狀、染色不均等。這些情況可能會影響手動計數(shù)的準確性和可靠性。
最后,使用目標檢測進行血細胞計數(shù)可以幫助醫(yī)生更準確地分析和解讀血液樣本,從而為患者提供更準確的診斷和治療方案。這有助于提高醫(yī)療質(zhì)量和患者滿意度。
綜上所述,使用目標檢測對血細胞計數(shù)具有重要的意義,可以提高計數(shù)的準確性和效率,為醫(yī)生提供更可靠的診斷依據(jù),有助于提高醫(yī)療質(zhì)量。
本文介紹使用yolov8進行血細胞檢測的方法,在BCD數(shù)據(jù)集訓(xùn)練,其效果如下圖:
一、安裝YoloV8
yolov8官方文檔:主頁 - Ultralytics YOLOv8 文檔
安裝部分參考:官方安裝教程
二、數(shù)據(jù)集準備
本文使用的數(shù)據(jù)集是BCD數(shù)據(jù)集,該數(shù)據(jù)集包含3個類別:白細胞(WBC)、紅細胞(RBC)和血小板(Platelets)。該數(shù)據(jù)集共364張圖片,其中訓(xùn)練集包含255張圖片,驗證集包含73張圖片,測試集包含36張圖片。
示例圖片如下:
本文提供轉(zhuǎn)換好的BCD數(shù)據(jù)集YOLO8格式,可以直接用于訓(xùn)練Yolov8模型。BCD-yolov8數(shù)據(jù)集
三、模型訓(xùn)練
1、數(shù)據(jù)集配置文件
在ultralytics/ultralytics/cfg/datasets目錄下添加bcd.yaml,添加以下內(nèi)容(path修改為自己的路徑):
# Ultralytics YOLO ??, AGPL-3.0 license
# COCO 2017 dataset http://cocodataset.org by Microsoft
# Example usage: yolo train data=coco.yaml
# parent
# ├── ultralytics
# └── datasets
# └── coco ← downloads here (20.1 GB)
# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
path: datasets/medical/BCD_YOLO8 # dataset root dir
train: images/train # train images (relative to 'path') 118287 images
val: images/val # val images (relative to 'path') 5000 images
test: images/test # 20288 of 40670 images, submit to https://competitions.codalab.org/competitions/20794
# Classes
names:
0: Platelets
1: RBC
2: WBC
2、修改模型配置文件
新建ultralytics/cfg/models/medical/yolov8.yaml?,添加以下內(nèi)容:,添加以下內(nèi)容:
# Ultralytics YOLO ??, AGPL-3.0 license
# YOLOv8 object detection model with P3-P5 outputs. For Usage examples see https://docs.ultralytics.com/tasks/detect
# Parameters
nc: 3 # number of classes
scales: # model compound scaling constants, i.e. 'model=yolov8n.yaml' will call yolov8.yaml with scale 'n'
# [depth, width, max_channels]
n: [0.33, 0.25, 1024] # YOLOv8n summary: 225 layers, 3157200 parameters, 3157184 gradients, 8.9 GFLOPs
s: [0.33, 0.50, 1024] # YOLOv8s summary: 225 layers, 11166560 parameters, 11166544 gradients, 28.8 GFLOPs
m: [0.67, 0.75, 768] # YOLOv8m summary: 295 layers, 25902640 parameters, 25902624 gradients, 79.3 GFLOPs
l: [1.00, 1.00, 512] # YOLOv8l summary: 365 layers, 43691520 parameters, 43691504 gradients, 165.7 GFLOPs
x: [1.00, 1.25, 512] # YOLOv8x summary: 365 layers, 68229648 parameters, 68229632 gradients, 258.5 GFLOPs
# YOLOv8.0n backbone
backbone:
# [from, repeats, module, args]
- [-1, 1, Conv, [64, 3, 2]] # 0-P1/2
- [-1, 1, Conv, [128, 3, 2]] # 1-P2/4
- [-1, 3, C2f, [128, True]]
- [-1, 1, Conv, [256, 3, 2]] # 3-P3/8
- [-1, 6, C2f, [256, True]]
- [-1, 1, Conv, [512, 3, 2]] # 5-P4/16
- [-1, 6, C2f, [512, True]]
- [-1, 1, Conv, [1024, 3, 2]] # 7-P5/32
- [-1, 3, C2f, [1024, True]]
- [-1, 1, SPPF, [1024, 5]] # 9
# YOLOv8.0n head
head:
- [-1, 1, nn.Upsample, [None, 2, 'nearest']]
- [[-1, 6], 1, Concat, [1]] # cat backbone P4
- [-1, 3, C2f, [512]] # 12
- [-1, 1, nn.Upsample, [None, 2, 'nearest']]
- [[-1, 4], 1, Concat, [1]] # cat backbone P3
- [-1, 3, C2f, [256]] # 15 (P3/8-small)
- [-1, 1, Conv, [256, 3, 2]]
- [[-1, 12], 1, Concat, [1]] # cat head P4
- [-1, 3, C2f, [512]] # 18 (P4/16-medium)
- [-1, 1, Conv, [512, 3, 2]]
- [[-1, 9], 1, Concat, [1]] # cat head P5
- [-1, 3, C2f, [1024]] # 21 (P5/32-large)
- [[15, 18, 21], 1, Detect, [nc]] # Detect(P3, P4, P5)
3、訓(xùn)練模型
使用如下命令訓(xùn)練模型,相關(guān)路徑更改為自己的路徑,建議絕對路徑:
yolo detect train project=medical_output name=bcd_yolo8 exist_ok=True optimizer=auto val=True amp=True epochs=100 imgsz=640 model=ultralytics/cfg/models/medical/yolov8.yaml data=ultralytics/cfg/datasets/bcd.yaml
4、驗證模型
使用如下命令驗證模型,相關(guān)路徑根據(jù)需要修改:
yolo detect val model=medical_output/bcd_yolo8/weights/best.pt data=ultralytics/ultralytics/cfg/datasets/bcd.yaml
其精度如下:
# Ultralytics YOLOv8.1.0 ?? Python-3.9.18 torch-2.1.0+cu118 CUDA:0 (NVIDIA GeForce RTX 4090, 24210MiB)
# YOLOv8 summary (fused): 168 layers, 3006233 parameters, 0 gradients, 8.1 GFLOPs
# val: Scanning /home/yq/aitools/datasets/medical/BCD_YOLO8/labels/val.cache... 73 images, 0 backgrounds, 0 corrupt: 100%|██████████|
# Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 5/5 [00:05<00:00, 1.02s/
# all 73 967 0.844 0.915 0.929 0.644
# Platelets 73 76 0.817 0.882 0.904 0.502
# RBC 73 819 0.764 0.864 0.893 0.635
# WBC 73 72 0.951 1 0.989 0.797
# Speed: 2.1ms preprocess, 7.8ms inference, 0.0ms loss, 9.1ms postprocess per image
# Results saved to /home/yq/ultralytics/runs/detect/val15
# ?? Learn more at https://docs.ultralytics.com/modes/val
四、推理
訓(xùn)練好了模型,可以使用如下代碼實現(xiàn)推理,將權(quán)重放到同級目錄:
from PIL import Image
from ultralytics import YOLO
# 加載預(yù)訓(xùn)練的YOLOv8n模型
model = YOLO('best.pt')
image_path = 'BloodImage_00021_jpg.rf.a5330c5a9f79a73c6e526244171cf77b.jpg'
results = model(image_path) # 結(jié)果列表
# 展示結(jié)果
for r in results:
im_array = r.plot() # 繪制包含預(yù)測結(jié)果的BGR numpy數(shù)組
im = Image.fromarray(im_array[..., ::-1]) # RGB PIL圖像
im.show() # 顯示圖像
im.save('results.jpg') # 保存圖像
五、界面開發(fā)
使用pyqt5開發(fā)gui界面,支持圖片、視頻、攝像頭輸入,支持導(dǎo)出到指定路徑,其GUI如下圖:
本文提供開發(fā)好的GUI界面和訓(xùn)練好的模型權(quán)重,可以一鍵啟動。【BCD-yolov8-GUI檢測系統(tǒng)】
?文章來源地址http://www.zghlxwxcb.cn/news/detail-813671.html文章來源:http://www.zghlxwxcb.cn/news/detail-813671.html
?
到了這里,關(guān)于【深度學(xué)習(xí)目標檢測】十四、基于深度學(xué)習(xí)的血細胞計數(shù)系統(tǒng)-含GUI(BCD數(shù)據(jù)集,yolov8)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!