Cam-Le/landslide
0
1import tensorflow as tf2import numpy as np3import cv24from PIL import Image5 6 7def postProcessingPixelLevelThresholding(masks_pred, threshold=0.5):8 """9 # apply thresholding10 # input:11 - masks_pred (Bx128x128x2):12 - threshold (default=0.5): smaller threshold -> more focus on landslide pixel, larger threshold -> strictly focus on landslide pixel,13 # output:14 - mask_list (Bx128x128x2):15 """16 masks_pred = masks_pred.numpy()17 c1 = masks_pred[..., 0]18 c2 = np.where(masks_pred[..., 1] > threshold, 1, 0)19 c1 = np.expand_dims(c1,axis=-1)20 c2 = np.expand_dims(c2,axis=-1)21 new_masks = np.concatenate((c1,c2), axis=-1)22 new_masks = tf.convert_to_tensor(new_masks, dtype=tf.float32)23 24 return new_masks25 26 27def postProcessingMorphology(masks_pred, op=1):28 """29 # apply morphology30 # input:31 - masks_pred (Bx128x128x2):32 # output:33 - mask_list (Bx128x128x2):34 """35 masks_pred = masks_pred.numpy()36 masks_pred = np.argmax(masks_pred, axis=-1) # Bx128x12837 masks_pred = masks_pred.astype(np.uint8)38 39 kernel = np.ones((4, 4), np.uint8)40 if op == 0: # opening - removing salt noise 41 masks_erosion = cv2.erode(masks_pred, kernel, iterations=1)42 new_masks = cv2.dilate(masks_erosion, kernel, iterations=1)43 else: # closing - removing pepper noise44 masks_dilation = cv2.dilate(masks_pred, kernel, iterations=1)45 new_masks = cv2.erode(masks_dilation, kernel, iterations=1)46 47 new_masks = tf.convert_to_tensor(new_masks, dtype=tf.uint8)48 new_masks = tf.one_hot(new_masks, depth=2, axis=-1)49 50 return tf.cast(new_masks, dtype=tf.float32)51 52 53def funcProcessingVotingMask(masks):54 masks = tf.image.resize(masks,[128,128],method=tf.image.ResizeMethod.BILINEAR)55 return np.asarray(masks)56 57def postProcessingVotingMask(masks_pred):58 """59 # apply voting60 # input:61 - masks_preds 3 x (Bx128x128x2):62 # output:63 - mask_list (Bx128x128x2):64 """65 66 y_64 = masks_pred[2] 67 y_128 = masks_pred[1]68 y_256 = masks_pred[0]69 70 y_64 = funcProcessingVotingMask(y_64)71 y_128 = np.asarray(y_128)72 y_256 = funcProcessingVotingMask(y_256)73 74 new_masks = (y_64 + y_128 + y_256)/3.75 new_masks = tf.convert_to_tensor(new_masks)76 77 return tf.cast(new_masks, dtype=tf.float32)78 79 80def postProcessingMultiAngle(y,y90,y180,y270):81 """82 # apply multi angle83 # input:84 - masks_preds 4 x 3 x (Bx128x128x2):85 # output:86 - mask_list (Bx128x128x2):87 """88 y90 = tf.image.rot90(y90, k=3)89 y180 = tf.image.rot90(y180, k=2)90 y270 = tf.image.rot90(y270, k=1)91 92 new_masks = (y + y90 + y180 + y270) / 4.93 new_masks = tf.convert_to_tensor(new_masks)94 95 return tf.cast(new_masks, dtype=tf.float32)96 