🚀 乳腺影像裁剪模型
本模型用於裁剪乳腺影像,去除不必要的背景。它採用輕量級的 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
許可證。