csaybar's picture
Upload 14 files
dc938c7 verified
raw
history blame
1.28 kB
import segmentation_models_pytorch as smp
import sklearn.metrics
import torch
import timm
# -- Replace with your data --
x = torch.randn(1, 13, 512, 512) # S2 L1C image
y = torch.randint(0, 4, (1, 512, 512)).numpy() # Target
# -- Load the segmentation model - UNetMobV2 --
segmodel = smp.Unet(
encoder_name="mobilenet_v2",
encoder_weights=None,
in_channels=13,
classes=4
)
segmodel.load_state_dict(torch.load("models/UNetMobV2.pt"))
segmodel.eval()
# -- Predict the cloud mask --
with torch.no_grad():
yhat = segmodel(x)
cloudmask = torch.argmax(yhat, dim=1).cpu().numpy().squeeze()
# -- Predict the trustworthiness index (TI) --
ti_index = sklearn.metrics.fbeta_score(
y_true=y.flatten(),
y_pred=cloudmask.flatten(),
beta=2.0,
average="macro"
)
# -- Load the hardness index (HI) model --
hi_model = timm.create_model(
model_name="resnet10t",
pretrained=True,
num_classes=1,
in_chans=13
)
hi_model.load_state_dict(torch.load("models/resnet10.pt"))
hi_model.eval()
# -- Estimate the hardness index (HI) --
with torch.no_grad():
y = hi_model(x)
hi_index = torch.sigmoid(y).cpu().numpy().squeeze().item()
# -- Decision making --
if (ti_index < 0.3) & (hi_index > 0.5):
perror = 1
else:
perror = 0