Update app.py
Browse files
app.py
CHANGED
|
@@ -227,88 +227,28 @@ try:
|
|
| 227 |
Repo.clone_from(URI, clone_dir)
|
| 228 |
print(f"✅ Repository cloned to {clone_dir}")
|
| 229 |
|
| 230 |
-
# ===== MONKEY PATCH FOR
|
| 231 |
-
# This patch fixes the '
|
| 232 |
-
from fastMONAI.vision_augmentation import
|
| 233 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
|
| 235 |
-
# Create a patched version of the encodes method
|
| 236 |
-
def patched_znormalization_encodes(self, o):
|
| 237 |
-
"""Patched version of ZNormalization.encodes to handle attribute and dimension issues."""
|
| 238 |
-
# Handle the attribute mismatch - ensure we have what we need
|
| 239 |
-
if not hasattr(self, 'z_normalization'):
|
| 240 |
-
# Get masking_method with fallback
|
| 241 |
-
masking_method = getattr(self, 'masking_method', None)
|
| 242 |
-
self.z_normalization = tio.ZNormalization(masking_method=masking_method)
|
| 243 |
-
|
| 244 |
-
# Ensure channel_wise exists
|
| 245 |
-
if not hasattr(self, 'channel_wise'):
|
| 246 |
-
self.channel_wise = True
|
| 247 |
-
|
| 248 |
-
# Handle dimension issues properly
|
| 249 |
-
try:
|
| 250 |
-
if self.channel_wise:
|
| 251 |
-
if o.dim() == 5: # (batch, channels, x, y, z)
|
| 252 |
-
# Process each item in the batch
|
| 253 |
-
results = []
|
| 254 |
-
for batch_item in o:
|
| 255 |
-
channel_results = []
|
| 256 |
-
for channel in batch_item:
|
| 257 |
-
# channel should be 3D (x, y, z)
|
| 258 |
-
normalized = self.z_normalization(channel[None])[0]
|
| 259 |
-
channel_results.append(normalized)
|
| 260 |
-
results.append(torch.stack(channel_results))
|
| 261 |
-
o = torch.stack(results)
|
| 262 |
-
elif o.dim() == 4: # (channels, x, y, z)
|
| 263 |
-
# Process each channel
|
| 264 |
-
channel_results = []
|
| 265 |
-
for channel in o:
|
| 266 |
-
# channel should be 3D (x, y, z)
|
| 267 |
-
normalized = self.z_normalization(channel[None])[0]
|
| 268 |
-
channel_results.append(normalized)
|
| 269 |
-
o = torch.stack(channel_results)
|
| 270 |
-
else: # (x, y, z) - single channel image
|
| 271 |
-
o = self.z_normalization(o[None])[0]
|
| 272 |
-
else:
|
| 273 |
-
# For non-channel-wise normalization, handle batched tensors correctly
|
| 274 |
-
if o.dim() == 5: # (batch, channels, x, y, z)
|
| 275 |
-
# Process each item in the batch separately
|
| 276 |
-
results = []
|
| 277 |
-
for batch_item in o:
|
| 278 |
-
# batch_item is 4D: (channels, x, y, z)
|
| 279 |
-
normalized = self.z_normalization(batch_item)
|
| 280 |
-
results.append(normalized)
|
| 281 |
-
o = torch.stack(results)
|
| 282 |
-
else:
|
| 283 |
-
# For non-batched tensors (4D: channels, x, y, z)
|
| 284 |
-
o = self.z_normalization(o)
|
| 285 |
-
|
| 286 |
-
except RuntimeError as e:
|
| 287 |
-
if "Standard deviation is 0" in str(e):
|
| 288 |
-
mean = float(o.mean())
|
| 289 |
-
error_msg = (
|
| 290 |
-
f"Standard deviation is 0 for image (mean={mean:.3f}).\n"
|
| 291 |
-
f"This indicates uniform pixel values.\n\n"
|
| 292 |
-
f"Possible causes:\n"
|
| 293 |
-
f"• Corrupted or blank image\n"
|
| 294 |
-
f"• Oversaturated regions\n"
|
| 295 |
-
f"• Background-only regions\n"
|
| 296 |
-
f"• All-zero mask being processed as image\n\n"
|
| 297 |
-
f"Suggested solutions:\n"
|
| 298 |
-
f"• Check image quality and acquisition\n"
|
| 299 |
-
f"• Verify image vs mask data loading"
|
| 300 |
-
)
|
| 301 |
-
raise RuntimeError(error_msg) from e
|
| 302 |
-
|
| 303 |
-
return MedImage.create(o)
|
| 304 |
-
|
| 305 |
# Replace the problematic method
|
| 306 |
-
|
| 307 |
-
print("✅ Applied
|
| 308 |
# ===== END OF MONKEY PATCH =====
|
| 309 |
|
| 310 |
-
#
|
| 311 |
-
# The
|
| 312 |
|
| 313 |
models_path = clone_dir
|
| 314 |
save_dir = Path.cwd() / 'hs_pred'
|
|
|
|
| 227 |
Repo.clone_from(URI, clone_dir)
|
| 228 |
print(f"✅ Repository cloned to {clone_dir}")
|
| 229 |
|
| 230 |
+
# ===== MONKEY PATCH FOR PADORCROP ERROR =====
|
| 231 |
+
# This patch fixes the 'PadOrCrop' object has no attribute 'size' error
|
| 232 |
+
from fastMONAI.vision_augmentation import PadOrCrop
|
| 233 |
+
|
| 234 |
+
def patched_pad_or_crop_encodes(self, o):
|
| 235 |
+
"""Patched version of PadOrCrop.encodes to handle attribute mismatch."""
|
| 236 |
+
# Access the attributes through self.pad_or_crop instead of directly
|
| 237 |
+
return do_pad_or_crop(
|
| 238 |
+
o,
|
| 239 |
+
target_shape=self.pad_or_crop.target_shape,
|
| 240 |
+
padding_mode=self.pad_or_crop.padding_mode,
|
| 241 |
+
mask_name=self.pad_or_crop.mask_name,
|
| 242 |
+
dtype=type(o)
|
| 243 |
+
)
|
| 244 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
# Replace the problematic method
|
| 246 |
+
PadOrCrop.encodes = patched_pad_or_crop_encodes
|
| 247 |
+
print("✅ Applied PadOrCrop patch to fix attribute errors")
|
| 248 |
# ===== END OF MONKEY PATCH =====
|
| 249 |
|
| 250 |
+
# Note: We don't need the ZNormalization patch anymore
|
| 251 |
+
# The error is specifically with PadOrCrop
|
| 252 |
|
| 253 |
models_path = clone_dir
|
| 254 |
save_dir = Path.cwd() / 'hs_pred'
|