-
Notifications
You must be signed in to change notification settings - Fork 13
/
brain_dataset.py
183 lines (140 loc) · 6.77 KB
/
brain_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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# Developing Brain Atlas through Deep Learning
# Asim Iqbal, Romesa Khan, Theofanis Karayannis
"Custom functions for loading Brain Dataset "
import utils
import os
import glob #for selecting png files in training images folder
from natsort import natsorted, ns #for sorting filenames in a directory
import skimage
from skimage import io
import numpy as np
########### Create training dataset:
class BrainDataset_Train(utils.Dataset):
"""Generates the brain section dataset. The dataset consists of locally stored
brain section images, to which file access is required.
"""
#see utils.py for default def load_image() function; modify according to your dataset
def load_brain(self):
"""
for naming image files follow this convention: '*_(image_id).jpg'
"""
os.chdir('DATASETsubmit')
self.add_class('brain','1','cortex:')
self.add_class('brain','2','hippocampus:')
self.add_class('brain','3','basal ganglia:')
self.add_class('brain','4','thalamus:')
self.add_class('brain','5','prethalamus:')
self.add_class('brain','6','midbrain:')
self.add_class('brain','7','hindbrain:')
self.add_class('brain','8','telencephalic vesicle:')
training_images_folder = 'mrcnn_train_dataset_images'
os.chdir(training_images_folder)
im_id = 0
cwd = os.getcwd()
img_list = glob.glob('*.jpg')
img_list = natsorted(img_list, key=lambda y: y.lower())
#print(img_list)
for i in img_list: #image_ids start at 0 (to keep correspondence with load_mask which begins at image_id=0)!
img = skimage.io.imread(i) #grayscale = 0
im_dims = np.shape(img)
self.add_image("brain", image_id=im_id, path = cwd+'/'+glob.glob('*_'+str(im_id)+'.jpg')[0],height = im_dims[0], width = im_dims[1])#, depth = im_dims[2])
im_id += 1
#print(im_dims)
def load_mask(self,image_id):
"""Load instance masks for the given image.
Different datasets use different ways to store masks. This
function converts the different mask format to one format
in the form of a bitmap [height, width, instances].
Returns:
masks: A bool array of shape [height, width, instance count] with
one mask per instance.
class_ids: a 1D array of class IDs of the instance masks."""
os.chdir('DATASETsubmit')
#print(image_id)
masks_folder = 'mrcnn_train_dataset_masks'
os.chdir(masks_folder)
subfolder = glob.glob('*_'+str(image_id))[0]#add 1 to image_id, to get to correct corresponding masks folder for a given image
#print(subfolder)
os.chdir(subfolder)
info = self.image_info[image_id]
#print(info)
mk_list = glob.glob('*.png')
#print(mk_list)
count = len(mk_list)
mk_id = 0
mask = np.zeros([info['height'], info['width'], count], dtype=np.uint8)
#print(np.shape(mask))
class_ids = np.zeros(count)
for m in mk_list:
bin_mask = skimage.io.imread(m,as_grey=True) # grayscale=0
mk_size = np.shape(bin_mask)
mask[:, :, mk_id]= bin_mask
# Map class names to class IDs.
class_ids[mk_id] = m[-5] #fifth last position from mask_image name = class_id #need to update(range) if class_ids become two/three-digit numbers
mk_id += 1
return mask, class_ids.astype(np.int32)
########### Create validation dataset:
class BrainDataset_Val(utils.Dataset):
"""Generates the brain section dataset. The dataset consists of locally stored
brain section images, to which file access is required.
"""
#see utils.py for default def load_image() function; modify according to your dataset
def load_brain(self):
"""
for naming image files follow this convention: '*_(image_id+1).jpg'
"""
os.chdir('DATASETsubmit')
self.add_class('brain','1','cortex:')
self.add_class('brain','2','hippocampus:')
self.add_class('brain','3','basal ganglia:')
self.add_class('brain','4','thalamus:')
self.add_class('brain','5','prethalamus:')
self.add_class('brain','6','midbrain:')
self.add_class('brain','7','hindbrain:')
self.add_class('brain','8','telencephalic vesicle:')
training_images_folder = 'mrcnn_val_dataset_images'
os.chdir(training_images_folder)
im_id = 0
cwd = os.getcwd()
img_list = glob.glob('*.jpg')
img_list = natsorted(img_list, key=lambda y: y.lower())
#print(img_list)
for i in img_list: #image_ids start at 0 (to keep correspondence with load_mask which begins at image_id=0)!
img = skimage.io.imread(i) #grayscale = 0
im_dims = np.shape(img)
self.add_image("brain", image_id=im_id, path = cwd+'/'+glob.glob('*_'+str(im_id)+'.jpg')[0],height = im_dims[0], width = im_dims[1])#, depth = im_dims[2])
im_id += 1
#print(im_dims)
def load_mask(self,image_id):
"""Load instance masks for the given image.
Different datasets use different ways to store masks. This
function converts the different mask format to one format
in the form of a bitmap [height, width, instances].
Returns:
masks: A bool array of shape [height, width, instance count] with
one mask per instance.
class_ids: a 1D array of class IDs of the instance masks."""
os.chdir('DATASETsubmit')
#print(image_id)
masks_folder = 'mrcnn_val_dataset_masks'
os.chdir(masks_folder)
subfolder = glob.glob('*_'+str(image_id))[0]#add 1 to image_id, to get to correct corresponding masks folder for a given image
#print(subfolder)
os.chdir(subfolder)
info = self.image_info[image_id]
#print(info)
mk_list = glob.glob('*.png')
#print(mk_list)
count = len(mk_list)
mk_id = 0
mask = np.zeros([info['height'], info['width'], count], dtype=np.uint8)
#print(np.shape(mask))
class_ids = np.zeros(count)
for m in mk_list:
bin_mask = skimage.io.imread(m,as_grey=True)
mk_size = np.shape(bin_mask)
mask[:, :, mk_id]= bin_mask
# Map class names to class IDs.
class_ids[mk_id] = m[-5] #fifth last position from mask_image name = class_id #need to update(range) if class_ids become two/three-digit numbers
mk_id += 1
return mask, class_ids.astype(np.int32)