File size: 1,539 Bytes
43abac3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | import numpy as np
def calculate_iou(pred_mask, true_mask):
"""
Calculate Intersection over Union (IoU) for binary masks.
"""
intersection = np.logical_and(pred_mask, true_mask).sum()
union = np.logical_or(pred_mask, true_mask).sum()
if union == 0:
return 1.0 # If both are empty, perfect match
return intersection / union
def calculate_dice(pred_mask, true_mask):
"""
Calculate Dice Coefficient for binary masks.
"""
intersection = np.logical_and(pred_mask, true_mask).sum()
return (2. * intersection) / (pred_mask.sum() + true_mask.sum() + 1e-6)
def calculate_accuracy(preds, labels):
"""
Calculate simple classification accuracy.
"""
correct = (preds == labels).sum().item()
total = labels.size(0)
return correct / total
def calculate_ndvi(nir_band, red_band):
"""
Calculate NDVI given NIR and Red bands.
NDVI = (NIR - Red) / (NIR + Red)
"""
nir = nir_band.astype(float)
red = red_band.astype(float)
denominator = (nir + red)
# Avoid division by zero
denominator[denominator == 0] = 1e-6
ndvi = (nir - red) / denominator
return ndvi
def ndvi_correlation(predicted_greenery_percentages, true_mean_ndvis):
"""
Calculate Pearson correlation coefficient between predicted greenery
percentage and true mean NDVI across a set of images.
"""
if len(predicted_greenery_percentages) < 2:
return 0.0
return np.corrcoef(predicted_greenery_percentages, true_mean_ndvis)[0, 1]
|