File size: 6,742 Bytes
ab687e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
from pl_bolts.models.vision.unet import UNet
from pytorch_lightning import LightningModule
from pytorch_lightning.utilities.cli import MODEL_REGISTRY

import torch
from torch.nn import functional as F
from torchmetrics import MetricCollection, Accuracy, IoU


# -------------------------------------------------------------------------------
# class UNet
# This class performs training and classification of satellite imagery using a
# UNet CNN.
# -------------------------------------------------------------------------------
@MODEL_REGISTRY
class UNetSegmentation(LightningModule):

    # ---------------------------------------------------------------------------
    # __init__
    # ---------------------------------------------------------------------------
    def __init__(
        self,
        input_channels: int = 4,
        num_classes: int = 19,
        num_layers: int = 5,
        features_start: int = 64,
        bilinear: bool = False,
    ):
        super().__init__()

        self.input_channels = input_channels
        self.num_classes = num_classes
        self.num_layers = num_layers
        self.features_start = features_start
        self.bilinear = bilinear

        self.net = UNet(
            input_channels=self.input_channels,
            num_classes=num_classes,
            num_layers=self.num_layers,
            features_start=self.features_start,
            bilinear=self.bilinear,
        )

        metrics = MetricCollection(
            [
                Accuracy(), IoU(num_classes=self.num_classes)
            ]
        )
        self.train_metrics = metrics.clone(prefix='train_')
        self.val_metrics = metrics.clone(prefix='val_')

    # ---------------------------------------------------------------------------
    # model methods
    # ---------------------------------------------------------------------------
    def forward(self, x):
        return self.net(x)

    def training_step(self, batch, batch_nb):
        img, mask = batch
        img, mask = img.float(), mask.long()

        # Forward step, calculate logits and loss
        logits = self(img)
        # loss_val = F.cross_entropy(logits, mask)

        # Get target tensor from logits for metrics, calculate metrics
        probs = torch.nn.functional.softmax(logits, dim=1)
        probs = torch.argmax(probs, dim=1)

        # metrics_train = self.train_metrics(probs, mask)
        # log_dict = {"train_loss": loss_val.detach()}
        # return {"loss": loss_val, "log": log_dict, "progress_bar": log_dict}
        # return {
        #    "loss": loss_val, "train_acc": metrics_train['train_Accuracy'],
        #    "train_iou": metrics_train['train_IoU']
        # }

        tensorboard_logs = self.train_metrics(probs, mask)
        tensorboard_logs['loss'] = F.cross_entropy(logits, mask)
        # tensorboard_logs['lr'] = self._get_current_lr()

        self.log(
            'acc', tensorboard_logs['train_Accuracy'],
            sync_dist=True, prog_bar=True
        )
        self.log(
            'iou', tensorboard_logs['train_IoU'],
            sync_dist=True, prog_bar=True
        )
        return tensorboard_logs

    def training_epoch_end(self, outputs):
        pass

        # Get average metrics from multi-GPU batch sources
        # loss_val = torch.stack([x["loss"] for x in outputs]).mean()
        # acc_train = torch.stack([x["train_acc"] for x in outputs]).mean()
        # iou_train = torch.stack([x["train_iou"] for x in outputs]).mean()

        # tensorboard_logs = self.train_metrics(probs, mask)
        # tensorboard_logs['loss'] = F.cross_entropy(logits, mask)
        # tensorboard_logs['lr'] = self._get_current_lr()

        # self.log(
        #     'acc', tensorboard_logs['train_Accuracy'],
        #     sync_dist=True, prog_bar=True
        # )
        # self.log(
        #     'iou', tensorboard_logs['train_IoU'],
        #     sync_dist=True, prog_bar=True
        # )
        #    # Send output to logger
        #    self.log(
        #        "loss", loss_val, on_epoch=True, prog_bar=True, logger=True)
        #    self.log(
        #        "train_acc", acc_train,
        #        on_epoch=True, prog_bar=True, logger=True)
        #    self.log(
        #        "train_iou", iou_train,
        #        on_epoch=True, prog_bar=True, logger=True)
        # return tensorboard_logs

    def validation_step(self, batch, batch_idx):

        # Get data, change type for validation
        img, mask = batch
        img, mask = img.float(), mask.long()

        # Forward step, calculate logits and loss
        logits = self(img)
        # loss_val = F.cross_entropy(logits, mask)

        # Get target tensor from logits for metrics, calculate metrics
        probs = torch.nn.functional.softmax(logits, dim=1)
        probs = torch.argmax(probs, dim=1)
        # metrics_val = self.val_metrics(probs, mask)

        # return {
        #    "val_loss": loss_val, "val_acc": metrics_val['val_Accuracy'],
        #    "val_iou": metrics_val['val_IoU']
        # }
        tensorboard_logs = self.val_metrics(probs, mask)
        tensorboard_logs['val_loss'] = F.cross_entropy(logits, mask)

        self.log(
            'val_loss', tensorboard_logs['val_loss'],
            sync_dist=True, prog_bar=True
        )
        self.log(
            'val_acc', tensorboard_logs['val_Accuracy'],
            sync_dist=True, prog_bar=True
        )
        self.log(
            'val_iou', tensorboard_logs['val_IoU'],
            sync_dist=True, prog_bar=True
        )
        return tensorboard_logs

    # def validation_epoch_end(self, outputs):

    #    # Get average metrics from multi-GPU batch sources
    #    loss_val = torch.stack([x["val_loss"] for x in outputs]).mean()
    #    acc_val = torch.stack([x["val_acc"] for x in outputs]).mean()
    #    iou_val = torch.stack([x["val_iou"] for x in outputs]).mean()

    #    # Send output to logger
    #    self.log(
    #        "val_loss", torch.mean(self.all_gather(loss_val)),
    #        on_epoch=True, prog_bar=True, logger=True)
    #    self.log(
    #        "val_acc", torch.mean(self.all_gather(acc_val)),
    #        on_epoch=True, prog_bar=True, logger=True)
    #    self.log(
    #        "val_iou", torch.mean(self.all_gather(iou_val)),
    #        on_epoch=True, prog_bar=True, logger=True)

    # def configure_optimizers(self):
    #    opt = torch.optim.Adam(self.net.parameters(), lr=self.lr)
    #    sch = torch.optim.lr_scheduler.CosineAnnealingLR(opt, T_max=10)
    #    return [opt], [sch]

    def test_step(self, batch, batch_idx, dataloader_idx=0):
        return self(batch)

    def predict_step(self, batch, batch_idx, dataloader_idx=0):
        return self(batch)