Spaces:
Sleeping
Sleeping
Commit
·
3f1d82b
1
Parent(s):
0cdca7b
Track image files using Git LFS
Browse files- .DS_Store +0 -0
- .gitattributes +1 -0
- app.py +130 -15
- examples/example1.jpg +3 -0
- examples/example2.jpg +3 -0
- examples/example3.jpg +3 -0
- examples/example4.jpg +3 -0
- examples/example5.jpg +3 -0
- examples/example6.jpg +3 -0
- examples/example7.jpg +3 -0
.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
*.jpg filter=lfs diff=lfs merge=lfs -text
|
app.py
CHANGED
|
@@ -1,3 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import torch
|
| 3 |
import numpy as np
|
|
@@ -22,10 +105,7 @@ model = smp.UnetPlusPlus(
|
|
| 22 |
encoder_name='resnet34',
|
| 23 |
encoder_depth=5,
|
| 24 |
encoder_weights='imagenet',
|
| 25 |
-
decoder_use_norm='batchnorm',
|
| 26 |
decoder_channels=(256, 128, 64, 32, 16),
|
| 27 |
-
decoder_attention_type=None,
|
| 28 |
-
decoder_interpolation='nearest',
|
| 29 |
in_channels=1,
|
| 30 |
classes=1,
|
| 31 |
activation=None
|
|
@@ -46,7 +126,7 @@ transform = get_transform(CONFIG["img_size"])
|
|
| 46 |
|
| 47 |
# ─── Prediction Function ───────────────────────────────────
|
| 48 |
def predict(image):
|
| 49 |
-
image = image.convert("L") #
|
| 50 |
img_np = np.array(image)
|
| 51 |
img_tensor = transform(image=img_np)["image"].unsqueeze(0).to(device)
|
| 52 |
|
|
@@ -57,16 +137,51 @@ def predict(image):
|
|
| 57 |
mask_img = Image.fromarray((mask * 255).astype(np.uint8))
|
| 58 |
return mask_img
|
| 59 |
|
| 60 |
-
# ─── Gradio
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
|
|
|
| 71 |
if __name__ == "__main__":
|
| 72 |
-
demo.launch()
|
|
|
|
| 1 |
+
# # +++++++++++++++ Fist, Version: 1.0.0 +++++++++++++++++++++
|
| 2 |
+
# # Last Updated: 08 July 2025
|
| 3 |
+
# # Fibril Segmentation with UNet++ using Gradio
|
| 4 |
+
|
| 5 |
+
# import os
|
| 6 |
+
# import torch
|
| 7 |
+
# import numpy as np
|
| 8 |
+
# from PIL import Image
|
| 9 |
+
# import albumentations as A
|
| 10 |
+
# from albumentations.pytorch import ToTensorV2
|
| 11 |
+
# import segmentation_models_pytorch as smp
|
| 12 |
+
# import gradio as gr
|
| 13 |
+
|
| 14 |
+
# # ─── Configuration ─────────────────────────────────────────
|
| 15 |
+
# CONFIG = {
|
| 16 |
+
# "model_path": "./model/encoder_resnet34_decoder_UnetPlusPlus_fibril_seg_model.pth",
|
| 17 |
+
# "img_size": 512
|
| 18 |
+
# }
|
| 19 |
+
|
| 20 |
+
# # ─── Device Setup ──────────────────────────────────────────
|
| 21 |
+
# device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 22 |
+
# print(f"✅ Using device: {device}")
|
| 23 |
+
|
| 24 |
+
# # ─── Load Model ────────────────────────────────────────────
|
| 25 |
+
# model = smp.UnetPlusPlus(
|
| 26 |
+
# encoder_name='resnet34',
|
| 27 |
+
# encoder_depth=5,
|
| 28 |
+
# encoder_weights='imagenet',
|
| 29 |
+
# decoder_use_norm='batchnorm',
|
| 30 |
+
# decoder_channels=(256, 128, 64, 32, 16),
|
| 31 |
+
# decoder_attention_type=None,
|
| 32 |
+
# decoder_interpolation='nearest',
|
| 33 |
+
# in_channels=1,
|
| 34 |
+
# classes=1,
|
| 35 |
+
# activation=None
|
| 36 |
+
# ).to(device)
|
| 37 |
+
|
| 38 |
+
# model.load_state_dict(torch.load(CONFIG["model_path"], map_location=device))
|
| 39 |
+
# model.eval()
|
| 40 |
+
|
| 41 |
+
# # ─── Transform Function ────────────────────────────────────
|
| 42 |
+
# def get_transform(size):
|
| 43 |
+
# return A.Compose([
|
| 44 |
+
# A.Resize(size, size),
|
| 45 |
+
# A.Normalize(mean=(0.5,), std=(0.5,)),
|
| 46 |
+
# ToTensorV2()
|
| 47 |
+
# ])
|
| 48 |
+
|
| 49 |
+
# transform = get_transform(CONFIG["img_size"])
|
| 50 |
+
|
| 51 |
+
# # ─── Prediction Function ───────────────────────────────────
|
| 52 |
+
# def predict(image):
|
| 53 |
+
# image = image.convert("L") # Convert to grayscale
|
| 54 |
+
# img_np = np.array(image)
|
| 55 |
+
# img_tensor = transform(image=img_np)["image"].unsqueeze(0).to(device)
|
| 56 |
+
|
| 57 |
+
# with torch.no_grad():
|
| 58 |
+
# pred = torch.sigmoid(model(img_tensor))
|
| 59 |
+
# mask = (pred > 0.5).float().cpu().squeeze().numpy()
|
| 60 |
+
|
| 61 |
+
# mask_img = Image.fromarray((mask * 255).astype(np.uint8))
|
| 62 |
+
# return mask_img
|
| 63 |
+
|
| 64 |
+
# # ─── Gradio Interface ──────────────────────────────────────
|
| 65 |
+
# demo = gr.Interface(
|
| 66 |
+
# fn=predict,
|
| 67 |
+
# inputs=gr.Image(type="pil", label="Upload Microscopy Image"),
|
| 68 |
+
# outputs=gr.Image(type="pil", label="Predicted Segmentation Mask"),
|
| 69 |
+
# title="Fibril Segmentation with Unet++",
|
| 70 |
+
# description="Upload a grayscale microscopy image to get its predicted segmentation mask.",
|
| 71 |
+
# allow_flagging="never",
|
| 72 |
+
# live=False
|
| 73 |
+
# )
|
| 74 |
+
|
| 75 |
+
# if __name__ == "__main__":
|
| 76 |
+
# demo.launch()
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
# # +++++++++++++++ Second Version: 1.1.0 ++++++++++++++++++++
|
| 81 |
+
# # Last Updated: 08 July 2025
|
| 82 |
+
# # Improvements: Added examples, better UI, and device handling
|
| 83 |
+
|
| 84 |
import os
|
| 85 |
import torch
|
| 86 |
import numpy as np
|
|
|
|
| 105 |
encoder_name='resnet34',
|
| 106 |
encoder_depth=5,
|
| 107 |
encoder_weights='imagenet',
|
|
|
|
| 108 |
decoder_channels=(256, 128, 64, 32, 16),
|
|
|
|
|
|
|
| 109 |
in_channels=1,
|
| 110 |
classes=1,
|
| 111 |
activation=None
|
|
|
|
| 126 |
|
| 127 |
# ─── Prediction Function ───────────────────────────────────
|
| 128 |
def predict(image):
|
| 129 |
+
image = image.convert("L") # Ensure grayscale
|
| 130 |
img_np = np.array(image)
|
| 131 |
img_tensor = transform(image=img_np)["image"].unsqueeze(0).to(device)
|
| 132 |
|
|
|
|
| 137 |
mask_img = Image.fromarray((mask * 255).astype(np.uint8))
|
| 138 |
return mask_img
|
| 139 |
|
| 140 |
+
# ─── Gradio UI (Improved) ──────────────────────────────────
|
| 141 |
+
examples = [
|
| 142 |
+
["examples/example1.jpg"],
|
| 143 |
+
["examples/example2.jpg"],
|
| 144 |
+
["examples/example3.jpg"],
|
| 145 |
+
["examples/example4.jpg"],
|
| 146 |
+
["examples/example5.jpg"],
|
| 147 |
+
["examples/example6.jpg"],
|
| 148 |
+
["examples/example7.jpg"]
|
| 149 |
+
]
|
| 150 |
+
|
| 151 |
+
css = """
|
| 152 |
+
.gradio-container {
|
| 153 |
+
max-width: 950px;
|
| 154 |
+
margin: auto;
|
| 155 |
+
}
|
| 156 |
+
.gr-button {
|
| 157 |
+
background-color: #4a90e2;
|
| 158 |
+
color: white;
|
| 159 |
+
border-radius: 5px;
|
| 160 |
+
}
|
| 161 |
+
.gr-button:hover {
|
| 162 |
+
background-color: #357ABD;
|
| 163 |
+
}
|
| 164 |
+
"""
|
| 165 |
+
|
| 166 |
+
with gr.Blocks(css=css) as demo:
|
| 167 |
+
gr.Markdown("## 🧬 Fibril Segmentation with UNet++")
|
| 168 |
+
gr.Markdown("Upload a **grayscale microscopy image**, and this model will predict the **segmentation mask of fibrillar structures**.\n\nModel: ResNet34 encoder + UNet++ decoder")
|
| 169 |
+
|
| 170 |
+
with gr.Row():
|
| 171 |
+
input_img = gr.Image(label="Upload Microscopy Image", type="pil")
|
| 172 |
+
output_mask = gr.Image(label="Predicted Segmentation Mask", type="pil")
|
| 173 |
+
|
| 174 |
+
submit_btn = gr.Button("Segment Image")
|
| 175 |
+
|
| 176 |
+
submit_btn.click(fn=predict, inputs=input_img, outputs=output_mask)
|
| 177 |
+
|
| 178 |
+
gr.Examples(
|
| 179 |
+
examples=examples,
|
| 180 |
+
inputs=input_img,
|
| 181 |
+
label="Try with Example Images",
|
| 182 |
+
cache_examples=False
|
| 183 |
+
)
|
| 184 |
|
| 185 |
+
# ─── Launch App ────────────────────────────────────────────
|
| 186 |
if __name__ == "__main__":
|
| 187 |
+
demo.launch()
|
examples/example1.jpg
ADDED
|
Git LFS Details
|
examples/example2.jpg
ADDED
|
Git LFS Details
|
examples/example3.jpg
ADDED
|
Git LFS Details
|
examples/example4.jpg
ADDED
|
Git LFS Details
|
examples/example5.jpg
ADDED
|
Git LFS Details
|
examples/example6.jpg
ADDED
|
Git LFS Details
|
examples/example7.jpg
ADDED
|
Git LFS Details
|