数据集地址
数据集包含 360 张红血细胞图像及其注释文件,分为训练集与验证集。训练文件夹包含 300 张带有注释的图像。测试和验证文件夹都包含 60 张带有注释的图像。我们对原始数据集进行了一些修改以准备此 CBC 数据集,并将数据集分成三部分。在360张涂片图像中,首先使用300张带注释的血细胞图像作为训练集,然后将其余60张带有注释的图像用作测试集。CBC数据集地址如下:
[https://github.com/MahmudulAlam/Complete-Blood-Cell-Count-Dataset](https://github.com/MahmudulAlam/Complete-Blood-Cell-Count-Dataset)
模型训练
准备好数据集以后,直接按下面的命令行运行即可:
yolo train model=yolov8n.pt data=cbc_dataset.yaml epochs=25 imgsz=640 batch=1
导出与测试
模型导出与测试
yolo export model=cbc _best.pt format=onnx
部署推理
转成ONNX格式文件以后,基于OpenVINO-Python部署推理,相关代码如下
ie = Core()
for device in ie.available_devices:
print(device)
# Read IR
model = ie.read_model(model="cbc_best.onnx")
compiled_model = ie.compile_model(model=model, device_name="CPU")
output_layer = compiled_model.output(0)
frame = cv.imread("D:/cbc_analysis/data/image_002.jpg")
bgr = format_yolov8(frame)
img_h, img_w, img_c = bgr.shape
start = time.time()
image = cv.dnn.blobFromImage(bgr, 1 / 255.0, (640, 640), swapRB=True, crop=False)
res = compiled_model([image])[output_layer] # 1x84x8400
rows = np.squeeze(res, 0).T
class_ids = []
confidences = []
boxes = []
x_factor = img_w / 640
y_factor = img_h / 640
for r in range(rows.shape[0]):
row = rows[r]
classes_scores = row[4:]
_, _, _, max_indx = cv.minMaxLoc(classes_scores)
class_id = max_indx[1]
if (classes_scores[class_id] > .25):
confidences.append(classes_scores[class_id])
class_ids.append(class_id)
x, y, w, h = row[0].item(), row[1].item(), row[2].item(), row[3].item()
left = int((x - 0.5 * w) * x_factor)
top = int((y - 0.5 * h) * y_factor)
width = int(w * x_factor)
height = int(h * y_factor)
box = np.array([left, top, width, height])
boxes.append(box)
indexes = cv.dnn.NMSBoxes(boxes, confidences, 0.25, 0.45)
for index in indexes:
box = boxes[index]
color = colors[int(class_ids[index]) % len(colors)]
rr = int((box[2] + box[3])/4)
cv.circle(frame, (box[0]+int(box[2]/2), box[1]+int(box[3]/2)), rr-4, color, 2)
cv.putText(frame, class_list[class_ids[index]], (box[0] + int(box[2] / 2), box[1] + int(box[3] / 2)),
cv.FONT_HERSHEY_SIMPLEX, .5, (0, 0, 0))
cv.putText(frame, "gloomyfish@2024", (20, 45), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
cv.imshow("YOLOv8+OpenVINO2023 RBC(Red Blood Cell) Count", frame)
cv.waitKey(0)
cv.destroyAllWindows()
作者:gloomyfish
文章来源:OpenCV学堂
推荐阅读
- 小模型展现上下文学习能力!字节跳动 & 华东师大联合提出自进化文本识别器
- 华为诺亚实验室+中科大提出TinySAM | 比SAM小10倍,但是依旧完成了精度的超车!
- 苹果发布开源多模态大语言模型Ferret
- 2D检测教3D检测做事情,YOLOv7让BEVFusion无痛涨6个点,长尾也解决了
更多嵌入式AI干货请关注嵌入式AI专栏。欢迎添加极术小姐姐微信(id:aijishu20)加入技术交流群,请备注研究方向。