🚀 基于DETR的医学图像目标检测模型
本模型是一个基于DETR的目标检测模型,专为医学图像分析而训练,可用于检测和分类肺炎类型或判断是否正常。
🚀 快速开始
模型信息
属性 |
详情 |
模型类型 |
基于DETR(Detection Transformers)架构的目标检测模型 |
基础模型 |
microsoft/conditional-detr-resnet-50 |
训练数据 |
在自定义的带注释医学图像数据集上训练 |
适用场景 |
用于分析胸部X光图像,检测肺炎的存在和类型,或分类为正常 |
分类类别 |
4 个类别:0: 肺炎、1: 正常、2: 细菌性肺炎、3: 病毒性肺炎 |
💻 使用示例
基础用法
import os
from transformers import AutoImageProcessor, AutoModelForObjectDetection
import torch
from PIL import Image
import pandas as pd
folder_path = ""
processor = AutoImageProcessor.from_pretrained("0llheaven/CON-DETR-V5")
model = AutoModelForObjectDetection.from_pretrained("0llheaven/CON-DETR-V5")
results_list = []
for image_name in os.listdir(folder_path):
if image_name.endswith((".jpg", ".png", ".jpeg")):
image_path = os.path.join(folder_path, image_name)
image = Image.open(image_path)
if image.mode != "RGB":
image = image.convert("RGB")
inputs = processor(images=image, return_tensors="pt")
outputs = model(**inputs)
target_sizes = torch.tensor([image.size[::-1]])
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes)
print(f"Processing image: {image_name}")
detected_any = False
for result in results:
scores = result["scores"]
labels = result["labels"]
boxes = result["boxes"]
filtered_data = [(score, label, box) for score, label, box in zip(scores, labels, boxes) if score > 0.5][:2]
for score, label, box in zip(scores, labels, boxes):
if score > 0.5:
if len(filtered_data) > 0:
detected_any = True
for score, label, box in filtered_data:
if label.item() == 0:
label_name = "Pneumonia"
elif label.item() == 1:
label_name = "Normal"
elif label.item() == 2:
label_name = "Pneumonia_bacteria"
else:
label_name = "Pneumonia_virus"
xmin, ymin, xmax, ymax = [round(i, 2) for i in box.tolist()]
print(f" - Detected {label_name} with score {round(score.item(), 3)} at {xmin, ymin, xmax, ymax}")
results_list.append({
"image_name": image_name,
"label": label_name,
"xmin": xmin,
"ymin": ymin,
"xmax": xmax,
"ymax": ymax,
"score": round(score.item(), 3),
})
if not detected_any:
print(" - No Detect")
results_list.append({
"image_name": image_name,
"label": "Other",
"xmin": 0,
"ymin": 0,
"xmax": 0,
"ymax": 0,
"score": 0,
})
results_df = pd.DataFrame(results_list)
print("\nFinal results:")
results_df.to_csv("testmodel.csv", index=False)