-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprithvi.py
130 lines (118 loc) · 4.19 KB
/
prithvi.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import torch
import torch.nn.functional as F
import pytorch_lightning as pl
from terratorch.models import PrithviModelFactory
from terratorch.datasets import HLSBands
from torchmetrics import JaccardIndex
from sklearn.metrics import precision_recall_fscore_support
class PrithviSemanticSegmentation(pl.LightningModule):
def __init__(
self,
num_classes,
in_channels,
num_frames,
decoder_num_convs,
img_size,
learning_rate,
):
super().__init__()
model_factory = PrithviModelFactory()
self.model = model_factory.build_model(
task="segmentation",
backbone="prithvi_vit_100",
decoder="FCNDecoder",
decoder_num_convs=decoder_num_convs,
in_channels=in_channels,
bands=[
HLSBands.BLUE,
HLSBands.GREEN,
HLSBands.RED,
HLSBands.NIR_NARROW,
HLSBands.SWIR_1,
HLSBands.SWIR_2,
],
num_classes=num_classes,
pretrained=True,
num_frames=num_frames,
head_dropout=0.0,
img_size=img_size,
)
self.learning_rate = learning_rate
for param in self.model.encoder.parameters():
param.requires_grad = False
self.jaccard_index = JaccardIndex(task="multiclass", num_classes=num_classes)
def training_step(self, batch, batch_idx):
x, y = batch
model_output = self.model(x)
mask = model_output.output
loss = F.cross_entropy(mask, y)
self.log("train/loss", loss, prog_bar=True, on_step=True, on_epoch=True)
pred = torch.argmax(mask, dim=1)
iou = self.jaccard_index(pred, y)
self.log("train/iou", iou, on_step=False, on_epoch=True)
y_flat = y.flatten().cpu()
pred_flat = pred.flatten().cpu()
precision_macro, recall_macro, f1_macro, _ = precision_recall_fscore_support(
y_flat, pred_flat, average="macro"
)
self.log_dict(
{
"train/precision_macro": precision_macro,
"train/recall_macro": recall_macro,
"train/f1_macro": f1_macro,
},
on_step=False,
on_epoch=True,
)
precision_weighted, recall_weighted, f1_weighted, _ = (
precision_recall_fscore_support(y_flat, pred_flat, average="weighted")
)
self.log_dict(
{
"train/precision_weighted": precision_weighted,
"train/recall_weighted": recall_weighted,
"train/f1_weighted": f1_weighted,
},
on_step=False,
on_epoch=True,
)
return loss
def validation_step(self, batch, batch_idx):
x, y = batch
model_output = self.model(x)
mask = model_output.output
loss = F.cross_entropy(mask, y)
self.log("val/loss", loss, prog_bar=True, on_step=False, on_epoch=True)
pred = torch.argmax(mask, dim=1)
iou = self.jaccard_index(pred, y)
self.log("val/iou", iou, on_step=False, on_epoch=True)
y_flat = y.flatten().cpu()
pred_flat = pred.flatten().cpu()
precision_macro, recall_macro, f1_macro, _ = precision_recall_fscore_support(
y_flat, pred_flat, average="macro"
)
self.log_dict(
{
"val/precision_macro": precision_macro,
"val/recall_macro": recall_macro,
"val/f1_macro": f1_macro,
},
on_step=False,
on_epoch=True,
)
precision_weighted, recall_weighted, f1_weighted, _ = (
precision_recall_fscore_support(y_flat, pred_flat, average="weighted")
)
self.log_dict(
{
"val/precision_weighted": precision_weighted,
"val/recall_weighted": recall_weighted,
"val/f1_weighted": f1_weighted,
},
on_step=False,
on_epoch=True,
)
return loss
def configure_optimizers(self):
optimizer = torch.optim.Adam(self.parameters(), lr=self.learning_rate)
return optimizer