sourize commited on
Commit
f5e419d
·
verified ·
1 Parent(s): 0e7bcf4

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +67 -0
  2. autoencoder_model.keras +0 -0
  3. scaler.pkl +3 -0
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import numpy as np
3
+ import joblib
4
+ import pandas as pd
5
+ from tensorflow.keras.models import load_model
6
+
7
+ # Load the trained autoencoder model
8
+ try:
9
+ model = load_model("autoencoder_model.keras")
10
+ except Exception as e:
11
+ st.error(f"Error loading model: {e}")
12
+ st.stop()
13
+
14
+ # Load the scaler
15
+ try:
16
+ with open("scaler.pkl", "rb") as file:
17
+ scaler = joblib.load(file)
18
+ # Check that the scaler has a transform method
19
+ if not hasattr(scaler, "transform"):
20
+ raise ValueError("Loaded scaler object does not have a 'transform' method. Please save a valid scaler.")
21
+ except Exception as e:
22
+ st.error(f"Error loading scaler: {e}")
23
+ st.stop()
24
+
25
+ # Set an anomaly threshold (adjust based on your model calibration)
26
+ THRESHOLD = 0.7
27
+
28
+ st.title("Maritime Anomaly Detection")
29
+ st.markdown("### Input the following AIS features:")
30
+
31
+ # Taking inputs from the user
32
+ timestamp_str = st.text_input("# Timestamp (Format: DD/MM/YYYY HH:MM:SS)", value="27/02/2024 03:42:19")
33
+ mmsi = st.number_input("MMSI", value=0.0)
34
+ latitude = st.number_input("Latitude", value=0.0)
35
+ longitude = st.number_input("Longitude", value=0.0)
36
+ sog = st.number_input("SOG", value=0.0)
37
+ cog = st.number_input("COG", value=0.0)
38
+ heading = st.number_input("Heading", value=0.0)
39
+
40
+ if st.button("Run Detection"):
41
+ try:
42
+ # Convert timestamp to seconds since epoch
43
+ timestamp_sec = pd.to_datetime(timestamp_str, format="%d/%m/%Y %H:%M:%S").value / 1e9
44
+ except Exception as e:
45
+ st.error(f"Invalid Timestamp Format: {e}")
46
+ st.stop()
47
+
48
+ # Create an array from the inputs
49
+ input_data = np.array([[timestamp_sec, mmsi, latitude, longitude, sog, cog, heading]])
50
+
51
+ try:
52
+ # Scale the input data using the scaler's transform method
53
+ input_scaled = scaler.transform(input_data)
54
+
55
+ # Reconstruct the input using the autoencoder
56
+ reconstructed = model.predict(input_scaled)
57
+
58
+ # Calculate the reconstruction error (Mean Squared Error)
59
+ reconstruction_error = np.mean(np.square(input_scaled - reconstructed))
60
+
61
+ # Display the result based on the threshold
62
+ if reconstruction_error > THRESHOLD:
63
+ st.error(f"Anomaly detected, Reconstruction Error = {reconstruction_error:.4f}")
64
+ else:
65
+ st.success(f"No anomaly detected, Reconstruction Error = {reconstruction_error:.4f}")
66
+ except Exception as e:
67
+ st.error(f"Error during detection: {e}")
autoencoder_model.keras ADDED
Binary file (62.4 kB). View file
 
scaler.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0f9db2af34080e55f61f2a8b4e9a458bd419a8197f456c0c626ce7fec2e26c5f
3
+ size 1151