File size: 5,958 Bytes
d60982d |
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 |
import streamlit as st
import openslide
import os
from streamlit_option_menu import option_menu
import torch
if torch.cuda.is_available():
os.system("pip install torch-scatter -f https://pytorch-geometric.com/whl/torch-1.7.1+cu113.html")
os.system("pip install torch-sparse -f https://pytorch-geometric.com/whl/torch-1.7.1+cu113.html")
os.system("pip install torch-geometric -f https://pytorch-geometric.com/whl/torch-1.7.1+cu113.html")
else:
os.system("pip install torch-scatter -f https://pytorch-geometric.com/whl/torch-1.7.1+cpu.html")
os.system("pip install torch-sparse -f https://pytorch-geometric.com/whl/torch-1.7.1+cpu.html")
os.system("pip install torch-geometric -f https://pytorch-geometric.com/whl/torch-1.7.1+cpu.html")
from predict import Predictor
# environment variables for the inference api
os.environ['DATA_DIR'] = 'queries'
os.environ['PATCHES_DIR'] = os.path.join(os.environ['DATA_DIR'], 'patches')
os.environ['SLIDES_DIR'] = os.path.join(os.environ['DATA_DIR'], 'slides')
os.environ['GRAPHCAM_DIR'] = os.path.join(os.environ['DATA_DIR'], 'graphcam_plots')
os.makedirs(os.environ['GRAPHCAM_DIR'], exist_ok=True)
# manually put the metadata in the metadata folder
os.environ['CLASS_METADATA'] ='metadata/label_map.pkl'
# manually put the desired weights in the weights folder
os.environ['WEIGHTS_PATH'] = WEIGHTS_PATH='weights'
os.environ['FEATURE_EXTRACTOR_WEIGHT_PATH'] = os.path.join(os.environ['WEIGHTS_PATH'], 'feature_extractor', 'model.pth')
os.environ['GT_WEIGHT_PATH'] = os.path.join(os.environ['WEIGHTS_PATH'], 'graph_transformer', 'GraphCAM.pth')
st.set_page_config(page_title="",layout='wide')
predictor = Predictor()
ABOUT_TEXT = "🤗 LastMinute Medical - Web diagnosis tool."
CONTACT_TEXT = """
_Built by Christian Cancedda and LabLab lads with love_ ❤️
[](https://github.com/Chris1nexus)
[](https://twitter.com/intent/follow?screen_name=chris_cancedda)
"""
VISUALIZE_TEXT = "Visualize WSI slide by uploading it on the provided window"
DETECT_TEXT = "Generate a preliminary diagnosis about the presence of pulmonary disease"
with st.sidebar:
choice = option_menu("LastMinute - Diagnosis",
["About", "Visualize WSI slide", "Cancer Detection", "Contact"],
icons=['house', 'upload', 'activity', 'person lines fill'],
menu_icon="app-indicator", default_index=0,
styles={
# "container": {"padding": "5!important", "background-color": "#fafafa", },
"container": {"border-radius": ".0rem"},
# "icon": {"color": "orange", "font-size": "25px"},
# "nav-link": {"font-size": "16px", "text-align": "left", "margin": "0px",
# "--hover-color": "#eee"},
# "nav-link-selected": {"background-color": "#02ab21"},
}
)
st.sidebar.markdown(
"""
<style>
.aligncenter {
text-align: center;
}
</style>
<p class="aligncenter">
<a href="https://twitter.com/chris_cancedda" target="_blank">
<img src="https://img.shields.io/twitter/follow/chris_cancedda?style=social"/>
</a>
</p>
""",
unsafe_allow_html=True,
)
if choice == "About":
st.title(choice)
if choice == "Visualize WSI slide":
st.title(choice)
st.markdown(VISUALIZE_TEXT)
uploaded_file = st.file_uploader("Choose a WSI slide file to diagnose (.svs)")
if uploaded_file is not None:
ori = openslide.OpenSlide(uploaded_file.name)
width, height = ori.dimensions
REDUCTION_FACTOR = 20
w, h = int(width/512), int(height/512)
w_r, h_r = int(width/20), int(height/20)
resized_img = ori.get_thumbnail((w_r,h_r))
resized_img = resized_img.resize((w_r,h_r))
ratio_w, ratio_h = width/resized_img.width, height/resized_img.height
#print('ratios ', ratio_w, ratio_h)
w_s, h_s = float(512/REDUCTION_FACTOR), float(512/REDUCTION_FACTOR)
st.image(resized_img, use_column_width='never')
if choice == "Cancer Detection":
state = dict()
st.title(choice)
st.markdown(DETECT_TEXT)
uploaded_file = st.file_uploader("Choose a WSI slide file to diagnose (.svs)")
if uploaded_file is not None:
# To read file as bytes:
#print(uploaded_file)
with open(os.path.join(uploaded_file.name),"wb") as f:
f.write(uploaded_file.getbuffer())
with st.spinner(text="Computation is running"):
predicted_class, viz_dict = predictor.predict(uploaded_file.name)
st.info('Computation completed.')
st.header(f'Predicted to be: {predicted_class}')
st.text('Heatmap of the areas that show markers correlated with the disease.\nIncreasing red tones represent higher likelihood that the area is affected')
state['cur'] = predicted_class
mapper = {'ORI': predicted_class, predicted_class:'ORI'}
readable_mapper = {'ORI': 'Original', predicted_class :'Disease heatmap' }
#def fn():
# st.image(viz_dict[mapper[state['cur']]], use_column_width='never', channels='BGR')
# state['cur'] = mapper[state['cur']]
# return
#st.button(f'See {readable_mapper[mapper[state["cur"]] ]}', on_click=fn )
#st.image(viz_dict[state['cur']], use_column_width='never', channels='BGR')
st.image([viz_dict[state['cur']],viz_dict['ORI']], caption=['Original', f'{predicted_class} heatmap'] ,channels='BGR'
# use_column_width='never',
)
if choice == "Contact":
st.title(choice)
st.markdown(CONTACT_TEXT) |