def create_sliceinfo_w(images_fn, labels_fn, regint_fn, kernels, i, slice_no=None): # figure out the biggest slice if slice_no == None: slice_no = find_biggest_slice(path + labels_fn[i]) print("Creating Weighted Slice Info... {}/{}: Slice {}".format( i + 1, len(images_fn), slice_no)) # get the slice, label, and associated orientations slice_im, slice_im_or = get_nifti_slice(path + images_fn[i], slice_no) slice_lb, slice_lb_or = get_nifti_slice(path + labels_fn[i], slice_no) slice_ro, slice_ro_or = get_nifti_slice(path + regint_fn[i], slice_no) # if crop, we crop the image down if crop: crop_x, crop_y = crop start_x = slice_im.shape[0] / 2 - crop_x / 2 end_x = start_x + crop_x start_y = slice_im.shape[1] / 2 - crop_y / 2 end_y = start_y + crop_y slice_im = slice_im[start_x:end_x, start_y:end_y] slice_lb = slice_lb[start_x:end_x, start_y:end_y] slice_ro = slice_ro[start_x:end_x, start_y:end_y] # figure out the principal patches pc_payload = (slice_im, slice_lb) patches_m_pc, patches_n_pc, vals_m, vals_n = create_pc_patches_w( *pc_payload) # compute gabor features for the patches feats_m = [] feats_n = [] intens_m = [] intens_n = [] for patch in patches_m_pc: feats_m.append(compute_feats(patch, kernels)) intens_m.append(compute_intens(patch)) for patch in patches_n_pc: feats_n.append(compute_feats(patch, kernels)) intens_n.append(compute_intens(patch)) # package it into a SliceInfo object si_payload = (images_fn[i], slice_no, slice_im, slice_im_or, slice_lb, slice_lb_or, slice_ro, slice_ro_or, patches_m_pc, patches_n_pc, feats_m, feats_n, intens_m, intens_n, vals_m, vals_n) return SliceInfo(*si_payload)
def create_sliceinfo_w(images_fn, labels_fn, regint_fn, kernels, i, slice_no=None): # figure out the biggest slice if slice_no == None: slice_no = find_biggest_slice(path + labels_fn[i]) print("Creating Weighted Slice Info... {}/{}: Slice {}".format(i+1, len(images_fn), slice_no)) # get the slice, label, and associated orientations slice_im, slice_im_or = get_nifti_slice(path + images_fn[i], slice_no) slice_lb, slice_lb_or = get_nifti_slice(path + labels_fn[i], slice_no) slice_ro, slice_ro_or = get_nifti_slice(path + regint_fn[i], slice_no) # if crop, we crop the image down if crop: crop_x, crop_y = crop start_x = slice_im.shape[0] / 2 - crop_x / 2 end_x = start_x + crop_x start_y = slice_im.shape[1] / 2 - crop_y / 2 end_y = start_y + crop_y slice_im = slice_im[start_x:end_x, start_y:end_y] slice_lb = slice_lb[start_x:end_x, start_y:end_y] slice_ro = slice_ro[start_x:end_x, start_y:end_y] # figure out the principal patches pc_payload = (slice_im, slice_lb) patches_m_pc, patches_n_pc, vals_m, vals_n = create_pc_patches_w(*pc_payload) # compute gabor features for the patches feats_m = [] feats_n = [] intens_m = [] intens_n = [] for patch in patches_m_pc: feats_m.append(compute_feats(patch, kernels)) intens_m.append(compute_intens(patch)) for patch in patches_n_pc: feats_n.append(compute_feats(patch, kernels)) intens_n.append(compute_intens(patch)) # package it into a SliceInfo object si_payload = (images_fn[i], slice_no, slice_im, slice_im_or, slice_lb, slice_lb_or, slice_ro, slice_ro_or, patches_m_pc, patches_n_pc, feats_m, feats_n, intens_m, intens_n, vals_m, vals_n) return SliceInfo(*si_payload)
def classify_patch_group(fn_rf, fn_kern, fn_patch_info): t0 = time() RF = dill.load(open(fn_rf, 'rb')) # unpack the RF classifier kernels = dill.load(open(fn_kern, 'rb')) # unpack the kernels patch_info = dill.load(open(fn_patch_info, 'rb')) # unpack the patch info a, b, c = patch_info[0] # get the bounds of the set patches_a = patch_info[1] # grab the set to classify patches_r = patch_info[2] # grab the set to compare with (atlas mask) results = [] for i, patch in enumerate(patches_a): # go through each patch if np.all(patches_r[i]): # if the patch is entirely masked feat = compute_feats(patch, kernels).flatten().reshape(1, -1) intens = np.array(compute_intens(patch)).flatten().reshape(1, -1) feat = np.concatenate((feat, intens), axis=1) prediction = RF.predict(feat) #print("Classifying patch {}/{}: {}".format(i, len(patches), prediction)) results.append(np.full(patch.shape, prediction)) else: # the associated ROI patch is totally zero results.append(np.zeros(patch.shape)) dt = time() - t0 print("Classified group {}-{}/{} in {:.2f} time".format(a, b, c, dt)) return results
def classify_patch_group(fn_rf, fn_kern, fn_patch_info): t0 = time() RF = dill.load(open(fn_rf, "rb")) # unpack the RF classifier kernels = dill.load(open(fn_kern, "rb")) # unpack the kernels patch_info = dill.load(open(fn_patch_info, "rb")) # unpack the patch info a, b, c = patch_info[0] # get the bounds of the set patches_a = patch_info[1] # grab the set to classify patches_r = patch_info[2] # grab the set to compare with (atlas mask) results = [] for i, patch in enumerate(patches_a): # go through each patch if np.all(patches_r[i]): # if the patch is entirely masked feat = compute_feats(patch, kernels).flatten().reshape(1, -1) intens = np.array(compute_intens(patch)).flatten().reshape(1, -1) feat = np.concatenate((feat, intens), axis=1) prediction = RF.predict(feat) # print("Classifying patch {}/{}: {}".format(i, len(patches), prediction)) results.append(np.full(patch.shape, prediction)) else: # the associated ROI patch is totally zero results.append(np.zeros(patch.shape)) dt = time() - t0 print("Classified group {}-{}/{} in {:.2f} time".format(a, b, c, dt)) return results
def check_classify(RF, kernels, patch, patch_label, i, tot): # kernel the patch print("Checking Classifying patch {}/{}".format(i, tot)) feat = compute_feats(patch, kernels) feat = feat.flatten().reshape(1, -1) prediction = RF.predict(feat) if prediction == patch_label: return True else: return False
def classify_patch_w(fn, kernels, patches, i): RF = joblib.load(fn) patch = patches[i] feat = compute_feats(patch, kernels) feat = feat.flatten().reshape(1, -1) hogs = compute_hogs(patch) hogs = hogs.flatten().reshape(1, -1) feat = np.concatenate((feat, hogs), axis=1) prediction = RF.predict(feat) print("Classifying patch {}/{}: {}".format(i, len(patches), prediction)) if prediction == 'M': return np.ones(patch.shape) elif prediction == 'N': return np.zeros(patch.shape) else: return np.full(patch.shape, prediction)
def process(filename, filename_label, slice_no): # Grab the image image_slice, orientation_slice = get_nifti_slice(filename, slice_no) if SHOW_IMG: plt.imshow(image_slice, cmap = plt.cm.gray) plt.show() # Grab the labels label_slice, orientation_label = get_nrrd_data(filename_label, slice_no) if SHOW_IMG: plt.imshow(label_slice, cmap=plt.cm.gray) plt.show() # Show the mask if SHOW_IMG: print("Masked version: ") mask = np.where(label_slice == 0, label_slice, image_slice) plt.imshow(mask, cmap=plt.cm.gray) plt.show() # Extract patches in ROI patches_mask, patches_nonmask = extract_roi_patches(image_slice, label_slice, PATCH_SIZE) # Get the decomposed patches eigens_mask = get_eigenpatches(patches_mask, PATCH_SIZE, MAX_EIGEN) eigens_nonmask = get_eigenpatches(patches_nonmask, PATCH_SIZE, MAX_EIGEN) # Show the eigens, if you want if SHOW_IMG: show_eigenpatches(eigens_mask) # Generate Gabor Kernels kernels = generate_kernels() # Show the Gabors if SHOW_IMG: plot_gabor(eigens_mask) # Store all the features and Gabor responses all_features_mask = [] all_powers_mask = [] complete_features_mask = [] all_features_nonmask = [] all_powers_nonmask = [] complete_features_nonmask = [] for eigen in eigens_mask: all_features_mask.append(compute_feats(eigen, kernels)) all_powers_mask.append(compute_powers(eigen, kernels)) for eigen in eigens_nonmask: all_features_nonmask.append(compute_feats(eigen, kernels)) all_powers_nonmask.append(compute_powers(eigen, kernels)) # for patch in patches_mask: # complete_features_mask.append(compute_feats(patch, kernels)) # # for patch in patches_nonmask: # complete_features_nonmask.append(compute_feats(patch, kernels)) return SliceInfo(filename, slice_no, image_slice, orientation_slice, label_slice, orientation_label, kernels, patches_mask, eigens_mask, all_features_mask, all_powers_mask, patches_nonmask, eigens_nonmask, all_features_nonmask, all_powers_nonmask)
def process(filename, filename_label, slice_no): # Grab the image image_slice, orientation_slice = get_nifti_slice(filename, slice_no) image_slice = normalise(image_slice) if SHOW_IMG: plt.imshow(image_slice, cmap=plt.cm.gray) plt.show() # Grab the labels label_slice, orientation_label = get_nrrd_data(filename_label, slice_no) #if SHOW_IMG: # plt.imshow(label_slice, cmap=plt.cm.gray) # plt.show() # Show the mask if SHOW_IMG: print("Masked version: ") mask = np.where(label_slice == 0, label_slice, image_slice) plt.imshow(mask, cmap=plt.cm.gray) plt.show() # Extract patches in ROI patches_mask, patches_nonmask = extract_roi_patches( image_slice, label_slice, PATCH_SIZE) # Get the decomposed patches eigens_mask = get_randoms(patches_mask, MAX_EIGEN) eigens_nonmask = get_randoms(patches_nonmask, MAX_EIGEN) # Show the eigens, if you want if SHOW_IMG: show_eigenpatches(eigens_mask) # Generate Gabor Kernels kernels = generate_kernels() # Show the Gabors if SHOW_IMG: plot_gabor(eigens_mask) # Store all the features and Gabor responses all_features_mask = [] all_powers_mask = [] all_features_nonmask = [] all_powers_nonmask = [] for eigen in eigens_mask: all_features_mask.append(compute_feats(eigen, kernels)) all_powers_mask.append(compute_powers(eigen, kernels)) for eigen in eigens_nonmask: all_features_nonmask.append(compute_feats(eigen, kernels)) all_powers_nonmask.append(compute_powers(eigen, kernels)) return SliceInfo(filename, slice_no, image_slice, orientation_slice, label_slice, orientation_label, kernels, patches_mask, eigens_mask, all_features_mask, all_powers_mask, patches_nonmask, eigens_nonmask, all_features_nonmask, all_powers_nonmask)