Created
June 30, 2023 10:15
-
-
Save srmsoumya/51969adf651c66bf73f4fcd573428d15 to your computer and use it in GitHub Desktop.
Model code for time series LULC classification.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
import torch.nn as nn | |
import torch.nn.functional as F | |
def _conv(ni, nf, ks): | |
return nn.Conv1d(ni, nf, kernel_size=ks, stride=1, padding=ks // 2) | |
def _conv_layer(ni, nf, ks, drop=0.0): | |
return nn.Sequential( | |
_conv(ni, nf, ks), nn.BatchNorm1d(nf), nn.ReLU(), nn.Dropout(p=drop) | |
) | |
def stacked_conv(ni, nf, ks, drop): | |
return nn.Sequential( | |
_conv_layer(ni, nf, ks, drop=drop), | |
nn.MaxPool1d(kernel_size=2, stride=2), | |
_conv_layer(nf, nf, ks, drop=0.0), | |
# nn.AdaptiveAvgPool1d(output_size=1), | |
nn.Flatten(), | |
) | |
class Pixel(nn.Module): | |
def __init__(self, num_bands, num_classes, timestep, hidden_dims, drop): | |
super().__init__() | |
self.bn = nn.BatchNorm1d(num_features=num_bands) | |
self.conv1 = stacked_conv(ni=num_bands, nf=hidden_dims, ks=3, drop=drop) | |
self.conv2 = stacked_conv(ni=num_bands, nf=hidden_dims, ks=5, drop=drop) | |
self.head = nn.Sequential( | |
nn.Linear( | |
in_features=2 * (hidden_dims * (timestep // 2)), | |
out_features=2 * hidden_dims, | |
), | |
nn.BatchNorm1d(2 * hidden_dims), | |
nn.ReLU(), | |
nn.Linear(in_features=2 * hidden_dims, out_features=num_classes), | |
) | |
self.drop = drop | |
def forward(self, x): | |
# normalize the input | |
x = self.bn(x) | |
x1 = self.conv1(x) | |
x2 = self.conv2(x) | |
# bs x (2 * nf) | |
x3 = torch.cat([x1, x2], dim=1) | |
x3 = F.dropout(x3, p=self.drop) | |
logits = self.head(x3) | |
return logits |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment