feat: 0.38 local

This commit is contained in:
Yadunand Prem 2024-04-28 17:40:37 +08:00
parent 2149406885
commit 9702f0e3eb
No known key found for this signature in database

View File

@ -21,31 +21,44 @@ import os
class CNN3D(nn.Module):
def __init__(self):
super(CNN3D, self).__init__()
self.conv1 = nn.Conv3d(1, 12, 2, 1, 2)
self.mp = nn.AvgPool3d(2)
self.relu = nn.LeakyReLU()
self.fc1 = nn.Linear(3888, 6)
self.fc2 = nn.Linear(128, 6)
self.flatten = nn.Flatten()
self.conv1 = nn.Conv3d(1, 16, 2, 1, 2)
self.batchnorm3d = nn.BatchNorm3d(16)
self.batchnorm1d = nn.BatchNorm1d(64)
self.dropout = nn.Dropout(0.5)
self.mp3d = nn.AvgPool3d(2)
self.relu = nn.ReLU()
self.lstm = nn.LSTM(5184, 64, 1, batch_first=True)
self.fc2 = nn.Linear(64, 6)
def forward(self, x):
x = self.conv1(x)
x = self.mp(x)
x = self.mp3d(x)
x = self.batchnorm3d(x)
x = self.relu(x)
x = self.dropout(x)
x = x.view(-1, 5184)
# print(x.shape)
x = x.view(-1, 3888)
x = self.fc1(x)
# x = self.fc2(x)
return x
x, _ = self.lstm(x)
# print(x.shape)
x = self.batchnorm1d(x)
x = self.relu(x)
x = self.dropout(x)
x = self.fc2(x)
return torch.softmax(x, dim=1)
def train(model, criterion, optimizer, loader, epochs=10):
def train(model, criterion, optimizer, loader, epochs=20):
for epoch in range(epochs):
for idx, (inputs, labels) in enumerate(loader):
optimizer.zero_grad()
outputs = model(inputs)
# print(outputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
@ -90,22 +103,23 @@ class Model():
X, y = process_data(X, y)
train_dataset = torch.utils.data.TensorDataset(X, y)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=32, shuffle=True)
train(self.model, self.criterion, self.optimizer, train_loader)
train(self.model, self.criterion, self.optimizer, train_loader, 10)
def predict(self, X):
self.model.eval()
X = np.array([video[:6] for video in X])
tensor_videos = torch.tensor(X, dtype=torch.float32)
# Clip values to 0 and 255
tensor_videos = np.clip(tensor_videos, 0, 255)
# Replace NaNs in each frame, with the average of the frame. This was generated with GPT
for i in range(tensor_videos.shape[0]):
for j in range(tensor_videos.shape[1]):
tensor_videos[i][j][torch.isnan(tensor_videos[i][j])] = torch.mean(
tensor_videos[i][j][~torch.isnan(tensor_videos[i][j])])
X = torch.Tensor(tensor_videos.unsqueeze(1))
return np.argmax(self.model(X).detach().numpy(), axis=1)
with torch.no_grad():
X = np.array([video[:6] for video in X])
tensor_videos = torch.tensor(X, dtype=torch.float32)
# Clip values to 0 and 255
tensor_videos = np.clip(tensor_videos, 0, 255)
# Replace NaNs in each frame, with the average of the frame. This was generated with GPT
for i in range(tensor_videos.shape[0]):
for j in range(tensor_videos.shape[1]):
tensor_videos[i][j][torch.isnan(tensor_videos[i][j])] = torch.mean(
tensor_videos[i][j][~torch.isnan(tensor_videos[i][j])])
X = torch.Tensor(tensor_videos.unsqueeze(1))
result = self.model(X)
return torch.max(result, dim=1)[1].numpy()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1)