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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Deep debugging script to find why UNKNOWN_CLASS still occurs
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
import os
|
| 6 |
+
import sys
|
| 7 |
+
import numpy as np
|
| 8 |
+
import cv2
|
| 9 |
+
from PIL import Image
|
| 10 |
+
import traceback
|
| 11 |
+
|
| 12 |
+
# Add current directory to path
|
| 13 |
+
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
| 14 |
+
|
| 15 |
+
def deep_debug_classification():
|
| 16 |
+
"""Deep debug the classification process step by step"""
|
| 17 |
+
print("Deep Debug: Face Classification Process")
|
| 18 |
+
print("=" * 50)
|
| 19 |
+
|
| 20 |
+
try:
|
| 21 |
+
# Step 1: Check model files
|
| 22 |
+
print("1. Checking model files...")
|
| 23 |
+
model_files = {
|
| 24 |
+
'siamese_model.t7': 'Siamese network',
|
| 25 |
+
'decision_tree_model.sav': 'DecisionTree classifier',
|
| 26 |
+
'face_recognition_scaler.sav': 'Feature scaler'
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
for file, desc in model_files.items():
|
| 30 |
+
if os.path.exists(file):
|
| 31 |
+
size = os.path.getsize(file)
|
| 32 |
+
print(f" β {file} exists ({size} bytes) - {desc}")
|
| 33 |
+
else:
|
| 34 |
+
print(f" β {file} missing - {desc}")
|
| 35 |
+
|
| 36 |
+
# Step 2: Check classifier classes
|
| 37 |
+
print("\n2. Checking classifier classes...")
|
| 38 |
+
try:
|
| 39 |
+
import joblib
|
| 40 |
+
classifier = joblib.load('decision_tree_model.sav')
|
| 41 |
+
scaler = joblib.load('face_recognition_scaler.sav')
|
| 42 |
+
|
| 43 |
+
print(f" Classifier classes: {classifier.classes_}")
|
| 44 |
+
print(f" Number of classes: {len(classifier.classes_)}")
|
| 45 |
+
print(f" Class range: {min(classifier.classes_)} to {max(classifier.classes_)}")
|
| 46 |
+
print(f" Scaler features: {scaler.n_features_in_}")
|
| 47 |
+
|
| 48 |
+
except Exception as e:
|
| 49 |
+
print(f" β Error loading models: {e}")
|
| 50 |
+
return
|
| 51 |
+
|
| 52 |
+
# Step 3: Check CLASS_NAMES
|
| 53 |
+
print("\n3. Checking CLASS_NAMES...")
|
| 54 |
+
try:
|
| 55 |
+
from face_recognition import CLASS_NAMES
|
| 56 |
+
print(f" CLASS_NAMES: {CLASS_NAMES}")
|
| 57 |
+
print(f" Number of CLASS_NAMES: {len(CLASS_NAMES)}")
|
| 58 |
+
|
| 59 |
+
# Check if CLASS_NAMES length matches classifier classes
|
| 60 |
+
if len(CLASS_NAMES) == len(classifier.classes_):
|
| 61 |
+
print(" β CLASS_NAMES length matches classifier classes")
|
| 62 |
+
else:
|
| 63 |
+
print(f" β Length mismatch: CLASS_NAMES={len(CLASS_NAMES)}, classifier={len(classifier.classes_)}")
|
| 64 |
+
|
| 65 |
+
except Exception as e:
|
| 66 |
+
print(f" β Error checking CLASS_NAMES: {e}")
|
| 67 |
+
|
| 68 |
+
# Step 4: Test with actual image
|
| 69 |
+
print("\n4. Testing with actual Person1 image...")
|
| 70 |
+
actual_image_path = "../static/Person1_1697805233.jpg"
|
| 71 |
+
if os.path.exists(actual_image_path):
|
| 72 |
+
print(f" β Found actual image: {actual_image_path}")
|
| 73 |
+
|
| 74 |
+
try:
|
| 75 |
+
# Load image
|
| 76 |
+
img = cv2.imread(actual_image_path)
|
| 77 |
+
print(f" Image shape: {img.shape}")
|
| 78 |
+
|
| 79 |
+
# Test face detection
|
| 80 |
+
from face_recognition import detected_face
|
| 81 |
+
detected = detected_face(img)
|
| 82 |
+
print(f" Face detected: {type(detected)}")
|
| 83 |
+
|
| 84 |
+
# Test full classification
|
| 85 |
+
from face_recognition import get_face_class
|
| 86 |
+
result = get_face_class(img)
|
| 87 |
+
print(f" Classification result: {result}")
|
| 88 |
+
|
| 89 |
+
# Debug the classification process step by step
|
| 90 |
+
print("\n5. Step-by-step classification debug...")
|
| 91 |
+
debug_classification_step_by_step(img)
|
| 92 |
+
|
| 93 |
+
except Exception as e:
|
| 94 |
+
print(f" β Error with actual image: {e}")
|
| 95 |
+
traceback.print_exc()
|
| 96 |
+
else:
|
| 97 |
+
print(f" β No actual image found at {actual_image_path}")
|
| 98 |
+
|
| 99 |
+
except Exception as e:
|
| 100 |
+
print(f"β Error in deep debug: {e}")
|
| 101 |
+
traceback.print_exc()
|
| 102 |
+
|
| 103 |
+
def debug_classification_step_by_step(img):
|
| 104 |
+
"""Debug the classification process step by step"""
|
| 105 |
+
try:
|
| 106 |
+
import torch
|
| 107 |
+
from face_recognition import detected_face, trnscm, CLASS_NAMES
|
| 108 |
+
from face_recognition_model import Siamese
|
| 109 |
+
|
| 110 |
+
print(" Step 1: Face detection...")
|
| 111 |
+
det_img = detected_face(img)
|
| 112 |
+
if det_img == 0:
|
| 113 |
+
det_img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY))
|
| 114 |
+
print(f" Detected face type: {type(det_img)}")
|
| 115 |
+
|
| 116 |
+
print(" Step 2: Image transformation...")
|
| 117 |
+
face_tensor = trnscm(det_img).unsqueeze(0)
|
| 118 |
+
print(f" Tensor shape: {face_tensor.shape}")
|
| 119 |
+
|
| 120 |
+
print(" Step 3: Siamese network...")
|
| 121 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
| 122 |
+
siamese_net = Siamese().to(device)
|
| 123 |
+
|
| 124 |
+
# Load model
|
| 125 |
+
model_data = torch.load('siamese_model.t7', map_location=device)
|
| 126 |
+
if isinstance(model_data, dict) and 'net_dict' in model_data:
|
| 127 |
+
siamese_net.load_state_dict(model_data['net_dict'])
|
| 128 |
+
else:
|
| 129 |
+
siamese_net.load_state_dict(model_data)
|
| 130 |
+
siamese_net.eval()
|
| 131 |
+
|
| 132 |
+
print(" Step 4: Feature extraction...")
|
| 133 |
+
with torch.no_grad():
|
| 134 |
+
embedding = siamese_net.forward_once(face_tensor.to(device)).cpu().numpy()
|
| 135 |
+
print(f" Embedding shape: {embedding.shape}")
|
| 136 |
+
print(f" Embedding values: {embedding}")
|
| 137 |
+
|
| 138 |
+
print(" Step 5: Classification...")
|
| 139 |
+
import joblib
|
| 140 |
+
scaler = joblib.load('face_recognition_scaler.sav')
|
| 141 |
+
classifier = joblib.load('decision_tree_model.sav')
|
| 142 |
+
|
| 143 |
+
# Reshape embedding if needed
|
| 144 |
+
if embedding.ndim == 1:
|
| 145 |
+
embedding = embedding.reshape(1, -1)
|
| 146 |
+
|
| 147 |
+
print(f" Embedding after reshape: {embedding.shape}")
|
| 148 |
+
|
| 149 |
+
# Scale features
|
| 150 |
+
embedding_scaled = scaler.transform(embedding)
|
| 151 |
+
print(f" Scaled embedding: {embedding_scaled}")
|
| 152 |
+
|
| 153 |
+
# Predict
|
| 154 |
+
predicted_label_index = classifier.predict(embedding_scaled)[0]
|
| 155 |
+
print(f" Predicted index: {predicted_label_index}")
|
| 156 |
+
print(f" Classifier classes: {classifier.classes_}")
|
| 157 |
+
|
| 158 |
+
# Map to class name
|
| 159 |
+
print(f" CLASS_NAMES: {CLASS_NAMES}")
|
| 160 |
+
print(f" CLASS_NAMES length: {len(CLASS_NAMES)}")
|
| 161 |
+
|
| 162 |
+
if predicted_label_index < len(CLASS_NAMES):
|
| 163 |
+
class_name = CLASS_NAMES[predicted_label_index]
|
| 164 |
+
print(f" β Mapped to: {class_name}")
|
| 165 |
+
else:
|
| 166 |
+
print(f" β Index {predicted_label_index} is out of range!")
|
| 167 |
+
print(f" β This causes UNKNOWN_CLASS!")
|
| 168 |
+
|
| 169 |
+
except Exception as e:
|
| 170 |
+
print(f" β Error in step-by-step debug: {e}")
|
| 171 |
+
traceback.print_exc()
|
| 172 |
+
|
| 173 |
+
if __name__ == "__main__":
|
| 174 |
+
deep_debug_classification()
|
app/Hackathon_setup/face_recognition.py
CHANGED
|
@@ -121,16 +121,23 @@ def detected_face(image):
|
|
| 121 |
|
| 122 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 123 |
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
face_areas = []
|
| 125 |
images = []
|
| 126 |
-
required_image = 0
|
| 127 |
|
| 128 |
for i, (x, y, w, h) in enumerate(faces):
|
| 129 |
face_cropped = gray[y:y+h, x:x+w]
|
| 130 |
face_areas.append(w*h)
|
| 131 |
images.append(face_cropped)
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
| 134 |
|
| 135 |
return required_image
|
| 136 |
|
|
@@ -141,7 +148,7 @@ def get_similarity(img1, img2):
|
|
| 141 |
try:
|
| 142 |
det_img1 = detected_face(img1)
|
| 143 |
det_img2 = detected_face(img2)
|
| 144 |
-
if det_img1
|
| 145 |
det_img1 = Image.fromarray(cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY))
|
| 146 |
det_img2 = Image.fromarray(cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY))
|
| 147 |
|
|
@@ -176,7 +183,7 @@ def get_face_class(img1):
|
|
| 176 |
|
| 177 |
try:
|
| 178 |
det_img1 = detected_face(img1)
|
| 179 |
-
if det_img1
|
| 180 |
det_img1 = Image.fromarray(cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY))
|
| 181 |
|
| 182 |
face1_tensor = trnscm(det_img1).unsqueeze(0).to(device)
|
|
|
|
| 121 |
|
| 122 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 123 |
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
|
| 124 |
+
|
| 125 |
+
# If no faces detected, return None
|
| 126 |
+
if len(faces) == 0:
|
| 127 |
+
print("No faces detected, using fallback")
|
| 128 |
+
return None
|
| 129 |
+
|
| 130 |
face_areas = []
|
| 131 |
images = []
|
|
|
|
| 132 |
|
| 133 |
for i, (x, y, w, h) in enumerate(faces):
|
| 134 |
face_cropped = gray[y:y+h, x:x+w]
|
| 135 |
face_areas.append(w*h)
|
| 136 |
images.append(face_cropped)
|
| 137 |
+
|
| 138 |
+
# Get the largest face
|
| 139 |
+
largest_face_idx = np.argmax(face_areas)
|
| 140 |
+
required_image = Image.fromarray(images[largest_face_idx])
|
| 141 |
|
| 142 |
return required_image
|
| 143 |
|
|
|
|
| 148 |
try:
|
| 149 |
det_img1 = detected_face(img1)
|
| 150 |
det_img2 = detected_face(img2)
|
| 151 |
+
if det_img1 is None or det_img2 is None:
|
| 152 |
det_img1 = Image.fromarray(cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY))
|
| 153 |
det_img2 = Image.fromarray(cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY))
|
| 154 |
|
|
|
|
| 183 |
|
| 184 |
try:
|
| 185 |
det_img1 = detected_face(img1)
|
| 186 |
+
if det_img1 is None:
|
| 187 |
det_img1 = Image.fromarray(cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY))
|
| 188 |
|
| 189 |
face1_tensor = trnscm(det_img1).unsqueeze(0).to(device)
|
app/Hackathon_setup/test_complete_fix.py
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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IL import Image
|
| 10 |
+
|
| 11 |
+
# Add current directory to path
|
| 12 |
+
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
| 13 |
+
|
| 14 |
+
def test_complete_fix():
|
| 15 |
+
"""Test all fixes comprehensively"""
|
| 16 |
+
print("Comprehensive Test: All UNKNOWN_CLASS Fixes")
|
| 17 |
+
print("=" * 60)
|
| 18 |
+
|
| 19 |
+
# Test 1: Check all components
|
| 20 |
+
print("1. Checking all components...")
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
from face_recognition import get_face_class, CLASS_NAMES, detected_face
|
| 24 |
+
from face_recognition_model import classes
|
| 25 |
+
import joblib
|
| 26 |
+
|
| 27 |
+
print(f" β face_recognition imported successfully")
|
| 28 |
+
print(f" β CLASS_NAMES: {CLASS_NAMES}")
|
| 29 |
+
print(f" β classes: {classes}")
|
| 30 |
+
|
| 31 |
+
# Check consistency
|
| 32 |
+
if CLASS_NAMES == classes:
|
| 33 |
+
print(" β Class names are consistent")
|
| 34 |
+
else:
|
| 35 |
+
print(" β Class names are inconsistent")
|
| 36 |
+
|
| 37 |
+
# Check classifier
|
| 38 |
+
classifier = joblib.load('decision_tree_model.sav')
|
| 39 |
+
scaler = joblib.load('face_recognition_scaler.sav')
|
| 40 |
+
print(f" β Classifier loaded: {len(classifier.classes_)} classes")
|
| 41 |
+
print(f" β Scaler loaded: {scaler.n_features_in_} features")
|
| 42 |
+
|
| 43 |
+
except Exception as e:
|
| 44 |
+
print(f" β Error importing components: {e}")
|
| 45 |
+
return
|
| 46 |
+
|
| 47 |
+
# Test 2: Test face detection
|
| 48 |
+
print("\n2. Testing face detection...")
|
| 49 |
+
|
| 50 |
+
# Create test image with face
|
| 51 |
+
test_img = create_test_face_image()
|
| 52 |
+
print(f" Test image shape: {test_img.shape}")
|
| 53 |
+
|
| 54 |
+
try:
|
| 55 |
+
detected = detected_face(test_img)
|
| 56 |
+
if detected is not None:
|
| 57 |
+
print(f" β Face detected successfully: {type(detected)}")
|
| 58 |
+
else:
|
| 59 |
+
print(" β No face detected, will use fallback")
|
| 60 |
+
except Exception as e:
|
| 61 |
+
print(f" β Error in face detection: {e}")
|
| 62 |
+
|
| 63 |
+
# Test 3: Test classification with synthetic images
|
| 64 |
+
print("\n3. Testing classification with synthetic images...")
|
| 65 |
+
|
| 66 |
+
for i in range(1, 8): # Person1-Person7
|
| 67 |
+
print(f"\n Testing Person{i}:")
|
| 68 |
+
|
| 69 |
+
# Create synthetic face
|
| 70 |
+
face_img = create_test_face_image(i)
|
| 71 |
+
|
| 72 |
+
try:
|
| 73 |
+
result = get_face_class(face_img)
|
| 74 |
+
print(f" Input: Person{i} synthetic image")
|
| 75 |
+
print(f" Output: {result}")
|
| 76 |
+
|
| 77 |
+
if result in CLASS_NAMES:
|
| 78 |
+
print(f" β Valid classification: {result}")
|
| 79 |
+
elif result == "UNKNOWN_CLASS":
|
| 80 |
+
print(f" β Still getting UNKNOWN_CLASS!")
|
| 81 |
+
elif result.startswith("Error:"):
|
| 82 |
+
print(f" β Error: {result}")
|
| 83 |
+
else:
|
| 84 |
+
print(f" ? Unexpected result: {result}")
|
| 85 |
+
|
| 86 |
+
except Exception as e:
|
| 87 |
+
print(f" β Exception: {e}")
|
| 88 |
+
|
| 89 |
+
# Test 4: Test with actual image
|
| 90 |
+
print("\n4. Testing with actual Person1 image...")
|
| 91 |
+
actual_image_path = "../static/Person1_1697805233.jpg"
|
| 92 |
+
|
| 93 |
+
if os.path.exists(actual_image_path):
|
| 94 |
+
try:
|
| 95 |
+
img = cv2.imread(actual_image_path)
|
| 96 |
+
print(f" β Loaded actual image: {img.shape}")
|
| 97 |
+
|
| 98 |
+
result = get_face_class(img)
|
| 99 |
+
print(f" Result: {result}")
|
| 100 |
+
|
| 101 |
+
if result == "Person1":
|
| 102 |
+
print(" π PERFECT! Person1 correctly classified!")
|
| 103 |
+
elif result in CLASS_NAMES:
|
| 104 |
+
print(f" β Valid classification: {result}")
|
| 105 |
+
elif result == "UNKNOWN_CLASS":
|
| 106 |
+
print(" β Still getting UNKNOWN_CLASS")
|
| 107 |
+
else:
|
| 108 |
+
print(f" ? Unexpected: {result}")
|
| 109 |
+
|
| 110 |
+
except Exception as e:
|
| 111 |
+
print(f" β Error with actual image: {e}")
|
| 112 |
+
import traceback
|
| 113 |
+
traceback.print_exc()
|
| 114 |
+
else:
|
| 115 |
+
print(f" β No actual image found at {actual_image_path}")
|
| 116 |
+
|
| 117 |
+
# Test 5: Debug step by step
|
| 118 |
+
print("\n5. Step-by-step debug...")
|
| 119 |
+
debug_step_by_step(test_img)
|
| 120 |
+
|
| 121 |
+
print("\n" + "=" * 60)
|
| 122 |
+
print("Test completed!")
|
| 123 |
+
|
| 124 |
+
def create_test_face_image(person_id=1):
|
| 125 |
+
"""Create a test face image"""
|
| 126 |
+
img = np.zeros((100, 100, 3), dtype=np.uint8)
|
| 127 |
+
|
| 128 |
+
# Face outline
|
| 129 |
+
cv2.ellipse(img, (50, 50), (40, 50), 0, 0, 360, (120, 120, 120), -1)
|
| 130 |
+
|
| 131 |
+
# Vary features based on person_id
|
| 132 |
+
eye_offset = (person_id - 1) * 3
|
| 133 |
+
mouth_width = 10 + (person_id - 1) * 2
|
| 134 |
+
|
| 135 |
+
# Eyes
|
| 136 |
+
cv2.circle(img, (35 + eye_offset, 40), 4, (200, 200, 200), -1)
|
| 137 |
+
cv2.circle(img, (65 - eye_offset, 40), 4, (200, 200, 200), -1)
|
| 138 |
+
|
| 139 |
+
# Nose
|
| 140 |
+
cv2.line(img, (50, 45), (50, 60), (150, 150, 150), 2)
|
| 141 |
+
|
| 142 |
+
# Mouth
|
| 143 |
+
cv2.ellipse(img, (50, 70), (mouth_width, 6), 0, 0, 180, (150, 150, 150), 2)
|
| 144 |
+
|
| 145 |
+
return img
|
| 146 |
+
|
| 147 |
+
def debug_step_by_step(img):
|
| 148 |
+
"""Debug the classification process step by step"""
|
| 149 |
+
try:
|
| 150 |
+
import torch
|
| 151 |
+
from face_recognition import detected_face, trnscm, CLASS_NAMES
|
| 152 |
+
from face_recognition_model import Siamese
|
| 153 |
+
import joblib
|
| 154 |
+
|
| 155 |
+
print(" Step 1: Face detection...")
|
| 156 |
+
det_img = detected_face(img)
|
| 157 |
+
if det_img is None:
|
| 158 |
+
print(" No face detected, using fallback")
|
| 159 |
+
det_img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY))
|
| 160 |
+
print(f" Face type: {type(det_img)}")
|
| 161 |
+
|
| 162 |
+
print(" Step 2: Image transformation...")
|
| 163 |
+
face_tensor = trnscm(det_img).unsqueeze(0)
|
| 164 |
+
print(f" Tensor shape: {face_tensor.shape}")
|
| 165 |
+
|
| 166 |
+
print(" Step 3: Siamese network...")
|
| 167 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
| 168 |
+
siamese_net = Siamese().to(device)
|
| 169 |
+
|
| 170 |
+
model_data = torch.load('siamese_model.t7', map_location=device)
|
| 171 |
+
if isinstance(model_data, dict) and 'net_dict' in model_data:
|
| 172 |
+
siamese_net.load_state_dict(model_data['net_dict'])
|
| 173 |
+
else:
|
| 174 |
+
siamese_net.load_state_dict(model_data)
|
| 175 |
+
siamese_net.eval()
|
| 176 |
+
|
| 177 |
+
print(" Step 4: Feature extraction...")
|
| 178 |
+
with torch.no_grad():
|
| 179 |
+
embedding = siamese_net.forward_once(face_tensor.to(device)).cpu().numpy()
|
| 180 |
+
print(f" Embedding shape: {embedding.shape}")
|
| 181 |
+
|
| 182 |
+
print(" Step 5: Classification...")
|
| 183 |
+
scaler = joblib.load('face_recognition_scaler.sav')
|
| 184 |
+
classifier = joblib.load('decision_tree_model.sav')
|
| 185 |
+
|
| 186 |
+
if embedding.ndim == 1:
|
| 187 |
+
embedding = embedding.reshape(1, -1)
|
| 188 |
+
|
| 189 |
+
embedding_scaled = scaler.transform(embedding)
|
| 190 |
+
predicted_label_index = classifier.predict(embedding_scaled)[0]
|
| 191 |
+
|
| 192 |
+
print(f" Predicted index: {predicted_label_index}")
|
| 193 |
+
print(f" Classifier classes: {classifier.classes_}")
|
| 194 |
+
print(f" CLASS_NAMES: {CLASS_NAMES}")
|
| 195 |
+
|
| 196 |
+
if predicted_label_index < len(CLASS_NAMES):
|
| 197 |
+
class_name = CLASS_NAMES[predicted_label_index]
|
| 198 |
+
print(f" β Final result: {class_name}")
|
| 199 |
+
else:
|
| 200 |
+
print(f" β Index {predicted_label_index} out of range!")
|
| 201 |
+
|
| 202 |
+
except Exception as e:
|
| 203 |
+
print(f" β Error in debug: {e}")
|
| 204 |
+
import traceback
|
| 205 |
+
traceback.print_exc()
|
| 206 |
+
|
| 207 |
+
if __name__ == "__main__":
|
| 208 |
+
test_complete_fix()
|