devoworm-group/Lineage_Population
2
1import streamlit as st2import numpy as np3import pandas as pd4from PIL import Image5from pathlib import Path6import joblib7 8import numpy as np9import cv210import onnxruntime as ort11import imutils12# import matplotlib.pyplot as plt13import pandas as pd14import plotly.express as px15 16 17def scale_model_outputs(scaler_path, data):18 scaler= joblib.load(scaler_path)19 scaled=scaler.inverse_transform(data)20 return(scaled)21 22 23def onnx_predict_lineage_population(input_image):24 ort_session = ort.InferenceSession('onnx_models/lineage_population_model.onnx')25 img = Image.fromarray(np.uint8(input_image))26 resized = img.resize((256, 256), Image.NEAREST)27 28 transposed=np.transpose(resized, (2, 1, 0)) 29 img_unsqueeze = expand_dims(transposed)30 31 onnx_outputs = ort_session.run(None, {'input': img_unsqueeze.astype('float32')}) 32 return(onnx_outputs[0])33 34 35 36def expand_dims(arr):37 norm=(arr-np.min(arr))/(np.max(arr)-np.min(arr)) #normalize38 ret = np.expand_dims(norm, axis=0)39 return(ret)40 41 42 43def lineage_population_model():44 selected_box2 = st.sidebar.selectbox(45 'Choose Example Input',46 (['Example_1.png'])47 )48 49 st.title('Predict Cell Lineage Populations')50 instructions = """51 Predict the population of cells in C. elegans embryo using fluorescence microscopy data. \n52 Either upload your own image or select from the sidebar to get a preconfigured image. 53 The image you select or upload will be fed through the Deep Neural Network in real-time 54 and the output will be displayed to the screen.55 """56 st.text(instructions)57 file = st.file_uploader('Upload an image or choose an example')58 example_image = Image.open('./images/lineage_population_examples/'+selected_box2).convert("RGB")59 60 col1, col2= st.columns(2)61 62 if file:63 input = Image.open(file).convert("RGB")64 fig1 = px.imshow(input, binary_string=True, labels=dict(x="Input Image"))65 fig1.update(layout_coloraxis_showscale=False)66 fig1.update_layout(margin=dict(l=0, r=0, b=0, t=0))67 col1.plotly_chart(fig1, use_container_width=True)68 else:69 input = example_image70 fig1 = px.imshow(input, binary_string=True, labels=dict(x="Input Image"))71 fig1.update(layout_coloraxis_showscale=False)72 fig1.update_layout(margin=dict(l=0, r=0, b=0, t=0))73 col1.plotly_chart(fig1, use_container_width=True)74 75 pressed = st.button('Run')76 if pressed:77 st.empty()78 output = onnx_predict_lineage_population(np.array(input))79 scaled_output = scale_model_outputs(scaler_path="./scaler.gz", data=output)80 81 for i in range(len(scaled_output[0])):82 scaled_output[0][i]=int(round(scaled_output[0][i]))83 84 df = pd.DataFrame({"Lineage":["A", "E", "M", "P", "C", "D", "Z"] , "Population": scaled_output[0]})85 col2.table(df)