Spaces:
Sleeping
Sleeping
Fix person classification model inference
Browse files- Fixed input dimensions to 448x640 (was incorrectly 640x480)
- Added better error handling with full traceback
- Fixed quantization handling to check if quantization info exists
- Added debug output for model input/output shapes and dtypes
- Updated UI to show correct input dimensions
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <[email protected]>
app.py
CHANGED
|
@@ -32,6 +32,9 @@ output_details = interpreter.get_output_details()
|
|
| 32 |
class_labels = ["No Person", "Person"]
|
| 33 |
|
| 34 |
print(f"Model loaded successfully! Input shape: {input_details[0]['shape']}")
|
|
|
|
|
|
|
|
|
|
| 35 |
print(f"Number of classes: {len(class_labels)}")
|
| 36 |
print(f"SRAM-optimized model also available: {SRAM_MODEL_FILE}")
|
| 37 |
# Force rebuild with modern design
|
|
@@ -41,8 +44,8 @@ def preprocess_image(image):
|
|
| 41 |
"""
|
| 42 |
Preprocess image for Person Classification INT8 quantized model.
|
| 43 |
"""
|
| 44 |
-
# Resize to
|
| 45 |
-
image = image.resize((
|
| 46 |
|
| 47 |
# Convert to numpy array
|
| 48 |
img_array = np.array(image, dtype=np.float32)
|
|
@@ -87,10 +90,14 @@ def classify_image(image):
|
|
| 87 |
predictions = output_data[0] # Remove batch dimension
|
| 88 |
|
| 89 |
# Convert from INT8 quantized output to probabilities
|
| 90 |
-
# Dequantize the output
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
# For binary classification, get the probability
|
| 96 |
# The model outputs a single value for person probability
|
|
@@ -112,8 +119,11 @@ def classify_image(image):
|
|
| 112 |
|
| 113 |
return result_text
|
| 114 |
|
| 115 |
-
except Exception:
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
def load_example_image(example_path):
|
| 119 |
"""Load example images for demonstration."""
|
|
@@ -368,7 +378,7 @@ with gr.Blocks(
|
|
| 368 |
SRAM: ~1.5 MB<br>
|
| 369 |
Flash: ~1.5 MB<br>
|
| 370 |
Model: Person Classifier<br>
|
| 371 |
-
Input: 640×
|
| 372 |
</div>
|
| 373 |
</div>
|
| 374 |
</div>
|
|
|
|
| 32 |
class_labels = ["No Person", "Person"]
|
| 33 |
|
| 34 |
print(f"Model loaded successfully! Input shape: {input_details[0]['shape']}")
|
| 35 |
+
print(f"Input dtype: {input_details[0]['dtype']}")
|
| 36 |
+
print(f"Output shape: {output_details[0]['shape']}")
|
| 37 |
+
print(f"Output dtype: {output_details[0]['dtype']}")
|
| 38 |
print(f"Number of classes: {len(class_labels)}")
|
| 39 |
print(f"SRAM-optimized model also available: {SRAM_MODEL_FILE}")
|
| 40 |
# Force rebuild with modern design
|
|
|
|
| 44 |
"""
|
| 45 |
Preprocess image for Person Classification INT8 quantized model.
|
| 46 |
"""
|
| 47 |
+
# Resize to 448x640 as expected by the model
|
| 48 |
+
image = image.resize((448, 640))
|
| 49 |
|
| 50 |
# Convert to numpy array
|
| 51 |
img_array = np.array(image, dtype=np.float32)
|
|
|
|
| 90 |
predictions = output_data[0] # Remove batch dimension
|
| 91 |
|
| 92 |
# Convert from INT8 quantized output to probabilities
|
| 93 |
+
# Dequantize the output if quantization info is available
|
| 94 |
+
if 'quantization' in output_details[0] and output_details[0]['quantization'] is not None:
|
| 95 |
+
scale = output_details[0]['quantization'][0]
|
| 96 |
+
zero_point = output_details[0]['quantization'][1]
|
| 97 |
+
predictions = scale * (predictions.astype(np.float32) - zero_point)
|
| 98 |
+
else:
|
| 99 |
+
# If no quantization info, assume output is already in correct format
|
| 100 |
+
predictions = predictions.astype(np.float32)
|
| 101 |
|
| 102 |
# For binary classification, get the probability
|
| 103 |
# The model outputs a single value for person probability
|
|
|
|
| 119 |
|
| 120 |
return result_text
|
| 121 |
|
| 122 |
+
except Exception as e:
|
| 123 |
+
import traceback
|
| 124 |
+
error_msg = f"Error processing image: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
|
| 125 |
+
print(error_msg) # Log to console
|
| 126 |
+
return f"Error processing image: {str(e)}"
|
| 127 |
|
| 128 |
def load_example_image(example_path):
|
| 129 |
"""Load example images for demonstration."""
|
|
|
|
| 378 |
SRAM: ~1.5 MB<br>
|
| 379 |
Flash: ~1.5 MB<br>
|
| 380 |
Model: Person Classifier<br>
|
| 381 |
+
Input: 448×640×3
|
| 382 |
</div>
|
| 383 |
</div>
|
| 384 |
</div>
|