DawnC commited on
Commit
3b2d17e
โ€ข
1 Parent(s): 70e3c21

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -21
app.py CHANGED
@@ -13,12 +13,10 @@ 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
- # 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,7 +48,7 @@ def classify_image(image):
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'
@@ -61,34 +59,23 @@ if os.path.exists(examples_path):
61
  else:
62
  print(f"[ERROR] Examples folder not found at {examples_path}")
63
 
64
- # Gradio interface
65
  # Load example images from the folder
66
  examples = [[examples_path + "/" + img] for img in os.listdir(examples_path)]
67
 
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", scale=0.5)
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()
 
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
  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'
 
59
  else:
60
  print(f"[ERROR] Examples folder not found at {examples_path}")
61
 
 
62
  # Load example images from the folder
63
  examples = [[examples_path + "/" + img] for img in os.listdir(examples_path)]
64
 
65
  # Create dropdown menu for users to see available classes (as reference, no direct connection to prediction)
66
  dropdown = gr.Dropdown(choices=class_names, label="Recognizable Breeds", type="value")
67
 
68
+ # Define Gradio Interface
69
  with gr.Blocks() as demo_with_dropdown:
70
+ gr.Markdown("# Oxford Pet ๐Ÿพ Recognizable Breeds")
71
+ dropdown # ้กฏ็คบไธ‹ๆ‹‰้ธๅ–ฎ
72
+ demo = gr.Interface(
 
 
 
 
 
 
 
 
73
  fn=classify_image,
74
+ inputs=gr.Image(type="pil"), # ๅชไฝฟ็”จๅœ–็‰‡่ผธๅ…ฅ้€ฒ่กŒ้ ๆธฌ
75
+ outputs=gr.Label(num_top_classes=3, label="Top 3 Predictions"), # ่ผธๅ‡บๅ‰ไธ‰ๅ€‹้ ๆธฌ
76
  examples=examples,
77
  title='Oxford Pet ๐Ÿˆ๐Ÿ•',
78
  description='A ResNet50-based model for classifying 37 different pet breeds.',
79
  article='[Oxford Project](https://github.com/Eric-Chung-0511/Learning-Record/tree/main/Data%20Science%20Projects/The%20Oxford-IIIT%20Pet%20Project)'
80
  )
81
+ demo.launch()