-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
90 lines (64 loc) · 2.45 KB
/
dataset.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
import os
import numpy as np
from glob import glob
import nibabel
train_img_dir = "YOUR_TRAIN_IMAGE_FOLDER_PATH"
train_mask_dir = "YOUR_TRAIN_MASK_FOLDER_PATH"
test_img_dir = 'YOUR_TEST_IMAGE_FOLDER_PATH'
image_rows = int(512/2)
image_cols = int(512/2)
def create_train_data():
print('-'*30)
print('Creating training data...')
print('-'*30)
train_img = os.path.join(train_img_dir, '*.nii')
train_label = os.path.join(train_mask_dir, '*.nii')
images = sorted(glob(train_img))
labels = sorted(glob(train_label))
imgs_train = []
masks_train = []
for label, img in zip(labels, images):
training_mask = nibabel.load(os.path.join(train_label, label))
training_image = nibabel.load(os.path.join(train_img, img))
for k in range(training_mask.shape[2]-1):
mask_2d = np.array(training_mask.get_fdata()[::2, ::2, k])
image_2d = np.array(training_image.get_fdata()[::2, ::2, k])
if len(np.unique(mask_2d)) != 1:
masks_train.append(mask_2d)
imgs_train.append(image_2d)
imgs = np.ndarray(
(len(imgs_train), image_rows, image_cols), dtype='uint8'
)
imgs_mask = np.ndarray(
(len(masks_train), image_rows, image_cols), dtype='uint8'
)
for index, img in enumerate(imgs_train):
imgs[index, :, :] = img
for index, img in enumerate(masks_train):
imgs_mask[index, :, :] = img
np.save('imgs_train.npy', imgs)
np.save('masks_train.npy', imgs_mask)
print('Saving to .npy files done.')
def create_test_data():
print('-'*30)
print('Creating test data...')
print('-'*30)
images = os.listdir(test_img_dir)
imgs_test = []
for image_name in images:
img = nibabel.load(os.path.join(test_img_dir, image_name))
image_2d = img.get_fdata()[::2, ::2, :]
for k in range(img.shape[2]):
image_slice = np.array(image_2d[:, :, k])
imgs_test.append(image_slice)
imgst = np.ndarray(
(len(imgs_test), image_rows, image_cols), dtype=np.uint8
)
for index, img in enumerate(imgs_test):
imgst[index, :, :] = img
np.save('imgs_test.npy', imgst)
print('Saving to .npy files done.')
create_train_data()
print('Creating train data was completed')
create_test_data()
print('Creating test data was completed')