-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
61 lines (50 loc) · 2.17 KB
/
model.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
import torch
import torch.nn as nn
from torchvision import models
class CNNLSTM(nn.Module):
def __init__(self, num_classes, hidden_size, num_lstm_layers=2, use_pretrained=True):
super(CNNLSTM, self).__init__()
# Choose the CNN backbone
if use_pretrained:
# Use pretrained ResNet18
self.backbone = models.resnet18(pretrained=True)
# Replace the final fully connected layer of ResNet18
self.backbone.fc = nn.Identity()
else:
self.backbone = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2, padding=1),
nn.Dropout(0.25), # Added dropout after the first MaxPool
nn.Conv2d(64, 128, kernel_size=3, stride=2, padding=1, bias=False),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True),
nn.Dropout(0.25), # Added dropout after the first MaxPool
nn.Conv2d(128, 256, kernel_size=3, stride=2, padding=1, bias=False),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
nn.Dropout(0.25), # Added dropout after the first MaxPool
nn.Conv2d(256, 512, kernel_size=3, stride=2, padding=1, bias=False),
nn.BatchNorm2d(512),
nn.ReLU(inplace=True),
nn.Dropout(0.5),
nn.AdaptiveAvgPool2d((1, 1))
)
# LSTM
self.lstm = nn.LSTM(512, hidden_size, num_lstm_layers, batch_first=True)
# Final classifier
self.fc = nn.Linear(hidden_size, num_classes)
def forward(self, x):
# x: batch, num_frames, channels, height, width
batch, num_frames, c, h, w = x.shape
# Process each frame with the CNN
x = x.view(batch * num_frames, c, h, w)
x = self.backbone(x)
# Reshape the output for the LSTM
x = x.view(batch, num_frames, -1)
# LSTM forward
x, (h_n, c_n) = self.lstm(x)
# Classifier
x = self.fc(h_n[-1, ...])
return x