debugged file
Browse files
app.py
CHANGED
@@ -1,49 +1,52 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
import torch
|
|
|
3 |
from torchvision import transforms as T
|
4 |
import torchvision.models as models
|
5 |
-
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
|
9 |
def classify(image):
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
open("labels.txt", "wb").write(response.content)
|
33 |
-
#extract the labels from the file
|
34 |
-
with open('labels.txt', "r") as f:
|
35 |
-
labels = [line.strip() for line in f.readlines()]
|
36 |
-
|
37 |
-
## predict the class
|
38 |
-
# Find the index (tensor) corresponding to the maximum score in the out tensor.
|
39 |
-
# Torch.max function can be used to find the information
|
40 |
-
_, index = torch.max(output, 1)
|
41 |
-
|
42 |
-
# Find the score in terms of percentage by using torch.nn.functional.softmax function
|
43 |
-
# which normalizes the output to range [0,1] and multiplying by 100
|
44 |
-
percentage = torch.nn.functional.softmax(output, dim=1)[0] * 100
|
45 |
-
|
46 |
-
return "The image depicts: " + labels[index[0]] + "with a score of " + percentage[index[0]].item() + "%"
|
47 |
|
48 |
iface = gr.Interface(fn=classify, inputs="image", outputs="text")
|
49 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from huggingface_hub import hf_hub_url
|
3 |
+
from PIL import Image
|
4 |
+
import requests
|
5 |
import torch
|
6 |
+
from torchvision.io import read_image
|
7 |
from torchvision import transforms as T
|
8 |
import torchvision.models as models
|
9 |
+
|
10 |
+
|
11 |
+
# we import the model
|
12 |
+
resnet34 = models.resnet34(pretrained=True)
|
13 |
+
# evaluation mode
|
14 |
+
resnet34.eval()
|
15 |
+
|
16 |
+
## labeling
|
17 |
+
# Load the file containing the 1,000 labels for the ImageNet dataset classes
|
18 |
+
url = hf_hub_url(repo_id="Selma/pytorch-resnet34", filename="imagenet_classes.txt")
|
19 |
+
response = requests.get(url)
|
20 |
+
# write to a label file
|
21 |
+
open("labels.txt", "wb").write(response.content)
|
22 |
+
# extract the labels from the file
|
23 |
+
with open('labels.txt', "r") as f:
|
24 |
+
labels = [line.strip() for line in f.readlines()]
|
25 |
|
26 |
|
27 |
def classify(image):
|
28 |
+
## preprocessing
|
29 |
+
# we need a transform step to normalise the pictures
|
30 |
+
transform = T.Compose([T.Resize(256), T.CenterCrop(224), T.ToTensor(),
|
31 |
+
T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])])
|
32 |
+
# normalise the image
|
33 |
+
image_transformed = transform(image)
|
34 |
+
# reshape
|
35 |
+
batch_image_transformed = torch.unsqueeze(image_transformed, 0)
|
36 |
+
|
37 |
+
# get the predictions
|
38 |
+
output = resnet34(batch_image_transformed)
|
39 |
+
|
40 |
+
## predict the class
|
41 |
+
# Find the index (tensor) corresponding to the maximum score in the out tensor.
|
42 |
+
# Torch.max function can be used to find the information
|
43 |
+
_, index = torch.max(output, 1)
|
44 |
+
|
45 |
+
# Find the score in terms of percentage by using torch.nn.functional.softmax function
|
46 |
+
# which normalizes the output to range [0,1] and multiplying by 100
|
47 |
+
percentage = torch.nn.functional.softmax(output, dim=1)[0] * 100
|
48 |
+
|
49 |
+
return "The image depicts: " + labels[index[0]] + " with a score of " + str(round(percentage[index[0]].item())) + "%"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
iface = gr.Interface(fn=classify, inputs="image", outputs="text")
|
52 |
iface.launch()
|