shubham5524 commited on
Commit
e683b39
·
verified ·
1 Parent(s): 0219b19

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +126 -120
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
- def preprocess_image_from_url(image_url: str) -> torch.Tensor:
27
- response = requests.get(image_url)
28
- img = skimage.io.imread(BytesIO(response.content))
29
- img = xrv.datasets.normalize(img, 255)
30
-
31
- if img.ndim == 3:
32
- img = img.mean(2)[None, ...]
33
- elif img.ndim == 2:
34
- img = img[None, ...]
35
-
36
- transform = torchvision.transforms.Compose([
37
- xrv.datasets.XRayCenterCrop(),
38
- xrv.datasets.XRayResizer(224)
39
- ])
40
- img = transform(img)
41
- img_tensor = torch.from_numpy(img)
42
- return img_tensor
43
-
44
-
45
- def get_predictions_and_bounding_box(img_tensor: torch.Tensor):
46
- with torch.no_grad():
47
- output = model(img_tensor[None, ...])[0]
48
-
49
- predictions = dict(zip(model.pathologies, output.numpy()))
50
- sorted_preds = sorted(predictions.items(), key=lambda x: -x[1])
51
-
52
- top_pred_label, top_conf = sorted_preds[0]
53
- top_pred_index = list(model.pathologies).index(top_pred_label)
54
-
55
- target_layer = model.features[-1]
56
- cam = GradCAM(model=model, target_layers=[target_layer])
57
- grayscale_cam = cam(input_tensor=img_tensor[None, ...],
58
- targets=[ClassifierOutputTarget(top_pred_index)])[0, :]
59
-
60
- input_img = img_tensor.numpy()[0]
61
- input_img_norm = (input_img - input_img.min()) / (input_img.max() - input_img.min())
62
- input_img_rgb = cv2.cvtColor((input_img_norm * 255).astype(np.uint8), cv2.COLOR_GRAY2RGB)
63
-
64
- cam_resized = cv2.resize(grayscale_cam, (224, 224))
65
- cam_uint8 = (cam_resized * 255).astype(np.uint8)
66
- _, thresh = cv2.threshold(cam_uint8, 100, 255, cv2.THRESH_BINARY)
67
- contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
68
-
69
- bounding_boxes = []
70
- for cnt in contours:
71
- x, y, w, h = cv2.boundingRect(cnt)
72
- bounding_boxes.append((int(x), int(y), int(x + w), int(y + h)))
73
-
74
- return sorted_preds, bounding_boxes
75
-
76
-
77
- class Prediction(BaseModel):
78
- label: str
79
- confidence: float
80
-
81
-
82
- class PredictionResponse(BaseModel):
83
- predictions: List[Prediction]
84
- top_prediction_bounding_boxes: List[Tuple[int, int, int, int]]
85
-
86
-
87
- @app.get("/predict", response_model=PredictionResponse)
88
- def predict(image_url: str = Query(..., description="URL of chest X-ray image")):
89
- try:
90
- img_tensor = preprocess_image_from_url(image_url)
91
- preds, bboxes = get_predictions_and_bounding_box(img_tensor)
92
- prediction_list = [Prediction(label=label, confidence=float(conf)) for label, conf in preds]
93
-
94
- return PredictionResponse(
95
- predictions=prediction_list,
96
- top_prediction_bounding_boxes=bboxes
97
- )
98
- except Exception as e:
99
- return {"error": str(e)}
100
-
101
- class URLRequest(BaseModel):
102
- url: str
103
-
104
- @app.post("/predict", response_model=PredictionResponse)
105
- def predict_from_url(body: URLRequest):
106
- try:
107
- img_tensor = preprocess_image_from_url(body.url)
108
- preds, bboxes = get_predictions_and_bounding_box(img_tensor)
109
- prediction_list = [Prediction(label=label, confidence=float(conf)) for label, conf in preds]
110
-
111
- return PredictionResponse(
112
- predictions=prediction_list,
113
- top_prediction_bounding_boxes=bboxes
114
- )
115
- except Exception as e:
116
- return {"error": str(e)}
117
-
118
-
119
-
120
- # uvicorn app:app --reload
 
 
 
 
 
 
 
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