truens66 commited on
Commit
28eea81
·
verified ·
1 Parent(s): 8304756

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -3
app.py CHANGED
@@ -131,9 +131,18 @@ mp_face_mesh = mp.solutions.face_mesh
131
  face_detection = mp_face_detection.FaceDetection(model_selection=1, min_detection_confidence=0.5)
132
  face_mesh = mp_face_mesh.FaceMesh(static_image_mode=False, max_num_faces=1, min_detection_confidence=0.5)
133
 
134
- # Load the model from the .pkl file
135
- model = create_model() # Ensure architecture matches the saved model
136
- model.load_state_dict(torch.load("resnet34.pkl", map_location=torch.device('cpu'))) # Use 'cuda' if loading on GPU
 
 
 
 
 
 
 
 
 
137
  model.eval()
138
 
139
  # Define transformation for face images
 
131
  face_detection = mp_face_detection.FaceDetection(model_selection=1, min_detection_confidence=0.5)
132
  face_mesh = mp_face_mesh.FaceMesh(static_image_mode=False, max_num_faces=1, min_detection_confidence=0.5)
133
 
134
+ def create_model():
135
+ model = models.resnet34(pretrained=False) # Must match the architecture used during training
136
+ model.fc = torch.nn.Linear(model.fc.in_features, 2) # Adjust final layer
137
+ return model
138
+
139
+ # Load the trained model
140
+ model = create_model()
141
+ try:
142
+ model.load_state_dict(torch.load("resnet34.pkl", map_location='cpu'))
143
+ except RuntimeError as e:
144
+ print(f"Error loading model: {e}")
145
+ # Handle architecture mismatch (e.g., load weights manually)
146
  model.eval()
147
 
148
  # Define transformation for face images