Xalphinions commited on
Commit
fbfa85f
·
verified ·
1 Parent(s): 6f4e394

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. app.py +19 -3
  2. app_moe.py +19 -3
app.py CHANGED
@@ -67,6 +67,14 @@ class WatermelonMoEModel(torch.nn.Module):
67
  print(f"\033[92mINFO\033[0m: Loaded {len(self.models)} models for MoE ensemble")
68
  print(f"\033[92mINFO\033[0m: Model weights: {self.weights}")
69
 
 
 
 
 
 
 
 
 
70
  def forward(self, mfcc, image):
71
  """
72
  Forward pass through the MoE model.
@@ -152,7 +160,8 @@ def predict_sugar_content(audio, image, model_dir="models", weights=None):
152
 
153
  # Load MoE model
154
  moe_model = WatermelonMoEModel(TOP_MODELS, model_dir, weights)
155
- moe_model.to(device)
 
156
  moe_model.eval()
157
  print(f"\033[92mINFO\033[0m: Loaded MoE model with {len(moe_model.models)} backbone models")
158
 
@@ -239,12 +248,19 @@ def predict_sugar_content(audio, image, model_dir="models", weights=None):
239
 
240
  # Add batch dimension for inference and move to device
241
  if mfcc is not None:
 
242
  mfcc = mfcc.unsqueeze(0).to(device)
243
- print(f"\033[92mDEBUG\033[0m: Final MFCC shape with batch dimension: {mfcc.shape}")
244
 
245
  if processed_image is not None:
 
246
  processed_image = processed_image.unsqueeze(0).to(device)
247
- print(f"\033[92mDEBUG\033[0m: Final image shape with batch dimension: {processed_image.shape}")
 
 
 
 
 
248
 
249
  # Run inference with MoE model
250
  print(f"\033[92mDEBUG\033[0m: Running inference with MoE model on device: {device}")
 
67
  print(f"\033[92mINFO\033[0m: Loaded {len(self.models)} models for MoE ensemble")
68
  print(f"\033[92mINFO\033[0m: Model weights: {self.weights}")
69
 
70
+ def to(self, device):
71
+ """
72
+ Override to() method to ensure all sub-models are moved to the same device
73
+ """
74
+ for model in self.models:
75
+ model.to(device)
76
+ return super(WatermelonMoEModel, self).to(device)
77
+
78
  def forward(self, mfcc, image):
79
  """
80
  Forward pass through the MoE model.
 
160
 
161
  # Load MoE model
162
  moe_model = WatermelonMoEModel(TOP_MODELS, model_dir, weights)
163
+ # Explicitly move the entire model to device
164
+ moe_model = moe_model.to(device)
165
  moe_model.eval()
166
  print(f"\033[92mINFO\033[0m: Loaded MoE model with {len(moe_model.models)} backbone models")
167
 
 
248
 
249
  # Add batch dimension for inference and move to device
250
  if mfcc is not None:
251
+ # Ensure mfcc is on the same device as the model
252
  mfcc = mfcc.unsqueeze(0).to(device)
253
+ print(f"\033[92mDEBUG\033[0m: Final MFCC shape with batch dimension: {mfcc.shape}, device: {mfcc.device}")
254
 
255
  if processed_image is not None:
256
+ # Ensure processed_image is on the same device as the model
257
  processed_image = processed_image.unsqueeze(0).to(device)
258
+ print(f"\033[92mDEBUG\033[0m: Final image shape with batch dimension: {processed_image.shape}, device: {processed_image.device}")
259
+
260
+ # Double-check model is on the correct device
261
+ print(f"\033[92mDEBUG\033[0m: MoE model device: {next(moe_model.parameters()).device}")
262
+ for i, model in enumerate(moe_model.models):
263
+ print(f"\033[92mDEBUG\033[0m: Model {i} device: {next(model.parameters()).device}")
264
 
265
  # Run inference with MoE model
