Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,7 +14,7 @@ import os
|
|
| 14 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 15 |
model = None
|
| 16 |
|
| 17 |
-
#
|
| 18 |
class DoubleConv(nn.Module):
|
| 19 |
def __init__(self, in_channels, out_channels):
|
| 20 |
super(DoubleConv, self).__init__()
|
|
@@ -36,22 +36,26 @@ class AttentionBlock(nn.Module):
|
|
| 36 |
nn.Conv2d(F_g, F_int, kernel_size=1, stride=1, padding=0, bias=True),
|
| 37 |
nn.BatchNorm2d(F_int)
|
| 38 |
)
|
|
|
|
| 39 |
self.W_x = nn.Sequential(
|
| 40 |
nn.Conv2d(F_l, F_int, kernel_size=1, stride=1, padding=0, bias=True),
|
| 41 |
nn.BatchNorm2d(F_int)
|
| 42 |
)
|
|
|
|
| 43 |
self.psi = nn.Sequential(
|
| 44 |
nn.Conv2d(F_int, 1, kernel_size=1, stride=1, padding=0, bias=True),
|
| 45 |
nn.BatchNorm2d(1),
|
| 46 |
nn.Sigmoid()
|
| 47 |
)
|
|
|
|
| 48 |
self.relu = nn.ReLU(inplace=True)
|
|
|
|
| 49 |
def forward(self, g, x):
|
| 50 |
g1 = self.W_g(g)
|
| 51 |
x1 = self.W_x(x)
|
| 52 |
psi = self.relu(g1 + x1)
|
| 53 |
psi = self.psi(psi)
|
| 54 |
-
return x * psi
|
| 55 |
|
| 56 |
class AttentionUNET(nn.Module):
|
| 57 |
def __init__(self, in_channels=1, out_channels=1, features=[32, 64, 128, 256]):
|
|
@@ -61,47 +65,49 @@ class AttentionUNET(nn.Module):
|
|
| 61 |
self.downs = nn.ModuleList()
|
| 62 |
self.attentions = nn.ModuleList()
|
| 63 |
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
|
| 64 |
-
|
| 65 |
-
#
|
| 66 |
for feature in features:
|
| 67 |
self.downs.append(DoubleConv(in_channels, feature))
|
| 68 |
in_channels = feature
|
| 69 |
-
|
| 70 |
-
#
|
| 71 |
self.bottleneck = DoubleConv(features[-1], features[-1]*2)
|
| 72 |
-
|
| 73 |
-
#
|
| 74 |
for feature in reversed(features):
|
| 75 |
self.ups.append(nn.ConvTranspose2d(feature*2, feature, kernel_size=2, stride=2))
|
| 76 |
self.attentions.append(AttentionBlock(F_g=feature, F_l=feature, F_int=feature // 2))
|
| 77 |
self.ups.append(DoubleConv(feature*2, feature))
|
| 78 |
-
|
| 79 |
self.final_conv = nn.Conv2d(features[0], out_channels, kernel_size=1)
|
| 80 |
-
|
| 81 |
def forward(self, x):
|
| 82 |
skip_connections = []
|
| 83 |
for down in self.downs:
|
| 84 |
x = down(x)
|
| 85 |
skip_connections.append(x)
|
| 86 |
x = self.pool(x)
|
|
|
|
| 87 |
x = self.bottleneck(x)
|
| 88 |
-
skip_connections = skip_connections[::-1]
|
| 89 |
-
|
| 90 |
-
for idx in range(0, len(self.ups), 2):
|
| 91 |
x = self.ups[idx](x)
|
| 92 |
skip_connection = skip_connections[idx//2]
|
| 93 |
if x.shape != skip_connection.shape:
|
| 94 |
x = TF.resize(x, size=skip_connection.shape[2:])
|
| 95 |
-
# attention applied exactly as in your working code
|
| 96 |
skip_connection = self.attentions[idx // 2](skip_connection, x)
|
| 97 |
concat_skip = torch.cat((skip_connection, x), dim=1)
|
| 98 |
x = self.ups[idx+1](concat_skip)
|
|
|
|
| 99 |
return self.final_conv(x)
|
| 100 |
|
| 101 |
-
# ---- model download/load helpers (same as yours) ----
|
| 102 |
def download_model():
|
|
|
|
| 103 |
model_url = "https://huggingface.co/spaces/ArchCoder/the-op-segmenter/resolve/main/best_attention_model.pth.tar"
|
| 104 |
model_path = "best_attention_model.pth.tar"
|
|
|
|
| 105 |
if not os.path.exists(model_path):
|
| 106 |
print("π₯ Downloading your trained model...")
|
| 107 |
try:
|
|
@@ -112,171 +118,195 @@ def download_model():
|
|
| 112 |
return None
|
| 113 |
else:
|
| 114 |
print("β
Model already exists!")
|
|
|
|
| 115 |
return model_path
|
| 116 |
|
| 117 |
def load_your_attention_model():
|
|
|
|
| 118 |
global model
|
| 119 |
if model is None:
|
| 120 |
try:
|
| 121 |
print("π Loading your trained Attention U-Net model...")
|
|
|
|
|
|
|
| 122 |
model_path = download_model()
|
| 123 |
if model_path is None:
|
| 124 |
return None
|
|
|
|
|
|
|
| 125 |
model = AttentionUNET(in_channels=1, out_channels=1).to(device)
|
| 126 |
-
|
| 127 |
-
#
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
else:
|
| 131 |
-
sd = checkpoint
|
| 132 |
-
model.load_state_dict(sd)
|
| 133 |
model.eval()
|
|
|
|
| 134 |
print("β
Your Attention U-Net model loaded successfully!")
|
| 135 |
except Exception as e:
|
| 136 |
print(f"β Error loading your model: {e}")
|
| 137 |
model = None
|
| 138 |
return model
|
| 139 |
|
| 140 |
-
# ---- preprocessing (same as your Colab code) ----
|
| 141 |
def preprocess_for_your_model(image):
|
|
|
|
|
|
|
| 142 |
if image.mode != 'L':
|
| 143 |
image = image.convert('L')
|
|
|
|
|
|
|
| 144 |
val_test_transform = transforms.Compose([
|
| 145 |
transforms.Resize((256,256)),
|
| 146 |
transforms.ToTensor()
|
| 147 |
])
|
|
|
|
| 148 |
return val_test_transform(image).unsqueeze(0) # Add batch dimension
|
| 149 |
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
def predict_tumor(image):
|
| 152 |
-
"""
|
| 153 |
-
Keeps the exact old 4-panel outputs the same, and adds a 5th panel with
|
| 154 |
-
the probability heatmap. The heatmap is computed from the sigmoid(probabilities)
|
| 155 |
-
and does not change any tensors used for predictions.
|
| 156 |
-
"""
|
| 157 |
current_model = load_your_attention_model()
|
|
|
|
| 158 |
if current_model is None:
|
| 159 |
return None, "β Failed to load your trained model."
|
| 160 |
if image is None:
|
| 161 |
return None, "β οΈ Please upload an image first."
|
| 162 |
-
|
| 163 |
try:
|
| 164 |
print("π§ Processing with YOUR trained Attention U-Net...")
|
| 165 |
-
|
| 166 |
-
#
|
| 167 |
-
input_tensor = preprocess_for_your_model(image).to(device)
|
| 168 |
-
|
| 169 |
-
#
|
| 170 |
with torch.no_grad():
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
pred_mask_np = pred_mask.cpu().squeeze().numpy() # shape (256,256)
|
| 177 |
original_np = np.array(image.convert('L').resize((256, 256)))
|
|
|
|
|
|
|
| 178 |
inv_pred_mask_np = np.where(pred_mask_np == 1, 0, 255)
|
|
|
|
|
|
|
| 179 |
tumor_only = np.where(pred_mask_np == 1, original_np, 255)
|
| 180 |
-
|
| 181 |
-
#
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
#
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
# Build the 5-panel figure
|
| 201 |
-
# Panels (left->right): Original | Pred segmentation (pred*255) | Inverted mask | Tumor only | Heatmap
|
| 202 |
-
# Panels 1-4 are produced exactly the same as your old code
|
| 203 |
-
# -------------------------
|
| 204 |
-
fig, axes = plt.subplots(1, 5, figsize=(24, 5))
|
| 205 |
-
fig.suptitle('π§ Your Attention U-Net Results (with Heatmap)', fontsize=16, fontweight='bold')
|
| 206 |
-
|
| 207 |
-
# 1 Original (gray)
|
| 208 |
-
axes[0].imshow(original_np, cmap='gray')
|
| 209 |
-
axes[0].set_title("Original Image", fontsize=12, fontweight='bold')
|
| 210 |
-
axes[0].axis('off')
|
| 211 |
-
|
| 212 |
-
# 2 Tumor Segmentation (pred*255) β identical to old code's second panel
|
| 213 |
-
axes[1].imshow(pred_mask_np * 255, cmap='hot')
|
| 214 |
-
axes[1].set_title("Tumor Segmentation (pred Γ 255)", fontsize=12, fontweight='bold')
|
| 215 |
-
axes[1].axis('off')
|
| 216 |
-
|
| 217 |
-
# 3 Inverted mask β identical
|
| 218 |
-
axes[2].imshow(inv_pred_mask_np, cmap='gray')
|
| 219 |
-
axes[2].set_title("Inverted Mask (visual)", fontsize=12, fontweight='bold')
|
| 220 |
-
axes[2].axis('off')
|
| 221 |
-
|
| 222 |
-
# 4 Tumor only (grayscale crop) β identical
|
| 223 |
-
axes[3].imshow(tumor_only, cmap='gray')
|
| 224 |
-
axes[3].set_title("Tumor Only", fontsize=12, fontweight='bold')
|
| 225 |
-
axes[3].axis('off')
|
| 226 |
-
|
| 227 |
-
# 5 Heatmap (RGB)
|
| 228 |
-
axes[4].imshow(prob_heatmap_rgb)
|
| 229 |
-
axes[4].set_title("Probability Heatmap (sigmoid)", fontsize=12, fontweight='bold')
|
| 230 |
-
axes[4].axis('off')
|
| 231 |
-
|
| 232 |
plt.tight_layout()
|
| 233 |
-
|
| 234 |
-
# Save
|
| 235 |
buf = io.BytesIO()
|
| 236 |
plt.savefig(buf, format='png', dpi=150, bbox_inches='tight', facecolor='white')
|
| 237 |
buf.seek(0)
|
| 238 |
plt.close()
|
|
|
|
| 239 |
result_image = Image.open(buf)
|
| 240 |
-
|
| 241 |
-
# Calculate statistics (like your Colab code)
|
| 242 |
-
tumor_pixels =
|
| 243 |
-
total_pixels =
|
| 244 |
-
tumor_percentage = (tumor_pixels / total_pixels) * 100
|
| 245 |
-
|
| 246 |
-
#
|
| 247 |
-
max_confidence =
|
| 248 |
-
mean_confidence =
|
| 249 |
-
|
| 250 |
analysis_text = f"""
|
| 251 |
## π§ Your Attention U-Net Analysis Results
|
| 252 |
### π Detection Summary:
|
| 253 |
- **Status**: {'π΄ TUMOR DETECTED' if tumor_pixels > 50 else 'π’ NO SIGNIFICANT TUMOR'}
|
| 254 |
-
- **Tumor Area**: {tumor_percentage:.2f}% of
|
| 255 |
- **Tumor Pixels**: {tumor_pixels:,} pixels
|
| 256 |
- **Max Confidence**: {max_confidence:.4f}
|
| 257 |
- **Mean Confidence**: {mean_confidence:.4f}
|
| 258 |
|
| 259 |
-
###
|
| 260 |
-
- **
|
| 261 |
-
- **
|
| 262 |
-
- **
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
- **Device**: {device.type.upper()}
|
| 264 |
|
| 265 |
-
###
|
| 266 |
-
|
| 267 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
print(f"β
Your model analysis completed! Tumor area: {tumor_percentage:.2f}%")
|
| 269 |
return result_image, analysis_text
|
| 270 |
-
|
| 271 |
except Exception as e:
|
| 272 |
error_msg = f"β Error with your model: {str(e)}"
|
| 273 |
print(error_msg)
|
| 274 |
return None, error_msg
|
| 275 |
|
| 276 |
def clear_all():
|
| 277 |
-
return None, "Upload a brain MRI image to test YOUR trained Attention U-Net model"
|
| 278 |
|
| 279 |
-
#
|
| 280 |
css = """
|
| 281 |
.gradio-container {
|
| 282 |
max-width: 1400px !important;
|
|
@@ -293,33 +323,36 @@ css = """
|
|
| 293 |
}
|
| 294 |
"""
|
| 295 |
|
| 296 |
-
|
|
|
|
|
|
|
| 297 |
gr.HTML("""
|
| 298 |
<div id="title">
|
| 299 |
-
<h1>π§ YOUR Attention U-Net Model</h1>
|
| 300 |
<p style="font-size: 18px; margin-top: 15px;">
|
| 301 |
-
Using Your Own Trained Model β’ Dice: 0.8420 β’ IoU: 0.7297
|
| 302 |
</p>
|
| 303 |
<p style="font-size: 14px; margin-top: 10px; opacity: 0.9;">
|
| 304 |
Loaded from: ArchCoder/the-op-segmenter HuggingFace Space
|
| 305 |
</p>
|
| 306 |
</div>
|
| 307 |
""")
|
| 308 |
-
|
| 309 |
with gr.Row():
|
| 310 |
with gr.Column(scale=1):
|
| 311 |
gr.Markdown("### π€ Upload Brain MRI")
|
|
|
|
| 312 |
image_input = gr.Image(
|
| 313 |
label="Brain MRI Scan",
|
| 314 |
type="pil",
|
| 315 |
sources=["upload", "webcam"],
|
| 316 |
height=350
|
| 317 |
)
|
| 318 |
-
|
| 319 |
with gr.Row():
|
| 320 |
analyze_btn = gr.Button("π Analyze with YOUR Model", variant="primary", scale=2, size="lg")
|
| 321 |
clear_btn = gr.Button("ποΈ Clear", variant="secondary", scale=1)
|
| 322 |
-
|
| 323 |
gr.HTML("""
|
| 324 |
<div style="margin-top: 20px; padding: 20px; background: linear-gradient(135deg, #F3E8FF 0%, #EDE9FE 100%); border-radius: 10px; border-left: 4px solid #8B5CF6;">
|
| 325 |
<h4 style="color: #8B5CF6; margin-bottom: 15px;">π Your Model Features:</h4>
|
|
@@ -328,35 +361,78 @@ with gr.Blocks(css=css, title="π§ Your Attention U-Net Model", theme=gr.themes
|
|
| 328 |
<li><strong>Proven Performance:</strong> 84.2% Dice Score, 72.97% IoU</li>
|
| 329 |
<li><strong>Attention Gates:</strong> Advanced feature selection</li>
|
| 330 |
<li><strong>Clean Output:</strong> Binary segmentation masks</li>
|
| 331 |
-
<li><strong>
|
|
|
|
| 332 |
</ul>
|
| 333 |
</div>
|
| 334 |
""")
|
|
|
|
| 335 |
with gr.Column(scale=2):
|
| 336 |
-
gr.Markdown("### π Your Model Results")
|
|
|
|
| 337 |
output_image = gr.Image(
|
| 338 |
-
label="Your Attention U-Net Analysis",
|
| 339 |
type="pil",
|
| 340 |
height=500
|
| 341 |
)
|
|
|
|
| 342 |
analysis_output = gr.Markdown(
|
| 343 |
-
value="Upload a brain MRI image to test YOUR trained Attention U-Net model.",
|
| 344 |
elem_id="analysis"
|
| 345 |
)
|
| 346 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 347 |
analyze_btn.click(
|
| 348 |
fn=predict_tumor,
|
| 349 |
inputs=[image_input],
|
| 350 |
outputs=[output_image, analysis_output],
|
| 351 |
show_progress=True
|
| 352 |
)
|
| 353 |
-
|
| 354 |
clear_btn.click(
|
| 355 |
fn=clear_all,
|
| 356 |
inputs=[],
|
| 357 |
-
outputs=[image_input, analysis_output]
|
| 358 |
)
|
| 359 |
|
| 360 |
if __name__ == "__main__":
|
| 361 |
-
print("π Starting YOUR Attention U-Net Model System...")
|
| 362 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 15 |
model = None
|
| 16 |
|
| 17 |
+
# Define your Attention U-Net architecture (from your training code)
|
| 18 |
class DoubleConv(nn.Module):
|
| 19 |
def __init__(self, in_channels, out_channels):
|
| 20 |
super(DoubleConv, self).__init__()
|
|
|
|
| 36 |
nn.Conv2d(F_g, F_int, kernel_size=1, stride=1, padding=0, bias=True),
|
| 37 |
nn.BatchNorm2d(F_int)
|
| 38 |
)
|
| 39 |
+
|
| 40 |
self.W_x = nn.Sequential(
|
| 41 |
nn.Conv2d(F_l, F_int, kernel_size=1, stride=1, padding=0, bias=True),
|
| 42 |
nn.BatchNorm2d(F_int)
|
| 43 |
)
|
| 44 |
+
|
| 45 |
self.psi = nn.Sequential(
|
| 46 |
nn.Conv2d(F_int, 1, kernel_size=1, stride=1, padding=0, bias=True),
|
| 47 |
nn.BatchNorm2d(1),
|
| 48 |
nn.Sigmoid()
|
| 49 |
)
|
| 50 |
+
|
| 51 |
self.relu = nn.ReLU(inplace=True)
|
| 52 |
+
|
| 53 |
def forward(self, g, x):
|
| 54 |
g1 = self.W_g(g)
|
| 55 |
x1 = self.W_x(x)
|
| 56 |
psi = self.relu(g1 + x1)
|
| 57 |
psi = self.psi(psi)
|
| 58 |
+
return x * psi
|
| 59 |
|
| 60 |
class AttentionUNET(nn.Module):
|
| 61 |
def __init__(self, in_channels=1, out_channels=1, features=[32, 64, 128, 256]):
|
|
|
|
| 65 |
self.downs = nn.ModuleList()
|
| 66 |
self.attentions = nn.ModuleList()
|
| 67 |
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
|
| 68 |
+
|
| 69 |
+
# Down part of UNET
|
| 70 |
for feature in features:
|
| 71 |
self.downs.append(DoubleConv(in_channels, feature))
|
| 72 |
in_channels = feature
|
| 73 |
+
|
| 74 |
+
# Bottleneck
|
| 75 |
self.bottleneck = DoubleConv(features[-1], features[-1]*2)
|
| 76 |
+
|
| 77 |
+
# Up part of UNET
|
| 78 |
for feature in reversed(features):
|
| 79 |
self.ups.append(nn.ConvTranspose2d(feature*2, feature, kernel_size=2, stride=2))
|
| 80 |
self.attentions.append(AttentionBlock(F_g=feature, F_l=feature, F_int=feature // 2))
|
| 81 |
self.ups.append(DoubleConv(feature*2, feature))
|
| 82 |
+
|
| 83 |
self.final_conv = nn.Conv2d(features[0], out_channels, kernel_size=1)
|
| 84 |
+
|
| 85 |
def forward(self, x):
|
| 86 |
skip_connections = []
|
| 87 |
for down in self.downs:
|
| 88 |
x = down(x)
|
| 89 |
skip_connections.append(x)
|
| 90 |
x = self.pool(x)
|
| 91 |
+
|
| 92 |
x = self.bottleneck(x)
|
| 93 |
+
skip_connections = skip_connections[::-1] #reverse list
|
| 94 |
+
|
| 95 |
+
for idx in range(0, len(self.ups), 2): #do up and double_conv
|
| 96 |
x = self.ups[idx](x)
|
| 97 |
skip_connection = skip_connections[idx//2]
|
| 98 |
if x.shape != skip_connection.shape:
|
| 99 |
x = TF.resize(x, size=skip_connection.shape[2:])
|
|
|
|
| 100 |
skip_connection = self.attentions[idx // 2](skip_connection, x)
|
| 101 |
concat_skip = torch.cat((skip_connection, x), dim=1)
|
| 102 |
x = self.ups[idx+1](concat_skip)
|
| 103 |
+
|
| 104 |
return self.final_conv(x)
|
| 105 |
|
|
|
|
| 106 |
def download_model():
|
| 107 |
+
"""Download your trained model from HuggingFace"""
|
| 108 |
model_url = "https://huggingface.co/spaces/ArchCoder/the-op-segmenter/resolve/main/best_attention_model.pth.tar"
|
| 109 |
model_path = "best_attention_model.pth.tar"
|
| 110 |
+
|
| 111 |
if not os.path.exists(model_path):
|
| 112 |
print("π₯ Downloading your trained model...")
|
| 113 |
try:
|
|
|
|
| 118 |
return None
|
| 119 |
else:
|
| 120 |
print("β
Model already exists!")
|
| 121 |
+
|
| 122 |
return model_path
|
| 123 |
|
| 124 |
def load_your_attention_model():
|
| 125 |
+
"""Load YOUR trained Attention U-Net model"""
|
| 126 |
global model
|
| 127 |
if model is None:
|
| 128 |
try:
|
| 129 |
print("π Loading your trained Attention U-Net model...")
|
| 130 |
+
|
| 131 |
+
# Download model if needed
|
| 132 |
model_path = download_model()
|
| 133 |
if model_path is None:
|
| 134 |
return None
|
| 135 |
+
|
| 136 |
+
# Initialize your model architecture
|
| 137 |
model = AttentionUNET(in_channels=1, out_channels=1).to(device)
|
| 138 |
+
|
| 139 |
+
# Load your trained weights
|
| 140 |
+
checkpoint = torch.load(model_path, map_location=device, weights_only=True)
|
| 141 |
+
model.load_state_dict(checkpoint["state_dict"])
|
|
|
|
|
|
|
|
|
|
| 142 |
model.eval()
|
| 143 |
+
|
| 144 |
print("β
Your Attention U-Net model loaded successfully!")
|
| 145 |
except Exception as e:
|
| 146 |
print(f"β Error loading your model: {e}")
|
| 147 |
model = None
|
| 148 |
return model
|
| 149 |
|
|
|
|
| 150 |
def preprocess_for_your_model(image):
|
| 151 |
+
"""Preprocessing exactly like your Colab code"""
|
| 152 |
+
# Convert to grayscale (like your Colab code)
|
| 153 |
if image.mode != 'L':
|
| 154 |
image = image.convert('L')
|
| 155 |
+
|
| 156 |
+
# Use the exact same transform as your Colab code
|
| 157 |
val_test_transform = transforms.Compose([
|
| 158 |
transforms.Resize((256,256)),
|
| 159 |
transforms.ToTensor()
|
| 160 |
])
|
| 161 |
+
|
| 162 |
return val_test_transform(image).unsqueeze(0) # Add batch dimension
|
| 163 |
|
| 164 |
+
def create_heatmap_visualization(pred_mask_continuous, original_image):
|
| 165 |
+
"""Create heatmap visualization from continuous prediction values"""
|
| 166 |
+
# pred_mask_continuous should be the raw sigmoid output (0-1 values)
|
| 167 |
+
heatmap_np = pred_mask_continuous.cpu().squeeze().numpy()
|
| 168 |
+
|
| 169 |
+
# Normalize to 0-255 for better visualization
|
| 170 |
+
heatmap_normalized = (heatmap_np * 255).astype(np.uint8)
|
| 171 |
+
|
| 172 |
+
# Apply colormap (using 'hot' colormap like in medical imaging)
|
| 173 |
+
heatmap_colored = cv2.applyColorMap(heatmap_normalized, cv2.COLORMAP_HOT)
|
| 174 |
+
heatmap_colored = cv2.cvtColor(heatmap_colored, cv2.COLOR_BGR2RGB)
|
| 175 |
+
|
| 176 |
+
# Convert original image to RGB for overlay
|
| 177 |
+
if len(original_image.shape) == 2: # Grayscale
|
| 178 |
+
original_rgb = cv2.cvtColor(original_image.astype(np.uint8), cv2.COLOR_GRAY2RGB)
|
| 179 |
+
else:
|
| 180 |
+
original_rgb = original_image.astype(np.uint8)
|
| 181 |
+
|
| 182 |
+
# Create overlay (blend original image with heatmap)
|
| 183 |
+
alpha = 0.6 # Transparency factor
|
| 184 |
+
overlay = cv2.addWeighted(original_rgb, 1-alpha, heatmap_colored, alpha, 0)
|
| 185 |
+
|
| 186 |
+
return overlay
|
| 187 |
+
|
| 188 |
def predict_tumor(image):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
current_model = load_your_attention_model()
|
| 190 |
+
|
| 191 |
if current_model is None:
|
| 192 |
return None, "β Failed to load your trained model."
|
| 193 |
if image is None:
|
| 194 |
return None, "β οΈ Please upload an image first."
|
| 195 |
+
|
| 196 |
try:
|
| 197 |
print("π§ Processing with YOUR trained Attention U-Net...")
|
| 198 |
+
|
| 199 |
+
# Use the exact preprocessing from your Colab code
|
| 200 |
+
input_tensor = preprocess_for_your_model(image).to(device)
|
| 201 |
+
|
| 202 |
+
# Predict using your model (exactly like your Colab code)
|
| 203 |
with torch.no_grad():
|
| 204 |
+
pred_mask_continuous = torch.sigmoid(current_model(input_tensor)) # Keep continuous values for heatmap
|
| 205 |
+
pred_mask_binary = (pred_mask_continuous > 0.5).float() # Binary mask for original visualizations
|
| 206 |
+
|
| 207 |
+
# Convert to numpy (like your Colab code) - KEEPING ORIGINAL LOGIC
|
| 208 |
+
pred_mask_np = pred_mask_binary.cpu().squeeze().numpy()
|
|
|
|
| 209 |
original_np = np.array(image.convert('L').resize((256, 256)))
|
| 210 |
+
|
| 211 |
+
# Create inverted mask for visualization (like your Colab code) - UNCHANGED
|
| 212 |
inv_pred_mask_np = np.where(pred_mask_np == 1, 0, 255)
|
| 213 |
+
|
| 214 |
+
# Create tumor-only image (like your Colab code) - UNCHANGED
|
| 215 |
tumor_only = np.where(pred_mask_np == 1, original_np, 255)
|
| 216 |
+
|
| 217 |
+
# NEW: Create heatmap visualization
|
| 218 |
+
heatmap_overlay = create_heatmap_visualization(pred_mask_continuous, original_np)
|
| 219 |
+
|
| 220 |
+
# Create visualization with 5 panels (original 4 + heatmap)
|
| 221 |
+
fig, axes = plt.subplots(1, 5, figsize=(25, 5))
|
| 222 |
+
fig.suptitle('π§ Your Attention U-Net Results with Heatmap', fontsize=16, fontweight='bold')
|
| 223 |
+
|
| 224 |
+
titles = ["Original Image", "Predicted Mask", "Inverted Mask", "Tumor Only", "Prediction Heatmap"]
|
| 225 |
+
images = [original_np, pred_mask_np * 255, inv_pred_mask_np, tumor_only, heatmap_overlay]
|
| 226 |
+
cmaps = ['gray', 'hot', 'gray', 'gray', None] # None for RGB heatmap
|
| 227 |
+
|
| 228 |
+
for i, ax in enumerate(axes):
|
| 229 |
+
if cmaps[i] is not None:
|
| 230 |
+
ax.imshow(images[i], cmap=cmaps[i])
|
| 231 |
+
else:
|
| 232 |
+
ax.imshow(images[i]) # RGB image
|
| 233 |
+
ax.set_title(titles[i], fontsize=12, fontweight='bold')
|
| 234 |
+
ax.axis('off')
|
| 235 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
plt.tight_layout()
|
| 237 |
+
|
| 238 |
+
# Save result
|
| 239 |
buf = io.BytesIO()
|
| 240 |
plt.savefig(buf, format='png', dpi=150, bbox_inches='tight', facecolor='white')
|
| 241 |
buf.seek(0)
|
| 242 |
plt.close()
|
| 243 |
+
|
| 244 |
result_image = Image.open(buf)
|
| 245 |
+
|
| 246 |
+
# Calculate statistics (like your Colab code) - UNCHANGED
|
| 247 |
+
tumor_pixels = np.sum(pred_mask_np)
|
| 248 |
+
total_pixels = pred_mask_np.size
|
| 249 |
+
tumor_percentage = (tumor_pixels / total_pixels) * 100
|
| 250 |
+
|
| 251 |
+
# Calculate confidence metrics
|
| 252 |
+
max_confidence = torch.max(pred_mask_continuous).item()
|
| 253 |
+
mean_confidence = torch.mean(pred_mask_continuous).item()
|
| 254 |
+
|
| 255 |
analysis_text = f"""
|
| 256 |
## π§ Your Attention U-Net Analysis Results
|
| 257 |
### π Detection Summary:
|
| 258 |
- **Status**: {'π΄ TUMOR DETECTED' if tumor_pixels > 50 else 'π’ NO SIGNIFICANT TUMOR'}
|
| 259 |
+
- **Tumor Area**: {tumor_percentage:.2f}% of brain region
|
| 260 |
- **Tumor Pixels**: {tumor_pixels:,} pixels
|
| 261 |
- **Max Confidence**: {max_confidence:.4f}
|
| 262 |
- **Mean Confidence**: {mean_confidence:.4f}
|
| 263 |
|
| 264 |
+
### π₯ New Heatmap Features:
|
| 265 |
+
- **Continuous Predictions**: Shows confidence levels (0-1)
|
| 266 |
+
- **Color Coding**: Red/Yellow = High confidence, Blue/Black = Low confidence
|
| 267 |
+
- **Overlay Visualization**: Heatmap overlaid on original image
|
| 268 |
+
- **Enhanced Analysis**: Better understanding of model uncertainty
|
| 269 |
+
|
| 270 |
+
### π¬ Your Model Information:
|
| 271 |
+
- **Architecture**: YOUR trained Attention U-Net
|
| 272 |
+
- **Training Performance**: Dice: 0.8420, IoU: 0.7297
|
| 273 |
+
- **Input**: Grayscale (single channel)
|
| 274 |
+
- **Output**: Binary segmentation mask + Continuous heatmap
|
| 275 |
- **Device**: {device.type.upper()}
|
| 276 |
|
| 277 |
+
### π― Model Performance:
|
| 278 |
+
- **Training Accuracy**: 98.90%
|
| 279 |
+
- **Best Dice Score**: 0.8420
|
| 280 |
+
- **Best IoU Score**: 0.7297
|
| 281 |
+
- **Training Dataset**: Brain tumor segmentation dataset
|
| 282 |
+
|
| 283 |
+
### π Processing Details:
|
| 284 |
+
- **Preprocessing**: Resize(256Γ256) + ToTensor (your exact method)
|
| 285 |
+
- **Threshold**: 0.5 (sigmoid > 0.5)
|
| 286 |
+
- **Architecture**: Attention gates + Skip connections
|
| 287 |
+
- **Features**: [32, 64, 128, 256] channels
|
| 288 |
+
- **Heatmap**: Continuous sigmoid output with hot colormap
|
| 289 |
+
|
| 290 |
+
### β οΈ Medical Disclaimer:
|
| 291 |
+
This is YOUR trained AI model for **research and educational purposes only**.
|
| 292 |
+
Results should be validated by medical professionals. Not for clinical diagnosis.
|
| 293 |
+
|
| 294 |
+
### π Model Quality:
|
| 295 |
+
β
This is your own trained model with proven {tumor_percentage:.2f}% detection capability!
|
| 296 |
+
"""
|
| 297 |
+
|
| 298 |
print(f"β
Your model analysis completed! Tumor area: {tumor_percentage:.2f}%")
|
| 299 |
return result_image, analysis_text
|
| 300 |
+
|
| 301 |
except Exception as e:
|
| 302 |
error_msg = f"β Error with your model: {str(e)}"
|
| 303 |
print(error_msg)
|
| 304 |
return None, error_msg
|
| 305 |
|
| 306 |
def clear_all():
|
| 307 |
+
return None, None, "Upload a brain MRI image to test YOUR trained Attention U-Net model with heatmap visualization"
|
| 308 |
|
| 309 |
+
# Enhanced CSS for your model
|
| 310 |
css = """
|
| 311 |
.gradio-container {
|
| 312 |
max-width: 1400px !important;
|
|
|
|
| 323 |
}
|
| 324 |
"""
|
| 325 |
|
| 326 |
+
# Create Gradio interface for your model
|
| 327 |
+
with gr.Blocks(css=css, title="π§ Your Attention U-Net Model with Heatmap", theme=gr.themes.Soft()) as app:
|
| 328 |
+
|
| 329 |
gr.HTML("""
|
| 330 |
<div id="title">
|
| 331 |
+
<h1>π§ YOUR Attention U-Net Model with Heatmap</h1>
|
| 332 |
<p style="font-size: 18px; margin-top: 15px;">
|
| 333 |
+
Using Your Own Trained Model β’ Dice: 0.8420 β’ IoU: 0.7297 β’ Now with Heatmap Visualization
|
| 334 |
</p>
|
| 335 |
<p style="font-size: 14px; margin-top: 10px; opacity: 0.9;">
|
| 336 |
Loaded from: ArchCoder/the-op-segmenter HuggingFace Space
|
| 337 |
</p>
|
| 338 |
</div>
|
| 339 |
""")
|
| 340 |
+
|
| 341 |
with gr.Row():
|
| 342 |
with gr.Column(scale=1):
|
| 343 |
gr.Markdown("### π€ Upload Brain MRI")
|
| 344 |
+
|
| 345 |
image_input = gr.Image(
|
| 346 |
label="Brain MRI Scan",
|
| 347 |
type="pil",
|
| 348 |
sources=["upload", "webcam"],
|
| 349 |
height=350
|
| 350 |
)
|
| 351 |
+
|
| 352 |
with gr.Row():
|
| 353 |
analyze_btn = gr.Button("π Analyze with YOUR Model", variant="primary", scale=2, size="lg")
|
| 354 |
clear_btn = gr.Button("ποΈ Clear", variant="secondary", scale=1)
|
| 355 |
+
|
| 356 |
gr.HTML("""
|
| 357 |
<div style="margin-top: 20px; padding: 20px; background: linear-gradient(135deg, #F3E8FF 0%, #EDE9FE 100%); border-radius: 10px; border-left: 4px solid #8B5CF6;">
|
| 358 |
<h4 style="color: #8B5CF6; margin-bottom: 15px;">π Your Model Features:</h4>
|
|
|
|
| 361 |
<li><strong>Proven Performance:</strong> 84.2% Dice Score, 72.97% IoU</li>
|
| 362 |
<li><strong>Attention Gates:</strong> Advanced feature selection</li>
|
| 363 |
<li><strong>Clean Output:</strong> Binary segmentation masks</li>
|
| 364 |
+
<li><strong>NEW: Heatmap:</strong> Continuous confidence visualization</li>
|
| 365 |
+
<li><strong>5-Panel View:</strong> Complete analysis with heatmap</li>
|
| 366 |
</ul>
|
| 367 |
</div>
|
| 368 |
""")
|
| 369 |
+
|
| 370 |
with gr.Column(scale=2):
|
| 371 |
+
gr.Markdown("### π Your Model Results with Heatmap")
|
| 372 |
+
|
| 373 |
output_image = gr.Image(
|
| 374 |
+
label="Your Attention U-Net Analysis with Heatmap",
|
| 375 |
type="pil",
|
| 376 |
height=500
|
| 377 |
)
|
| 378 |
+
|
| 379 |
analysis_output = gr.Markdown(
|
| 380 |
+
value="Upload a brain MRI image to test YOUR trained Attention U-Net model with heatmap visualization.",
|
| 381 |
elem_id="analysis"
|
| 382 |
)
|
| 383 |
+
|
| 384 |
+
# Footer highlighting your model with heatmap features
|
| 385 |
+
gr.HTML("""
|
| 386 |
+
<div style="margin-top: 30px; padding: 25px; background-color: #F8FAFC; border-radius: 15px; border: 2px solid #8B5CF6;">
|
| 387 |
+
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 30px;">
|
| 388 |
+
<div>
|
| 389 |
+
<h4 style="color: #8B5CF6; margin-bottom: 15px;">π Your Personal AI Model</h4>
|
| 390 |
+
<p><strong>Architecture:</strong> Attention U-Net with skip connections</p>
|
| 391 |
+
<p><strong>Performance:</strong> Dice: 0.8420, IoU: 0.7297, Accuracy: 98.90%</p>
|
| 392 |
+
<p><strong>Training:</strong> Your own dataset-specific training</p>
|
| 393 |
+
<p><strong>Features:</strong> [32, 64, 128, 256] channel progression</p>
|
| 394 |
+
<p><strong>NEW:</strong> Continuous heatmap visualization for confidence</p>
|
| 395 |
+
</div>
|
| 396 |
+
<div>
|
| 397 |
+
<h4 style="color: #DC2626; margin-bottom: 15px;">β οΈ Your Model Disclaimer</h4>
|
| 398 |
+
<p style="color: #DC2626; font-weight: 600; line-height: 1.4;">
|
| 399 |
+
This is YOUR personally trained AI model for <strong>research purposes only</strong>.<br>
|
| 400 |
+
Results reflect your model's training performance.<br>
|
| 401 |
+
Always validate with medical professionals for any clinical application.
|
| 402 |
+
</p>
|
| 403 |
+
</div>
|
| 404 |
+
</div>
|
| 405 |
+
<hr style="margin: 20px 0; border: none; border-top: 2px solid #E5E7EB;">
|
| 406 |
+
<p style="text-align: center; color: #6B7280; margin: 10px 0; font-weight: 600;">
|
| 407 |
+
π Your Personal Attention U-Net β’ Downloaded from HuggingFace β’ Research-Grade Performance β’ Now with Heatmap! π₯
|
| 408 |
+
</p>
|
| 409 |
+
</div>
|
| 410 |
+
""")
|
| 411 |
+
|
| 412 |
+
# Event handlers
|
| 413 |
analyze_btn.click(
|
| 414 |
fn=predict_tumor,
|
| 415 |
inputs=[image_input],
|
| 416 |
outputs=[output_image, analysis_output],
|
| 417 |
show_progress=True
|
| 418 |
)
|
| 419 |
+
|
| 420 |
clear_btn.click(
|
| 421 |
fn=clear_all,
|
| 422 |
inputs=[],
|
| 423 |
+
outputs=[image_input, output_image, analysis_output]
|
| 424 |
)
|
| 425 |
|
| 426 |
if __name__ == "__main__":
|
| 427 |
+
print("π Starting YOUR Attention U-Net Model System with Heatmap...")
|
| 428 |
+
print("π Using your personally trained model")
|
| 429 |
+
print("π₯ Auto-downloading from HuggingFace...")
|
| 430 |
+
print("π― Expected performance: Dice 0.8420, IoU 0.7297")
|
| 431 |
+
print("π₯ NEW: Heatmap visualization added!")
|
| 432 |
+
|
| 433 |
+
app.launch(
|
| 434 |
+
server_name="0.0.0.0",
|
| 435 |
+
server_port=7860,
|
| 436 |
+
show_error=True,
|
| 437 |
+
share=False
|
| 438 |
+
)
|