-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathetl3d.py
149 lines (110 loc) · 4.7 KB
/
etl3d.py
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
import torch
from torch.utils.data import Dataset, DataLoader
import numpy as np
import os, glob
import json
import skimage
import pandas as pd
from scipy.ndimage import zoom
from skimage.measure import label, regionprops
from skimage.filters import threshold_otsu
import pdb
def resample(volume, old_spacing, new_spacing, output_size=512):
scale = np.array(old_spacing) / np.array(new_spacing)
volume = zoom(volume, scale, order=0)
siz = volume.shape[1]
if siz < output_size:
pad = (output_size - siz)//2
volume = np.pad(volume, ((0, 0), (pad, pad), (pad, pad)), mode='constant')
elif siz > output_size:
crop = (siz - output_size)//2
volume = volume[:, crop : siz - crop, crop : siz - crop]
return volume
def get_valid_mask(img):
if img.max()==img.min():
valid_mask = np.zeros_like(img)
return valid_mask
thresh = threshold_otsu(img)
valid_mask = img > 0.2*thresh
valid_mask, num = label(valid_mask, connectivity=2, return_num=True)
props = regionprops(valid_mask)
props = sorted(props, key=lambda x: x.area, reverse=True)
valid_mask[valid_mask != props[0].label] = 0
valid_mask[valid_mask == props[0].label] = 1
return valid_mask.astype(np.float64)
class LandmarkDataset(Dataset):
"""Dataset class defining dataloader"""
def __init__(self, root_dir, initial_transform, deformation, augmentation=None, is_training=True):
super(Dataset, self).__init__()
"""
Args:
data
"""
self.datainfo = pd.read_csv(root_dir)
self.datainfo = self.datainfo[self.datainfo.train == is_training]
self.datainfo = self.datainfo[self.datainfo.discard == 0]
self.initial_transform = initial_transform
self.deformation = deformation
self.augmentation = augmentation
def __len__(self):
return len(self.datainfo)
def __getitem__(self, idx):
row = self.datainfo.iloc[idx]
study_path = row.path
image1 = self.load_volume(study_path)
valid_mask1 = get_valid_mask(image1)
image1, valid_mask1, _ = self.initial_transform(image1, valid_mask1)
image2, valid_mask2, deformation = self.deformation(image1, valid_mask1)
if self.augmentation is not None:
image1, _, _ = self.augmentation(image1)
image2, _, _ = self.augmentation(image2)
return image1.astype(np.float32), image2.astype(np.float32), deformation.astype(np.float32), valid_mask1.astype(np.float32), valid_mask2.astype(np.float32)
def load_volume(self, study_path):
"""
TODO:
FILL WITH CODE TO READ IMAGE VOLUME FROM A study_path
"""
return None
def visualize(volume1, volume2, volume3=None, out_dir="./sanity", base_name=0):
os.makedirs(out_dir, exist_ok=True)
slices = volume1.shape[0]
imlist = []
for i in range(slices):
if volume3 is None:
im = np.concatenate([volume1[i], volume2[i]], axis=1)
else:
im = np.concatenate([volume1[i], volume2[i], volume3[i]], axis=1)
imlist.append(im)
if len(imlist)==4:
im = np.concatenate(imlist, axis=0)
skimage.io.imsave(os.path.join(out_dir, "im_{}_{}.jpg".format(base_name, i)), (im*255).astype(np.uint8))
imlist = []
if __name__ == '__main__':
from custom_transforms3d import *
root_dir = '/export/scratch3/grewal/Data/Projects_JPG_data/ThreeD/MODIR_data_train_split/landmark_train_val_info.csv'
initial_transform = Compose([
CropDepthwise(p=1.0, crop_size=48, crop_mode='random'),
CropInplane(p=1.0, crop_size=128, crop_mode='random', border=50),
ToTensorShape(p=1.0)
])
deformation = AnyOf([
# RandomScale3D(p=1.0),
# RandomRotate3D(p=1.0),
RandomElasticTransform3D(p=1.0, sigma=64)
])
# augmentation = Compose([
# RandomBrightness(),
# RandomContrast()
# ])
augmentation = None
dataset = LandmarkDataset(root_dir, initial_transform, deformation, augmentation=augmentation, is_training=True)
print(len(dataset))
for i in range(len(dataset)):
print(i)
image1, image2, _, valid_mask1, valid_mask2 = dataset[i]
image3 = np.abs(image1 - image2)
print(np.max(image3), np.min(image3))
image3 = (image3 + 1e-3) / (np.max(image3) + 1e-3)
visualize(image1[0], image2[0], image3[0], base_name=i)
if i>20:
break