Update
Browse files- app.py +17 -6
- extract.py +53 -24
app.py
CHANGED
@@ -1,35 +1,41 @@
|
|
1 |
import gradio as gr
|
2 |
import predict as predict
|
3 |
-
|
4 |
-
from pprint import pprint
|
5 |
-
|
6 |
-
translator = Translator()
|
7 |
-
|
8 |
|
9 |
def upload_file(files):
|
10 |
file_paths = [file.name for file in files]
|
11 |
return file_paths
|
12 |
|
13 |
|
14 |
-
def process_file(webcam_filepath, upload_filepath):
|
15 |
result = []
|
16 |
if webcam_filepath == None:
|
17 |
sorted_classes = predict.predict_image(upload_filepath)
|
18 |
for class_label, class_prob in sorted_classes:
|
19 |
class_prob = class_prob.item().__round__(2)
|
20 |
result.append(f"{class_label}: {class_prob}%")
|
|
|
|
|
|
|
21 |
return result
|
22 |
elif upload_filepath == None:
|
23 |
sorted_classes = predict.predict_image(webcam_filepath)
|
24 |
for class_label, class_prob in sorted_classes:
|
25 |
class_prob = class_prob.item().__round__(2)
|
26 |
result.append(f"{class_label}: {class_prob}%")
|
|
|
|
|
|
|
27 |
return result
|
28 |
else:
|
29 |
sorted_classes = predict.predict_image(upload_filepath)
|
30 |
for class_label, class_prob in sorted_classes:
|
31 |
class_prob = class_prob.item().__round__(2)
|
32 |
result.append(f"{class_label}: {class_prob}%")
|
|
|
|
|
|
|
|
|
33 |
return result
|
34 |
|
35 |
|
@@ -46,7 +52,12 @@ demo = gr.Interface(
|
|
46 |
gr.outputs.Textbox(label="Probability 1"),
|
47 |
gr.outputs.Textbox(label="Probability 2"),
|
48 |
gr.outputs.Textbox(label="Probability 3"),
|
|
|
|
|
|
|
|
|
49 |
],
|
|
|
50 |
)
|
51 |
|
52 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import predict as predict
|
3 |
+
import extract as extract
|
|
|
|
|
|
|
|
|
4 |
|
5 |
def upload_file(files):
|
6 |
file_paths = [file.name for file in files]
|
7 |
return file_paths
|
8 |
|
9 |
|
10 |
+
def process_file(webcam_filepath, upload_filepath, ):
|
11 |
result = []
|
12 |
if webcam_filepath == None:
|
13 |
sorted_classes = predict.predict_image(upload_filepath)
|
14 |
for class_label, class_prob in sorted_classes:
|
15 |
class_prob = class_prob.item().__round__(2)
|
16 |
result.append(f"{class_label}: {class_prob}%")
|
17 |
+
cam = extract.extract_gradcam(upload_filepath, save_path="gradcam.jpg")
|
18 |
+
result = result[:3]
|
19 |
+
result.append("gradcam.jpg")
|
20 |
return result
|
21 |
elif upload_filepath == None:
|
22 |
sorted_classes = predict.predict_image(webcam_filepath)
|
23 |
for class_label, class_prob in sorted_classes:
|
24 |
class_prob = class_prob.item().__round__(2)
|
25 |
result.append(f"{class_label}: {class_prob}%")
|
26 |
+
cam = extract.extract_gradcam(webcam_filepath, save_path="gradcam.jpg")
|
27 |
+
result = result[:3]
|
28 |
+
result.append("gradcam.jpg")
|
29 |
return result
|
30 |
else:
|
31 |
sorted_classes = predict.predict_image(upload_filepath)
|
32 |
for class_label, class_prob in sorted_classes:
|
33 |
class_prob = class_prob.item().__round__(2)
|
34 |
result.append(f"{class_label}: {class_prob}%")
|
35 |
+
cam = extract.extract_gradcam(upload_filepath, save_path="gradcam.jpg")
|
36 |
+
# Only keep the first 3 results
|
37 |
+
result = result[:3]
|
38 |
+
result.append("gradcam.jpg")
|
39 |
return result
|
40 |
|
41 |
|
|
|
52 |
gr.outputs.Textbox(label="Probability 1"),
|
53 |
gr.outputs.Textbox(label="Probability 2"),
|
54 |
gr.outputs.Textbox(label="Probability 3"),
|
55 |
+
# GradCAM
|
56 |
+
gr.outputs.Image(label="GradCAM++", type="filepath"),
|
57 |
+
|
58 |
+
|
59 |
],
|
60 |
+
|
61 |
)
|
62 |
|
63 |
demo.launch()
|
extract.py
CHANGED
@@ -8,10 +8,9 @@ from configs import *
|
|
8 |
import os, random
|
9 |
|
10 |
# Load your model (replace with your model class)
|
11 |
-
model = MODEL
|
12 |
model.load_state_dict(torch.load(MODEL_SAVE_PATH))
|
13 |
model.eval()
|
14 |
-
model = model.to(DEVICE)
|
15 |
|
16 |
# Find the target layer (modify this based on your model architecture)
|
17 |
target_layer = None
|
@@ -22,33 +21,63 @@ for child in model.features[-1]:
|
|
22 |
if target_layer is None:
|
23 |
raise ValueError("Invalid layer name: {}".format(target_layer))
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
image_path = random.choice(os.listdir("data/test/Task 1/" + disease))
|
29 |
-
image_path = "data/test/Task 1/" + disease + "/" + image_path
|
30 |
-
rgb_img = cv2.imread(image_path, 1)
|
31 |
-
rgb_img = np.float32(rgb_img) / 255
|
32 |
-
input_tensor = preprocess_image(rgb_img, mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
|
33 |
-
input_tensor = input_tensor.to(DEVICE)
|
34 |
|
35 |
-
|
36 |
-
|
37 |
|
38 |
-
|
39 |
-
|
40 |
|
41 |
-
|
42 |
-
|
43 |
|
44 |
-
|
45 |
-
|
46 |
|
47 |
-
|
48 |
-
|
49 |
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
import os, random
|
9 |
|
10 |
# Load your model (replace with your model class)
|
11 |
+
model = MODEL.to(DEVICE)
|
12 |
model.load_state_dict(torch.load(MODEL_SAVE_PATH))
|
13 |
model.eval()
|
|
|
14 |
|
15 |
# Find the target layer (modify this based on your model architecture)
|
16 |
target_layer = None
|
|
|
21 |
if target_layer is None:
|
22 |
raise ValueError("Invalid layer name: {}".format(target_layer))
|
23 |
|
24 |
+
def extract_gradcam(image_path=None, save_path=None):
|
25 |
+
if image_path is None:
|
26 |
+
for disease in CLASSES:
|
27 |
+
print("Processing", disease)
|
28 |
+
image_path = random.choice(os.listdir("data/test/Task 1/" + disease))
|
29 |
+
image_path = "data/test/Task 1/" + disease + "/" + image_path
|
30 |
+
rgb_img = cv2.imread(image_path, 1)
|
31 |
+
rgb_img = np.float32(rgb_img) / 255
|
32 |
+
input_tensor = preprocess_image(rgb_img, mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
|
33 |
+
input_tensor = input_tensor.to(DEVICE)
|
34 |
|
35 |
+
# Create a GradCAMPlusPlus object
|
36 |
+
cam = GradCAMPlusPlus(model=model, target_layers=[target_layer], use_cuda=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
+
# Generate the GradCAM heatmap
|
39 |
+
grayscale_cam = cam(input_tensor=input_tensor)[0]
|
40 |
|
41 |
+
# Apply a colormap to the grayscale heatmap
|
42 |
+
heatmap_colored = cv2.applyColorMap(np.uint8(255 * grayscale_cam), cv2.COLORMAP_JET)
|
43 |
|
44 |
+
# Ensure heatmap_colored has the same dtype as rgb_img
|
45 |
+
heatmap_colored = heatmap_colored.astype(np.float32) / 255
|
46 |
|
47 |
+
# Adjust the alpha value to control transparency
|
48 |
+
alpha = 0.3 # You can change this value to make the original image more or less transparent
|
49 |
|
50 |
+
# Overlay the colored heatmap on the original image
|
51 |
+
final_output = cv2.addWeighted(rgb_img, 0.3, heatmap_colored, 0.7, 0)
|
52 |
|
53 |
+
# Save the final output
|
54 |
+
cv2.imwrite(f'docs/efficientnet/gradcam/{disease}.jpg', (final_output * 255).astype(np.uint8))
|
55 |
+
else:
|
56 |
+
rgb_img = cv2.imread(image_path, 1)
|
57 |
+
rgb_img = np.float32(rgb_img) / 255
|
58 |
+
input_tensor = preprocess_image(rgb_img, mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
|
59 |
+
input_tensor = input_tensor.to(DEVICE)
|
60 |
|
61 |
+
# Create a GradCAMPlusPlus object
|
62 |
+
cam = GradCAMPlusPlus(model=model, target_layers=[target_layer], use_cuda=True)
|
63 |
+
|
64 |
+
# Generate the GradCAM heatmap
|
65 |
+
grayscale_cam = cam(input_tensor=input_tensor)[0]
|
66 |
+
|
67 |
+
# Apply a colormap to the grayscale heatmap
|
68 |
+
heatmap_colored = cv2.applyColorMap(np.uint8(255 * grayscale_cam), cv2.COLORMAP_JET)
|
69 |
+
|
70 |
+
# Ensure heatmap_colored has the same dtype as rgb_img
|
71 |
+
heatmap_colored = heatmap_colored.astype(np.float32) / 255
|
72 |
+
|
73 |
+
# Adjust the alpha value to control transparency
|
74 |
+
alpha = 0.3 # You can change this value to make the original image more or less transparent
|
75 |
+
|
76 |
+
# Overlay the colored heatmap on the original image
|
77 |
+
final_output = cv2.addWeighted(rgb_img, 0.3, heatmap_colored, 0.7, 0)
|
78 |
+
|
79 |
+
# Save the final output
|
80 |
+
cv2.imwrite(save_path, (final_output * 255).astype(np.uint8))
|
81 |
+
|
82 |
+
return save_path
|
83 |
+
|