Spaces:
Runtime error
Runtime error
add application file
Browse files
app.py
ADDED
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
import torch
|
4 |
+
from torch.utils.data import DataLoader
|
5 |
+
from config import get_config_universal
|
6 |
+
from dataset import DataSet
|
7 |
+
from datasetbuilder import DataSetBuilder
|
8 |
+
from test import Test
|
9 |
+
from visualization.steamlit_plot import plot_kinematic_predictions
|
10 |
+
|
11 |
+
x = st.slider('Select a value')
|
12 |
+
st.write(x, 'squared is', x * x)
|
13 |
+
dataset_name = 'camargo'
|
14 |
+
config = get_config_universal(dataset_name)
|
15 |
+
|
16 |
+
# model_file = 'transformertsai_g1g2rardsasd_g1g2rardsasd.pt'
|
17 |
+
# model = torch.load(os.path.join('./caches/trained_model/v05', model_file))
|
18 |
+
sensor_options = {'Thigh & Shank & Foot': ['foot', 'shank', 'thigh'],
|
19 |
+
'Thigh & Shank': ['thigh', 'shank'],
|
20 |
+
'Thigh & Foot': ['thigh', 'foot'],
|
21 |
+
'Shank & Foot': ['shank', 'foot'],
|
22 |
+
'Thigh': ['thigh'],
|
23 |
+
'Shank': ['shank'],
|
24 |
+
'Foot': ['foot']}
|
25 |
+
|
26 |
+
@st.cache
|
27 |
+
def fetch_data(config):
|
28 |
+
dataset_handler = DataSet(config, load_dataset=True)
|
29 |
+
kihadataset_train, kihadataset_test = dataset_handler.run_dataset_split_loop()
|
30 |
+
kihadataset_train['x'], kihadataset_train['y'], kihadataset_train['labels'] = dataset_handler.run_segmentation(
|
31 |
+
kihadataset_train['x'],
|
32 |
+
kihadataset_train['y'], kihadataset_train['labels'])
|
33 |
+
kihadataset_test['x'], kihadataset_test['y'], kihadataset_test['labels'] = dataset_handler.run_segmentation(
|
34 |
+
kihadataset_test['x'],
|
35 |
+
kihadataset_test['y'], kihadataset_test['labels'])
|
36 |
+
train_dataset = DataSetBuilder(kihadataset_train['x'], kihadataset_train['y'], kihadataset_train['labels'],
|
37 |
+
transform_method=config['data_transformer'], scaler=None, noise=None)
|
38 |
+
test_dataset = DataSetBuilder(kihadataset_test['x'], kihadataset_test['y'], kihadataset_test['labels'],
|
39 |
+
transform_method=config['data_transformer'], scaler=train_dataset.scaler,
|
40 |
+
noise=None)
|
41 |
+
test_dataloader = DataLoader(dataset=test_dataset, batch_size=config['batch_size'], shuffle=False)
|
42 |
+
return test_dataloader, kihadataset_test
|
43 |
+
|
44 |
+
# @st.cache()
|
45 |
+
def fetch_model(sensor_name, model_name):
|
46 |
+
device = torch.device('cpu')
|
47 |
+
model_names = {'CNNLSTM':'hernandez2021cnnlstm', 'BiLSTM':'bilstm', 'BioMAT': 'transformertsai'}
|
48 |
+
sensor_names = {'Thigh & Shank & Foot':'thighshankfoot',
|
49 |
+
'Thigh & Shank':'thighshank',
|
50 |
+
'Thigh & Foot':'thighfoot',
|
51 |
+
'Shank & Foot':'shankfoot',
|
52 |
+
'Thigh':'thigh',
|
53 |
+
'Shank':'shank',
|
54 |
+
'Foot':'foot'}
|
55 |
+
if sensor_names[sensor_name]=='thighshankfoot':
|
56 |
+
model_file = model_names[model_name] + '_g1g2rardsasd_g1g2rardsasd.pt'
|
57 |
+
else:
|
58 |
+
model_file = sensor_names[sensor_name] + '_' + model_names[model_name]+'_g1g2rardsasd_g1g2rardsasd.pt'
|
59 |
+
st.write(model_file)
|
60 |
+
model = torch.load(os.path.join('./caches/trained_model/v05', model_file))
|
61 |
+
return model
|
62 |
+
|
63 |
+
# @st.cache
|
64 |
+
def fetch_predictions(model):
|
65 |
+
test_handler = Test()
|
66 |
+
y_pred, y_true, loss = test_handler.run_testing(config, model, test_dataloader=test_dataloader)
|
67 |
+
y_true = y_true.detach().cpu().clone().numpy()
|
68 |
+
y_pred = y_pred.detach().cpu().clone().numpy()
|
69 |
+
return y_pred, y_true, loss
|
70 |
+
|
71 |
+
st.set_page_config(layout="wide")
|
72 |
+
st.title('BioMAT:Biomechanical Multi-Activity Transformer Model for Joint Kinematic Prediction From IMUs')
|
73 |
+
st.info('If you change the sensor configuration, press rerun', icon="ℹ️")
|
74 |
+
|
75 |
+
st.sidebar.title('Sensor Configuration')
|
76 |
+
selected_sensor = st.sidebar.selectbox('Pick one', ['Thigh & Shank & Foot',
|
77 |
+
'Thigh & Shank',
|
78 |
+
'Thigh & Foot',
|
79 |
+
'Shank & Foot',
|
80 |
+
'Thigh',
|
81 |
+
'Shank',
|
82 |
+
'Foot'])
|
83 |
+
|
84 |
+
config['selected_sensors'] = sensor_options[selected_sensor]
|
85 |
+
|
86 |
+
st.sidebar.title('Model Configuration')
|
87 |
+
selected_model = st.sidebar.selectbox('Pick one', ['CNNLSTM',
|
88 |
+
'BiLSTM',
|
89 |
+
'BioMAT'])
|
90 |
+
|
91 |
+
st.sidebar.title('Subject')
|
92 |
+
selected_subject = st.sidebar.slider('Pick a Subject Number', min_value=23, max_value=25, step=1)
|
93 |
+
|
94 |
+
st.sidebar.title('Activity')
|
95 |
+
selected_activities = st.sidebar.multiselect('Pick Output Activities',
|
96 |
+
['LevelGround Walking', 'Ramp Ascent', 'Ramp Descent', 'Stair Ascent', 'Stair Descent'])
|
97 |
+
|
98 |
+
index_to_plot = st.sidebar.number_input('Enter a number between 0 and 5', min_value=0, max_value=5)
|
99 |
+
|
100 |
+
if st.sidebar.button('Predict'):
|
101 |
+
with st.spinner('Data is loading...'):
|
102 |
+
test_dataloader, kihadataset_test = fetch_data(config)
|
103 |
+
st.success('Data is loaded!')
|
104 |
+
with st.spinner('Model is loading...'):
|
105 |
+
model = fetch_model(selected_sensor, selected_model)
|
106 |
+
st.success('Model is loaded!')
|
107 |
+
with st.spinner('Prediction ...'):
|
108 |
+
y_pred, y_true, loss = fetch_predictions(model)
|
109 |
+
st.success('Prediction is Completed!')
|
110 |
+
st.write('plot ...')
|
111 |
+
subject = 'AB' + str(selected_subject)
|
112 |
+
fig = plot_kinematic_predictions(y_true, y_pred, kihadataset_test['labels'], subject,
|
113 |
+
selected_activities=selected_activities, selected_index_to_plot=index_to_plot)
|
114 |
+
st.plotly_chart(fig, use_container_width=True)
|
115 |
+
|
116 |
+
|
117 |
+
|
118 |
+
|