Update code to save results to a zip
Browse files- app.py +144 -14
- tmp/,gitkeep +0 -0
app.py
CHANGED
|
@@ -7,16 +7,18 @@ from utils.utils import scipy_to_torch_sparse, genMatrixesLungsHeart
|
|
| 7 |
import scipy.sparse as sp
|
| 8 |
import torch
|
| 9 |
import pandas as pd
|
|
|
|
| 10 |
|
| 11 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
| 12 |
hybrid = None
|
| 13 |
|
| 14 |
-
def getDenseMask(landmarks):
|
|
|
|
| 15 |
RL = landmarks[0:44]
|
| 16 |
LL = landmarks[44:94]
|
| 17 |
H = landmarks[94:]
|
| 18 |
|
| 19 |
-
img = np.zeros([
|
| 20 |
|
| 21 |
RL = RL.reshape(-1, 1, 2).astype('int')
|
| 22 |
LL = LL.reshape(-1, 1, 2).astype('int')
|
|
@@ -28,11 +30,31 @@ def getDenseMask(landmarks):
|
|
| 28 |
|
| 29 |
return img
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
def drawOnTop(img, landmarks):
|
| 33 |
-
|
|
|
|
| 34 |
|
| 35 |
-
image = np.zeros([
|
| 36 |
image[:,:,0] = img + 0.3 * (output == 1).astype('float') - 0.1 * (output == 2).astype('float')
|
| 37 |
image[:,:,1] = img + 0.3 * (output == 2).astype('float') - 0.1 * (output == 1).astype('float')
|
| 38 |
image[:,:,2] = img - 0.1 * (output == 1).astype('float') - 0.2 * (output == 2).astype('float')
|
|
@@ -118,7 +140,28 @@ def preprocess(input_img):
|
|
| 118 |
|
| 119 |
return img, (h, w, padding)
|
| 120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
def segment(input_img):
|
| 123 |
global hybrid, device
|
| 124 |
|
|
@@ -126,25 +169,112 @@ def segment(input_img):
|
|
| 126 |
hybrid = loadModel(device)
|
| 127 |
|
| 128 |
input_img = cv2.imread(input_img, 0) / 255.0
|
|
|
|
| 129 |
|
| 130 |
img, (h, w, padding) = preprocess(input_img)
|
| 131 |
|
| 132 |
data = torch.from_numpy(img).unsqueeze(0).unsqueeze(0).to(device).float()
|
| 133 |
|
| 134 |
with torch.no_grad():
|
| 135 |
-
output = hybrid(data)[0].cpu().numpy().reshape(-1, 2)
|
| 136 |
-
|
| 137 |
-
|
| 138 |
|
| 139 |
output = output.astype('int')
|
| 140 |
|
| 141 |
-
|
| 142 |
-
LL = pd.DataFrame(output[44:94], columns=["x","y"])
|
| 143 |
-
H = pd.DataFrame(output[94:], columns=["x","y"])
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
if __name__ == "__main__":
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
demo.launch()
|
|
|
|
| 7 |
import scipy.sparse as sp
|
| 8 |
import torch
|
| 9 |
import pandas as pd
|
| 10 |
+
from zipfile import ZipFile
|
| 11 |
|
| 12 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
| 13 |
hybrid = None
|
| 14 |
|
| 15 |
+
def getDenseMask(landmarks, h, w):
|
| 16 |
+
|
| 17 |
RL = landmarks[0:44]
|
| 18 |
LL = landmarks[44:94]
|
| 19 |
H = landmarks[94:]
|
| 20 |
|
| 21 |
+
img = np.zeros([h, w], dtype = 'uint8')
|
| 22 |
|
| 23 |
RL = RL.reshape(-1, 1, 2).astype('int')
|
| 24 |
LL = LL.reshape(-1, 1, 2).astype('int')
|
|
|
|
| 30 |
|
| 31 |
return img
|
| 32 |
|
| 33 |
+
def getMasks(landmarks, h, w):
|
| 34 |
+
|
| 35 |
+
RL = landmarks[0:44]
|
| 36 |
+
LL = landmarks[44:94]
|
| 37 |
+
H = landmarks[94:]
|
| 38 |
+
|
| 39 |
+
RL = RL.reshape(-1, 1, 2).astype('int')
|
| 40 |
+
LL = LL.reshape(-1, 1, 2).astype('int')
|
| 41 |
+
H = H.reshape(-1, 1, 2).astype('int')
|
| 42 |
+
|
| 43 |
+
RL_mask = np.zeros([h, w], dtype = 'uint8')
|
| 44 |
+
LL_mask = np.zeros([h, w], dtype = 'uint8')
|
| 45 |
+
H_mask = np.zeros([h, w], dtype = 'uint8')
|
| 46 |
+
|
| 47 |
+
RL_mask = cv2.drawContours(RL_mask, [RL], -1, 255, -1)
|
| 48 |
+
LL_mask = cv2.drawContours(LL_mask, [LL], -1, 255, -1)
|
| 49 |
+
H_mask = cv2.drawContours(H_mask, [H], -1, 255, -1)
|
| 50 |
+
|
| 51 |
+
return RL_mask, LL_mask, H_mask
|
| 52 |
|
| 53 |
+
def drawOnTop(img, landmarks, original_shape):
|
| 54 |
+
h, w = original_shape
|
| 55 |
+
output = getDenseMask(landmarks, h, w)
|
| 56 |
|
| 57 |
+
image = np.zeros([h, w, 3])
|
| 58 |
image[:,:,0] = img + 0.3 * (output == 1).astype('float') - 0.1 * (output == 2).astype('float')
|
| 59 |
image[:,:,1] = img + 0.3 * (output == 2).astype('float') - 0.1 * (output == 1).astype('float')
|
| 60 |
image[:,:,2] = img - 0.1 * (output == 1).astype('float') - 0.2 * (output == 2).astype('float')
|
|
|
|
| 140 |
|
| 141 |
return img, (h, w, padding)
|
| 142 |
|
| 143 |
+
|
| 144 |
+
def removePreprocess(output, info):
|
| 145 |
+
h, w, padding = info
|
| 146 |
+
|
| 147 |
+
if h != 1024 or w != 1024:
|
| 148 |
+
output = output * h
|
| 149 |
|
| 150 |
+
padh, padw, auxh, auxw = padding
|
| 151 |
+
|
| 152 |
+
output[:, 0] = output[:, 0] - padw//2
|
| 153 |
+
output[:, 1] = output[:, 1] - padh//2
|
| 154 |
+
|
| 155 |
+
return output
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
def zip_files(files):
|
| 159 |
+
with ZipFile("complete_results.zip", "w") as zipObj:
|
| 160 |
+
for idx, file in enumerate(files):
|
| 161 |
+
zipObj.write(file, arcname=file.split("/")[-1])
|
| 162 |
+
return "complete_results.zip"
|
| 163 |
+
|
| 164 |
+
|
| 165 |
def segment(input_img):
|
| 166 |
global hybrid, device
|
| 167 |
|
|
|
|
| 169 |
hybrid = loadModel(device)
|
| 170 |
|
| 171 |
input_img = cv2.imread(input_img, 0) / 255.0
|
| 172 |
+
original_shape = input_img.shape[:2]
|
| 173 |
|
| 174 |
img, (h, w, padding) = preprocess(input_img)
|
| 175 |
|
| 176 |
data = torch.from_numpy(img).unsqueeze(0).unsqueeze(0).to(device).float()
|
| 177 |
|
| 178 |
with torch.no_grad():
|
| 179 |
+
output = hybrid(data)[0].cpu().numpy().reshape(-1, 2)
|
| 180 |
+
|
| 181 |
+
output = removePreprocess(output, (h, w, padding))
|
| 182 |
|
| 183 |
output = output.astype('int')
|
| 184 |
|
| 185 |
+
outseg = drawOnTop(input_img, output, original_shape)
|
|
|
|
|
|
|
| 186 |
|
| 187 |
+
seg_to_save = (outseg.copy() * 255).astype('uint8')
|
| 188 |
+
cv2.imwrite("tmp/overlap_segmentation.png" , cv2.cvtColor(seg_to_save, cv2.COLOR_RGB2BGR))
|
| 189 |
+
|
| 190 |
+
RL = output[0:44]
|
| 191 |
+
LL = output[44:94]
|
| 192 |
+
H = output[94:]
|
| 193 |
+
|
| 194 |
+
np.savetxt("tmp/RL_landmarks.txt", RL, delimiter=" ", fmt="%d")
|
| 195 |
+
np.savetxt("tmp/LL_landmarks.txt", LL, delimiter=" ", fmt="%d")
|
| 196 |
+
np.savetxt("tmp/H_landmarks.txt", H, delimiter=" ", fmt="%d")
|
| 197 |
+
|
| 198 |
+
RL_mask, LL_mask, H_mask = getMasks(output, original_shape[0], original_shape[1])
|
| 199 |
+
|
| 200 |
+
cv2.imwrite("tmp/RL_mask.png", RL_mask)
|
| 201 |
+
cv2.imwrite("tmp/LL_mask.png", LL_mask)
|
| 202 |
+
cv2.imwrite("tmp/H_mask.png", H_mask)
|
| 203 |
+
|
| 204 |
+
zip = zip_files(["tmp/RL_landmarks.txt", "tmp/LL_landmarks.txt", "tmp/H_landmarks.txt", "tmp/RL_mask.png", "tmp/LL_mask.png", "tmp/H_mask.png", "tmp/overlap_segmentation.png"])
|
| 205 |
+
|
| 206 |
+
return outseg, ["tmp/RL_landmarks.txt", "tmp/LL_landmarks.txt", "tmp/H_landmarks.txt", "tmp/RL_mask.png", "tmp/LL_mask.png", "tmp/H_mask.png", "tmp/overlap_segmentation.png", zip]
|
| 207 |
|
| 208 |
if __name__ == "__main__":
|
| 209 |
+
|
| 210 |
+
with gr.Blocks() as demo:
|
| 211 |
+
|
| 212 |
+
gr.Markdown("""
|
| 213 |
+
# Chest X-ray HybridGNet Segmentation.
|
| 214 |
+
|
| 215 |
+
Demo of the HybridGNet model introduced in "Improving anatomical plausibility in medical image segmentation via hybrid graph neural networks: applications to chest x-ray analysis."
|
| 216 |
+
|
| 217 |
+
Instructions:
|
| 218 |
+
1. Upload a chest X-ray image (PA or AP) in PNG or JPEG format.
|
| 219 |
+
2. Click on "Segment Image".
|
| 220 |
+
|
| 221 |
+
Note: Pre-processing is not needed, it will be done automatically and removed after the segmentation.
|
| 222 |
+
|
| 223 |
+
Please check citations below.
|
| 224 |
+
""")
|
| 225 |
+
|
| 226 |
+
with gr.Tab("Segment Image"):
|
| 227 |
+
with gr.Row():
|
| 228 |
+
with gr.Column():
|
| 229 |
+
image_input = gr.Image(type="filepath", height=750)
|
| 230 |
+
|
| 231 |
+
with gr.Row():
|
| 232 |
+
clear_button = gr.Button("Clear")
|
| 233 |
+
image_button = gr.Button("Segment Image")
|
| 234 |
+
|
| 235 |
+
gr.Examples(inputs=image_input, examples=['utils/example.jpg'])
|
| 236 |
+
|
| 237 |
+
with gr.Column():
|
| 238 |
+
image_output = gr.Image(type="filepath", height=750)
|
| 239 |
+
results = gr.File()
|
| 240 |
+
|
| 241 |
+
gr.Markdown("""If you use this code, please cite:
|
| 242 |
+
|
| 243 |
+
```
|
| 244 |
+
@article{gaggion2022TMI,
|
| 245 |
+
doi = {10.1109/tmi.2022.3224660},
|
| 246 |
+
url = {https://doi.org/10.1109%2Ftmi.2022.3224660},
|
| 247 |
+
year = 2022,
|
| 248 |
+
publisher = {Institute of Electrical and Electronics Engineers ({IEEE})},
|
| 249 |
+
author = {Nicolas Gaggion and Lucas Mansilla and Candelaria Mosquera and Diego H. Milone and Enzo Ferrante},
|
| 250 |
+
title = {Improving anatomical plausibility in medical image segmentation via hybrid graph neural networks: applications to chest x-ray analysis},
|
| 251 |
+
journal = {{IEEE} Transactions on Medical Imaging}
|
| 252 |
+
}
|
| 253 |
+
```
|
| 254 |
+
|
| 255 |
+
This model was trained following the procedure explained on:
|
| 256 |
+
|
| 257 |
+
```
|
| 258 |
+
@misc{gaggion2022ISBI,
|
| 259 |
+
title={Multi-center anatomical segmentation with heterogeneous labels via landmark-based models},
|
| 260 |
+
author={Nicolás Gaggion and Maria Vakalopoulou and Diego H. Milone and Enzo Ferrante},
|
| 261 |
+
year={2022},
|
| 262 |
+
eprint={2211.07395},
|
| 263 |
+
archivePrefix={arXiv},
|
| 264 |
+
primaryClass={eess.IV}
|
| 265 |
+
}
|
| 266 |
+
```
|
| 267 |
+
|
| 268 |
+
Author: Nicolás Gaggion
|
| 269 |
+
Website: [ngaggion.github.io](https://ngaggion.github.io/)
|
| 270 |
+
|
| 271 |
+
""")
|
| 272 |
+
|
| 273 |
+
|
| 274 |
+
clear_button.click(lambda: None, None, image_input, queue=False)
|
| 275 |
+
clear_button.click(lambda: None, None, image_output, queue=False)
|
| 276 |
+
|
| 277 |
+
image_button.click(segment, inputs=image_input, outputs=[image_output, results], queue=False)
|
| 278 |
+
|
| 279 |
+
|
| 280 |
demo.launch()
|
tmp/,gitkeep
ADDED
|
File without changes
|