ryu34 commited on
Commit
1736640
·
verified ·
1 Parent(s): cae68c5

Fix BatchNorm1d error: enable only Dropout (not BatchNorm) for MC uncertainty

Browse files
Files changed (1) hide show
  1. app.py +21 -3
app.py CHANGED
@@ -84,6 +84,20 @@ NETWORK_FUNCTIONS = {
84
  }
85
 
86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  # ============================================================
88
  # BrainEncoder model (must match training architecture exactly)
89
  # ============================================================
@@ -412,18 +426,22 @@ class ModelManager:
412
  with torch.no_grad():
413
  deep_pred, intermediates = self.brain_encoder(input_features, return_intermediates=True)
414
 
415
- # Compute uncertainty via dropout MC (deep encoder)
416
- self.brain_encoder.train()
 
 
 
417
  mc_predictions = []
418
  for _ in range(10):
419
  with torch.no_grad():
420
  mc_pred = self.brain_encoder(input_features)
421
  mc_predictions.append(mc_pred.cpu().numpy().flatten())
422
- self.brain_encoder.eval()
423
  mc_predictions = np.array(mc_predictions)
424
  uncertainty = np.std(mc_predictions, axis=0)
425
  else:
426
  # Estimate uncertainty from ridge prediction variance across feature perturbation
 
427
  mc_predictions = []
428
  for _ in range(10):
429
  noise = np.random.normal(0, 0.01, size=input_features_np.shape)
 
84
  }
85
 
86
 
87
+ # ============================================================
88
+ # Helper: enable only Dropout for MC sampling (keep BatchNorm in eval)
89
+ # ============================================================
90
+ def enable_dropout_only(model):
91
+ """Enable Dropout layers while keeping BatchNorm in eval mode.
92
+
93
+ This is needed for MC Dropout uncertainty estimation with batch_size=1,
94
+ because BatchNorm1d requires batch_size > 1 in training mode.
95
+ """
96
+ for module in model.modules():
97
+ if isinstance(module, nn.Dropout):
98
+ module.train()
99
+
100
+
101
  # ============================================================
102
  # BrainEncoder model (must match training architecture exactly)
103
  # ============================================================
 
426
  with torch.no_grad():
427
  deep_pred, intermediates = self.brain_encoder(input_features, return_intermediates=True)
428
 
429
+ # Compute uncertainty via MC Dropout
430
+ # IMPORTANT: Only enable Dropout layers, keep BatchNorm in eval mode.
431
+ # BatchNorm1d requires batch_size > 1 in training mode, but we have batch_size=1.
432
+ self.brain_encoder.eval() # Ensure everything is in eval mode first
433
+ enable_dropout_only(self.brain_encoder) # Selectively enable only Dropout
434
  mc_predictions = []
435
  for _ in range(10):
436
  with torch.no_grad():
437
  mc_pred = self.brain_encoder(input_features)
438
  mc_predictions.append(mc_pred.cpu().numpy().flatten())
439
+ self.brain_encoder.eval() # Restore full eval mode
440
  mc_predictions = np.array(mc_predictions)
441
  uncertainty = np.std(mc_predictions, axis=0)
442
  else:
443
  # Estimate uncertainty from ridge prediction variance across feature perturbation
444
+ ridge = self.ridge_model
445
  mc_predictions = []
446
  for _ in range(10):
447
  noise = np.random.normal(0, 0.01, size=input_features_np.shape)