drankush-ai commited on
Commit
904a4b1
·
verified ·
1 Parent(s): dd577ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -19
app.py CHANGED
@@ -227,29 +227,78 @@ try:
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'
255
  save_dir.mkdir(parents=True, exist_ok=True)
 
227
  Repo.clone_from(URI, clone_dir)
228
  print(f"✅ Repository cloned to {clone_dir}")
229
 
230
+ # ===== MONKEY PATCH FOR ZNORMALIZATION ERROR =====
231
+ # This patch fixes the 'ZNormalization' object has no attribute 'masking_method' error
232
+ from fastMONAI.vision_augmentation import ZNormalization
233
+ from fastMONAI.vision_core import MedImage
 
 
 
 
 
 
 
 
 
 
234
 
235
+ def patched_znormalization_encodes(self, o):
236
+ """Patched version of ZNormalization.encodes to handle attribute mismatch."""
237
+ # Handle the attribute mismatch - ensure we have what we need
238
+ if not hasattr(self, 'z_normalization'):
239
+ # Get masking_method with fallback
240
+ masking_method = getattr(self, 'masking_method', None)
241
+ self.z_normalization = tio.ZNormalization(masking_method=masking_method)
242
+
243
+ # Ensure channel_wise exists
244
+ if not hasattr(self, 'channel_wise'):
245
+ self.channel_wise = True
246
+
247
+ # Use the current implementation logic with proper dimension handling
248
+ try:
249
+ if self.channel_wise:
250
+ if o.dim() == 5: # (batch, channels, x, y, z)
251
+ results = []
252
+ for batch_item in o:
253
+ channel_results = []
254
+ for channel in batch_item:
255
+ # channel should be 3D (x, y, z)
256
+ normalized = self.z_normalization(channel[None])[0]
257
+ channel_results.append(normalized)
258
+ results.append(torch.stack(channel_results))
259
+ o = torch.stack(results)
260
+ elif o.dim() == 4: # (channels, x, y, z)
261
+ channel_results = []
262
+ for channel in o:
263
+ # channel should be 3D (x, y, z)
264
+ normalized = self.z_normalization(channel[None])[0]
265
+ channel_results.append(normalized)
266
+ o = torch.stack(channel_results)
267
+ else: # (x, y, z) - single channel image
268
+ o = self.z_normalization(o[None])[0]
269
+ else:
270
+ if o.dim() == 5: # (batch, channels, x, y, z)
271
+ results = []
272
+ for batch_item in o:
273
+ normalized = self.z_normalization(batch_item)
274
+ results.append(normalized)
275
+ o = torch.stack(results)
276
+ else:
277
+ o = self.z_normalization(o)
278
+ except RuntimeError as e:
279
+ if "Standard deviation is 0" in str(e):
280
+ mean = float(o.mean())
281
+ error_msg = (
282
+ f"Standard deviation is 0 for image (mean={mean:.3f}).\n"
283
+ f"This indicates uniform pixel values.\n\n"
284
+ f"Possible causes:\n"
285
+ f"• Corrupted or blank image\n"
286
+ f"• Oversaturated regions\n"
287
+ f"• Background-only regions\n"
288
+ f"• All-zero mask being processed as image\n\n"
289
+ f"Suggested solutions:\n"
290
+ f"• Check image quality and acquisition\n"
291
+ f"• Verify image vs mask data loading"
292
+ )
293
+ raise RuntimeError(error_msg) from e
294
+
295
+ return MedImage.create(o)
296
+
297
  # Replace the problematic method
298
+ ZNormalization.encodes = patched_znormalization_encodes
299
+ print("✅ Applied ZNormalization patch to fix attribute errors")
300
  # ===== END OF MONKEY PATCH =====
301
 
 
 
 
302
  models_path = clone_dir
303
  save_dir = Path.cwd() / 'hs_pred'
304
  save_dir.mkdir(parents=True, exist_ok=True)