def get_cnn_centers(names, labels_names, balanced=True, neigh_width=15): rois = list(load_masks(names)) rois_p = list(load_masks(labels_names)) rois_p_neigh = [log_and(log_and(imdilate(roi_p, iterations=neigh_width), log_not(roi_p)), roi) for roi, roi_p in izip(rois, rois_p)] rois_n_global = [log_and(roi, log_not(log_or(roi_pn, roi_p))) for roi, roi_pn, roi_p in izip(rois, rois_p_neigh, rois_p)] rois = list() for roi_pn, roi_ng, roi_p in izip(rois_p_neigh, rois_n_global, rois_p): # The goal of this for is to randomly select the same number of nonlesion and lesion samples for each image. # We also want to make sure that we select the same number of boundary negatives and general negatives to # try to account for the variability in the brain. n_positive = np.count_nonzero(roi_p) if balanced: roi_pn[roi_pn] = np.random.permutation(xrange(np.count_nonzero(roi_pn))) < n_positive / 2 roi_ng[roi_ng] = np.random.permutation(xrange(np.count_nonzero(roi_ng))) < n_positive / 2 rois.append(log_or(log_or(roi_ng, roi_pn), roi_p)) # In order to be able to permute the centers to randomly select them, or just shuffle them for training, we need # to keep the image reference with the center. That's why we are doing the next following lines of code. centers_list = [get_mask_voxels(roi) for roi in rois] idx_lesion_centers = np.concatenate([np.array([(i, c) for c in centers], dtype=object) for i, centers in enumerate(centers_list)]) return idx_lesion_centers
def get_slices_boundary(masks, patch_size, rois=None, rate=0.1): patch_half = map(lambda p_length: p_length // 2, patch_size) boundaries = map( lambda m: map(lambda l: log_and(m == l, log_not(imerode(m == l))), range(1, m.max() + 1)), masks) max_shape = masks[0].shape mesh = get_mesh(max_shape) legal_mask = reduce( np.logical_and, map( lambda (m_j, p_ij, max_j): np.logical_and(m_j >= p_ij, m_j <= max_j - p_ij), zip(mesh, patch_half, max_shape))) boundaries = map( lambda b: map(lambda b_i: np.logical_and(b_i, legal_mask), b), boundaries) centers = map( lambda b: np.concatenate(filter( lambda arr: arr.size > 0, map( lambda b_i: np.random.permutation(zip(*np.where(b_i)))[:int( np.sum(b_i) * rate)], b)), axis=0), boundaries) patch_slices = map(lambda c: centers_to_slice(c, patch_half), centers) return patch_slices
def __init__(self, cases, labels, rois, patch_size=32, flip=False): # Init # Image and mask should be numpy arrays self.cases = cases self.labels = labels self.flip = flip data_shape = self.cases[0].shape if type(patch_size) is not tuple: patch_size = (patch_size, ) * len(data_shape) self.patch_size = patch_size self.patch_slices_pos = get_slices_mask_bb(self.labels, patch_size, patch_size[0] - 8) brains = map( lambda (l, r): log_and(log_not(l.astype(np.bool)), r.astype(np.bool)), zip(labels, rois)) patch_slices_neg = get_slices_mask_bb(brains, patch_size, patch_size[0] - 8) self.patch_slices_neg = map( lambda (pos, neg): map(lambda idx: neg[idx], np.random.permutation(len(neg))[:len(pos)]), zip(self.patch_slices_pos, patch_slices_neg)) self.max_slice = np.cumsum(map(len, self.patch_slices_pos))