Update app.py
Browse files189 unmodified lines19019119219319419528 unmodified lines224225226227228229230189 unmodified linesnp.array(train_sar_aug), np.array(train_optic_aug), np.array(train_masks_aug),np.array(test_sar), np.array(test_optic), np.array(test_masks))# Streamlit App Titlest.title("Satellite Mining Segmentation: SAR + Optic Image Inference")28 unmodified linesoptic_images = dataset.normalizeImages(optic_images, 'i')# Load modelmodel = load_model(model_path)# Predictpred_masks = model.predict([optic_images, sar_images], verbose=0)189 unmodified lines19019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323428 unmodified lines263264265266267268269270189 unmodified linesnp.array(train_sar_aug), np.array(train_optic_aug), np.array(train_masks_aug),np.array(test_sar), np.array(test_optic), np.array(test_masks))@tf.keras.saving.register_keras_serializable()def dice_score(y_true, y_pred, threshold=0.5, smooth=1.0):#determine binary or multiclass segmentationis_multiclass = y_true.shape[-1] > 1if not is_multiclass:# Binary segmentationy_true_flat = tf.cast(tf.reshape(y_true, [-1]), dtype=tf.float32)y_pred_flat = tf.cast(tf.reshape(y_pred >= threshold, [-1]), dtype=tf.float32)intersection = tf.reduce_sum(y_true_flat * y_pred_flat)score = (2. * intersection + smooth) / (tf.reduce_sum(y_true_flat) + tf.reduce_sum(y_pred_flat) + smooth)return scoreelse:# Multiclass segmentationnum_classes = y_true.shape[-1]score_per_class = []for i in range(num_classes):y_true_flat = tf.cast(tf.reshape(y_true, [-1]), dtype=tf.float32)y_pred_flat = tf.cast(tf.reshape(y_pred >= threshold, [-1]), dtype=tf.float32)intersection = tf.reduce_sum(y_true_flat * y_pred_flat)score = (2. * intersection + smooth) / (tf.reduce_sum(y_true_flat) + tf.reduce_sum(y_pred_flat) + smooth)score_per_class.append(score)return tf.reduce_mean(score_per_class)@tf.keras.saving.register_keras_serializable()def dice_loss(y_true, y_pred):dice = dice_score(y_true, y_pred)loss = 1. - dicereturn tf.cast(loss, dtype=tf.float32)@tf.keras.saving.register_keras_serializable()def cce_dice_loss(y_true, y_pred):cce = tf.keras.losses.CategoricalCrossentropy()(y_true, y_pred)dice = dice_loss(y_true, y_pred)return tf.cast(cce, dtype=tf.float32) + dice# Streamlit App Titlest.title("Satellite Mining Segmentation: SAR + Optic Image Inference")28 unmodified linesoptic_images = dataset.normalizeImages(optic_images, 'i')# Load modelmodel = tf.keras.models.load_model(model_path,custom_objects={"cce_dice_loss": cce_dice_loss, "dice_score": dice_score})# Predictpred_masks = model.predict([optic_images, sar_images], verbose=0)
app.py+41-1