def acc_rate(predictions, labels): """ Return the error rate based on dense predictions and labels. :param predictions: list of output predictions :param labels: list of ground truths """ assert len(predictions) == len(labels), "Number of predictions and labels don't equal." err = np.array([]) n = len(predictions) for i in range(n): err = np.hstack((err, (100.0 * np.average( np.argmax(predictions[i], -1) == np.argmax(utils.crop_to_shape(labels[i], predictions[i].shape), -1))))) return err
def auc_score(predictions, labels): """ Return the auc score based on dense predictions and labels. :param predictions: list of output predictions :param labels: list of ground truths """ assert len(predictions) == len(labels), "Number of predictions and labels don't equal." auc = np.array([]) n = len(predictions) n_class = np.shape(labels[0])[-1] for i in range(n): flat_score = np.reshape(predictions[i], [-1, n_class]) flat_true = np.reshape(utils.crop_to_shape(labels[i], predictions[i].shape), [-1, n_class]) auc = np.hstack((auc, roc_auc_score(flat_true, flat_score))) return auc
def average_foreground_jaccard(predictions, labels): """ Return the averaged foreground Jaccard based on dense predictions and labels. :param predictions: list of output predictions :param labels: list of ground truths """ n_class = labels[0].shape[-1] n = len(predictions) jaccard = np.empty([n]) for i in range(n): pred = np.array(predictions[i]) label = utils.crop_to_shape(np.array(labels[i]), pred.shape) Jaccard = OverlapMetrics(n_class, mode='np') jaccard[i] = Jaccard.averaged_foreground_jaccard(label, pred) return jaccard
def average_foreground_dice(predictions, labels): """ Return the dice score based on dense predictions and labels. :param predictions: list of output predictions :param labels: list of ground truths """ assert len(predictions) == len(labels), "Number of predictions and labels don't equal." n_class = labels[0].shape[-1] dice = np.array([]) n = len(predictions) for i in range(n): pred = np.array(predictions[i]) label = utils.crop_to_shape(np.array(labels[i]), pred.shape) Dice = OverlapMetrics(n_class, mode='np') dice = np.hstack((dice, Dice.averaged_foreground_dice(label, pred))) return dice
def myocardial_dice_score(predictions, labels): """ Return the myocardial dice score between predictions and ground truths. :param predictions: list of output predictions :param labels: list of ground truths :return: a list of myocardial dice score """ assert len(predictions) == len(labels), "Number of predictions and labels don't equal." n = len(predictions) dice = np.zeros([n]) for i in range(n): pred = np.array(predictions[i]) label = utils.crop_to_shape(np.array(labels[i]), pred.shape) dice[i] = OverlapMetrics(mode='np').class_specific_dice(label, pred, i=1) return dice
# pre-processing for image and label image_data = process_image(image) label_data = process_label(label, label_intensities) # produce gradient images image_grad = utils.compute_gradnorm_from_volume(np.sum(image_data, axis=-1, keepdims=True), mode='np') label_grad = utils.compute_gradnorm_from_volume(label_data, mode='np') # save into nifty files # grad_nii = nib.Nifti1Image(image_grad.squeeze(0), affine=affine, header=header) # nib.save(grad_nii, os.path.join(save_path, image_name.replace(image_suffix, 'image_grad.nii.gz'))) # grad_nii = nib.Nifti1Image(label_grad.squeeze(0), affine=affine, header=header) # nib.save(grad_nii, os.path.join(save_path, image_name.replace(image_suffix, 'label_grad.nii.gz'))) # crop data image_grad_crop = utils.crop_to_shape(image_grad, (80, 80, 80)) label_grad_crop = utils.crop_to_shape(label_grad, (80, 80, 80)) # # compute local conditional entropy lecc = np.concatenate([mi.lecc(image_grad_crop, label_grad_crop[..., i, None]) for i in range(len(label_intensities))], axis=-1) # lecc = losses._lecc(image_grad_crop, label_grad_crop[..., 0, None], n_bins=4, win=7, sigma=3) # save into nifty files lecc_nii = nib.Nifti1Image(utils.pad_to_shape_image(mi._normalize(-lecc), original_size, mode='np', method='edge').squeeze(0), affine=affine, header=header) nib.save(lecc_nii, os.path.join(save_path, image_name.replace(image_suffix, 'lecc.nii.gz'))) time_end = time.time() print("Elapsing time: %s" % (time_end - time_start))