Spaces:
Sleeping
Sleeping
rolling back to good version
Browse files
app.py
CHANGED
|
@@ -327,14 +327,14 @@ def transform_image(image_bytes):
|
|
| 327 |
def get_prediction(image_tensor, model):
|
| 328 |
"""Get prediction from model - optimized"""
|
| 329 |
try:
|
| 330 |
-
|
| 331 |
# Ensure tensor is on CPU for consistency
|
| 332 |
if image_tensor.device.type != 'cpu':
|
| 333 |
image_tensor = image_tensor.cpu()
|
| 334 |
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
|
| 339 |
# Convert to Python types immediately for faster processing
|
| 340 |
top5_prob = top5_prob.cpu().numpy()
|
|
@@ -344,7 +344,7 @@ def get_prediction(image_tensor, model):
|
|
| 344 |
for i in range(5):
|
| 345 |
predictions.append({
|
| 346 |
'food': CLASS_NAMES[top5_indices[i]],
|
| 347 |
-
|
| 348 |
})
|
| 349 |
|
| 350 |
return predictions
|
|
|
|
| 327 |
def get_prediction(image_tensor, model):
|
| 328 |
"""Get prediction from model - optimized"""
|
| 329 |
try:
|
| 330 |
+
with torch.no_grad():
|
| 331 |
# Ensure tensor is on CPU for consistency
|
| 332 |
if image_tensor.device.type != 'cpu':
|
| 333 |
image_tensor = image_tensor.cpu()
|
| 334 |
|
| 335 |
+
outputs = model(image_tensor)
|
| 336 |
+
probabilities = torch.nn.functional.softmax(outputs[0], dim=0)
|
| 337 |
+
top5_prob, top5_indices = torch.topk(probabilities, 5)
|
| 338 |
|
| 339 |
# Convert to Python types immediately for faster processing
|
| 340 |
top5_prob = top5_prob.cpu().numpy()
|
|
|
|
| 344 |
for i in range(5):
|
| 345 |
predictions.append({
|
| 346 |
'food': CLASS_NAMES[top5_indices[i]],
|
| 347 |
+
'confidence': float(top5_prob[i] * 100)
|
| 348 |
})
|
| 349 |
|
| 350 |
return predictions
|