DawnC commited on
Commit
f4dbfb8
β€’
1 Parent(s): 1038938

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -17
app.py CHANGED
@@ -13,10 +13,12 @@ model = models.resnet50(weights=None)
13
  # Revise fully connected layer to output 37 classes (num_classes = 37)
14
  model.fc = torch.nn.Linear(2048, 37)
15
 
 
16
  model.load_state_dict(torch.load('./resnet50_model_weights.pth', map_location=device))
17
 
18
  model.eval()
19
 
 
20
  transform = transforms.Compose([
21
  transforms.Resize((224, 224)),
22
  transforms.ToTensor(),
@@ -48,7 +50,7 @@ def classify_image(image):
48
  probabilities, indices = torch.topk(probabilities, k=3)
49
  # Return the class names with their corresponding probabilities
50
  predictions = [(class_names[idx], prob.item()) for idx, prob in zip(indices[0], probabilities[0])]
51
- return {class_name: prob for class_name, prob in predictions} # Return raw float numbers
52
 
53
  # Path to the folder containing example images
54
  examples_path = './examples'
@@ -66,22 +68,27 @@ examples = [[examples_path + "/" + img] for img in os.listdir(examples_path)]
66
  # Create dropdown menu for users to see available classes (as reference, no direct connection to prediction)
67
  dropdown = gr.Dropdown(choices=class_names, label="Recognizable Breeds", type="value")
68
 
69
- # Define Gradio Interface
70
- demo = gr.Interface(
71
- fn=classify_image,
72
- inputs=[gr.Image(type="pil")], # Only image input is used for prediction
73
- outputs=[gr.Label(num_top_classes=3, label="Top 3 Predictions")], # Outputs top 3 predictions with probabilities
74
- examples=examples,
75
- title='Oxford Pet πŸˆπŸ•',
76
- description='A ResNet50-based model for classifying 37 different pet breeds.',
77
- article='[Oxford Project](https://github.com/Eric-Chung-0511/Learning-Record/tree/main/Data%20Science%20Projects/The%20Oxford-IIIT%20Pet%20Project)'
78
- )
79
-
80
- # Add dropdown to reference the recognizable breeds
81
  with gr.Blocks() as demo_with_dropdown:
82
- gr.Markdown("# Oxford Pet 🐾 Recognizable Breeds")
83
- dropdown # Display the dropdown for reference
84
- demo # Add the existing demo
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
- # Launch Gradio demo with dropdown
87
  demo_with_dropdown.launch()
 
13
  # Revise fully connected layer to output 37 classes (num_classes = 37)
14
  model.fc = torch.nn.Linear(2048, 37)
15
 
16
+ # Load Model weights
17
  model.load_state_dict(torch.load('./resnet50_model_weights.pth', map_location=device))
18
 
19
  model.eval()
20
 
21
+ # Transformation for the input image
22
  transform = transforms.Compose([
23
  transforms.Resize((224, 224)),
24
  transforms.ToTensor(),
 
50
  probabilities, indices = torch.topk(probabilities, k=3)
51
  # Return the class names with their corresponding probabilities
52
  predictions = [(class_names[idx], prob.item()) for idx, prob in zip(indices[0], probabilities[0])]
53
+ return {class_name: prob for class_name, prob in predictions} # Return raw float numbers # Return formatted percentages
54
 
55
  # Path to the folder containing example images
56
  examples_path = './examples'
 
68
  # Create dropdown menu for users to see available classes (as reference, no direct connection to prediction)
69
  dropdown = gr.Dropdown(choices=class_names, label="Recognizable Breeds", type="value")
70
 
71
+ # Use `gr.Blocks()` to define the full interface
 
 
 
 
 
 
 
 
 
 
 
72
  with gr.Blocks() as demo_with_dropdown:
73
+ # Display markdown heading
74
+ gr.Markdown("# Oxford Pet πŸ•πŸˆ Recognizable Breeds")
75
+
76
+ # Dropdown as a reference for users
77
+ dropdown
78
+
79
+ # Image classification demo
80
+ gr.Image(label="Upload an image to classify").style(height="auto", width="100%")
81
+
82
+ # Gradio interface for the image input and label output
83
+ gr.Interface(
84
+ fn=classify_image,
85
+ inputs=gr.Image(type="pil"), # Only image input is used for prediction
86
+ outputs=gr.Label(num_top_classes=3, label="Top 3 Predictions"), # Outputs top 3 predictions with probabilities
87
+ examples=examples,
88
+ title='Oxford Pet πŸˆπŸ•',
89
+ description='A ResNet50-based model for classifying 37 different pet breeds.',
90
+ article='[Oxford Project](https://github.com/Eric-Chung-0511/Learning-Record/tree/main/Data%20Science%20Projects/The%20Oxford-IIIT%20Pet%20Project)'
91
+ )
92
 
93
+ # Launch Gradio app
94
  demo_with_dropdown.launch()