[5943d3]: / rocaseg / datasets / dataset_oai_imo.py

Download this file

204 lines (166 with data), 7.5 kB

  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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import os
import glob
import logging
from collections import defaultdict
import numpy as np
import pandas as pd
from joblib import Parallel, delayed
from tqdm import tqdm
import cv2
from torch.utils.data.dataset import Dataset
from rocaseg.datasets.constants import locations_mh53
logging.basicConfig()
logger = logging.getLogger('dataset')
logger.setLevel(logging.DEBUG)
def index_from_path_oai_imo(path_root, force=False):
fname_meta_dyn = os.path.join(path_root, 'meta_dynamic.csv')
fname_meta_base = os.path.join(path_root, 'meta_base.csv')
if not os.path.exists(fname_meta_dyn) or force:
fnames_image = glob.glob(
os.path.join(path_root, '**', 'images', '*.png'), recursive=True)
logger.info('{} images found'.format(len(fnames_image)))
fnames_mask = glob.glob(
os.path.join(path_root, '**', 'masks', '*.png'), recursive=True)
logger.info('{} masks found'.format(len(fnames_mask)))
df_meta = pd.read_csv(fname_meta_base,
dtype={'patient': str,
'release': str,
'prefix_var': str,
'sequence': str,
'side': str,
'slice_idx': int,
'pixel_spacing_0': float,
'pixel_spacing_1': float,
'slice_thickness': float,
'KL': int},
index_col=False)
if len(fnames_image) != len(df_meta):
raise ValueError("Number of images doesn't match with the metadata")
if len(fnames_mask) != len(df_meta):
raise ValueError("Number of masks doesn't match with the metadata")
df_meta['path_image'] = [os.path.join(path_root, e)
for e in df_meta['path_rel_image']]
df_meta['path_mask'] = [os.path.join(path_root, e)
for e in df_meta['path_rel_mask']]
# Check for in-slice mask presence
def worker_nv7(fname):
mask = read_mask(path_file=fname, mask_mode='raw')
if np.any(mask[1:] > 0):
return 1
else:
return 0
logger.info('Exploring the annotations')
tmp = Parallel(n_jobs=-1)(
delayed(worker_nv7)(row['path_mask'])
for _, row in tqdm(df_meta.iterrows(), total=len(df_meta)))
df_meta['has_mask'] = tmp
# Sort the records
df_meta_sorted = (df_meta
.sort_values(['patient', 'prefix_var',
'sequence', 'slice_idx'])
.reset_index()
.drop('index', axis=1))
df_meta_sorted.to_csv(fname_meta_dyn, index=False)
else:
df_meta_sorted = pd.read_csv(fname_meta_dyn,
dtype={'patient': str,
'release': str,
'prefix_var': str,
'sequence': str,
'side': str,
'slice_idx': int,
'pixel_spacing_0': float,
'pixel_spacing_1': float,
'slice_thickness': float,
'KL': int,
'has_mask': int},
index_col=False)
return df_meta_sorted
def read_image(path_file):
image = cv2.imread(path_file, cv2.IMREAD_GRAYSCALE)
return image.reshape((1, *image.shape))
def read_mask(path_file, mask_mode):
"""Read mask from the file, and pre-process it.
IMPORTANT: currently, we handle the inter-class collisions by assigning
the joint pixels to a class with a lower index.
Parameters
----------
path_file: str
Full path to mask file.
mask_mode: str
Specifies which channels of mask to use.
Returns
-------
out : (ch, d0, d1) uint8 ndarray
"""
mask = cv2.imread(path_file, cv2.IMREAD_GRAYSCALE)
locations = {
'_background': (locations_mh53['Background'], ),
'femoral': (locations_mh53['FemoralCartilage'], ),
'tibial': (locations_mh53['LateralTibialCartilage'],
locations_mh53['MedialTibialCartilage']),
'patellar': (locations_mh53['PatellarCartilage'], ),
'menisci': (locations_mh53['LateralMeniscus'],
locations_mh53['MedialMeniscus']),
}
if mask_mode == 'raw':
return mask
elif mask_mode == 'all_unitibial_unimeniscus':
ret = np.empty((5, *mask.shape), dtype=mask.dtype)
ret[0, :, :] = np.isin(mask, locations['_background']).astype(np.uint8)
ret[1, :, :] = np.isin(mask, locations['femoral']).astype(np.uint8)
ret[2, :, :] = np.isin(mask, locations['tibial']).astype(np.uint8)
ret[3, :, :] = np.isin(mask, locations['patellar']).astype(np.uint8)
ret[4, :, :] = np.isin(mask, locations['menisci']).astype(np.uint8)
return ret
else:
raise ValueError('Invalid `mask_mode`')
class DatasetOAIiMoSagittal2d(Dataset):
def __init__(self, df_meta, mask_mode=None, name=None, transforms=None,
sample_mode='x_y', **kwargs):
logger.warning('Redundant dataset init arguments:\n{}'
.format(repr(kwargs)))
self.df_meta = df_meta
self.mask_mode = mask_mode
self.name = name
self.transforms = transforms
self.sample_mode = sample_mode
def __len__(self):
return len(self.df_meta)
def _getitem_x_y(self, idx):
image = read_image(self.df_meta['path_image'].iloc[idx])
mask = read_mask(self.df_meta['path_mask'].iloc[idx], self.mask_mode)
# Apply transformations
if self.transforms is not None:
for t in self.transforms:
if hasattr(t, 'randomize'):
t.randomize()
image, mask = t(image, mask)
tmp = dict(self.df_meta.iloc[idx])
tmp['image'] = image
tmp['mask'] = mask
tmp['xs'] = tmp['image']
tmp['ys'] = tmp['mask']
return tmp
def __getitem__(self, idx):
if self.sample_mode == 'x_y':
return self._getitem_x_y(idx)
else:
raise ValueError('Invalid `sample_mode`')
def read_image(self, path_file):
return read_image(path_file=path_file)
def read_mask(self, path_file):
return read_mask(path_file=path_file, mask_mode=self.mask_mode)
def describe(self):
summary = defaultdict(float)
for i in range(len(self)):
if self.sample_mode == 'x_y':
_, mask = self.__getitem__(i)
else:
mask = self.__getitem__(i)['mask']
summary['num_class_pixels'] += mask.numpy().sum(axis=(1, 2))
summary['class_importance'] = \
np.sum(summary['num_class_pixels']) / summary['num_class_pixels']
summary['class_importance'] /= np.sum(summary['class_importance'])
logger.info('Dataset statistics:')
logger.info(sorted(summary.items()))