-
Notifications
You must be signed in to change notification settings - Fork 0
/
KTH_VideoBlockClassifier.py
84 lines (73 loc) · 2.83 KB
/
KTH_VideoBlockClassifier.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import torch
import torch.nn as nn
import pytorch_lightning as pl
from torch.optim import Adam, SGD
from torch.nn import functional as F
class KTH_VideoBlockClassifier(pl.LightningModule):
def __init__(self):
super(KTH_VideoBlockClassifier, self).__init__()
# input video block shape (1,15,120,160) - with N (batch)
self.conv1 = nn.Sequential(
nn.Conv3d(1, 16, kernel_size=(4, 5, 5)), # output Size (16,12,116,156)
nn.BatchNorm3d(16),
nn.ReLU(),
nn.MaxPool3d(kernel_size=(1, 2, 2)), # output Size (16,12,58,78)
nn.Dropout(0.5))
self.conv2 = nn.Sequential(
nn.Conv3d(16, 32, kernel_size=(4, 3, 3)),
nn.BatchNorm3d(32),
nn.ReLU(),
nn.MaxPool3d(kernel_size=(2, 2, 2)), # output Size (32,4,28,38)
nn.Dropout(0.5))
self.conv3 = nn.Sequential(
nn.Conv3d(32, 64, kernel_size=(3, 3, 3)),
nn.BatchNorm3d(64),
nn.ReLU(),
nn.MaxPool3d(kernel_size=(2, 2, 2)), # output Size (64,1,13,18)
nn.Dropout(0.5))
self.fc1 = nn.Linear(64 * 1 * 13 * 18, 128)
self.dropfc1 = nn.Dropout(0.5)
self.fc2 = nn.Linear(128, 6)
def forward(self, x):
out = self.conv1(x)
out = self.conv2(out)
out = self.conv3(out)
out = out.view(out.size(0), -1)
out = self.fc1(out)
out = nn.ReLU()(out)
out = self.dropfc1(out)
out = self.fc2(out)
return out
def configure_optimizers(self):
optimizer = torch.optim.Adam(self.parameters(), lr=1e-3)
return optimizer
def cross_entropy_loss(self, logits, labels):
return F.nll_loss(logits, labels)
def training_step(self, train_batch, batch_idx):
x, y = train_batch
logits = self.forward(x)
loss = self.cross_entropy_loss(logits, y)
self.log('train_loss', loss)
return loss
def validation_step(self, val_batch, batch_idx):
x, y = val_batch
logits = self.forward(x)
loss = self.cross_entropy_loss(logits, y)
score, y_pred = torch.max(logits, 1)
accuracy = torch.sum(y == y_pred).item() / (len(y) * 1.0)
output = dict({
'test_loss': loss,
'test_acc': torch.tensor(accuracy),
})
return output
def test_step(self, test_batch, batch_idx):
x, y = test_batch
logits = self.forward(x)
loss = self.cross_entropy_loss(logits, y)
score, y_pred = torch.max(logits, 1)
accuracy = torch.sum(y == y_pred).item() / (len(y) * 1.0)
output = dict({
'test_loss': loss,
'test_acc': torch.tensor(accuracy),
})
return output