Spaces:
Runtime error
Runtime error
File size: 9,191 Bytes
cd96633 a65288a 287e508 cd96633 3641ff7 cd96633 13747e9 cd96633 a65288a 7057729 a65288a 6b7cf40 a65288a 6b7cf40 a65288a 6b7cf40 a65288a b87f5fe 7057729 30642f0 7057729 3641ff7 30642f0 b87f5fe 30642f0 7057729 30642f0 b87f5fe 7057729 b77223e a65288a 3641ff7 a65288a 2705bc4 a65288a 3641ff7 6b7cf40 3641ff7 6b7cf40 3641ff7 a65288a 3641ff7 a65288a 3641ff7 a65288a 3641ff7 6b7cf40 3641ff7 6b7cf40 a65288a 2705bc4 a65288a cd96633 3641ff7 a65288a 6b7cf40 a65288a 2705bc4 a65288a 2705bc4 3641ff7 6b7cf40 a65288a 3641ff7 a65288a 3641ff7 a65288a 13747e9 a65288a 13747e9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | import os
import streamlit as st
import numpy as np
import pandas as pd
# Paths anchored to the repo root regardless of working directory
_SRC_DIR = os.path.dirname(os.path.abspath(__file__))
_REPO_ROOT = os.path.dirname(_SRC_DIR)
_SAMPLES_PATH = os.path.join(_REPO_ROOT, "data", "samples.csv")
_NORM_PATH = os.path.join(_REPO_ROOT, "data", "norm_params.json")
# ββ Constants ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
LABEL_MAP = {
0: "WALKING",
1: "WALKING_UPSTAIRS",
2: "WALKING_DOWNSTAIRS",
3: "SITTING",
4: "STANDING",
5: "LAYING",
}
EXPLANATIONS = {
"LAYING": "Minimal movement detected across all axes with low acceleration magnitude: consistent with a stationary horizontal posture.",
"SITTING": "Low dynamic acceleration with a stable gravity component suggests a stationary upright posture with little body movement.",
"STANDING": "Similar to sitting but with slight postural micro-movements. This class is often the hardest to distinguish from sitting.",
"WALKING": "Rhythmic periodic acceleration with peaks on the vertical axis: consistent with level walking at normal cadence.",
"WALKING_DOWNSTAIRS": "Downward gravitational shift with higher impact peaks characteristic of descending a staircase.",
"WALKING_UPSTAIRS": "Elevated vertical acceleration effort with upward body displacement: consistent with climbing stairs.",
}
# ββ Model loader ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@st.cache_resource
def load_model(filename: str):
try:
from huggingface_hub import hf_hub_download
import tensorflow as tf
from model_def import FeedForwardNetwork, Conv1DNetwork # noqa: F401
model_path = hf_hub_download(
repo_id="Group3DActRecog/actRecog",
filename=filename,
repo_type="space",
)
model = tf.keras.models.load_model(
model_path,
custom_objects={
"FeedForwardNetwork": FeedForwardNetwork,
"Conv1DNetwork": Conv1DNetwork,
},
)
return model, "ready"
except Exception as e:
return None, f"error: {e}"
# ββ Page config βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
st.set_page_config(
page_title="Human Activity Recognition",
page_icon="π",
layout="wide",
)
st.title("Human Activity Recognition")
st.markdown(
"Deep learning classifier trained on 561 smartphone sensor features "
"from the [UCI HAR dataset](https://www.kaggle.com/datasets/uciml/human-activity-recognition-with-smartphones). "
"Classifies six daily activities from accelerometer and gyroscope readings."
)
# ββ Sidebar ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
with st.sidebar:
st.header("About")
st.markdown("""
**Dataset:** UCI Human Activity Recognition
**Subjects:** 30 volunteers aged 19β48
**Sensor:** Samsung Galaxy S II (waist-mounted)
**Sampling rate:** 50Hz
**Features:** 561 time + frequency domain features
**Classes:** 6 activities of daily living
""")
st.markdown("---")
st.markdown("**Models**")
st.markdown("""
**FFN**: Feedforward Network
Dense(512) β Dense(256) β Dense(128)
BatchNorm + Dropout(0.3) per layer
**CNN**: 1D Convolutional Network
Conv1D(64) β Conv1D(128) β Conv1D(256)
GlobalAvgPool β Dense(128)
""")
st.markdown("---")
st.caption("DAT606 Group Assignment Β· Pan-Atlantic University")
# ββ Load both models at startup βββββββββββββββββββββββββββββββββββββββββββββββ
ffn_model, ffn_status = load_model("model.keras")
cnn_model, cnn_status = load_model("har_cnn.keras")
if ffn_status != "ready" or cnn_status != "ready":
if ffn_status != "ready":
st.warning(f"FFN not loaded: {ffn_status}")
if cnn_status != "ready":
st.warning(f"CNN not loaded: {cnn_status}")
# ββ Tabs βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
tab1, tab2 = st.tabs(["Select a Sample", "Upload Phyphox CSV"])
# ββ Tab 1: Sample selector βββββββββββββββββββββββββββββββββββββββββββββββββββ
with tab1:
st.subheader("Select a pre-loaded test sample")
st.caption(
"Each sample is one 2.56-second window of sensor data "
"from a test subject the model has never seen during training."
)
try:
samples_df = pd.read_csv(_SAMPLES_PATH)
feature_cols = [c for c in samples_df.columns if c not in ["Activity", "subject"]]
sample_labels = [
f"Sample {i+1} : {row['Activity']}"
for i, (_, row) in enumerate(samples_df.iterrows())
]
selected = st.selectbox("Choose a sample:", sample_labels)
selected_idx = sample_labels.index(selected)
selected_row = samples_df.iloc[selected_idx]
true_label = selected_row["Activity"]
feature_vector = selected_row[feature_cols].values.astype(np.float32)
col1, col2 = st.columns(2)
with col1:
st.metric("True Activity", true_label)
with col2:
st.metric("Feature count", len(feature_vector))
if st.button("Classify this sample", type="primary"):
if ffn_status != "ready" or cnn_status != "ready":
st.error("One or both models not loaded: cannot predict yet.")
else:
arr = feature_vector.reshape(1, -1)
ffn_probs = ffn_model.predict(arr, verbose=0)[0]
cnn_probs = cnn_model.predict(arr, verbose=0)[0]
ffn_idx = int(np.argmax(ffn_probs))
cnn_idx = int(np.argmax(cnn_probs))
ffn_label = LABEL_MAP[ffn_idx]
cnn_label = LABEL_MAP[cnn_idx]
ffn_conf = float(ffn_probs[ffn_idx]) * 100
cnn_conf = float(cnn_probs[cnn_idx]) * 100
st.markdown("---")
st.subheader("Model comparison")
left, right = st.columns(2)
# ββ FFN column ββββββββββββββββββββββββββββββββββββββββββββββ
with left:
st.markdown("#### Feedforward Network")
if ffn_label == true_label:
st.success(f"**{ffn_label}** Β· {ffn_conf:.1f}% confidence Β· β Correct")
else:
st.error(f"**{ffn_label}** Β· {ffn_conf:.1f}% confidence Β· β Incorrect (true: {true_label})")
st.markdown(f"_{EXPLANATIONS[ffn_label]}_")
st.markdown("**Confidence across all classes**")
st.bar_chart(pd.DataFrame(
{"Confidence (%)": [float(ffn_probs[i]) * 100 for i in range(6)]},
index=[LABEL_MAP[i] for i in range(6)]
))
# ββ CNN column ββββββββββββββββββββββββββββββββββββββββββββββ
with right:
st.markdown("#### 1D Convolutional Network")
if cnn_label == true_label:
st.success(f"**{cnn_label}** Β· {cnn_conf:.1f}% confidence Β· β Correct")
else:
st.error(f"**{cnn_label}** Β· {cnn_conf:.1f}% confidence Β· β Incorrect (true: {true_label})")
st.markdown(f"_{EXPLANATIONS[cnn_label]}_")
st.markdown("**Confidence across all classes**")
st.bar_chart(pd.DataFrame(
{"Confidence (%)": [float(cnn_probs[i]) * 100 for i in range(6)]},
index=[LABEL_MAP[i] for i in range(6)]
))
except FileNotFoundError:
st.error("Sample data file not found. Add `data/samples.csv` to the repo.")
# ββ Tab 2: Phyphox upload βββββββββββββββββββββββββββββββββββββββββββββββββββββ
with tab2:
from phyphox_app_block import render_phyphox_tab
render_phyphox_tab(ffn_model, ffn_status, cnn_model, cnn_status, _NORM_PATH)
|