🚀 乳腺影像裁剪模型
本模型用于裁剪乳腺影像,去除不必要的背景。它采用轻量级的 mobilenetv3_small_100
骨干网络,并预测归一化的 xywh
坐标。
🚀 快速开始
本模型可有效裁剪乳腺影像,去除多余背景。以下是使用该模型的步骤:
import cv2
import torch
from transformers import AutoModel
model = AutoModel.from_pretrained("ianpan/mammo-crop", trust_remote_code=True)
model = model.eval()
img = cv2.imread(..., 0)
img_shape = torch.tensor([img.shape[:2]])
x = model.preprocess(img)
x = torch.from_numpy(x).unsqueeze(0).unsqueeze(0)
x = x.float()
with torch.inference_mode():
coords = model(x, img_shape)
coords = coords[0].numpy()
x, y, w, h = coords
cropped_img = img[y: y + h, x: x + w]
✨ 主要特性
- 高效裁剪:能够精准裁剪乳腺影像,去除不必要的背景。
- 轻量级骨干网络:采用
mobilenetv3_small_100
骨干网络,减少计算资源消耗。
- 归一化坐标预测:输出归一化的
xywh
坐标,方便后续处理。
📦 安装指南
暂未提及安装相关内容,可根据 transformers
库的常规安装方式进行安装。
💻 使用示例
基础用法
import cv2
import torch
from transformers import AutoModel
model = AutoModel.from_pretrained("ianpan/mammo-crop", trust_remote_code=True)
model = model.eval()
img = cv2.imread(..., 0)
img_shape = torch.tensor([img.shape[:2]])
x = model.preprocess(img)
x = torch.from_numpy(x).unsqueeze(0).unsqueeze(0)
x = x.float()
with torch.inference_mode():
coords = model(x, img_shape)
coords = coords[0].numpy()
x, y, w, h = coords
cropped_img = img[y: y + h, x: x + w]
高级用法
如果你安装了 pydicom
,可以直接加载 DICOM 图像:
img = model.load_image_from_dicom(path_to_dicom)
🔧 技术细节
x: 0.0032
y: 0.0030
w: 0.0054
h: 0.0088
📚 详细文档
真实坐标生成代码
import cv2
def crop_roi(img):
img = img[5:-5, 5:-5]
output = cv2.connectedComponentsWithStats((img > 10).astype("uint8")[:, :], 8, cv2.CV_32S)
stats = output[2]
idx = stats[1:, 4].argmax() + 1
x1, y1, w, h = stats[idx][:4]
x1 = max(0, x1 - 5)
y1 = max(0, y1 - 5)
img_h, img_w = img.shape[:2]
return x1, y1, w, h)
输入处理说明
如果从 DICOM 加载图像,应将输入转换为 8 位图像,通过 model.preprocess
处理,并确保在传递给模型之前是浮点型 torch.Tensor
。归一化步骤在模型内部完成。
📄 许可证
本模型使用 apache-2.0
许可证。