File size: 1,282 Bytes
dc938c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
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