-
Notifications
You must be signed in to change notification settings - Fork 10
/
validate_rnn.py
104 lines (71 loc) · 3.25 KB
/
validate_rnn.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
import numpy as np
import os
import cv2
# Defining LSTM RNN
class RNN(nn.Module):
def __init__(self, input_size, hidden_size, num_layers, num_classes):
super(RNN, self).__init__() # inheriting from existing RNN class
self.num_layers = num_layers # number of input layers
self.hidden_size = hidden_size # number of hidden players
self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True) # creating LSTM layer
self.fc = nn.Linear(hidden_size, num_classes) # creating linear output layer
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# x -> (batch_size, seq_size, input_size)
def forward(self, x):
h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(self.device)
c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(self.device)
out, _ = self.lstm(x, (h0, c0))
# out -> (batch_size, seq_size, input_size) = (N, 50, 512)
out = out[:, -1, :]
# out -> (N, 512)
out = self.fc(out)
return torch.sigmoid(out) # returning one forward step of the NN
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
num_classes = 1
num_epochs = 100
learning_rate = 0.001
input_size = 512
sequence_length = 50
hidden_size = 512
num_layers = 2
# model = RNN(input_size, hidden_size, num_layers, num_classes).to(device)
model = torch.load("./models/model.pt")
# Loading the model
hacks_data = torch.load("./hacks_data_tensor/clips.pt")
hacks_labels = torch.ones(hacks_data.shape[0]).unsqueeze(1)
no_hacks_data = torch.load("./no_hacks_data_tensor/clips.pt")
no_hacks_labels = torch.zeros(no_hacks_data.shape[0]).unsqueeze(1)
hacks_data_train = hacks_data[:int(len(hacks_data) * 0.9)]
hacks_data_test = hacks_data[int(len(hacks_data) * 0.9):]
no_hacks_data_train = no_hacks_data[:int(len(no_hacks_data) * 0.9)]
no_hacks_data_test = no_hacks_data[int(len(no_hacks_data) * 0.9):]
hacks_labels_train = hacks_labels[:int(len(hacks_labels) * 0.9)]
hacks_labels_test = hacks_labels[int(len(hacks_labels) * 0.9):]
no_hacks_labels_train = no_hacks_labels[:int(len(no_hacks_labels) * 0.9)]
no_hacks_labels_test = no_hacks_labels[int(len(no_hacks_labels) * 0.9):]
# train_data = torch.cat((hacks_data_train, no_hacks_data_train))
# train_labels = torch.cat((hacks_labels_train, no_hacks_labels_train))
test_data = torch.cat((hacks_data_test, no_hacks_data_test))
test_labels = torch.cat((hacks_labels_test, no_hacks_labels_test))
print(test_data.shape)
model.eval() # Model is set to evaluate
# Checking for whether clips are accurately predicted or not
with torch.no_grad():
n_correct = 0
n_samples = 0
for i in range(len(test_data)):
curr_test = test_data[i].unsqueeze(0)
curr_label = test_labels[i][0].item()
outputs = model(curr_test)
if(abs(outputs.item()-curr_label) < 0.5):
print("success" + str(outputs.item()))
n_correct += 1
n_samples += 1
print(n_samples)
print("percentage correct: " + str(n_correct/n_samples * 100.))