Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

790 add scientific use case frb to dashboard #832

Merged
merged 44 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
b53b927
use radio buttons for example instead of checkbox
laurasootes Jul 31, 2024
e44c4d0
disable examples when own upload
laurasootes Jul 31, 2024
cd7c46b
add FRB data
laurasootes Jul 31, 2024
668554c
add specific FRB preprocessing
laurasootes Jul 31, 2024
2fda558
fix preproccing kwarg name
laurasootes Jul 31, 2024
bc08928
add all exceptions needed to explain and plot the FRB case
laurasootes Jul 31, 2024
935f224
delete checkbox
laurasootes Jul 31, 2024
fa561ec
check method checkbox when changing example
laurasootes Jul 31, 2024
c53e20c
Change label text
laurasootes Jul 31, 2024
5f0ecc5
convert example selection to radio bottons
laurasootes Jul 31, 2024
d361262
split menu example from own data usage
laurasootes Jul 31, 2024
db3139b
add resets for input choice and params
laurasootes Jul 31, 2024
d480d06
add import
laurasootes Jul 31, 2024
ce03c2f
fix key name for auto reset
laurasootes Jul 31, 2024
4722b90
split example and own data in menu
laurasootes Jul 31, 2024
cb017cd
add info
laurasootes Jul 31, 2024
217cb9a
update and extend tests
laurasootes Jul 31, 2024
e4d1033
change option name
laurasootes Jul 31, 2024
5258024
add preprocess function for FRB to lime
laurasootes Jul 31, 2024
030056b
change selection example title
laurasootes Jul 31, 2024
f064001
use for frb case only rise
laurasootes Aug 2, 2024
89d667a
Merge branch 'main' of https://github.com/dianna-ai/dianna into 790-a…
laurasootes Aug 7, 2024
a18a084
fix line lengths
laurasootes Aug 7, 2024
be6b87c
isort
laurasootes Aug 7, 2024
1c0b410
fix imports
laurasootes Aug 7, 2024
f0e313b
add newline at end of file
laurasootes Aug 7, 2024
b6c8333
fix line lengths
laurasootes Aug 7, 2024
543cf43
ruff fix
laurasootes Aug 7, 2024
c2e4885
ruff fixes
laurasootes Aug 7, 2024
bb24c2b
delete unused import
laurasootes Aug 7, 2024
576cf75
add import
laurasootes Aug 7, 2024
5b90a55
typo
laurasootes Aug 7, 2024
a40c4c0
fix typo
laurasootes Aug 7, 2024
be83bf4
implement downloader function
laurasootes Aug 8, 2024
17517c2
fix isort
laurasootes Aug 8, 2024
98537e4
rename ts_file to ts_data file param
laurasootes Aug 8, 2024
48ccd1f
rename variables
laurasootes Aug 8, 2024
a3fd361
use download function
laurasootes Aug 8, 2024
d4c2f89
delete unused libraries
laurasootes Aug 8, 2024
e562b95
fix import
laurasootes Aug 8, 2024
07c9f1a
add FRB example test
laurasootes Aug 8, 2024
f53b02a
fix whitespace
laurasootes Aug 8, 2024
7407f3c
remove background plot of original data
laurasootes Aug 13, 2024
0bb0693
update inline comment
laurasootes Aug 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions dianna/dashboard/_models_ts.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def predict(*, model, ts_data):

@st.cache_data
def _run_rise_timeseries(_model, ts_data, **kwargs):
# convert streamlit kwarg requirement back to dianna kwarg requirement
if "_preprocess_function" in kwargs:
kwargs["preprocess_function"] = kwargs["_preprocess_function"]
del kwargs["_preprocess_function"]

def run_model(ts_data):
return predict(model=_model, ts_data=ts_data)
Expand All @@ -37,6 +41,10 @@ def run_model(ts_data):

@st.cache_data
def _run_lime_timeseries(_model, ts_data, **kwargs):
# convert streamlit kwarg requirement back to dianna kwarg requirement
if "_preprocess_function" in kwargs:
kwargs["preprocess_function"] = kwargs["_preprocess_function"]
del kwargs["_preprocess_function"]

def run_model(ts_data):
return predict(model=_model, ts_data=ts_data)
Expand Down
14 changes: 14 additions & 0 deletions dianna/dashboard/_shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,17 @@ def _get_top_indices_and_labels(*, predictions, labels):
st.metric('Predicted class', top_labels[0])

return top_indices, top_labels

def reset_method():
# Clear selection
for k in st.session_state.keys():
if '_cb_' in k:
st.session_state[k] = False
if 'params' in k:
st.session_state.pop(k)

def reset_example():
# Clear selection
for k in st.session_state.keys():
if '_load_' in k:
st.session_state.pop(k)
93 changes: 58 additions & 35 deletions dianna/dashboard/pages/Images.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
from _shared import _get_top_indices_and_labels
from _shared import _methods_checkboxes
from _shared import add_sidebar_logo
from _shared import data_directory
from _shared import label_directory
from _shared import model_directory
from _shared import reset_example
from _shared import reset_method
from dianna.utils.downloader import download
from dianna.visualization import plot_image

add_sidebar_logo()
Expand All @@ -19,40 +19,63 @@

st.sidebar.header('Input data')

load_example_digits = st.sidebar.checkbox('Load hand-written digits example',
key='Image_digits_example_check')

