Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -153,8 +153,8 @@ def format_description(description, breed):
|
|
| 153 |
|
| 154 |
return formatted_description
|
| 155 |
|
| 156 |
-
async def predict_single_dog(image):
|
| 157 |
-
|
| 158 |
|
| 159 |
# def _predict_single_dog(image):
|
| 160 |
# image_tensor = preprocess_image(image)
|
|
@@ -182,14 +182,16 @@ async def predict_single_dog(image):
|
|
| 182 |
|
| 183 |
|
| 184 |
async def predict_single_dog(image):
|
| 185 |
-
image_tensor =
|
| 186 |
with torch.no_grad():
|
| 187 |
output = model(image_tensor)
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
|
|
|
|
|
|
| 193 |
|
| 194 |
async def detect_multiple_dogs(image, conf_threshold=0.3, iou_threshold=0.45):
|
| 195 |
results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
|
@@ -402,7 +404,7 @@ async def process_single_dog(image):
|
|
| 402 |
|
| 403 |
async def predict(image):
|
| 404 |
if image is None:
|
| 405 |
-
return "Please upload an image to start.", None,
|
| 406 |
|
| 407 |
try:
|
| 408 |
if isinstance(image, np.ndarray):
|
|
@@ -460,13 +462,12 @@ async def predict(image):
|
|
| 460 |
"is_multi_dog": len(dogs) > 1,
|
| 461 |
"dogs_info": explanations
|
| 462 |
}
|
| 463 |
-
return final_explanation, annotated_image, gr.update(visible=False), initial_state
|
| 464 |
|
| 465 |
except Exception as e:
|
| 466 |
-
error_msg = f"An error occurred: {str(e)}"
|
| 467 |
print(error_msg)
|
| 468 |
-
return error_msg, None, gr.update(visible=False), None
|
| 469 |
-
|
| 470 |
|
| 471 |
def show_details(choice, previous_output, initial_state):
|
| 472 |
if not choice:
|
|
@@ -483,7 +484,7 @@ def show_details(choice, previous_output, initial_state):
|
|
| 483 |
return formatted_description, gr.update(visible=True), initial_state
|
| 484 |
except Exception as e:
|
| 485 |
error_msg = f"An error occurred while showing details: {e}"
|
| 486 |
-
|
| 487 |
return error_msg, gr.update(visible=True), initial_state
|
| 488 |
|
| 489 |
def go_back(state):
|
|
@@ -495,7 +496,6 @@ def go_back(state):
|
|
| 495 |
gr.update(visible=False),
|
| 496 |
state
|
| 497 |
)
|
| 498 |
-
|
| 499 |
|
| 500 |
with gr.Blocks() as iface:
|
| 501 |
gr.HTML("<h1 style='text-align: center;'>๐ถ Dog Breed Classifier ๐</h1>")
|
|
@@ -530,7 +530,7 @@ with gr.Blocks() as iface:
|
|
| 530 |
inputs=[initial_state],
|
| 531 |
outputs=[output, output_image, breed_buttons, back_button, initial_state]
|
| 532 |
)
|
| 533 |
-
|
| 534 |
gr.Examples(
|
| 535 |
examples=['Border_Collie.jpg', 'Golden_Retriever.jpeg', 'Saint_Bernard.jpeg', 'French_Bulldog.jpeg', 'Samoyed.jpg'],
|
| 536 |
inputs=input_image
|
|
@@ -539,6 +539,4 @@ with gr.Blocks() as iface:
|
|
| 539 |
gr.HTML('For more details on this project and other work, feel free to visit my GitHub <a href="https://github.com/Eric-Chung-0511/Learning-Record/tree/main/Data%20Science%20Projects/Dog_Breed_Classifier">Dog Breed Classifier</a>')
|
| 540 |
|
| 541 |
if __name__ == "__main__":
|
| 542 |
-
iface.launch()
|
| 543 |
-
|
| 544 |
-
|
|
|
|
| 153 |
|
| 154 |
return formatted_description
|
| 155 |
|
| 156 |
+
# async def predict_single_dog(image):
|
| 157 |
+
# return await asyncio.to_thread(_predict_single_dog, image)
|
| 158 |
|
| 159 |
# def _predict_single_dog(image):
|
| 160 |
# image_tensor = preprocess_image(image)
|
|
|
|
| 182 |
|
| 183 |
|
| 184 |
async def predict_single_dog(image):
|
| 185 |
+
image_tensor = preprocess_image(image)
|
| 186 |
with torch.no_grad():
|
| 187 |
output = model(image_tensor)
|
| 188 |
+
logits = output[0] if isinstance(output, tuple) else output
|
| 189 |
+
probabilities = F.softmax(logits, dim=1)
|
| 190 |
+
topk_probs, topk_indices = torch.topk(probabilities, k=3)
|
| 191 |
+
top1_prob = topk_probs[0][0].item()
|
| 192 |
+
topk_breeds = [dog_breeds[idx.item()] for idx in topk_indices[0]]
|
| 193 |
+
topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
|
| 194 |
+
return top1_prob, topk_breeds, topk_probs_percent
|
| 195 |
|
| 196 |
async def detect_multiple_dogs(image, conf_threshold=0.3, iou_threshold=0.45):
|
| 197 |
results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
|
|
|
| 404 |
|
| 405 |
async def predict(image):
|
| 406 |
if image is None:
|
| 407 |
+
return "Please upload an image to start.", None, gr.update(visible=False, choices=[]), None
|
| 408 |
|
| 409 |
try:
|
| 410 |
if isinstance(image, np.ndarray):
|
|
|
|
| 462 |
"is_multi_dog": len(dogs) > 1,
|
| 463 |
"dogs_info": explanations
|
| 464 |
}
|
| 465 |
+
return final_explanation, annotated_image, gr.update(visible=False, choices=[]), initial_state
|
| 466 |
|
| 467 |
except Exception as e:
|
| 468 |
+
error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
|
| 469 |
print(error_msg)
|
| 470 |
+
return error_msg, None, gr.update(visible=False, choices=[]), None
|
|
|
|
| 471 |
|
| 472 |
def show_details(choice, previous_output, initial_state):
|
| 473 |
if not choice:
|
|
|
|
| 484 |
return formatted_description, gr.update(visible=True), initial_state
|
| 485 |
except Exception as e:
|
| 486 |
error_msg = f"An error occurred while showing details: {e}"
|
| 487 |
+
print(error_msg)
|
| 488 |
return error_msg, gr.update(visible=True), initial_state
|
| 489 |
|
| 490 |
def go_back(state):
|
|
|
|
| 496 |
gr.update(visible=False),
|
| 497 |
state
|
| 498 |
)
|
|
|
|
| 499 |
|
| 500 |
with gr.Blocks() as iface:
|
| 501 |
gr.HTML("<h1 style='text-align: center;'>๐ถ Dog Breed Classifier ๐</h1>")
|
|
|
|
| 530 |
inputs=[initial_state],
|
| 531 |
outputs=[output, output_image, breed_buttons, back_button, initial_state]
|
| 532 |
)
|
| 533 |
+
|
| 534 |
gr.Examples(
|
| 535 |
examples=['Border_Collie.jpg', 'Golden_Retriever.jpeg', 'Saint_Bernard.jpeg', 'French_Bulldog.jpeg', 'Samoyed.jpg'],
|
| 536 |
inputs=input_image
|
|
|
|
| 539 |
gr.HTML('For more details on this project and other work, feel free to visit my GitHub <a href="https://github.com/Eric-Chung-0511/Learning-Record/tree/main/Data%20Science%20Projects/Dog_Breed_Classifier">Dog Breed Classifier</a>')
|
| 540 |
|
| 541 |
if __name__ == "__main__":
|
| 542 |
+
iface.launch()
|
|
|
|
|
|