feat: Calculate and log prediction confidence
Co-authored-by: aider (gemini/gemini-2.5-pro-preview-05-06) <aider@aider.chat>
This commit is contained in:
13
server.py
13
server.py
@@ -6,6 +6,7 @@ import os
|
||||
import io
|
||||
|
||||
import torch
|
||||
import torch.nn.functional as F
|
||||
from torchvision import transforms
|
||||
from PIL import Image
|
||||
|
||||
@@ -46,8 +47,9 @@ def get_prediction(model, image_bytes, device):
|
||||
|
||||
with torch.no_grad():
|
||||
output = model(input_tensor)
|
||||
_, pred_idx = torch.max(output, 1)
|
||||
return CLASS_NAMES[pred_idx.item()]
|
||||
probabilities = F.softmax(output, dim=1)
|
||||
confidence, pred_idx = torch.max(probabilities, 1)
|
||||
return CLASS_NAMES[pred_idx.item()], confidence.item()
|
||||
|
||||
# --- Background Task ---
|
||||
async def monitor_garage_door(app):
|
||||
@@ -65,9 +67,10 @@ async def monitor_garage_door(app):
|
||||
async with session.get(CAMERA_URL, headers=headers, timeout=REQUEST_TIMEOUT_SECONDS) as response:
|
||||
if response.status == 200:
|
||||
image_bytes = await response.read()
|
||||
prediction = get_prediction(model, image_bytes, device)
|
||||
if prediction:
|
||||
logging.debug(f"Garage door status: {prediction}")
|
||||
result = get_prediction(model, image_bytes, device)
|
||||
if result:
|
||||
prediction, confidence = result
|
||||
logging.debug(f"Garage door status: {prediction} (confidence: {confidence:.4f})")
|
||||
else:
|
||||
logging.error(f"Failed to fetch image. Status: {response.status}, Reason: {response.reason}")
|
||||
|
||||
|
Reference in New Issue
Block a user