def compute_accuracy_keras(predictions, instance_labels_ground, P, iou_threshold, class_nr): """ Computes the image probability as loss. Image probability calculation is different when computing in the loss function and in the testing conditions ( production). Difference is only for images with available segmentation labels. Image probability of images with segmentation in the loss are calculated according by Eq.(1) in https://arxiv.org/pdf/1711.06373.pdf Image probability of images with NO segmentation in testing/production environment are calculated by Eq.(2) in https://arxiv.org/pdf/1711.06373.pdf :param y_true: patch labels :param y_pred: patch predictions :return: Accuracy of images computed according to the formula used during training. Training includes supervised training of images with available segmentation. While in testing all images probabilities are calculated in an weakly supervised way. """ m = P * P sum_active_patches, class_label_ground, has_bbox = compute_ground_truth(instance_labels_ground, m, class_nr) IoU, accuracy_bbox = compute_accuracy_image_bbox(predictions, instance_labels_ground, class_label_ground, P, iou_threshold, class_nr) img_pred_norm = compute_image_label_in_classification_NORM(predictions, P, class_nr) img_pred_bin = tf.cast(img_pred_norm > 0.5, tf.float32) correct_prediction_img = tf.cast(tf.equal(img_pred_bin, class_label_ground), tf.float32) accuracy_per_obs_per_class = tf.where(has_bbox, accuracy_bbox, correct_prediction_img) accuracy_per_class = tf.reduce_mean(accuracy_per_obs_per_class, 0) return accuracy_per_class
def compute_image_probability_production(nn_output,instance_label_ground_truth, P, class_nr): ''' This method considers patches with prediction above Ts as active. If a patch is active, and then it belongs to the localization and then ONLY EQUATION 2 of the paper is used :param nn_output: output from the last layers :param P: number of patches :return: image probability per class computed using the active patches ''' image_probability = compute_image_label_in_classification_NORM(nn_output, P, class_nr) _, class_label_ground_truth, _= compute_ground_truth(instance_label_ground_truth, P*P, class_nr) return class_label_ground_truth, image_probability
def compute_image_probability_asloss(nn_output, instance_label_ground_truth, P, class_nr): ''' Computes image probability the same way it is computed in the loss :param nn_output: :param instance_label_ground_truth: :param P: :return: ''' sum_active_patches, class_label_ground_truth, has_bbox = compute_ground_truth(instance_label_ground_truth, P * P, class_nr) img_label_pred = compute_image_label_prediction(has_bbox, nn_output, instance_label_ground_truth, P, class_nr) return class_label_ground_truth, img_label_pred
def test_function_acc_class(y_pred, instance_labels_ground, P, iou_threshold): _, _, has_bbox = compute_ground_truth(instance_labels_ground, P * P) iou_scores = tf.where(has_bbox, compute_IoU(y_pred, instance_labels_ground, P), tf.zeros(tf.shape(has_bbox))) image_label_pred = tf.cast(tf.greater_equal(iou_scores, iou_threshold), tf.float32) # compare image_label prediction and has_bbox # tf equal will NOT be a good idea, as 0 in has bbox means absence of bbox and shouldnt be comuted in accuracy acc_pred = tf.reduce_sum(image_label_pred, axis=0) true_labels = tf.reduce_sum(tf.cast(has_bbox, tf.float32), axis=0) acc_per_class = tf.where(tf.greater( true_labels, 0), acc_pred / true_labels, tf.zeros(tf.shape( true_labels))) # tf.constant(-1.0, shape=(tf.shape(true_labels))) return has_bbox, true_labels, acc_pred, acc_per_class # , tf.reduce_mean(acc_per_class)
def compute_accuracy_keras(predictions, instance_labels_ground, P, iou_threshold, class_nr): m = P * P sum_active_patches, class_label_ground, has_bbox = compute_ground_truth( instance_labels_ground, m, class_nr) IoU, accuracy_bbox = compute_accuracy_image_bbox(predictions, instance_labels_ground, class_label_ground, P, iou_threshold, class_nr) img_pred_norm = compute_image_label_in_classification_NORM( predictions, P, class_nr) img_pred_bin = tf.cast(img_pred_norm > 0.5, tf.float32) correct_prediction_img = tf.cast( tf.equal(img_pred_bin, class_label_ground), tf.float32) accuracy_per_obs_per_class = tf.where(has_bbox, accuracy_bbox, correct_prediction_img) accuracy_per_class = tf.reduce_mean(accuracy_per_obs_per_class, 0) return accuracy_per_class
def visualize_single_image_all_classes(batch_df, img_ind, results_path, batch_predictions, batch_img_prob, img_label, skip_process): ind = 0 # for each row/observation in the batch for row in batch_df.values: labels_df = [] instance_labels_gt = prepare_labels_all_classes(row, skip_process) sum_active_patches, class_label_ground_truth, has_bbox = compute_ground_truth( instance_labels_gt, 256, 1) #for each class for i in range(1, row.shape[0]): # (15) init_op = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init_op) # slicing on index 0 it will have shape of (1, class_number) class_gt = class_label_ground_truth[0, i - 1].eval() # instance labels have shape of [16, 16, class_nr] instance_gt = instance_labels_gt[:, :, i - 1] total_active_patches = sum_active_patches[0, i - 1].eval() has_segmentation = has_bbox[0, i - 1].eval() img_probab = batch_img_prob[ind].eval() img_label_test = img_label[ind].eval() assert img_label_test == class_gt print(img_probab) print(batch_predictions[ind, :, :, i - 1]) fig, axs = plt.subplots(2, 2, figsize=(10, 10)) ## show prediction active patches ax1 = plt.subplot(2, 2, 1) ax1.set_title('Original image', {'fontsize': 8}) img_dir = Path(batch_df['Dir Path'].values[0]).__str__() img = plt.imread(img_dir) ax1.imshow(img, 'bone') ## PREDICTION ax2 = plt.subplot(2, 2, 2) ax2.set_title('Predictions: ' + batch_df.columns.values[i], {'fontsize': 8}) im2 = ax2.imshow(batch_predictions[ind, :, :, i - 1], 'BuPu', vmin=0, vmax=1) fig.colorbar(im2, ax=ax2) ax2.set_xlabel("Image prediction : " + str(img_probab)) ## LABELS ax3 = plt.subplot(2, 2, 3) # class_gt = class_label_ground_truth.eval() ax3.set_title('Labels: ' + batch_df.columns.values[i], {'fontsize': 8}) ax3.set_xlabel("Image label: " + str(class_gt) + str(img_label_test) + " Bbox available: " + str(has_segmentation)) im3 = ax3.imshow(instance_gt, vmin=0, vmax=1) fig.colorbar(im3, ax=ax3) ## BBOX of prediction and label ax4 = plt.subplot(2, 2, 4) ax4.set_title('Bounding boxes', {'fontsize': 8}) y = (np.where(instance_gt == instance_gt.max()))[0] x = (np.where(instance_gt == instance_gt.max()))[1] upper_left_x = np.min(x) # width = np.amax(x) - upper_left_x + 1 upper_left_y = np.amin(y) # height = np.amax(y) - upper_left_y + 1 # todo: to draw using pyplot img4_labels = cv2.rectangle(img, (upper_left_x * 64, upper_left_y * 64), ((np.amax(x) + 1) * 64, (np.amax(y) + 1) * 64), (0, 255, 0), 5) img4_labels = cv2.rectangle(img, (upper_left_x * 64, upper_left_y * 64), ((np.amax(x) + 1) * 64, (np.amax(y) + 1) * 64), (0, 255, 0), 5) ax4.imshow(img, 'bone') # ax4.imshow(img4_labels, 'GnBu') pred_resized = np.kron(batch_predictions[ind, :, :, i - 1], np.ones((64, 64), dtype=float)) img4_mask = ax4.imshow(pred_resized, 'BuPu', zorder=0, alpha=0.4) fig.text(0, 0, " Image prediction : " + str(img_probab) + '\n image label: ' + str(class_gt), horizontalalignment='center', verticalalignment='center', fontsize=9) plt.tight_layout() fig.savefig(results_path + get_image_index_from_pathstring(img_ind[ind]) + '_' + batch_df.columns.values[i][:-4] + '.jpg', bbox_inches='tight') plt.close(fig) ind += 1
def image_prob_active_patches(nn_output, P, class_nr): detected_active_patches = tf.cast(tf.greater(nn_output, 0.5), tf.float32) sum_detected_active_patches, _, detected_bbox = compute_ground_truth( detected_active_patches, P * P, class_nr) return compute_image_label_prediction(detected_bbox, nn_output, detected_active_patches, P, class_nr)