reab5555 commited on
Commit
28891a7
·
verified ·
1 Parent(s): 6663329

Update anomaly_detection.py

Browse files
Files changed (1) hide show
  1. anomaly_detection.py +87 -87
anomaly_detection.py CHANGED
@@ -1,88 +1,88 @@
1
- import torch
2
- import torch.nn as nn
3
- import torch.optim as optim
4
- import numpy as np
5
- from sklearn.preprocessing import MinMaxScaler
6
-
7
-
8
- class Autoencoder(nn.Module):
9
- def __init__(self, input_size):
10
- super(Autoencoder, self).__init__()
11
- self.encoder = nn.Sequential(
12
- nn.Linear(input_size, 256),
13
- nn.ReLU(),
14
- nn.Linear(256, 128),
15
- nn.ReLU(),
16
- nn.Linear(128, 64),
17
- nn.ReLU(),
18
- nn.Linear(64, 32)
19
- )
20
- self.decoder = nn.Sequential(
21
- nn.Linear(32, 64),
22
- nn.ReLU(),
23
- nn.Linear(64, 128),
24
- nn.ReLU(),
25
- nn.Linear(128, 256),
26
- nn.ReLU(),
27
- nn.Linear(256, input_size)
28
- )
29
-
30
- def forward(self, x):
31
- batch_size, seq_len, _ = x.size()
32
- x = x.view(batch_size * seq_len, -1)
33
- encoded = self.encoder(x)
34
- decoded = self.decoder(encoded)
35
- return decoded.view(batch_size, seq_len, -1)
36
-
37
- def anomaly_detection(X_embeddings, X_posture, epochs=200, patience=5):
38
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
39
-
40
- # Normalize posture
41
- scaler_posture = MinMaxScaler()
42
- X_posture_scaled = scaler_posture.fit_transform(X_posture.reshape(-1, 1))
43
-
44
- # Process facial embeddings
45
- X_embeddings = torch.FloatTensor(X_embeddings).to(device)
46
- if X_embeddings.dim() == 2:
47
- X_embeddings = X_embeddings.unsqueeze(0)
48
-
49
- # Process posture
50
- X_posture_scaled = torch.FloatTensor(X_posture_scaled).to(device)
51
- if X_posture_scaled.dim() == 2:
52
- X_posture_scaled = X_posture_scaled.unsqueeze(0)
53
-
54
- model_embeddings = Autoencoder(input_size=X_embeddings.shape[2]).to(device)
55
- model_posture = Autoencoder(input_size=X_posture_scaled.shape[2]).to(device)
56
-
57
- criterion = nn.MSELoss()
58
- optimizer_embeddings = optim.Adam(model_embeddings.parameters())
59
- optimizer_posture = optim.Adam(model_posture.parameters())
60
-
61
- # Train models
62
- for epoch in range(epochs):
63
- for model, optimizer, X in [(model_embeddings, optimizer_embeddings, X_embeddings),
64
- (model_posture, optimizer_posture, X_posture_scaled)]:
65
- model.train()
66
- optimizer.zero_grad()
67
- output = model(X)
68
- loss = criterion(output, X)
69
- loss.backward()
70
- optimizer.step()
71
-
72
- # Compute MSE for embeddings and posture
73
- model_embeddings.eval()
74
- model_posture.eval()
75
- with torch.no_grad():
76
- reconstructed_embeddings = model_embeddings(X_embeddings).cpu().numpy()
77
- reconstructed_posture = model_posture(X_posture_scaled).cpu().numpy()
78
-
79
- mse_embeddings = np.mean(np.power(X_embeddings.cpu().numpy() - reconstructed_embeddings, 2), axis=2).squeeze()
80
- mse_posture = np.mean(np.power(X_posture_scaled.cpu().numpy() - reconstructed_posture, 2), axis=2).squeeze()
81
-
82
- return mse_embeddings, mse_posture
83
-
84
- def determine_anomalies(mse_values, threshold):
85
- mean = np.mean(mse_values)
86
- std = np.std(mse_values)
87
- anomalies = mse_values > (mean + threshold * std)
88
  return anomalies
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import torch.optim as optim
4
+ import numpy as np
5
+ from sklearn.preprocessing import MinMaxScaler
6
+
7
+ @spaces.GPU(duration=300)
8
+ class Autoencoder(nn.Module):
9
+ def __init__(self, input_size):
10
+ super(Autoencoder, self).__init__()
11
+ self.encoder = nn.Sequential(
12
+ nn.Linear(input_size, 256),
13
+ nn.ReLU(),
14
+ nn.Linear(256, 128),
15
+ nn.ReLU(),
16
+ nn.Linear(128, 64),
17
+ nn.ReLU(),
18
+ nn.Linear(64, 32)
19
+ )
20
+ self.decoder = nn.Sequential(
21
+ nn.Linear(32, 64),
22
+ nn.ReLU(),
23
+ nn.Linear(64, 128),
24
+ nn.ReLU(),
25
+ nn.Linear(128, 256),
26
+ nn.ReLU(),
27
+ nn.Linear(256, input_size)
28
+ )
29
+
30
+ def forward(self, x):
31
+ batch_size, seq_len, _ = x.size()
32
+ x = x.view(batch_size * seq_len, -1)
33
+ encoded = self.encoder(x)
34
+ decoded = self.decoder(encoded)
35
+ return decoded.view(batch_size, seq_len, -1)
36
+
37
+ def anomaly_detection(X_embeddings, X_posture, epochs=200, patience=5):
38
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
39
+
40
+ # Normalize posture
41
+ scaler_posture = MinMaxScaler()
42
+ X_posture_scaled = scaler_posture.fit_transform(X_posture.reshape(-1, 1))
43
+
44
+ # Process facial embeddings
45
+ X_embeddings = torch.FloatTensor(X_embeddings).to(device)
46
+ if X_embeddings.dim() == 2:
47
+ X_embeddings = X_embeddings.unsqueeze(0)
48
+
49
+ # Process posture
50
+ X_posture_scaled = torch.FloatTensor(X_posture_scaled).to(device)
51
+ if X_posture_scaled.dim() == 2:
52
+ X_posture_scaled = X_posture_scaled.unsqueeze(0)
53
+
54
+ model_embeddings = Autoencoder(input_size=X_embeddings.shape[2]).to(device)
55
+ model_posture = Autoencoder(input_size=X_posture_scaled.shape[2]).to(device)
56
+
57
+ criterion = nn.MSELoss()
58
+ optimizer_embeddings = optim.Adam(model_embeddings.parameters())
59
+ optimizer_posture = optim.Adam(model_posture.parameters())
60
+
61
+ # Train models
62
+ for epoch in range(epochs):
63
+ for model, optimizer, X in [(model_embeddings, optimizer_embeddings, X_embeddings),
64
+ (model_posture, optimizer_posture, X_posture_scaled)]:
65
+ model.train()
66
+ optimizer.zero_grad()
67
+ output = model(X)
68
+ loss = criterion(output, X)
69
+ loss.backward()
70
+ optimizer.step()
71
+
72
+ # Compute MSE for embeddings and posture
73
+ model_embeddings.eval()
74
+ model_posture.eval()
75
+ with torch.no_grad():
76
+ reconstructed_embeddings = model_embeddings(X_embeddings).cpu().numpy()
77
+ reconstructed_posture = model_posture(X_posture_scaled).cpu().numpy()
78
+
79
+ mse_embeddings = np.mean(np.power(X_embeddings.cpu().numpy() - reconstructed_embeddings, 2), axis=2).squeeze()
80
+ mse_posture = np.mean(np.power(X_posture_scaled.cpu().numpy() - reconstructed_posture, 2), axis=2).squeeze()
81
+
82
+ return mse_embeddings, mse_posture
83
+
84
+ def determine_anomalies(mse_values, threshold):
85
+ mean = np.mean(mse_values)
86
+ std = np.std(mse_values)
87
+ anomalies = mse_values > (mean + threshold * std)
88
  return anomalies