layout | background-class | body-class | category | title | summary | image | author | tags | github-link | github-id | featured_image_1 | accelerator | order | demo-model-link | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
hub_detail |
hub-background |
hub |
researchers |
U-Net for brain MRI |
U-Net with batch normalization for biomedical image segmentation with pretrained weights for abnormality segmentation in brain MRI |
unet_tcga_cs_4944.png |
mateuszbuda |
|
mateuszbuda/brain-segmentation-pytorch |
unet_brain_mri.png |
cuda-optional |
10 |
import torch
model = torch.hub.load('mateuszbuda/brain-segmentation-pytorch', 'unet',
in_channels=3, out_channels=1, init_features=32, pretrained=True)
μ μ½λλ λ MRI λ³Όλ₯¨ λ°μ΄ν° μ kaggle.com/mateuszbuda/lgg-mri-segmentationμ μ΄μ νμ§λ₯Ό μν΄ μ¬μ νμ΅λ U-Net λͺ¨λΈμ λΆλ¬μ΅λλ€. μ¬μ νμ΅λ λͺ¨λΈμ 첫 λ²μ§Έ κ³μΈ΅μμ 3κ°μ μ λ ₯ μ±λ, 1κ°μ μΆλ ₯ μ±λ κ·Έλ¦¬κ³ 32κ°μ νΉμ§μ κ°μ§λλ€.
U-Net λͺ¨λΈμ λ°°μΉ μ κ·ν λ° ReLU νμ± ν¨μλ₯Ό κ°μ§ λ κ°μ ν©μ±κ³± κ³μΈ΅, μΈμ½λ© κ³Όμ μ λ§₯μ€ νλ§(max-pooling) κ³μΈ΅ κ·Έλ¦¬κ³ λμ½λ© κ³Όμ μ μ 컨볼루μ λ(up-convolutional) κ³μΈ΅μ ν¬ν¨ν λ€ κ°μ§ λ¨κ³μ λΈλ‘μΌλ‘ ꡬμ±λ©λλ€. κ° λΈλ‘μ ν©μ±κ³± νν° μλ 32, 64, 128, 256κ°μ λλ€. λ³λͺ© κ³μΈ΅(bottleneck layer)μ 512κ°μ ν©μ±κ³± νν°λ₯Ό κ°μ§λλ€. μΈμ½λ© κ³Όμ μ κ³μΈ΅μμ μ»μ νΉμ§μ μ΄μ μμνλ λμ½λ© κ³Όμ μ κ³μΈ΅μ ν©μΉλ μ€ν΅ μ°κ²°(skip connections)μ΄ μ§νλ©λλ€. μ λ ₯ μ΄λ―Έμ§λ pre-contrast, FLAIR λ° post-contrast κ³Όμ μμ μ»μ 3-μ±λ λ MRI μ¬λΌμ΄μ€μ λλ€. μΆλ ₯μ μ λ ₯ μ΄λ―Έμ§μ λμΌν ν¬κΈ°λ₯Ό κ°μ§κ³ 1-μ±λμ μ΄μ νμ§ μμμ νλ₯ μ μΌλ‘ λνλ λλ€. μλμ μμμ²λΌ μκ³ κ°μ μ€μ νλ©΄ μΆλ ₯ μ΄λ―Έμ§λ₯Ό μ΄μ§ λΆν λ§μ€ν¬λ‘ λ³νν μ μμ΅λλ€.
μ¬μ νμ΅λ λͺ¨λΈμ μ λ ₯λλ μ΄λ―Έμ§λ 3κ°μ μ±λμ κ°μ ΈμΌ νλ©° 256x256 ν½μ λ‘ ν¬κΈ°κ° μ‘°μ λκ³ κ° λ³Όλ₯¨λ§λ€ z-μ μλ‘ μ κ·νλ μνμ¬μΌ ν©λλ€.
# μμ μ΄λ―Έμ§ λ€μ΄λ‘λ
import urllib
url, filename = ("https://github.com/mateuszbuda/brain-segmentation-pytorch/raw/master/assets/TCGA_CS_4944.png", "TCGA_CS_4944.png")
try: urllib.URLopener().retrieve(url, filename)
except: urllib.request.urlretrieve(url, filename)
import numpy as np
from PIL import Image
from torchvision import transforms
input_image = Image.open(filename)
m, s = np.mean(input_image, axis=(0, 1)), np.std(input_image, axis=(0, 1))
preprocess = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=m, std=s),
])
input_tensor = preprocess(input_image)
input_batch = input_tensor.unsqueeze(0)
if torch.cuda.is_available():
input_batch = input_batch.to('cuda')
model = model.to('cuda')
with torch.no_grad():
output = model(input_batch)
print(torch.round(output[0]))