Selma commited on
Commit
15d6ba8
1 Parent(s): f534f3b

debugged file

Browse files
Files changed (1) hide show
  1. app.py +42 -39
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
- import requests
6
- from huggingface_hub import hf_hub_url
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
 
9
  def classify(image):
10
-
11
- ## preprocessing
12
- # we need a transform step to normalise the pictures
13
- transform = T.Compose([T.Resize(256), T.CenterCrop(224), T.ToTensor(), T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])])
14
-
15
- # we import the model
16
- resnet34 = models.resnet34(pretrained=True)
17
-
18
- # normalise the image
19
- image_transformed = transform(image)
20
- # reshape
21
- batch_image_transformed = torch.unsqueeze(image_transformed, 0)
22
- # evaluation mode
23
- resnet34.eval()
24
- # get the predictions
25
- output = resnet34(batch_image_transformed)
26
-
27
- ## labeling
28
- # Load the file containing the 1,000 labels for the ImageNet dataset classes
29
- url = hf_hub_url(repo_id="Selma/pytorch-resnet34", filename="imagenet_classes.txt")
30
- response = requests.get(url)
31
- # write to a label file
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()