Spaces:
Sleeping
Sleeping
Create model.py
Browse files
model.py
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch.nn as nn
|
2 |
+
import torchvision.models as models
|
3 |
+
|
4 |
+
class CNNLSTMClassifier(nn.Module):
|
5 |
+
def __init__(self, hidden_dim=128, num_classes=2):
|
6 |
+
super().__init__()
|
7 |
+
resnet = models.resnet18(weights=models.ResNet18_Weights.DEFAULT)
|
8 |
+
self.cnn = nn.Sequential(*list(resnet.children())[:-1])
|
9 |
+
self.cnn_out_dim = 512
|
10 |
+
self.lstm = nn.LSTM(self.cnn_out_dim, hidden_dim, batch_first=True)
|
11 |
+
self.fc = nn.Linear(hidden_dim, num_classes)
|
12 |
+
|
13 |
+
def forward(self, x):
|
14 |
+
B, T, C, H, W = x.shape
|
15 |
+
x = x.view(B * T, C, H, W)
|
16 |
+
with torch.no_grad():
|
17 |
+
cnn_out = self.cnn(x).view(B, T, -1)
|
18 |
+
lstm_out, _ = self.lstm(cnn_out)
|
19 |
+
return self.fc(lstm_out[:, -1, :])
|