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 io
|
||||||
|
|
||||||
import torch
|
import torch
|
||||||
|
import torch.nn.functional as F
|
||||||
from torchvision import transforms
|
from torchvision import transforms
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
@@ -46,8 +47,9 @@ def get_prediction(model, image_bytes, device):
|
|||||||
|
|
||||||
with torch.no_grad():
|
with torch.no_grad():
|
||||||
output = model(input_tensor)
|
output = model(input_tensor)
|
||||||
_, pred_idx = torch.max(output, 1)
|
probabilities = F.softmax(output, dim=1)
|
||||||
return CLASS_NAMES[pred_idx.item()]
|
confidence, pred_idx = torch.max(probabilities, 1)
|
||||||
|
return CLASS_NAMES[pred_idx.item()], confidence.item()
|
||||||
|
|
||||||
# --- Background Task ---
|
# --- Background Task ---
|
||||||
async def monitor_garage_door(app):
|
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:
|
async with session.get(CAMERA_URL, headers=headers, timeout=REQUEST_TIMEOUT_SECONDS) as response:
|
||||||
if response.status == 200:
|
if response.status == 200:
|
||||||
image_bytes = await response.read()
|
image_bytes = await response.read()
|
||||||
prediction = get_prediction(model, image_bytes, device)
|
result = get_prediction(model, image_bytes, device)
|
||||||
if prediction:
|
if result:
|
||||||
logging.debug(f"Garage door status: {prediction}")
|
prediction, confidence = result
|
||||||
|
logging.debug(f"Garage door status: {prediction} (confidence: {confidence:.4f})")
|
||||||
else:
|
else:
|
||||||
logging.error(f"Failed to fetch image. Status: {response.status}, Reason: {response.reason}")
|
logging.error(f"Failed to fetch image. Status: {response.status}, Reason: {response.reason}")
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user