diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/architectures/RNN.py b/architectures/RNN.py new file mode 100644 index 0000000..3590a3f --- /dev/null +++ b/architectures/RNN.py @@ -0,0 +1,28 @@ +import torch +import torch.nn as nn + +# 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 \ No newline at end of file diff --git a/architectures/__init__.py b/architectures/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/auto_clip.py b/auto_clip.py index d79e9d1..776e350 100644 --- a/auto_clip.py +++ b/auto_clip.py @@ -9,7 +9,6 @@ """ import cv2 -import numpy as np import sys import os import datetime diff --git a/combine_cnn_output.py b/combine_cnn_output.py index 31ff5c8..cb0b2fe 100644 --- a/combine_cnn_output.py +++ b/combine_cnn_output.py @@ -1,12 +1,5 @@ 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 import sys diff --git a/data_loader.py b/data_loader.py new file mode 100644 index 0000000..ec73545 --- /dev/null +++ b/data_loader.py @@ -0,0 +1,31 @@ +import torch + +def load_data(path_to_data): + data = torch.load(path_to_data) + labels = torch.ones(data.shape[0]).unsqueeze(1) + return data, labels + +# torch.load("./no_hacks_data_tensor/clips.pt") +hacks_data, hacks_labels = load_data("./hacks_data_tensor/full_data/hacks_data_tensor_file_100.pt") +no_hacks_data, no_hacks_labels = load_data("./hacks_data_tensor/full_data/hacks_data_tensor_file_100.pt") + +# Seperating Training/Testing data into 90%/10% splits +def create_training_testing_sets(data, split=0.9): + training_data = data[:int(len(data) * split)] + testing_data = data[int(len(data) * split):] + return training_data, testing_data + +hacks_data_train, hacks_data_test = create_training_testing_sets(hacks_data) +no_hacks_data_train, no_hacks_data_test = create_training_testing_sets(no_hacks_data) +hacks_labels_train, hacks_labels_test = create_training_testing_sets(hacks_labels) +no_hacks_labels_train, no_hacks_labels_test = create_training_testing_sets(no_hacks_labels) + +def load_training_data(): + train_data = torch.cat((hacks_data_train, no_hacks_data_train)) + train_labels = torch.cat((hacks_labels_train, no_hacks_labels_train)) + return train_data, train_labels + +def load_testing_data(): + test_data = torch.cat((hacks_data_test, no_hacks_data_test)) + test_labels = torch.cat((hacks_labels_test, no_hacks_labels_test)) + return test_data, test_labels \ No newline at end of file diff --git a/old_test_rnn.py b/old_test_rnn.py index 1bee7d6..ea92a73 100644 --- a/old_test_rnn.py +++ b/old_test_rnn.py @@ -1,12 +1,5 @@ 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 diff --git a/save_cnn_output.py b/save_cnn_output.py index bcf2d77..b3dc26f 100644 --- a/save_cnn_output.py +++ b/save_cnn_output.py @@ -1,10 +1,6 @@ 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 import sys diff --git a/test_rnn.py b/test_rnn.py index ad803ef..26d5968 100644 --- a/test_rnn.py +++ b/test_rnn.py @@ -1,13 +1,6 @@ import sys 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 assert len(sys.argv) >= 2, "File requires input path" @@ -20,36 +13,6 @@ inDir = sys.argv[2] assert os.path.isdir(inDir), "not a valid directory" - - -# 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') model = torch.load("./models/model.pt") diff --git a/train_rnn.py b/train_rnn.py index 9a5f9e9..6b17f83 100644 --- a/train_rnn.py +++ b/train_rnn.py @@ -1,41 +1,7 @@ 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 +from architectures.RNN import RNN +from data_loader import load_training_data device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') @@ -56,34 +22,7 @@ def forward(self, x): criterion = nn.BCELoss() optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate) -# 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) - - -# Seperating Training/Testing data into 90%/10% splits -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)) +train_data, train_labels = load_training_data() model.train() # set model to training mode for epoch in range(num_epochs): diff --git a/validate_rnn.py b/validate_rnn.py index aac42ff..233452f 100644 --- a/validate_rnn.py +++ b/validate_rnn.py @@ -1,43 +1,6 @@ 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 - +from data_loader import load_testing_data +from architectures.RNN import RNN device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') num_classes = 1 @@ -55,30 +18,7 @@ def forward(self, x): 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)) +test_data, test_labels = load_testing_data() print(test_data.shape)