Sumayyea commited on
Commit
d074c91
Β·
verified Β·
1 Parent(s): b2047f0

Update model_utils.py

Browse files
Files changed (1) hide show
  1. model_utils.py +32 -27
model_utils.py CHANGED
@@ -163,13 +163,6 @@ class GoogleDriveModelLoader:
163
  def load_model(self, model_type, magnification):
164
  """
165
  Load a specific model (downloads if necessary)
166
-
167
- Args:
168
- model_type: 'efficientnet', 'vgg16', or 'vit'
169
- magnification: '100x' or '400x'
170
-
171
- Returns:
172
- Loaded model
173
  """
174
  model_key = f"{model_type}_{magnification}"
175
 
@@ -183,7 +176,7 @@ class GoogleDriveModelLoader:
183
  # Download model if needed
184
  model_path = self.download_model_from_gdrive(file_id, model_type, magnification)
185
 
186
- # Create model instance
187
  if model_type == 'efficientnet':
188
  model = OralGuardEfficientNet(num_classes=2, pretrained=False)
189
  elif model_type == 'vgg16':
@@ -193,31 +186,43 @@ class GoogleDriveModelLoader:
193
  else:
194
  raise ValueError(f"Unknown model type: {model_type}")
195
 
 
 
 
196
  # Load weights
197
  st.info(f"πŸ”„ Loading {model_key} into memory...")
198
- checkpoint = torch.load(model_path, map_location=self.device)
199
 
200
- # Handle different checkpoint formats
201
- if isinstance(checkpoint, dict):
202
- if 'model_state_dict' in checkpoint:
203
- model.load_state_dict(checkpoint['model_state_dict'])
204
- elif 'state_dict' in checkpoint:
205
- model.load_state_dict(checkpoint['state_dict'])
 
 
 
 
 
206
  else:
207
  model.load_state_dict(checkpoint)
208
- else:
209
- model.load_state_dict(checkpoint)
210
-
211
- # Move to device and set to eval mode
212
- model = model.to(self.device)
213
- model.eval()
214
-
215
- # Cache the loaded model
216
- self.loaded_models[model_key] = model
217
- st.success(f"βœ… {model_key} loaded successfully!")
 
 
 
 
 
 
 
218
 
219
- return model
220
-
221
  def get_model_info(self):
222
  """Get information about loaded models"""
223
  info = {}
 
163
  def load_model(self, model_type, magnification):
164
  """
165
  Load a specific model (downloads if necessary)
 
 
 
 
 
 
 
166
  """
167
  model_key = f"{model_type}_{magnification}"
168
 
 
176
  # Download model if needed
177
  model_path = self.download_model_from_gdrive(file_id, model_type, magnification)
178
 
179
+ # CREATE MODEL INSTANCE FIRST
180
  if model_type == 'efficientnet':
181
  model = OralGuardEfficientNet(num_classes=2, pretrained=False)
182
  elif model_type == 'vgg16':
 
186
  else:
187
  raise ValueError(f"Unknown model type: {model_type}")
188
 
189
+ # CRITICAL FIX: Convert Path to absolute string
190
+ model_path_str = str(model_path.absolute())
191
+
192
  # Load weights
193
  st.info(f"πŸ”„ Loading {model_key} into memory...")
 
194
 
195
+ try:
196
+ checkpoint = torch.load(model_path_str, map_location=self.device)
197
+
198
+ # Handle different checkpoint formats
199
+ if isinstance(checkpoint, dict):
200
+ if 'model_state_dict' in checkpoint:
201
+ model.load_state_dict(checkpoint['model_state_dict'])
202
+ elif 'state_dict' in checkpoint:
203
+ model.load_state_dict(checkpoint['state_dict'])
204
+ else:
205
+ model.load_state_dict(checkpoint)
206
  else:
207
  model.load_state_dict(checkpoint)
208
+
209
+ # Move to device and set to eval mode
210
+ model = model.to(self.device)
211
+ model.eval()
212
+
213
+ # Cache the loaded model
214
+ self.loaded_models[model_key] = model
215
+ st.success(f"βœ… {model_key} loaded successfully!")
216
+
217
+ return model
218
+
219
+ except Exception as e:
220
+ st.error(f"❌ Failed to load model: {str(e)}")
221
+ st.error(f"Model path: {model_path_str}")
222
+ st.error(f"File exists: {os.path.exists(model_path_str)}")
223
+ raise
224
+
225
 
 
 
226
  def get_model_info(self):
227
  """Get information about loaded models"""
228
  info = {}