Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,120 +1,126 @@
|
|
1 |
-
from fastapi import FastAPI, Query
|
2 |
-
from pydantic import BaseModel
|
3 |
-
from typing import List, Tuple
|
4 |
-
from fastapi import Body
|
5 |
-
|
6 |
-
import torch
|
7 |
-
import torchxrayvision as xrv
|
8 |
-
import torchvision
|
9 |
-
import skimage.io
|
10 |
-
import numpy as np
|
11 |
-
import requests
|
12 |
-
import cv2
|
13 |
-
|
14 |
-
from io import BytesIO
|
15 |
-
import matplotlib.pyplot as plt
|
16 |
-
from pytorch_grad_cam import GradCAM
|
17 |
-
from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
|
18 |
-
from pytorch_grad_cam.utils.image import show_cam_on_image
|
19 |
-
|
20 |
-
app = FastAPI()
|
21 |
-
|
22 |
-
model = xrv.models.DenseNet(weights="densenet121-res224-all")
|
23 |
-
model.eval()
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
)
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
)
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, Query
|
2 |
+
from pydantic import BaseModel
|
3 |
+
from typing import List, Tuple
|
4 |
+
from fastapi import Body
|
5 |
+
|
6 |
+
import torch
|
7 |
+
import torchxrayvision as xrv
|
8 |
+
import torchvision
|
9 |
+
import skimage.io
|
10 |
+
import numpy as np
|
11 |
+
import requests
|
12 |
+
import cv2
|
13 |
+
|
14 |
+
from io import BytesIO
|
15 |
+
import matplotlib.pyplot as plt
|
16 |
+
from pytorch_grad_cam import GradCAM
|
17 |
+
from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
|
18 |
+
from pytorch_grad_cam.utils.image import show_cam_on_image
|
19 |
+
|
20 |
+
app = FastAPI()
|
21 |
+
|
22 |
+
model = xrv.models.DenseNet(weights="densenet121-res224-all")
|
23 |
+
model.eval()
|
24 |
+
|
25 |
+
|
26 |
+
import os
|
27 |
+
|
28 |
+
# Set cache directory to a writable location
|
29 |
+
os.environ['TORCHXRAYVISION_CACHE'] = './torchxrayvision_cache'
|
30 |
+
os.environ['MPLCONFIGDIR'] = './.config/matplotlib' # For matplotlib font issues
|
31 |
+
|
32 |
+
def preprocess_image_from_url(image_url: str) -> torch.Tensor:
|
33 |
+
response = requests.get(image_url)
|
34 |
+
img = skimage.io.imread(BytesIO(response.content))
|
35 |
+
img = xrv.datasets.normalize(img, 255)
|
36 |
+
|
37 |
+
if img.ndim == 3:
|
38 |
+
img = img.mean(2)[None, ...]
|
39 |
+
elif img.ndim == 2:
|
40 |
+
img = img[None, ...]
|
41 |
+
|
42 |
+
transform = torchvision.transforms.Compose([
|
43 |
+
xrv.datasets.XRayCenterCrop(),
|
44 |
+
xrv.datasets.XRayResizer(224)
|
45 |
+
])
|
46 |
+
img = transform(img)
|
47 |
+
img_tensor = torch.from_numpy(img)
|
48 |
+
return img_tensor
|
49 |
+
|
50 |
+
|
51 |
+
def get_predictions_and_bounding_box(img_tensor: torch.Tensor):
|
52 |
+
with torch.no_grad():
|
53 |
+
output = model(img_tensor[None, ...])[0]
|
54 |
+
|
55 |
+
predictions = dict(zip(model.pathologies, output.numpy()))
|
56 |
+
sorted_preds = sorted(predictions.items(), key=lambda x: -x[1])
|
57 |
+
|
58 |
+
top_pred_label, top_conf = sorted_preds[0]
|
59 |
+
top_pred_index = list(model.pathologies).index(top_pred_label)
|
60 |
+
|
61 |
+
target_layer = model.features[-1]
|
62 |
+
cam = GradCAM(model=model, target_layers=[target_layer])
|
63 |
+
grayscale_cam = cam(input_tensor=img_tensor[None, ...],
|
64 |
+
targets=[ClassifierOutputTarget(top_pred_index)])[0, :]
|
65 |
+
|
66 |
+
input_img = img_tensor.numpy()[0]
|
67 |
+
input_img_norm = (input_img - input_img.min()) / (input_img.max() - input_img.min())
|
68 |
+
input_img_rgb = cv2.cvtColor((input_img_norm * 255).astype(np.uint8), cv2.COLOR_GRAY2RGB)
|
69 |
+
|
70 |
+
cam_resized = cv2.resize(grayscale_cam, (224, 224))
|
71 |
+
cam_uint8 = (cam_resized * 255).astype(np.uint8)
|
72 |
+
_, thresh = cv2.threshold(cam_uint8, 100, 255, cv2.THRESH_BINARY)
|
73 |
+
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
74 |
+
|
75 |
+
bounding_boxes = []
|
76 |
+
for cnt in contours:
|
77 |
+
x, y, w, h = cv2.boundingRect(cnt)
|
78 |
+
bounding_boxes.append((int(x), int(y), int(x + w), int(y + h)))
|
79 |
+
|
80 |
+
return sorted_preds, bounding_boxes
|
81 |
+
|
82 |
+
|
83 |
+
class Prediction(BaseModel):
|
84 |
+
label: str
|
85 |
+
confidence: float
|
86 |
+
|
87 |
+
|
88 |
+
class PredictionResponse(BaseModel):
|
89 |
+
predictions: List[Prediction]
|
90 |
+
top_prediction_bounding_boxes: List[Tuple[int, int, int, int]]
|
91 |
+
|
92 |
+
|
93 |
+
@app.get("/predict", response_model=PredictionResponse)
|
94 |
+
def predict(image_url: str = Query(..., description="URL of chest X-ray image")):
|
95 |
+
try:
|
96 |
+
img_tensor = preprocess_image_from_url(image_url)
|
97 |
+
preds, bboxes = get_predictions_and_bounding_box(img_tensor)
|
98 |
+
prediction_list = [Prediction(label=label, confidence=float(conf)) for label, conf in preds]
|
99 |
+
|
100 |
+
return PredictionResponse(
|
101 |
+
predictions=prediction_list,
|
102 |
+
top_prediction_bounding_boxes=bboxes
|
103 |
+
)
|
104 |
+
except Exception as e:
|
105 |
+
return {"error": str(e)}
|
106 |
+
|
107 |
+
class URLRequest(BaseModel):
|
108 |
+
url: str
|
109 |
+
|
110 |
+
@app.post("/predict", response_model=PredictionResponse)
|
111 |
+
def predict_from_url(body: URLRequest):
|
112 |
+
try:
|
113 |
+
img_tensor = preprocess_image_from_url(body.url)
|
114 |
+
preds, bboxes = get_predictions_and_bounding_box(img_tensor)
|
115 |
+
prediction_list = [Prediction(label=label, confidence=float(conf)) for label, conf in preds]
|
116 |
+
|
117 |
+
return PredictionResponse(
|
118 |
+
predictions=prediction_list,
|
119 |
+
top_prediction_bounding_boxes=bboxes
|
120 |
+
)
|
121 |
+
except Exception as e:
|
122 |
+
return {"error": str(e)}
|
123 |
+
|
124 |
+
|
125 |
+
|
126 |
+
# uvicorn app:app --reload
|