-
Notifications
You must be signed in to change notification settings - Fork 1
/
preprocess.py
125 lines (91 loc) · 3.35 KB
/
preprocess.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
import torch
import pandas as pd
import numpy as np
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
import os
from glob import glob
import nibabel as nib
import pickle
from tqdm import tqdm
import random
from torchvision.transforms import transforms
from scipy import ndimage
import matplotlib.pyplot as plt
device = 'cuda' if torch.cuda.is_available() else 'cpu'
train_imgs = '/home/sarucrcv/datasets/Task01_BrainTumour/imagesTr/'
train_labels = '/home/sarucrcv/datasets/Task01_BrainTumour/labelsTr/'
with open('train.txt','w') as f:
f.write('\n'.join(imgsList[:400]))
f.close()
with open('val.txt','w') as f:
f.write('\n'.join(imgsList[400:]))
modalities = ('flair', 't1ce', 't1', 't2')
# train
train_set = {
'tpath': '/home/sarucrcv/projects/3dunet/train_pkl_all/', #Target Path of pickle files
'root': '/home/sarucrcv/datasets/Task01_BrainTumour/',
'flist': 'train.txt',
'has_label': True
}
# test/validation data
valid_set = {
'tpath': '/home/sarucrcv/projects/3dunet/val_pkl_all/',
'root': '/home/sarucrcv/datasets/Task01_BrainTumour/',
'flist': 'val.txt',
'has_label': True
}
def nib_load(file_name):
if not os.path.exists(file_name):
print('Invalid file name, can not find the file!')
proxy = nib.load(file_name)
data = proxy.get_fdata()
proxy.uncache()
return data
def process_i16(path, has_label=True):
""" Save the original 3D MRI images with dtype=int16.
Noted that no normalization is used! """
label = np.array(nib_load(path + 'seg.nii.gz'), dtype='uint8', order='C')
images = np.stack([
np.array(nib_load(path + modal + '.nii.gz'), dtype='int16', order='C')
for modal in modalities], -1)# [240,240,155]
output = path + 'data_i16.pkl'
with open(output, 'wb') as f:
print(output)
print(images.shape, type(images), label.shape, type(label)) # (240,240,155,4) , (240,240,155)
pickle.dump((images, label), f)
if not has_label:
return
def process_f32b0(tpath, img_path, label_path, has_label=True):
""" Save the data with dtype=float32.
z-score is used but keep the background with zero! """
label = np.array(nib_load(label_path), dtype='uint8', order='C')
images = np.stack(np.array(nib_load(img_path), dtype='float32', order='C')) # [240,240,155]
foldername = img_path.split('/')[-1]
output = tpath + foldername + '_data_f32b0.pkl'
mask = images.sum(-1) > 0
for k in range(4):
x = images[..., k] #
y = x[mask]
# 0.8885
x[mask] -= y.mean()
x[mask] /= y.std()
images[..., k] = x
with open(output, 'wb') as f:
print(output)
if has_label:
pickle.dump((images, label), f)
else:
pickle.dump(images, f)
if not has_label:
return
def doit(dset):
root, has_label = dset['root'], dset['has_label']
file_list = os.path.join(dset['flist'])
names = open(file_list).read().splitlines()
imgs_paths = [os.path.join(root, 'imagesTr', name) for name in names]
labels_paths = [os.path.join(root, 'labelsTr', name) for name in names]
for i in tqdm(range(len(imgs_paths))):
process_f32b0(dset['tpath'],imgs_paths[i], labels_paths[i], has_label)
doit(train_set)
doit(valid_set)