def _prepare_data(self, X, y): X_data = [] n = len(X[0]) m = len(X) for i in tqdm(range(n), desc="Loading predictions"): x_preds = [] for j in range(m): x = np.load(X[j][i]) if self.apply_softmax == "pre": x = torch.from_numpy(x).float().softmax( dim=0).numpy().astype(np.float16) x_preds.append(x) x = np.mean(np.stack(x_preds), axis=0) if self.apply_softmax == "post": x = torch.from_numpy(x).float().softmax(dim=0).numpy().astype( np.float16) X_data.append(x) Y_data = [ read_mask(yi) for yi in tqdm(y, desc="Loading ground-truths") ] assert len(X_data) == len(Y_data) print("Loaded data into memory") return X_data, Y_data
def _prepare_data(self, X, y): assert self.apply_softmax == "pre" X_data = [ to_numpy(torch.from_numpy( np.load(xi)).float().softmax(dim=0)).astype(np.float16) for xi in X ] Y_data = [read_mask(yi) for yi in y] print("Loaded data into memory") return X_data, Y_data
def test_mask_resize(): fname = "d:\\datasets\\xview2\\train\\masks\\hurricane-harvey_00000402_post_disaster.png" mask = read_mask(fname) mask2 = cv2.resize(mask, (512, 512), interpolation=cv2.INTER_NEAREST) mask3 = resize_mask_one_hot(mask, (512, 512)) cv2.imshow("Original", mask * 255) cv2.imshow("Nearest", mask2 * 255) cv2.imshow("Smart", mask3 * 255) cv2.waitKey(-1)
def test_watershed_with_image(): dmg = read_mask("test_damage_00121_prediction.png") loc = read_mask("test_localization_00121_prediction.png") img = cv2.imread("test_post_00121.png") # Fix mask dmg[loc == 0] = 0 seed = dmg.copy() seed[loc == 0] = 0 markers = cv2.watershed(img, seed.astype(int)) markers[markers == 0] = 1 plt.figure() plt.imshow(dmg) plt.show() plt.figure() plt.imshow(loc) plt.show() plt.figure() plt.imshow(markers) plt.show()
def _compute_fn(args, postprocessing_fn): xi, yi = args dmg_pred = np.load(xi) dmg_true = read_mask(yi) loc_pred, dmg_pred = postprocessing_fn(dmg_pred) if loc_pred.shape[0] != 1024: loc_pred = cv2.resize(loc_pred, dsize=(1024, 1024), interpolation=cv2.INTER_NEAREST) dmg_pred = cv2.resize(dmg_pred, dsize=(1024, 1024), interpolation=cv2.INTER_NEAREST) row = CompetitionMetricCallback.get_row_pair(loc_pred, dmg_pred, dmg_true, dmg_true) return row
def test_watershed(actual, expected): dmg = np.load(actual) dmg_true = read_mask(expected) loc_cls, dmg_cls = make_predictions_dominant_v2(dmg) plt.figure() plt.imshow(make_rgb_image(dmg_true)) plt.show() plt.figure() plt.imshow(make_rgb_image(np.argmax(dmg, axis=0))) plt.show() plt.figure() plt.imshow(make_rgb_image(loc_cls)) plt.show() plt.figure() plt.imshow(make_rgb_image(dmg_cls)) plt.show()