image_file = st.sidebar.file_uploader('Select image',
type=('png', 'jpg', 'jpeg'),
disabled=load_example_digits)

if image_file:
st.sidebar.image(image_file)

image_model_file = st.sidebar.file_uploader('Select model',
type='onnx',
disabled=load_example_digits)

image_label_file = st.sidebar.file_uploader('Select labels',
type='txt',
disabled=load_example_digits)

if load_example_digits:
image_file = (data_directory / 'digit0.jpg')
image_model_file = (model_directory / 'mnist_model_tf.onnx')
image_label_file = (label_directory / 'labels_mnist.txt')

st.markdown(
"""
This example demonstrates the use of DIANNA on a pretrained binary
[MNIST](https://yann.lecun.com/exdb/mnist/) model using hand-written
digit images. The model predicts for an image of a hand-written 0 or 1,
which of the two it most likely is. This example visualizes the
relevance attributions for each pixel/super-pixel by displaying them on
top of the input image.
"""
input_type = st.sidebar.radio(
label='Select which input to use',
options = ('Use an example', 'Use your own data'),
index = None,
on_change = reset_example,
key = 'Image_input_type'
)

# Use the examples
if input_type == 'Use an example':
load_example = st.sidebar.radio(
label='Load example',
options=('Hand-written digit recognition',),
index = None,
on_change = reset_method,
key='Image_load_example'
)

if load_example == 'Hand-written digit recognition':
image_file = download('digit0.jpg', 'data')
image_model_file = download('mnist_model_tf.onnx', 'model')
image_label_file = download('labels_mnist.txt', 'label')

st.markdown(
"""
This example demonstrates the use of DIANNA on a pretrained binary
[MNIST](https://yann.lecun.com/exdb/mnist/) model using a hand-written digit images.
The model predict for an image of a hand-written 0 or 1, which of the two it most
likely is.
This example visualizes the relevance attributions for each pixel/super-pixel by
displaying them on top of the input image.
"""
)
else:
st.info('Select an example in the left panel to coninue')
st.stop()

# Option to upload your own data
if input_type == 'Use your own data':
load_example = None

image_file = st.sidebar.file_uploader('Select image',
type=('png', 'jpg', 'jpeg'))

if image_file:
st.sidebar.image(image_file)

image_model_file = st.sidebar.file_uploader('Select model',
type='onnx')

image_label_file = st.sidebar.file_uploader('Select labels',
type='txt')

if input_type is None:
st.info('Select which input type to use in the left panel to continue')
st.stop()

if not (image_file and image_model_file and image_label_file):
st.info('Add your input data in the left panel to continue')
st.stop()
Expand Down
70 changes: 46 additions & 24 deletions dianna/dashboard/pages/Text.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
from _shared import _get_top_indices_and_labels
from _shared import _methods_checkboxes
from _shared import add_sidebar_logo
from _shared import label_directory
from _shared import model_directory
from _shared import reset_example
from _shared import reset_method
from dianna.utils.downloader import download
from dianna.visualization.text import highlight_text

add_sidebar_logo()
Expand All @@ -18,35 +19,56 @@

st.sidebar.header('Input data')

load_example_moviesentiment = st.sidebar.checkbox('Load movie sentiment example',
key='Text_example_check_moviesentiment')

text_input = st.sidebar.text_input('Input string', disabled=load_example_moviesentiment)

if text_input:
st.sidebar.write(text_input)

text_model_file = st.sidebar.file_uploader('Select model',
type='onnx',
disabled=load_example_moviesentiment)

text_label_file = st.sidebar.file_uploader('Select labels',
type='txt',
disabled=load_example_moviesentiment)

if load_example_moviesentiment:
text_input = 'The movie started out great but the ending was dissappointing'
text_model_file = model_directory / 'movie_review_model.onnx'
text_label_file = label_directory / 'labels_text.txt'

st.markdown(
input_type = st.sidebar.radio(
label='Select which input to use',
options = ('Use an example', 'Use your own data'),
index = None,
on_change = reset_example,
key = 'Text_input_type'
)

# Use the examples
if input_type == 'Use an example':
load_example = st.sidebar.radio(
label='Use example',
options=('Movie sentiment',),
index = None,
on_change = reset_method,
key='Text_example_check_moviesentiment')

if load_example == 'Movie sentiment':
text_input = 'The movie started out great but the ending was dissappointing'
text_model_file = download('movie_review_model.onnx', 'model')
text_label_file = download('labels_text.txt', 'label')

st.markdown(
"""
This example demonstrates the use of DIANNA on the [Stanford Sentiment
Treebank dataset](https://nlp.stanford.edu/sentiment/index.html) which
contains one-sentence movie reviews. A pre-trained neural network
classifier is used, which identifies whether a movie review is positive
or negative.
""")
else:
st.info('Select an example in the left panel to coninue')
st.stop()

# Option to upload your own data
if input_type == 'Use your own data':
text_input = st.sidebar.text_input('Input string')

if text_input:
st.sidebar.write(text_input)

text_model_file = st.sidebar.file_uploader('Select model',
type='onnx')

text_label_file = st.sidebar.file_uploader('Select labels',
type='txt')

if input_type is None:
st.info('Select which input type to use in the left panel to continue')
st.stop()

if not (text_input and text_model_file and text_label_file):
st.info('Add your input data in the left panel to continue')
Expand Down
Loading
Loading