File size: 7,076 Bytes
a3a57c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7d03f1
a3a57c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8623c4c
a3a57c6
 
 
 
 
 
 
 
 
 
 
 
 
 
a97a6b4
a3a57c6
 
 
 
 
 
 
 
0854c30
 
a3a57c6
 
 
a97a6b4
a3a57c6
 
 
 
 
 
 
 
a97a6b4
a3a57c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0854c30
 
a3a57c6
 
a97a6b4
a3a57c6
 
 
 
 
 
 
 
0854c30
 
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
import streamlit as st
import nibabel as nib
import os.path
import os
from nilearn import plotting

import torch
from monai.transforms import (
    EnsureChannelFirst,
    Compose,
    Resize,
    ScaleIntensity,
    LoadImage,
)
import torch.nn.functional as F
import numpy as np
from statistics import mean

from constants import CLASSES
from model.download_model import load_model
from huggingface_hub import hf_hub_download

#SET PAGE TITLE
st.set_page_config(page_title = "Alzheimer Classifier", page_icon = ":brain:", layout = "wide")

#LOAD MODEL
model = load_model()

#SET NIFTI FILE LOADING AND PROCESSING CONFIGURATIONS
transforms = Compose([
    ScaleIntensity(),
    EnsureChannelFirst(),
    Resize((96, 96, 96)),
])
load_img = LoadImage(image_only=True)

#SET CLASSES
class_names = CLASSES

#SET STREAMLIT SESSION STATES
if 'clicked_pp' not in st.session_state:
    st.session_state.clicked_pp = False

if 'clicked_pred' not in st.session_state:
    st.session_state.clicked_pred = False

def click_pp_true():
    st.session_state.clicked_pp = True

def click_pred_true():
    st.session_state.clicked_pred = True

def click_false():
    st.session_state.clicked_pp = False
    st.session_state.clicked_pred = False

###########################################################
###################### STREAMLIT APP ######################
###########################################################   

with st.sidebar:
    st.title("Alzheimer Classifier Demo")
    img_path = st.selectbox(
            "Select Image",
            tuple(class_names),
            on_change= click_false,
        )
    col1, col2 = st.columns((1,1))
    with col1:
        run_preprocess = st.button("Preprocess Image", on_click=click_pp_true)
    if st.session_state.clicked_pp:
        with col2:
            run_pred = st.button("Run Prediction", on_click= click_pred_true)

with st.container():
    if img_path != "":
        if st.session_state.clicked_pp:
            if st.session_state.clicked_pred == False:
                with st.container():
                    pred_image = nib.load(hf_hub_download(repo_id= "rootstrap-org/Alzheimer-Classifier-Demo", repo_type="dataset", subfolder="preprocessed", filename = img_path + ".nii.gz"))

                    bounds_pred = plotting.find_cuts._get_auto_mask_bounds(pred_image)
                    
                    st.sidebar.write("#")
                    y_value_pred = st.sidebar.slider('Move the slider to adjust the coronal cut ', bounds_pred[1][0], bounds_pred[1][1], mean([bounds_pred[1][0], bounds_pred[1][1]]))
                    x_value_pred = st.sidebar.slider('Move the slider to adjust the sagittal cut ', bounds_pred[0][0], bounds_pred[0][1], mean([bounds_pred[0][0], bounds_pred[0][1]]))
                    z_value_pred = st.sidebar.slider('Move the slider to adjust the axial cut ', bounds_pred[2][0], bounds_pred[2][1], mean([bounds_pred[2][0], bounds_pred[2][1]]))

                    plotting.plot_img(pred_image, cmap="grey", cut_coords=(x_value_pred,y_value_pred,z_value_pred), black_bg=True)
                    st.pyplot()

            else:
                with st.container():
                    pred_image = nib.load(hf_hub_download(repo_id= "rootstrap-org/Alzheimer-Classifier-Demo", repo_type="dataset", subfolder="preprocessed", filename = img_path + ".nii.gz"))

                    bounds_pred = plotting.find_cuts._get_auto_mask_bounds(pred_image)
                    
                    st.sidebar.write("#")
                    y_value_pred = st.sidebar.slider('Move the slider to adjust the coronal cut ', bounds_pred[1][0], bounds_pred[1][1], mean([bounds_pred[1][0], bounds_pred[1][1]]))
                    x_value_pred = st.sidebar.slider('Move the slider to adjust the sagittal cut ', bounds_pred[0][0], bounds_pred[0][1], mean([bounds_pred[0][0], bounds_pred[0][1]]))
                    z_value_pred = st.sidebar.slider('Move the slider to adjust the axial cut ', bounds_pred[2][0], bounds_pred[2][1], mean([bounds_pred[2][0], bounds_pred[2][1]]))

                    img_array = load_img(hf_hub_download(repo_id= "rootstrap-org/Alzheimer-Classifier-Demo", repo_type="dataset", subfolder="preprocessed", filename = img_path + ".nii.gz"))
                    new_data = transforms(img_array)
                    new_data_tensor = torch.from_numpy(np.array([new_data]))

                    with torch.no_grad():
                        output = model(new_data_tensor)

                    probabilities = F.softmax(output, dim=1)
                    probabilities_np = probabilities.numpy()
                    probabilities_item = probabilities_np[0]
                    probabilities_percentage = probabilities_item * 100
                    predicted_class_index = np.argmax(probabilities_np[0])
                    predicted_class_name = class_names[predicted_class_index]
                    predicted_probability = probabilities_percentage[predicted_class_index]

                    st.sidebar.write("#")
                    if predicted_class_index == 0:
                        color_name = "red"
                    elif predicted_class_index == 1:
                        color_name = "blue"
                    elif predicted_class_index == 2:
                        color_name = "green"
                    
                    if predicted_probability > 80:
                        color_prob = "green"
                    elif predicted_probability > 60:
                        color_prob = "yellow"
                    else:
                        color_prob = "red"
                    
                    class_col, pred_col = st.columns((1,1))

                    with class_col:
                        st.write(f"### Predicted Class: :{color_name}[{predicted_class_name}]") 
                    
                    with pred_col:
                        st.write(f"### Probability: :{color_prob}[{predicted_probability:.2f}%]")   

                    plotting.plot_img(pred_image, cmap="grey", cut_coords=(x_value_pred,y_value_pred,z_value_pred), black_bg=True)
                    st.pyplot()
    
        else:
            raw_image = nib.load(hf_hub_download(repo_id= "rootstrap-org/Alzheimer-Classifier-Demo", repo_type="dataset", subfolder="raw", filename = img_path + ".nii"))

            bounds_raw = plotting.find_cuts._get_auto_mask_bounds(raw_image)

            st.sidebar.write("#")
            y_value_raw = st.sidebar.slider('Move the slider to adjust the coronal cut', bounds_raw[1][0], bounds_raw[1][1], mean([bounds_raw[1][0], bounds_raw[1][1]]))
            x_value_raw = st.sidebar.slider('Move the slider to adjust the sagittal cut', bounds_raw[0][0], bounds_raw[0][1], mean([bounds_raw[0][0], bounds_raw[0][1]]))
            z_value_raw = st.sidebar.slider('Move the slider to adjust the axial cut', bounds_raw[2][0], bounds_raw[2][1], mean([bounds_raw[2][0], bounds_raw[2][1]]))

            plotting.plot_img(raw_image, cmap = "grey", cut_coords=(x_value_raw,y_value_raw,z_value_raw), black_bg=True)
            st.pyplot()