add spaces for ZeroGPU
Browse files- app.py +17 -5
- requirements.txt +1 -0
app.py
CHANGED
@@ -2,6 +2,7 @@ import cv2
|
|
2 |
import gradio as gr
|
3 |
import json
|
4 |
import numpy as np
|
|
|
5 |
import torch
|
6 |
import torch.nn as nn
|
7 |
|
@@ -70,6 +71,8 @@ greulich_and_pyle_ages = {k: np.asarray(v) for k, v in greulich_and_pyle_ages.it
|
|
70 |
model_grad_cam = ModelForGradCAM(model_list[0])
|
71 |
target_layers = [model_grad_cam.model.backbone.stages[-1]]
|
72 |
|
|
|
|
|
73 |
def predict_bone_age(Radiograph, Sex, Heatmap):
|
74 |
x0 = rearrange(Radiograph, "h w -> h w 1")
|
75 |
x = cfg_crop.val_transforms(image=x0)["image"]
|
@@ -118,18 +121,27 @@ def predict_bone_age(Radiograph, Sex, Heatmap):
|
|
118 |
if Heatmap:
|
119 |
targets = [ClassifierOutputTarget(round(bone_age))]
|
120 |
with GradCAM(model=model_grad_cam, target_layers=target_layers) as cam:
|
121 |
-
grayscale_cam = cam(
|
|
|
|
|
122 |
|
123 |
-
heatmap = cv2.applyColorMap(
|
|
|
|
|
124 |
image = cv2.cvtColor(x[0, 0].cpu().numpy().astype("uint8"), cv2.COLOR_GRAY2RGB)
|
125 |
image_weight = 0.6
|
126 |
grad_cam_image = (1 - image_weight) * heatmap[..., ::-1] + image_weight * image
|
127 |
grad_cam_image = grad_cam_image.astype("uint8")
|
128 |
else:
|
129 |
# if no heatmap desired, just show image
|
130 |
-
grad_cam_image = cv2.cvtColor(
|
|
|
|
|
131 |
|
132 |
-
return
|
|
|
|
|
|
|
133 |
|
134 |
|
135 |
image = gr.Image(image_mode="L")
|
@@ -170,7 +182,7 @@ with gr.Blocks() as demo:
|
|
170 |
["examples/10043.png", "Female", "No"],
|
171 |
["examples/8888.png", "Female", "Yes"],
|
172 |
],
|
173 |
-
cache_examples=False
|
174 |
)
|
175 |
|
176 |
if __name__ == "__main__":
|
|
|
2 |
import gradio as gr
|
3 |
import json
|
4 |
import numpy as np
|
5 |
+
import spaces
|
6 |
import torch
|
7 |
import torch.nn as nn
|
8 |
|
|
|
71 |
model_grad_cam = ModelForGradCAM(model_list[0])
|
72 |
target_layers = [model_grad_cam.model.backbone.stages[-1]]
|
73 |
|
74 |
+
|
75 |
+
@spaces.GPU
|
76 |
def predict_bone_age(Radiograph, Sex, Heatmap):
|
77 |
x0 = rearrange(Radiograph, "h w -> h w 1")
|
78 |
x = cfg_crop.val_transforms(image=x0)["image"]
|
|
|
121 |
if Heatmap:
|
122 |
targets = [ClassifierOutputTarget(round(bone_age))]
|
123 |
with GradCAM(model=model_grad_cam, target_layers=target_layers) as cam:
|
124 |
+
grayscale_cam = cam(
|
125 |
+
input_tensor=x.to(device).float(), targets=targets, eigen_smooth=True
|
126 |
+
)
|
127 |
|
128 |
+
heatmap = cv2.applyColorMap(
|
129 |
+
(grayscale_cam[0] * 255).astype("uint8"), cv2.COLORMAP_JET
|
130 |
+
)
|
131 |
image = cv2.cvtColor(x[0, 0].cpu().numpy().astype("uint8"), cv2.COLOR_GRAY2RGB)
|
132 |
image_weight = 0.6
|
133 |
grad_cam_image = (1 - image_weight) * heatmap[..., ::-1] + image_weight * image
|
134 |
grad_cam_image = grad_cam_image.astype("uint8")
|
135 |
else:
|
136 |
# if no heatmap desired, just show image
|
137 |
+
grad_cam_image = cv2.cvtColor(
|
138 |
+
x[0, 0].cpu().numpy().astype("uint8"), cv2.COLOR_GRAY2RGB
|
139 |
+
)
|
140 |
|
141 |
+
return (
|
142 |
+
f"Predicted bone age: {bone_age_str}\n\nThe closest Greulich & Pyle bone ages are:\n 1) {closest1}\n 2) {closest2}",
|
143 |
+
grad_cam_image,
|
144 |
+
)
|
145 |
|
146 |
|
147 |
image = gr.Image(image_mode="L")
|
|
|
182 |
["examples/10043.png", "Female", "No"],
|
183 |
["examples/8888.png", "Female", "Yes"],
|
184 |
],
|
185 |
+
cache_examples=False,
|
186 |
)
|
187 |
|
188 |
if __name__ == "__main__":
|
requirements.txt
CHANGED
@@ -3,5 +3,6 @@ einops
|
|
3 |
grad-cam
|
4 |
gradio
|
5 |
scikit-image
|
|
|
6 |
timm
|
7 |
torch
|
|
|
3 |
grad-cam
|
4 |
gradio
|
5 |
scikit-image
|
6 |
+
spaces
|
7 |
timm
|
8 |
torch
|