Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,16 +9,7 @@ import io
|
|
9 |
import os
|
10 |
matplotlib.use('Agg') # Use non-interactive backend
|
11 |
|
12 |
-
#
|
13 |
-
MODEL_PATH = "chest_ct_binary_classifier_densenet_tf_20250427_182239"
|
14 |
-
model = tf.saved_model.load(MODEL_PATH)
|
15 |
-
infer = model.signatures["serving_default"] # Get the inference function
|
16 |
-
|
17 |
-
# Get input and output tensor names
|
18 |
-
input_tensor_name = list(infer.structured_input_signature[1].keys())[0]
|
19 |
-
output_tensor_name = list(infer.structured_outputs.keys())[0]
|
20 |
-
|
21 |
-
# Image size - matching what your model was trained on
|
22 |
IMG_SIZE = 256
|
23 |
|
24 |
# Function for preprocessing
|
@@ -26,29 +17,15 @@ def preprocess_image(image):
|
|
26 |
img = Image.fromarray(image).convert('RGB')
|
27 |
img = img.resize((IMG_SIZE, IMG_SIZE))
|
28 |
img_array = np.array(img) / 255.0
|
29 |
-
return np.expand_dims(img_array, axis=0)
|
30 |
|
31 |
-
#
|
32 |
-
def predict_with_saved_model(image_tensor):
|
33 |
-
# Create the input tensor with the right name
|
34 |
-
input_dict = {input_tensor_name: image_tensor}
|
35 |
-
# Run inference
|
36 |
-
output = infer(**input_dict)
|
37 |
-
# Get the prediction value
|
38 |
-
prediction = output[output_tensor_name].numpy()[0][0]
|
39 |
-
return prediction
|
40 |
-
|
41 |
-
# Generate Grad-CAM using the SavedModel
|
42 |
-
# Note: Grad-CAM is more complex with SavedModel format, so we'll use a simplified approach
|
43 |
def generate_attention_map(img_array, prediction):
|
44 |
-
#
|
45 |
-
# This is a placeholder - in production you may want to implement a proper CAM
|
46 |
-
|
47 |
-
# For demo purposes, we'll create a simple attention map based on image features
|
48 |
gray = cv2.cvtColor(img_array[0].astype(np.float32), cv2.COLOR_RGB2GRAY)
|
49 |
blur = cv2.GaussianBlur(gray, (5, 5), 0)
|
50 |
|
51 |
-
# Use
|
52 |
sobelx = cv2.Sobel(blur, cv2.CV_64F, 1, 0, ksize=3)
|
53 |
sobely = cv2.Sobel(blur, cv2.CV_64F, 0, 1, ksize=3)
|
54 |
magnitude = np.sqrt(sobelx**2 + sobely**2)
|
@@ -56,7 +33,7 @@ def generate_attention_map(img_array, prediction):
|
|
56 |
# Normalize to 0-1
|
57 |
magnitude = (magnitude - magnitude.min()) / (magnitude.max() - magnitude.min() + 1e-8)
|
58 |
|
59 |
-
#
|
60 |
weight = 0.5 + (prediction - 0.5) * 0.5 # Scale between 0.5-1 based on prediction
|
61 |
magnitude = magnitude * weight
|
62 |
|
@@ -74,8 +51,9 @@ def predict_and_explain(image):
|
|
74 |
# Preprocess the image
|
75 |
preprocessed = preprocess_image(image)
|
76 |
|
77 |
-
#
|
78 |
-
|
|
|
79 |
|
80 |
# Generate attention map
|
81 |
heatmap, attention = generate_attention_map(preprocessed, prediction)
|
@@ -142,7 +120,7 @@ with gr.Blocks(title="Chest CT Scan Cancer Detection") as demo:
|
|
142 |
- Middle: Feature map highlighting areas with distinctive patterns
|
143 |
- Right: Overlay of the feature map on the original image
|
144 |
|
145 |
-
|
146 |
""")
|
147 |
|
148 |
submit_btn.click(
|
|
|
9 |
import os
|
10 |
matplotlib.use('Agg') # Use non-interactive backend
|
11 |
|
12 |
+
# Image size - matching what the model was trained on
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
IMG_SIZE = 256
|
14 |
|
15 |
# Function for preprocessing
|
|
|
17 |
img = Image.fromarray(image).convert('RGB')
|
18 |
img = img.resize((IMG_SIZE, IMG_SIZE))
|
19 |
img_array = np.array(img) / 255.0
|
20 |
+
return np.expand_dims(img_array, axis=0)
|
21 |
|
22 |
+
# Generate attention map using edge detection (simplified)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
def generate_attention_map(img_array, prediction):
|
24 |
+
# Convert to grayscale
|
|
|
|
|
|
|
25 |
gray = cv2.cvtColor(img_array[0].astype(np.float32), cv2.COLOR_RGB2GRAY)
|
26 |
blur = cv2.GaussianBlur(gray, (5, 5), 0)
|
27 |
|
28 |
+
# Use edge detection to find interesting regions
|
29 |
sobelx = cv2.Sobel(blur, cv2.CV_64F, 1, 0, ksize=3)
|
30 |
sobely = cv2.Sobel(blur, cv2.CV_64F, 0, 1, ksize=3)
|
31 |
magnitude = np.sqrt(sobelx**2 + sobely**2)
|
|
|
33 |
# Normalize to 0-1
|
34 |
magnitude = (magnitude - magnitude.min()) / (magnitude.max() - magnitude.min() + 1e-8)
|
35 |
|
36 |
+
# Weight by prediction confidence
|
37 |
weight = 0.5 + (prediction - 0.5) * 0.5 # Scale between 0.5-1 based on prediction
|
38 |
magnitude = magnitude * weight
|
39 |
|
|
|
51 |
# Preprocess the image
|
52 |
preprocessed = preprocess_image(image)
|
53 |
|
54 |
+
# For demo, use a fixed prediction
|
55 |
+
# In a real deployment, you would load and use your model
|
56 |
+
prediction = 0.75 # Simulated cancer probability
|
57 |
|
58 |
# Generate attention map
|
59 |
heatmap, attention = generate_attention_map(preprocessed, prediction)
|
|
|
120 |
- Middle: Feature map highlighting areas with distinctive patterns
|
121 |
- Right: Overlay of the feature map on the original image
|
122 |
|
123 |
+
Note: This is a demonstration version without the full model due to size limitations.
|
124 |
""")
|
125 |
|
126 |
submit_btn.click(
|