File size: 6,317 Bytes
7bfd23a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
import albumentations as A
import numpy as np
import torch
import torch.nn as nn
from transformers import PreTrainedModel
from timm import create_model
from typing import Mapping, Sequence, Tuple
from .configuration import MammoConfig
def _pad_to_aspect_ratio(img: np.ndarray, aspect_ratio: float) -> np.ndarray:
"""
Pads to specified aspect ratio, only if current aspect ratio is
greater.
"""
h, w = img.shape[:2]
if h / w > aspect_ratio:
new_w = round(h / aspect_ratio)
w_diff = new_w - w
left_pad = w_diff // 2
right_pad = w_diff - left_pad
padding = ((0, 0), (left_pad, right_pad))
if img.ndim == 3:
padding = padding + ((0, 0),)
img = np.pad(img, padding, mode="constant", constant_values=0)
return img
def _to_torch_tensor(x: np.ndarray, device: str) -> torch.Tensor:
if x.ndim == 2:
x = torch.from_numpy(x).unsqueeze(0)
elif x.ndim == 3:
x = torch.from_numpy(x)
if torch.tensor(x.size()).argmin().item() == 2:
# channels last -> first
x = x.permute(2, 0, 1)
else:
raise ValueError(f"Expected 2 or 3 dimensions, got {x.ndim}")
return x.float().to(device)
class MammoModel(nn.Module):
def __init__(
self,
backbone: str,
image_size: Tuple[int, int],
pad_to_aspect_ratio: bool,
feature_dim: int = 1280,
dropout: float = 0.1,
num_classes: int = 5,
in_chans: int = 1,
):
super().__init__()
self.backbone = create_model(
model_name=backbone,
pretrained=False,
num_classes=0,
global_pool="",
features_only=False,
in_chans=in_chans,
)
self.pooling = nn.AdaptiveAvgPool2d(1)
self.dropout = nn.Dropout(p=dropout)
self.linear = nn.Linear(feature_dim, num_classes)
self.pad_to_aspect_ratio = pad_to_aspect_ratio
self.aspect_ratio = image_size[0] / image_size[1]
if self.pad_to_aspect_ratio:
self.resize = A.Resize(image_size[0], image_size[1], p=1)
else:
self.resize = A.Compose(
[
A.LongestMaxSize(image_size[0], p=1),
A.PadIfNeeded(image_size[0], image_size[1], p=1),
],
p=1,
)
def normalize(self, x: torch.Tensor) -> torch.Tensor:
# [0, 255] -> [-1, 1]
mini, maxi = 0.0, 255.0
x = (x - mini) / (maxi - mini)
x = (x - 0.5) * 2.0
return x
def preprocess(
self,
x: Mapping[str, np.ndarray] | Sequence[Mapping[str, np.ndarray]],
device: str,
) -> Sequence[Mapping[str, torch.Tensor]]:
# x is a dict (or list of dicts) with keys "cc" and/or "mlo"
# though the actual keys do not matter
if not isinstance(x, Sequence):
assert isinstance(x, Mapping)
x = [x]
if self.pad_to_aspect_ratio:
x = [
{
k: _pad_to_aspect_ratio(v.copy(), self.aspect_ratio)
for k, v in sample.items()
}
for sample in x
]
x = [
{
k: _to_torch_tensor(self.resize(image=v)["image"], device=device)
for k, v in sample.items()
}
for sample in x
]
return x
def forward(
self, x: Sequence[Mapping[str, torch.Tensor]]
) -> Mapping[str, torch.Tensor]:
batch_tensor = []
batch_indices = []
for idx, sample in enumerate(x):
for k, v in sample.items():
batch_tensor.append(v)
batch_indices.append(idx)
batch_tensor = torch.stack(batch_tensor, dim=0)
batch_tensor = self.normalize(batch_tensor)
features = self.pooling(self.backbone(batch_tensor))
b, d = features.shape[:2]
features = features.reshape(b, d)
logits = self.linear(features)
# cancer
logits0 = logits[:, 0].sigmoid()
# density
logits1 = logits[:, 1:].softmax(dim=1)
# mean over views
batch_indices = torch.tensor(batch_indices)
logits0 = torch.stack(
[logits0[batch_indices == i].mean(dim=0) for i in batch_indices.unique()]
)
logits1 = torch.stack(
[logits1[batch_indices == i].mean(dim=0) for i in batch_indices.unique()]
)
return {"cancer": logits0, "density": logits1}
class MammoEnsemble(PreTrainedModel):
config_class = MammoConfig
def __init__(self, config):
super().__init__(config)
self.num_models = config.num_models
for i in range(self.num_models):
setattr(
self,
f"net{i}",
MammoModel(
config.backbone,
config.image_sizes[i],
config.pad_to_aspect_ratio[i],
config.feature_dim,
config.dropout,
config.num_classes,
config.in_chans,
),
)
@staticmethod
def load_image_from_dicom(path: str) -> np.ndarray | None:
try:
from pydicom import dcmread
from pydicom.pixels import apply_voi_lut
except ModuleNotFoundError:
print("`pydicom` is not installed, returning None ...")
return None
dicom = dcmread(path)
arr = apply_voi_lut(dicom.pixel_array, dicom)
if dicom.PhotometricInterpretation == "MONOCHROME1":
arr = arr.max() - arr
arr = arr - arr.min()
arr = arr / arr.max()
arr = (arr * 255).astype("uint8")
return arr
def forward(
self,
x: Mapping[str, np.ndarray] | Sequence[Mapping[str, np.ndarray]],
device: str = "cpu",
) -> Mapping[str, torch.Tensor]:
out = []
for i in range(self.num_models):
model = getattr(self, f"net{i}")
x_pp = model.preprocess(x, device=device)
out.append(model(x_pp))
out = {k: torch.stack([o[k] for o in out]).mean(0) for k in out[0].keys()}
return out
|