Update app.py
Browse files
app.py
CHANGED
|
@@ -22,16 +22,24 @@ def gradio_demo(model_name, sequence_input, image):
|
|
| 22 |
dataset = "HPA"
|
| 23 |
|
| 24 |
|
| 25 |
-
nucleus_image = image['image']
|
| 26 |
-
protein_image = image['mask']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
protein_image = process_image(protein_image, dataset, "nucleus")
|
| 30 |
-
protein_image = 1.0*(protein_image > .5)
|
| 31 |
-
print(f'{nucleus_image=}')
|
| 32 |
-
print(f'{protein_image.shape=}')
|
| 33 |
|
| 34 |
-
|
|
|
|
| 35 |
sequence_input=sequence_input,
|
| 36 |
nucleus_image=nucleus_image,
|
| 37 |
protein_image=protein_image,
|
|
@@ -40,35 +48,16 @@ def gradio_demo(model_name, sequence_input, image):
|
|
| 40 |
device=device,
|
| 41 |
)
|
| 42 |
|
| 43 |
-
|
| 44 |
-
protein_image = protein_image * 1.0
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
# Plot the heatmap
|
| 48 |
-
plt.imshow(heatmap.cpu(), cmap="rainbow", interpolation="bicubic")
|
| 49 |
-
plt.axis("off")
|
| 50 |
-
|
| 51 |
-
# Save the plot to a temporary file
|
| 52 |
-
plt.savefig("temp.png", bbox_inches="tight", dpi=256)
|
| 53 |
-
|
| 54 |
-
# Open the temporary file as a PIL image
|
| 55 |
-
heatmap = Image.open("temp.png")
|
| 56 |
-
|
| 57 |
-
return (
|
| 58 |
-
T.ToPILImage()(nucleus_image[0, 0]),
|
| 59 |
-
T.ToPILImage()(protein_image),
|
| 60 |
-
T.ToPILImage()(threshold),
|
| 61 |
-
heatmap,
|
| 62 |
-
)
|
| 63 |
|
| 64 |
|
| 65 |
with gr.Blocks() as demo:
|
| 66 |
gr.Markdown("Select the prediction model.")
|
| 67 |
gr.Markdown(
|
| 68 |
-
"CELL-E_2_HPA_2560 is a good general purpose model for various cell types using ICC-IF."
|
| 69 |
)
|
| 70 |
gr.Markdown(
|
| 71 |
-
"CELL-E_2_OpenCell_2560 is trained on OpenCell and is good more live-cell predictions on HEK cells."
|
| 72 |
)
|
| 73 |
with gr.Row():
|
| 74 |
model_name = gr.Dropdown(
|
|
@@ -88,14 +77,14 @@ with gr.Blocks() as demo:
|
|
| 88 |
)
|
| 89 |
with gr.Row():
|
| 90 |
gr.Markdown(
|
| 91 |
-
"Uploading a nucleus image is necessary. A random crop of 256 x 256 will be applied if larger. We provide default images in [images](https://huggingface.co/spaces/HuangLab/CELL-E_2/tree/main/images)"
|
| 92 |
)
|
| 93 |
-
gr.Markdown("The protein image is optional and is just used for display.")
|
| 94 |
|
| 95 |
with gr.Row().style(equal_height=True):
|
| 96 |
nucleus_image = gr.Image(
|
| 97 |
source="upload",
|
| 98 |
tool="sketch",
|
|
|
|
| 99 |
label="Nucleus Image",
|
| 100 |
line_color="white",
|
| 101 |
interactive=True,
|
|
@@ -104,12 +93,11 @@ with gr.Blocks() as demo:
|
|
| 104 |
)
|
| 105 |
|
| 106 |
with gr.Row():
|
| 107 |
-
gr.Markdown("
|
| 108 |
|
| 109 |
with gr.Row().style(equal_height=True):
|
| 110 |
-
predicted_sequence = gr.Textbox(
|
| 111 |
-
|
| 112 |
-
)
|
| 113 |
|
| 114 |
with gr.Row():
|
| 115 |
button = gr.Button("Run Model")
|
|
@@ -120,4 +108,4 @@ with gr.Blocks() as demo:
|
|
| 120 |
|
| 121 |
button.click(gradio_demo, inputs, outputs)
|
| 122 |
|
| 123 |
-
demo.launch(
|
|
|
|
| 22 |
dataset = "HPA"
|
| 23 |
|
| 24 |
|
| 25 |
+
nucleus_image = image['image'].convert('L')
|
| 26 |
+
protein_image = image['mask'].convert('L')
|
| 27 |
+
|
| 28 |
+
to_tensor = T.ToTensor()
|
| 29 |
+
nucleus_tensor = to_tensor(nucleus_image)
|
| 30 |
+
protein_tensor = to_tensor(protein_image)
|
| 31 |
+
stacked_images = torch.stack([nucleus_tensor, protein_tensor], dim=0)
|
| 32 |
+
processed_images = process_image(stacked_images, dataset)
|
| 33 |
+
|
| 34 |
+
nucleus_image = processed_images[0].unsqueeze(0)
|
| 35 |
+
protein_image = processed_images[1].unsqueeze(0)
|
| 36 |
+
protein_image = protein_image > 0
|
| 37 |
+
protein_image = 1.0 * protein_image
|
| 38 |
|
| 39 |
+
print(f'{protein_image.sum()}')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
|
| 42 |
+
formatted_predicted_sequence = run_sequence_prediction(
|
| 43 |
sequence_input=sequence_input,
|
| 44 |
nucleus_image=nucleus_image,
|
| 45 |
protein_image=protein_image,
|
|
|
|
| 48 |
device=device,
|
| 49 |
)
|
| 50 |
|
| 51 |
+
return formatted_predicted_sequence
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
|
| 54 |
with gr.Blocks() as demo:
|
| 55 |
gr.Markdown("Select the prediction model.")
|
| 56 |
gr.Markdown(
|
| 57 |
+
"- CELL-E_2_HPA_2560 is a good general purpose model for various cell types using ICC-IF."
|
| 58 |
)
|
| 59 |
gr.Markdown(
|
| 60 |
+
"- CELL-E_2_OpenCell_2560 is trained on OpenCell and is good more live-cell predictions on HEK cells."
|
| 61 |
)
|
| 62 |
with gr.Row():
|
| 63 |
model_name = gr.Dropdown(
|
|
|
|
| 77 |
)
|
| 78 |
with gr.Row():
|
| 79 |
gr.Markdown(
|
| 80 |
+
"Uploading a nucleus image is necessary. A random crop of 256 x 256 will be applied if larger. We provide default images in [images](https://huggingface.co/spaces/HuangLab/CELL-E_2/tree/main/images). Draw the desired localization on top of the nucelus image."
|
| 81 |
)
|
|
|
|
| 82 |
|
| 83 |
with gr.Row().style(equal_height=True):
|
| 84 |
nucleus_image = gr.Image(
|
| 85 |
source="upload",
|
| 86 |
tool="sketch",
|
| 87 |
+
invert_colors=True,
|
| 88 |
label="Nucleus Image",
|
| 89 |
line_color="white",
|
| 90 |
interactive=True,
|
|
|
|
| 93 |
)
|
| 94 |
|
| 95 |
with gr.Row():
|
| 96 |
+
gr.Markdown("Sequence predictions are show below.")
|
| 97 |
|
| 98 |
with gr.Row().style(equal_height=True):
|
| 99 |
+
predicted_sequence = gr.Textbox(label='Predicted Sequence')
|
| 100 |
+
|
|
|
|
| 101 |
|
| 102 |
with gr.Row():
|
| 103 |
button = gr.Button("Run Model")
|
|
|
|
| 108 |
|
| 109 |
button.click(gradio_demo, inputs, outputs)
|
| 110 |
|
| 111 |
+
demo.launch(enable_queue=True)
|