Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,8 +5,6 @@ import torchvision.models as models
|
|
5 |
import os
|
6 |
import gradio as gr
|
7 |
|
8 |
-
|
9 |
-
|
10 |
# Load the pre-trained model that was used in the training script. In this case it was ResNet18 model
|
11 |
model = models.resnet18()
|
12 |
|
@@ -15,7 +13,7 @@ num_ftrs = model.fc.in_features
|
|
15 |
model.fc = torch.nn.Linear(num_ftrs, 73)
|
16 |
|
17 |
# Load the saved state dictionary (Device agnostic; inference for image classification is decent even on CPU)
|
18 |
-
model.load_state_dict(torch.load('Dogrun2.pth', map_location=torch.device('cpu')))
|
19 |
|
20 |
# Set the model to evaluation mode
|
21 |
model.eval()
|
@@ -33,8 +31,8 @@ transforms_test = v2.Compose([
|
|
33 |
v2.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
34 |
])
|
35 |
|
36 |
-
|
37 |
labels = ['Afghan-Hound', 'Airedale-Terrier', 'Akita', 'Alaskan-Malamute', 'American-Foxhound', 'American-Hairless-Terrier', 'American-Water-Spaniel', 'Basenji', 'Basset-Hound', 'Beagle', 'Bearded-Collie', 'Belgian-Malinois', 'Belgian-Sheepdog', 'Bernese-Mountain-Dog', 'Bichon-Frise', 'Bloodhound', 'Bluetick-Coonhound', 'Border-Collie', 'Borzoi', 'Boston-Terrier', 'Boxer', 'Bull-Terrier', 'Bulldog', 'Bullmastiff', 'Cairn-Terrier', 'Cane-Corso', 'Cavalier-King-Charles-Spaniel', 'Chihuahua', 'Chinese-Crested', 'Chinese-Shar-Pei', 'Chow-Chow', 'Clumber-Spaniel', 'Cockapoo', 'Cocker-Spaniel', 'Collie', 'Dachshund', 'Dalmatian', 'Doberman-Pinscher', 'French-Bulldog', 'German-Shepherd', 'German-Shorthaired-Pointer', 'Golden-Retriever', 'Great-Dane', 'Great-Pyrenees', 'Greyhound', 'Irish-Water-Spaniel', 'Irish-Wolfhound', 'Japanese-Chin', 'Komondor', 'Labradoodle', 'Labrador-Retriever', 'Lhasa-Apso', 'Maltese', 'Miniature-Schnauzer', 'Newfoundland', 'Norwegian-Elkhound', 'Pekingese', 'Pembroke-Welsh-Corgi', 'Pomeranian', 'Poodle', 'Pug', 'Rhodesian-Ridgeback', 'Rottweiler', 'Saint-Bernard', 'Samoyed', 'Scottish-Terrier', 'Shiba-Inu', 'Shih-Tzu', 'Siberian-Husky', 'Staffordshire-Bull-Terrier', 'Vizsla', 'Xoloitzcuintli', 'Yorkshire-Terrier']
|
|
|
38 |
#I have the breed_nicknames dictionary set because some breeds arent recognized that much by the official breed, such as the ones below.
|
39 |
breed_nicknames = {
|
40 |
'Xoloitzcuintli': ' (Mexican Hairless)',
|
@@ -49,20 +47,21 @@ def predict(input_img):
|
|
49 |
topk_values, topk_indices = torch.topk(output_softmax, 3)
|
50 |
topk_indices = topk_indices.tolist()[0]
|
51 |
topk_labels = [labels[index] for index in topk_indices]
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
56 |
gradio_app = gr.Interface(
|
57 |
-
|
58 |
-
inputs=
|
59 |
-
|
|
|
|
|
|
|
60 |
title="What is Your Dog Breed?",
|
61 |
)
|
62 |
|
63 |
if __name__ == "__main__":
|
64 |
gradio_app.launch()
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
|
|
5 |
import os
|
6 |
import gradio as gr
|
7 |
|
|
|
|
|
8 |
# Load the pre-trained model that was used in the training script. In this case it was ResNet18 model
|
9 |
model = models.resnet18()
|
10 |
|
|
|
13 |
model.fc = torch.nn.Linear(num_ftrs, 73)
|
14 |
|
15 |
# Load the saved state dictionary (Device agnostic; inference for image classification is decent even on CPU)
|
16 |
+
model.load_state_dict(torch.load('Dogrun2.pth', map_location=torch.device('cpu'), weights_only=True))
|
17 |
|
18 |
# Set the model to evaluation mode
|
19 |
model.eval()
|
|
|
31 |
v2.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
32 |
])
|
33 |
|
|
|
34 |
labels = ['Afghan-Hound', 'Airedale-Terrier', 'Akita', 'Alaskan-Malamute', 'American-Foxhound', 'American-Hairless-Terrier', 'American-Water-Spaniel', 'Basenji', 'Basset-Hound', 'Beagle', 'Bearded-Collie', 'Belgian-Malinois', 'Belgian-Sheepdog', 'Bernese-Mountain-Dog', 'Bichon-Frise', 'Bloodhound', 'Bluetick-Coonhound', 'Border-Collie', 'Borzoi', 'Boston-Terrier', 'Boxer', 'Bull-Terrier', 'Bulldog', 'Bullmastiff', 'Cairn-Terrier', 'Cane-Corso', 'Cavalier-King-Charles-Spaniel', 'Chihuahua', 'Chinese-Crested', 'Chinese-Shar-Pei', 'Chow-Chow', 'Clumber-Spaniel', 'Cockapoo', 'Cocker-Spaniel', 'Collie', 'Dachshund', 'Dalmatian', 'Doberman-Pinscher', 'French-Bulldog', 'German-Shepherd', 'German-Shorthaired-Pointer', 'Golden-Retriever', 'Great-Dane', 'Great-Pyrenees', 'Greyhound', 'Irish-Water-Spaniel', 'Irish-Wolfhound', 'Japanese-Chin', 'Komondor', 'Labradoodle', 'Labrador-Retriever', 'Lhasa-Apso', 'Maltese', 'Miniature-Schnauzer', 'Newfoundland', 'Norwegian-Elkhound', 'Pekingese', 'Pembroke-Welsh-Corgi', 'Pomeranian', 'Poodle', 'Pug', 'Rhodesian-Ridgeback', 'Rottweiler', 'Saint-Bernard', 'Samoyed', 'Scottish-Terrier', 'Shiba-Inu', 'Shih-Tzu', 'Siberian-Husky', 'Staffordshire-Bull-Terrier', 'Vizsla', 'Xoloitzcuintli', 'Yorkshire-Terrier']
|
35 |
+
|
36 |
#I have the breed_nicknames dictionary set because some breeds arent recognized that much by the official breed, such as the ones below.
|
37 |
breed_nicknames = {
|
38 |
'Xoloitzcuintli': ' (Mexican Hairless)',
|
|
|
47 |
topk_values, topk_indices = torch.topk(output_softmax, 3)
|
48 |
topk_indices = topk_indices.tolist()[0]
|
49 |
topk_labels = [labels[index] for index in topk_indices]
|
50 |
+
return topk_labels
|
51 |
+
|
52 |
+
def get_user_selection(input_img, selected_breed):
|
53 |
+
topk_labels = predict(input_img)
|
54 |
+
return f"You selected {selected_breed}, one of the top predicted breeds: {', '.join(topk_labels)}. I love {selected_breed}!"
|
55 |
+
|
56 |
gradio_app = gr.Interface(
|
57 |
+
fn=get_user_selection,
|
58 |
+
inputs=[
|
59 |
+
gr.Image(label="Please select a clear image of your good dog to upload, or use your camera to take a picture.", sources=['upload', 'webcam'], type="pil"),
|
60 |
+
gr.Dropdown(label="Select the breed you think is correct", choices=labels, type="value")
|
61 |
+
],
|
62 |
+
outputs="text",
|
63 |
title="What is Your Dog Breed?",
|
64 |
)
|
65 |
|
66 |
if __name__ == "__main__":
|
67 |
gradio_app.launch()
|
|
|
|
|
|
|
|