orig_test_shape, data_mask=Y_test, overlap=overlap) print("Saving predicted images . . .") save_img(Y=(preds_test > 0.5).astype(np.uint8), mask_dir=result_bin_dir_per_image, prefix="test_out_bin") save_img(Y=preds_test, mask_dir=result_no_bin_dir_per_image, prefix="test_out_no_bin") print("Calculate metrics (per image) . . .") iou_per_image = jaccard_index_numpy(Y_test, (preds_test > 0.5).astype(np.uint8)) ov_iou_per_image = voc_calculation(Y_test, (preds_test > 0.5).astype(np.uint8), iou_per_image) det_per_image = -1 print("~~~~ 16-Ensemble (per image) ~~~~") Y_test_ensemble = np.zeros(X_test.shape, dtype=np.float32) for i in tqdm(range(X_test.shape[0])): predictions_ensemble = ensemble16_3d_predictions( X_test[i], pred_func=(lambda img_batch_subdiv: model.predict(img_batch_subdiv)), n_classes=n_classes) Y_test_ensemble[i] = predictions_ensemble # Merge the volumes and convert them into 2D data Y_test_ensemble = merge_3D_data_with_overlap(Y_test_ensemble, orig_test_shape, overlap=overlap)
print("Reconstruct preds_test . . .") preds_test = merge_data_with_overlap(preds_test, orig_test_shape, crop_shape[0], test_ov_crops) print("Saving predicted images . . .") save_img(Y=(preds_test > 0.5).astype(np.uint8), mask_dir=result_bin_dir_per_image, prefix="test_out_bin") save_img(Y=preds_test, mask_dir=result_no_bin_dir_per_image, prefix="test_out_no_bin") print("Calculate metrics (per image) . . .") iou_per_image = jaccard_index_numpy(Y_test, (preds_test > 0.5).astype(np.uint8)) ov_iou_per_image = voc_calculation(Y_test, (preds_test > 0.5).astype(np.uint8), iou_per_image) det_per_image = DET_calculation(Y_test, (preds_test > 0.5).astype(np.uint8), det_eval_ge_path, det_eval_path, det_bin, n_dig, args.job_id) print("~~~~ Blending (per image) ~~~~") Y_test_blending = np.zeros(X_test.shape, dtype=np.float32) for i in tqdm(range(X_test.shape[0])): predictions_blending = predict_img_with_smooth_windowing( X_test[i], window_size=crop_shape[0], subdivisions=2, nb_classes=1, pred_func=(lambda img_batch_subdiv: model.predict(img_batch_subdiv)), softmax=softmax_out) Y_test_blending[i] = predictions_blending
preds_test = merge_data_with_overlap(preds_test, orig_test_shape, overlap=overlap) print("Saving predicted images . . .") save_img(Y=(preds_test > 0.5).astype(np.uint8), mask_dir=result_bin_dir_per_image, prefix="test_out_bin") save_img(Y=preds_test, mask_dir=result_no_bin_dir_per_image, prefix="test_out_no_bin") print("Calculate metrics (per image) . . .") iou_per_image = jaccard_index_numpy(Y_test, (preds_test > 0.5).astype(np.uint8)) ov_iou_per_image = voc_calculation(Y_test, (preds_test > 0.5).astype(np.uint8), iou_per_image) print("~~~~ Blending (per image) ~~~~") Y_test_blending = np.zeros(X_test.shape, dtype=np.float32) for i in tqdm(range(X_test.shape[0])): predictions_blending = predict_img_with_smooth_windowing( X_test[i], window_size=crop_shape[0], subdivisions=2, n_classes=n_classes, pred_func=(lambda img_batch_subdiv: model.predict(img_batch_subdiv))) if n_classes > 1: Y_test_blending[i] = np.expand_dims(np.argmax(predictions_blending, -1), axis=-1) else:
orig_Y_test = orig_Y_test.astype(np.float32) print("Saving predicted images . . .") os.makedirs(result_bin_dir_per_image, exist_ok=True) aux = np.expand_dims((orig_preds_test > 0.5).astype(np.uint8), 1) f = os.path.join(result_bin_dir_per_image, f_name) imsave(f, aux, imagej=True, metadata={'axes': 'ZCYXS'}) os.makedirs(result_no_bin_dir_per_image, exist_ok=True) aux = np.expand_dims(orig_preds_test.astype(np.uint8), 1) f = os.path.join(result_no_bin_dir_per_image, f_name) imsave(f, aux, imagej=True, metadata={'axes': 'ZCYXS'}) print("Calculate the Jaccard of the image 3D") j = jaccard_index_numpy(orig_Y_test, (orig_preds_test > 0.5).astype(np.uint8)) v = voc_calculation(orig_Y_test, (orig_preds_test > 0.5).astype(np.uint8), j) print("Image {} ; Foreground IoU: {} ; Overall IoU: {}".format(i, j, v)) iou += j ov_iou += v index += crop_3d_shape[0] del orig_preds_test print("Calculate metrics (per image) . . .") iou_per_image = iou / len(orig_test_shape) ov_iou_per_image = ov_iou / len(orig_test_shape) print("~~~~ 16-Ensemble (per image) ~~~~") Y_test_ensemble = np.zeros(Y_test.shape, dtype=np.float32) for i in tqdm(range(X_test.shape[0])): predictions_ensemble = ensemble16_3d_predictions(
print("Saving predicted images . . .") save_img(Y=(np.expand_dims(preds_test_full[..., 1], -1) > 0.5).astype( np.uint8), mask_dir=result_bin_dir_full, prefix="test_out_bin_full") save_img(Y=np.expand_dims(preds_test_full[..., 1], -1), mask_dir=result_no_bin_dir_full, prefix="test_out_no_bin_full") print("Calculate metrics (full image) . . .") iou_full = jaccard_index_numpy( Y_test, (np.expand_dims(preds_test_full[..., 1], -1) > 0.5).astype(np.uint8)) ov_iou_full = voc_calculation( Y_test, (np.expand_dims(preds_test_full[..., 1], -1) > 0.5).astype(np.uint8), iou_full) print("~~~~ 8-Ensemble (full image) ~~~~") Y_test_ensemble = np.zeros(X_test.shape, dtype=(np.float32)) for i in tqdm(range(X_test.shape[0])): pred_ensembled = ensemble8_2d_predictions( X_test[i], pred_func=(lambda img_batch_subdiv: model.predict(img_batch_subdiv)), n_classes=n_classes) Y_test_ensemble[i] = pred_ensembled print("Saving smooth predicted images . . .") save_img(Y=Y_test_ensemble, mask_dir=smo_no_bin_dir_full,
"########################\n") print("Making the predictions on test data . . .") preds_test_full = model.predict(X_test, batch_size=batch_size_value, verbose=1) if n_classes > 1: preds_test_full = np.expand_dims(np.argmax(preds_test_full, -1), -1) print("Saving predicted images . . .") save_img(Y=preds_test_full, mask_dir=result_no_bin_dir_full) print("Calculate metrics (full image) . . .") iou_full = jaccard_index_numpy(Y_test, (preds_test_full > 0.5).astype(np.uint8)) overall_IoU_full = voc_calculation(Y_test, (preds_test_full > 0.5).astype(np.uint8), iou_full) print("~~~~ 8-Ensemble (full image) ~~~~") Y_test_ensemble = np.zeros(X_test.shape, dtype=(np.float32)) for i in tqdm(range(X_test.shape[0])): pred_ensembled = ensemble8_2d_predictions( X_test[i], pred_func=(lambda img_batch_subdiv: model.predict(img_batch_subdiv)), n_classes=n_classes) Y_test_ensemble[i] = pred_ensembled print("Saving smooth predicted images . . .") save_img(Y=Y_test_ensemble, mask_dir=smo_no_bin_dir_full)