๐Ÿ”ฌ Medical Image/Processing

TorchIO๋ฅผ ์ด์šฉํ•œ 3D ์ด๋ฏธ์ง€ transform (preprocessing/augmentation)

๋ณต๋งŒ 2022. 6. 7. 15:51

 

TorchIO๋Š” 3D medical image์˜ loading, preprocessing, augmentation, patch-based sampling์„ ์œ„ํ•œ ์˜คํ”ˆ์†Œ์Šค ํŒŒ์ด์ฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค. 

 

TorchIO

PyPI downloads PyPI version Conda version Google Colab notebooks Documentation status Tests status Lint status Coverage status Code quality Maintainability pre-commit Slack Twitter Twitter commits ...

torchio.readthedocs.io

 

์„ค๋ช…์—๋Š” 3D medical image๋ผ๊ณ  ๋˜์–ด ์žˆ์ง€๋งŒ, ์ผ๋ฐ˜ 3D ์ด๋ฏธ์ง€์—๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. 

 

torchvision.transforms์˜ transform ํ•จ์ˆ˜๋“ค์€ ๋Œ€๋ถ€๋ถ„ 2D ์ด๋ฏธ์ง€์—๋งŒ ์ ์šฉ๊ฐ€๋Šฅํ•œ ๋ฐ˜๋ฉด, TorchIO์—์„œ ์ œ๊ณตํ•˜๋Š” transform ํ•จ์ˆ˜๋“ค์€ 3D ์ด๋ฏธ์ง€์—๋„ ์ ์šฉ๊ฐ€๋Šฅํ•ด์„œ 3D ์ด๋ฏธ์ง€๋ฅผ ๋‹ค๋ฃจ๋Š” ์ž‘์—…์—์„œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค.

 

Data loading, patch-based pipeline ๋“ฑ ์—ฌ๋Ÿฌ ๊ธฐ๋Šฅ์ด ์žˆ์ง€๋งŒ ๋ณธ ๊ธ€์—์„œ๋Š” TorchIO๋ฅผ ์ด์šฉํ•ด augmentation ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์†Œ๊ฐœํ•˜๊ณ ์ž ํ•œ๋‹ค.

 

 

Install

pip๋ฅผ ์ด์šฉํ•ด TorchIO๋ฅผ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋‹ค. PyTorch๊ฐ€ ๋จผ์ € ๊น”๋ ค ์žˆ์–ด์•ผ ํ•œ๋‹ค.

pip install torchio

 

 

Transform

Torchvision์˜ transform ํ•จ์ˆ˜์™€ ์‚ฌ์šฉ๋ฒ•์€ ๊ฑฐ์˜ ๋™์ผํ•˜๋‹ค.

๋‹ค์Œ๊ณผ ๊ฐ™์ด TorchIO์˜ transform์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

input์œผ๋กœ๋Š” torchio.Subject, torch.Tensor, numpy.ndarray๋ฅผ ๋ชจ๋‘ ์ง€์›ํ•œ๋‹ค.

import torchio as tio

transform = tio.RandomAffine(scales=(1.2, 1.2), degrees=30, translation=10)
img_transform = transform(img)

 

๊ฐ€์žฅ ๋Œ€ํ‘œ์ ์ธ transform ํ•จ์ˆ˜์ธ RandomAffine์˜ parameter๋“ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • scales : scaling parameter. ๊ฐ dimension ๋ฐฉํ–ฅ์œผ๋กœ์˜ min, max ๊ฐ’ ํ˜น์€ ๋ณ€ํ™”๋Ÿ‰์„ ์ง€์ •ํ•˜๊ฑฐ๋‚˜ - $(min_1, max_1, min_2, max_2, min_3, max_3)$ or $(x_1, x_2, x_3)$, ๋ชจ๋“  dimension์˜ min, max ๊ฐ’ ํ˜น์€ ๋ณ€ํ™”๋Ÿ‰์„ ๊ฐ™๊ฒŒ ์ง€์ •ํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค - $(min, max)$ or $x$.
  • degrees : rotation parameter. ๊ฐ€๋Šฅํ•œ parameter ํ˜•์‹์€ scales์™€ ๋™์ผํ•˜๋‹ค.
  • translation : translation parameter. ๊ฐ€๋Šฅํ•œ parameter ํ˜•์‹์€ scales์™€ ๋™์ผํ•˜๋‹ค.
  • isotropic : Scaling ์‹œ์— ์ด๋ฏธ์ง€์˜ ๋น„์œจ์„ ์œ ์ง€ํ•  ๊ฒƒ์ธ์ง€๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค.
  • center : Rotation๊ณผ scaling ์‹œ์— ์–ด๋–ค ์ ์„ ์ค‘์‹ฌ์œผ๋กœ ํ•  ๊ฒƒ์ธ์ง€๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค. ('image' / 'origin')
  • default_pad_value : Rotation์ด๋‚˜ translation ์‹œ์— ๋นˆ ๊ณต๊ฐ„์„ ์ฑ„์šธ ๊ฐ’์„ ๊ฒฐ์ •ํ•œ๋‹ค. ('minimum' / 'mean' / 'otsu')

 

 

์ด์™ธ์—๋„ RandomFlip, RandomBlur, RandomNoise ๋“ฑ ๋‹ค์–‘ํ•œ transform์„ ์ œ๊ณตํ•œ๋‹ค. TorchIO์˜ ๋ชจ๋“  transform ํ•จ์ˆ˜๋“ค์€ ๋‹ค์Œ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

Augmentation

Augmentation transforms generate different results every time they are called. Augmented image Base class: RandomTransform: Composition: Compose: OneOf: Spatial: RandomFlip: RandomAffine: RandomEla...

torchio.readthedocs.io

 

 

Multiple transforms

torchvision.transforms.Compose์™€ ๊ฐ™์ด, ์—ฌ๋Ÿฌ ๊ฐœ์˜ transform์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

torchio.Compose๋ฅผ ์ด์šฉํ•˜๋ฉด ์—ฌ๋Ÿฌ ๊ฐœ์˜ transform์„ ํ•จ๊ป˜ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

torchio.Oneof๋ฅผ ์ด์šฉํ•˜๋ฉด ์—ฌ๋Ÿฌ ๊ฐœ์˜ transform ์ค‘ ํ•˜๋‚˜๋ฅผ randomํ•˜๊ฒŒ ์ ์šฉํ•˜๋„๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค.

Example: 

import torchio as tio
colin = tio.datasets.Colin27()
transforms_dict = {
    tio.RandomAffine(): 0.75,
    tio.RandomElasticDeformation(): 0.25,
}  # Using 3 and 1 as probabilities would have the same effect
transform = tio.OneOf(transforms_dict)
transformed = transform(colin)
๋ฐ˜์‘ํ˜•