Update app.py
Browse files
app.py
CHANGED
@@ -235,8 +235,8 @@ class LSTMAutoencoder(nn.Module):
|
|
235 |
self.fc = nn.Linear(hidden_size, input_size)
|
236 |
|
237 |
def forward(self, x):
|
238 |
-
|
239 |
-
out = self.fc(
|
240 |
return out
|
241 |
|
242 |
def lstm_anomaly_detection(X, feature_columns, num_anomalies=10, epochs=100, batch_size=64):
|
@@ -244,38 +244,42 @@ def lstm_anomaly_detection(X, feature_columns, num_anomalies=10, epochs=100, bat
|
|
244 |
|
245 |
X = torch.FloatTensor(X).to(device)
|
246 |
|
247 |
-
# Ensure X is
|
248 |
-
if X.dim() ==
|
249 |
-
X = X.unsqueeze(
|
250 |
-
elif X.dim()
|
251 |
-
|
|
|
|
|
252 |
|
253 |
-
|
254 |
-
X_train, X_val = X[:train_size], X[train_size:]
|
255 |
|
256 |
-
|
|
|
|
|
|
|
257 |
criterion = nn.MSELoss()
|
258 |
optimizer = optim.Adam(model.parameters())
|
259 |
|
260 |
for epoch in range(epochs):
|
261 |
model.train()
|
262 |
optimizer.zero_grad()
|
263 |
-
output_train = model(X_train
|
264 |
-
loss_train = criterion(output_train, X_train)
|
265 |
loss_train.backward()
|
266 |
optimizer.step()
|
267 |
|
268 |
model.eval()
|
269 |
with torch.no_grad():
|
270 |
-
output_val = model(X_val
|
271 |
-
loss_val = criterion(output_val, X_val)
|
272 |
|
273 |
model.eval()
|
274 |
with torch.no_grad():
|
275 |
-
reconstructed = model(X
|
276 |
|
277 |
# Compute anomalies for all features
|
278 |
-
mse_all = np.mean(np.power(X.cpu().numpy() - reconstructed, 2), axis=1)
|
279 |
top_indices_all = mse_all.argsort()[-num_anomalies:][::-1]
|
280 |
anomalies_all = np.zeros(len(mse_all), dtype=bool)
|
281 |
anomalies_all[top_indices_all] = True
|
@@ -285,10 +289,7 @@ def lstm_anomaly_detection(X, feature_columns, num_anomalies=10, epochs=100, bat
|
|
285 |
component_indices = [feature_columns.index(col) for col in component_columns]
|
286 |
|
287 |
if len(component_indices) > 0:
|
288 |
-
|
289 |
-
mse_comp = mse_all # If X is 1D, we can't select specific components
|
290 |
-
else:
|
291 |
-
mse_comp = np.mean(np.power(X.cpu().numpy()[:, component_indices] - reconstructed[:, component_indices], 2), axis=1)
|
292 |
else:
|
293 |
mse_comp = mse_all # If no components, use all features
|
294 |
|
@@ -297,8 +298,8 @@ def lstm_anomaly_detection(X, feature_columns, num_anomalies=10, epochs=100, bat
|
|
297 |
anomalies_comp[top_indices_comp] = True
|
298 |
|
299 |
return (anomalies_all, mse_all, top_indices_all,
|
300 |
-
|
301 |
-
|
302 |
|
303 |
def plot_anomaly_scores(df, anomaly_scores, top_indices, title):
|
304 |
fig, ax = plt.subplots(figsize=(16, 8))
|
@@ -375,10 +376,14 @@ def process_video(video_path, num_anomalies, num_components, desired_fps, batch_
|
|
375 |
progress(0.9, "Performing anomaly detection")
|
376 |
feature_columns = [col for col in df.columns if col not in ['Frame', 'Timecode', 'Time (Minutes)', 'Embedding_Index']]
|
377 |
X = df[feature_columns].values
|
378 |
-
print(f"Shape of input data: {X.shape}")
|
|
|
379 |
try:
|
380 |
anomalies_all, anomaly_scores_all, top_indices_all, anomalies_comp, anomaly_scores_comp, top_indices_comp, _ = lstm_anomaly_detection(X, feature_columns, num_anomalies=num_anomalies, batch_size=batch_size)
|
381 |
except Exception as e:
|
|
|
|
|
|
|
382 |
return f"Error in anomaly detection: {str(e)}", None, None, None, None, None, None
|
383 |
|
384 |
progress(0.95, "Generating plots")
|
|
|
235 |
self.fc = nn.Linear(hidden_size, input_size)
|
236 |
|
237 |
def forward(self, x):
|
238 |
+
outputs, (hidden, _) = self.lstm(x)
|
239 |
+
out = self.fc(outputs)
|
240 |
return out
|
241 |
|
242 |
def lstm_anomaly_detection(X, feature_columns, num_anomalies=10, epochs=100, batch_size=64):
|
|
|
244 |
|
245 |
X = torch.FloatTensor(X).to(device)
|
246 |
|
247 |
+
# Ensure X is 3D (batch, sequence, features)
|
248 |
+
if X.dim() == 2:
|
249 |
+
X = X.unsqueeze(0)
|
250 |
+
elif X.dim() == 1:
|
251 |
+
X = X.unsqueeze(0).unsqueeze(2)
|
252 |
+
elif X.dim() > 3:
|
253 |
+
raise ValueError(f"Input X should be 1D, 2D or 3D, but got {X.dim()} dimensions")
|
254 |
|
255 |
+
print(f"X shape after reshaping: {X.shape}")
|
|
|
256 |
|
257 |
+
train_size = int(0.85 * X.shape[1])
|
258 |
+
X_train, X_val = X[:, :train_size, :], X[:, train_size:, :]
|
259 |
+
|
260 |
+
model = LSTMAutoencoder(input_size=X.shape[2]).to(device)
|
261 |
criterion = nn.MSELoss()
|
262 |
optimizer = optim.Adam(model.parameters())
|
263 |
|
264 |
for epoch in range(epochs):
|
265 |
model.train()
|
266 |
optimizer.zero_grad()
|
267 |
+
output_train = model(X_train)
|
268 |
+
loss_train = criterion(output_train, X_train.squeeze(0))
|
269 |
loss_train.backward()
|
270 |
optimizer.step()
|
271 |
|
272 |
model.eval()
|
273 |
with torch.no_grad():
|
274 |
+
output_val = model(X_val)
|
275 |
+
loss_val = criterion(output_val, X_val.squeeze(0))
|
276 |
|
277 |
model.eval()
|
278 |
with torch.no_grad():
|
279 |
+
reconstructed = model(X).squeeze(0).cpu().numpy()
|
280 |
|
281 |
# Compute anomalies for all features
|
282 |
+
mse_all = np.mean(np.power(X.squeeze(0).cpu().numpy() - reconstructed, 2), axis=1)
|
283 |
top_indices_all = mse_all.argsort()[-num_anomalies:][::-1]
|
284 |
anomalies_all = np.zeros(len(mse_all), dtype=bool)
|
285 |
anomalies_all[top_indices_all] = True
|
|
|
289 |
component_indices = [feature_columns.index(col) for col in component_columns]
|
290 |
|
291 |
if len(component_indices) > 0:
|
292 |
+
mse_comp = np.mean(np.power(X.squeeze(0).cpu().numpy()[:, component_indices] - reconstructed[:, component_indices], 2), axis=1)
|
|
|
|
|
|
|
293 |
else:
|
294 |
mse_comp = mse_all # If no components, use all features
|
295 |
|
|
|
298 |
anomalies_comp[top_indices_comp] = True
|
299 |
|
300 |
return (anomalies_all, mse_all, top_indices_all,
|
301 |
+
anomalies_comp, mse_comp, top_indices_comp,
|
302 |
+
model)
|
303 |
|
304 |
def plot_anomaly_scores(df, anomaly_scores, top_indices, title):
|
305 |
fig, ax = plt.subplots(figsize=(16, 8))
|
|
|
376 |
progress(0.9, "Performing anomaly detection")
|
377 |
feature_columns = [col for col in df.columns if col not in ['Frame', 'Timecode', 'Time (Minutes)', 'Embedding_Index']]
|
378 |
X = df[feature_columns].values
|
379 |
+
print(f"Shape of input data: {X.shape}")
|
380 |
+
print(f"Feature columns: {feature_columns}")
|
381 |
try:
|
382 |
anomalies_all, anomaly_scores_all, top_indices_all, anomalies_comp, anomaly_scores_comp, top_indices_comp, _ = lstm_anomaly_detection(X, feature_columns, num_anomalies=num_anomalies, batch_size=batch_size)
|
383 |
except Exception as e:
|
384 |
+
print(f"Error details: {str(e)}")
|
385 |
+
print(f"X shape: {X.shape}")
|
386 |
+
print(f"X dtype: {X.dtype}")
|
387 |
return f"Error in anomaly detection: {str(e)}", None, None, None, None, None, None
|
388 |
|
389 |
progress(0.95, "Generating plots")
|