chrismontes commited on
Commit
9ba2474
·
verified ·
1 Parent(s): 4919173

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -53
app.py CHANGED
@@ -1,4 +1,4 @@
1
- """import torch
2
  from PIL import Image
3
  from torchvision.transforms import v2
4
  import torchvision.models as models
@@ -63,57 +63,5 @@ gradio_app = gr.Interface(
63
 
64
  if __name__ == "__main__":
65
  gradio_app.launch()
66
- """
67
-
68
- import torch
69
- from PIL import Image
70
- from torchvision.transforms import v2
71
- import torchvision.models as models
72
- import gradio as gr
73
-
74
- # Load the pre-trained model and set it up (as in your original code)
75
- model = models.resnet18()
76
- num_ftrs = model.fc.in_features
77
- model.fc = torch.nn.Linear(num_ftrs, 73)
78
- model.load_state_dict(torch.load('Dogrun2.pth', map_location=torch.device('cpu')))
79
- model.eval()
80
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
81
- model.to(device)
82
-
83
- # Define image transformations
84
- transforms_test = v2.Compose([
85
- v2.Resize((224, 224), antialias=True),
86
- v2.CenterCrop((224, 224)),
87
- v2.ToImage(),
88
- v2.ToDtype(torch.float32, scale=True),
89
- v2.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
90
- ])
91
 
92
- labels = [...] # Your list of dog breeds
93
 
94
- def predict(input_img):
95
- transformed_img = transforms_test(input_img)
96
- transformed_img = transformed_img.unsqueeze(0).to(device)
97
- logits = model(transformed_img)
98
- output_softmax = torch.nn.functional.softmax(logits, dim=1)
99
- topk_values, topk_indices = torch.topk(output_softmax, 3)
100
-
101
- topk_indices = topk_indices.tolist()[0]
102
- topk_labels = [labels[index] for index in topk_indices]
103
- topk_probs = topk_values[0].tolist()
104
-
105
- result_lines = [f"**{label}**: {prob*100:.2f}%" for label, prob in zip(topk_labels, topk_probs)]
106
- result = "\n".join(result_lines)
107
-
108
- return result
109
-
110
- # Create Gradio interface
111
- gradio_app = gr.Interface(
112
- fn=predict,
113
- inputs=gr.Image(label="Please select a clear image of your good dog to upload.", sources=['upload'], type="pil"),
114
- outputs=gr.Markdown(label="**Predicted Breeds:**"),
115
- title="What is Your Dog Breed?",
116
- )
117
-
118
- if __name__ == "__main__":
119
- gradio_app.launch()
 
1
+ import torch
2
  from PIL import Image
3
  from torchvision.transforms import v2
4
  import torchvision.models as models
 
63
 
64
  if __name__ == "__main__":
65
  gradio_app.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
 
67