Claude commited on
Fix image analysis error: scalar conversion and return value mismatch
Browse files- Use .item() instead of float(.numpy()) for pred_score extraction to
handle non-0-dimensional tensors correctly
- Fix predict() returning 3 values when image is None (Gradio expects 2)
https://claude.ai/code/session_01WE2rQavbE8N5j1XBM874K5
- gradio_ui/handlers.py +2 -2
- inference.py +1 -1
gradio_ui/handlers.py
CHANGED
|
@@ -60,10 +60,10 @@ def predict(image, model_name: str, category: str):
|
|
| 60 |
category: Selected category
|
| 61 |
|
| 62 |
Returns:
|
| 63 |
-
Tuple of (visualization_image,
|
| 64 |
"""
|
| 65 |
if image is None:
|
| 66 |
-
return None, "⚠️ Please upload an image"
|
| 67 |
|
| 68 |
try:
|
| 69 |
# Save temp image if needed
|
|
|
|
| 60 |
category: Selected category
|
| 61 |
|
| 62 |
Returns:
|
| 63 |
+
Tuple of (visualization_image, result_text)
|
| 64 |
"""
|
| 65 |
if image is None:
|
| 66 |
+
return None, "⚠️ Please upload an image"
|
| 67 |
|
| 68 |
try:
|
| 69 |
# Save temp image if needed
|
inference.py
CHANGED
|
@@ -62,7 +62,7 @@ def run_inference(image_path: str, category: str = "bottle", model_name: str = "
|
|
| 62 |
results = {"image_path": image_path, "category": category, "model": model_name}
|
| 63 |
|
| 64 |
for batch in predictions:
|
| 65 |
-
results["anomaly_score"] =
|
| 66 |
results["anomaly_map"] = batch.anomaly_map[0].cpu().numpy() if batch.anomaly_map is not None else None
|
| 67 |
results["pred_mask"] = batch.pred_mask[0].cpu().numpy() if batch.pred_mask is not None else None
|
| 68 |
break
|
|
|
|
| 62 |
results = {"image_path": image_path, "category": category, "model": model_name}
|
| 63 |
|
| 64 |
for batch in predictions:
|
| 65 |
+
results["anomaly_score"] = batch.pred_score[0].cpu().item() if batch.pred_score is not None else None
|
| 66 |
results["anomaly_map"] = batch.anomaly_map[0].cpu().numpy() if batch.anomaly_map is not None else None
|
| 67 |
results["pred_mask"] = batch.pred_mask[0].cpu().numpy() if batch.pred_mask is not None else None
|
| 68 |
break
|