aupfe08 commited on
Commit
0ec08ae
·
1 Parent(s): 7a3c822

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -17
app.py CHANGED
@@ -1,27 +1,26 @@
 
1
  import requests
2
- import tensorflow as tf
 
3
 
4
- import gradio as gr
5
 
6
- inception_net = tf.keras.applications.MobileNetV2() # load the model
7
 
8
  # Download human-readable labels for ImageNet.
9
  response = requests.get("https://git.io/JJkYN")
10
  labels = response.text.split("\n")
11
 
 
 
 
 
 
 
 
 
12
 
13
- def classify_image(inp):
14
- inp = inp.reshape((-1, 224, 224, 3))
15
- inp = tf.keras.applications.mobilenet_v2.preprocess_input(inp)
16
- prediction = inception_net.predict(inp).flatten()
17
- return {labels[i]: float(prediction[i]) for i in range(1000)}
18
-
19
-
20
- image = gr.Image(shape=(224, 224))
21
- label = gr.Label(num_top_classes=3)
22
-
23
- demo = gr.Interface(
24
- fn=classify_image, inputs=image, outputs=label, interpretation="default"
25
- )
26
 
27
- demo.launch()
 
 
 
 
1
+ import torch
2
  import requests
3
+ from PIL import Image
4
+ from torchvision import transforms
5
 
6
+ model = torch.hub.load('pytorch/vision:v0.6.0', 'resnet18', pretrained=True).eval()
7
 
 
8
 
9
  # Download human-readable labels for ImageNet.
10
  response = requests.get("https://git.io/JJkYN")
11
  labels = response.text.split("\n")
12
 
13
+ def predict(inp):
14
+ inp = transforms.ToTensor()(inp).unsqueeze(0)
15
+ with torch.no_grad():
16
+ prediction = torch.nn.functional.softmax(model(inp)[0], dim=0)
17
+ confidences = {labels[i]: float(prediction[i]) for i in range(1000)}
18
+ return confidences
19
+
20
+ import gradio as gr
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
+ gr.Interface(fn=predict,
24
+ inputs=gr.Image(type="pil"),
25
+ outputs=gr.Label(num_top_classes=3),
26
+ examples=["lion.jpg", "cheetah.jpg"]).launch()