266
  print(f"\033[92mDEBUG\033[0m: Running inference with MoE model on device: {device}")
app_moe.py CHANGED
@@ -67,6 +67,14 @@ class WatermelonMoEModel(torch.nn.Module):
67
  print(f"\033[92mINFO\033[0m: Loaded {len(self.models)} models for MoE ensemble")
68
  print(f"\033[92mINFO\033[0m: Model weights: {self.weights}")
69
 
 
 
 
 
 
 
 
 
70
  def forward(self, mfcc, image):
71
  """
72
  Forward pass through the MoE model.
@@ -152,7 +160,8 @@ def predict_sugar_content(audio, image, model_dir="models", weights=None):
152
 
153
  # Load MoE model
154
  moe_model = WatermelonMoEModel(TOP_MODELS, model_dir, weights)
155
- moe_model.to(device)
 
156
  moe_model.eval()
157
  print(f"\033[92mINFO\033[0m: Loaded MoE model with {len(moe_model.models)} backbone models")
158
 
@@ -239,12 +248,19 @@ def predict_sugar_content(audio, image, model_dir="models", weights=None):
239
 
240
  # Add batch dimension for inference and move to device
241
  if mfcc is not None:
 
242
  mfcc = mfcc.unsqueeze(0).to(device)
243
- print(f"\033[92mDEBUG\033[0m: Final MFCC shape with batch dimension: {mfcc.shape}")
244
 
245
  if processed_image is not None:
 
246
  processed_image = processed_image.unsqueeze(0).to(device)
247
- print(f"\033[92mDEBUG\033[0m: Final image shape with batch dimension: {processed_image.shape}")
 
 
 
 
 
248
 
249
  # Run inference with MoE model
250
  print(f"\033[92mDEBUG\033[0m: Running inference with MoE model on device: {device}")
 
67
  print(f"\033[92mINFO\033[0m: Loaded {len(self.models)} models for MoE ensemble")
68
  print(f"\033[92mINFO\033[0m: Model weights: {self.weights}")
69
 
70
+ def to(self, device):
71
+ """
72
+ Override to() method to ensure all sub-models are moved to the same device
73
+ """
74
+ for model in self.models:
75
+ model.to(device)
76
+ return super(WatermelonMoEModel, self).to(device)
77
+
78
  def forward(self, mfcc, image):
79
  """
80
  Forward pass through the MoE model.
 
160
 
161
  # Load MoE model
162
  moe_model = WatermelonMoEModel(TOP_MODELS, model_dir, weights)
163
+ # Explicitly move the entire model to device
164
+ moe_model = moe_model.to(device)
165
  moe_model.eval()
166
  print(f"\033[92mINFO\033[0m: Loaded MoE model with {len(moe_model.models)} backbone models")
167
 
 
248
 
249
  # Add batch dimension for inference and move to device
250
  if mfcc is not None:
251
+ # Ensure mfcc is on the same device as the model
252
  mfcc = mfcc.unsqueeze(0).to(device)
253
+ print(f"\033[92mDEBUG\033[0m: Final MFCC shape with batch dimension: {mfcc.shape}, device: {mfcc.device}")
254
 
255
  if processed_image is not None:
256
+ # Ensure processed_image is on the same device as the model
257
  processed_image = processed_image.unsqueeze(0).to(device)
258
+ print(f"\033[92mDEBUG\033[0m: Final image shape with batch dimension: {processed_image.shape}, device: {processed_image.device}")
259
+
260
+ # Double-check model is on the correct device
261
+ print(f"\033[92mDEBUG\033[0m: MoE model device: {next(moe_model.parameters()).device}")
262
+ for i, model in enumerate(moe_model.models):
263
+ print(f"\033[92mDEBUG\033[0m: Model {i} device: {next(model.parameters()).device}")
264
 
265
  # Run inference with MoE model
266
  print(f"\033[92mDEBUG\033[0m: Running inference with MoE model on device: {device}")