dhruv2842 commited on
Commit
1b4fc02
·
verified ·
1 Parent(s): d6c8fb1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -9
app.py CHANGED
@@ -42,18 +42,22 @@ init_db()
42
  model = models.densenet169(pretrained=False)
43
  model.classifier = nn.Linear(model.classifier.in_features, 3)
44
 
45
- # 2️⃣ Load checkpoint
46
- checkpoint = torch.load('densenet169_seed40_best.pt', map_location='cpu')
47
 
48
- # If checkpoint contains state_dict directly
49
- # model.load_state_dict(checkpoint)
 
 
 
50
 
51
- # If checkpoint contains a dict
52
- if 'model_state_dict' in checkpoint:
53
- model.load_state_dict(checkpoint['model_state_dict'])
54
- else:
55
- model.load_state_dict(checkpoint)
56
 
 
 
 
 
57
  # ✅ Preprocess Image
58
  transform = transforms.Compose([
59
  transforms.Resize((224, 224)), # Adjust if needed
 
42
  model = models.densenet169(pretrained=False)
43
  model.classifier = nn.Linear(model.classifier.in_features, 3)
44
 
45
+ # 2️⃣ Load your checkpoint
46
+ state_dict = torch.load('densenet169_seed40_best.pt', map_location='cpu')
47
 
48
+ # 3️⃣ Strip the "features.0." prefix
49
+ new_state_dict = {}
50
+ for k, v in state_dict.items():
51
+ new_key = k.replace("features.0.", "")
52
+ new_state_dict[new_key] = v
53
 
54
+ # 4️⃣ Load the stripped state dict
55
+ model.load_state_dict(new_state_dict)
 
 
 
56
 
57
+ # 5️⃣ Set eval mode
58
+ model.eval()
59
+
60
+ print("✅ Model successfully loaded and ready for inference.")
61
  # ✅ Preprocess Image
62
  transform = transforms.Compose([
63
  transforms.Resize((224, 224)), # Adjust if needed