Spaces:
Sleeping
Sleeping
import torch.nn as nn | |
import torch | |
import torchvision.models as models | |
class CNNLSTMClassifier(nn.Module): | |
def __init__(self, hidden_dim=128, num_classes=2): | |
super().__init__() | |
resnet = models.resnet18(weights=models.ResNet18_Weights.DEFAULT) | |
self.cnn = nn.Sequential(*list(resnet.children())[:-1]) | |
self.cnn_out_dim = 512 | |
self.lstm = nn.LSTM(self.cnn_out_dim, hidden_dim, batch_first=True) | |
self.fc = nn.Linear(hidden_dim, num_classes) | |
def forward(self, x): | |
B, T, C, H, W = x.shape | |
x = x.view(B * T, C, H, W) | |
with torch.no_grad(): | |
cnn_out = self.cnn(x).view(B, T, -1) | |
lstm_out, _ = self.lstm(cnn_out) | |
return self.fc(lstm_out[:, -1, :]) | |