yuhe6 commited on
Commit
8788308
·
1 Parent(s): b8aa0fe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -83
app.py CHANGED
@@ -1,151 +1,86 @@
1
  import os
2
-
3
  import torch
4
-
5
  from PIL import Image
6
-
7
  from torchvision import transforms
8
-
9
  import gradio as gr
10
-
11
  #https://huggingface.co/spaces/yuhe6/final_project/blob/main/Net_Rotate9.pth
12
-
13
  #os.system("wget https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt")
14
 
15
-
16
-
17
  #model = torch.hub.load('huawei-noah/ghostnet', 'ghostnet_1x', pretrained=True)
18
-
19
  #model = torch.jit.load('https://huggingface.co/spaces/yuhe6/final_project/blob/main/Net_Rotate9.pth').eval().to(device)
20
 
21
-
22
-
23
- model = torch.jit.load('Net2_Flip_jit.pt', map_location = torch.device('cpu'))
24
 
 
25
  model.eval()
26
 
27
-
 
28
 
29
  #torch.hub.download_url_to_file('https://huggingface.co/spaces/yuhe6/final_project/blob/main/Net_Rotate9.pth', '/tmp/temporary_file')
30
-
31
  #model = torch.hub.load('/tmp', 'temporary_file', pretrained=True)
32
 
33
-
34
-
35
  #model.eval()
36
-
37
  # Download an example image from the pytorch website
38
-
39
- torch.hub.download_url_to_file("https://github.com/pytorch/hub/raw/master/images/dog.jpg", "dog.jpg")
40
-
41
-
42
 
43
  def inference(input_image):
44
-
45
  preprocess = transforms.Compose([
46
-
47
  transforms.Resize(size = (256, 256)), # Fixed resize from transforms.Resize(256)
48
-
49
  #transforms.CenterCrop(224),
50
-
51
  transforms.ToTensor(),
52
-
53
  #transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
54
-
55
  ])
56
 
57
-
58
-
59
  # Used print statements to detect shapes between input tensor & batch
60
-
61
  # e.g. input_tensor.shape
62
-
63
  input_tensor = preprocess(input_image)
64
-
65
  input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model
66
 
67
-
68
-
69
  # move the input and model to GPU for speed if available
70
-
71
  if torch.cuda.is_available():
72
-
73
  input_batch = input_batch.to('cuda')
74
-
75
  model.to('cuda')
76
 
77
-
78
-
79
  with torch.no_grad():
80
-
81
  output = model(input_batch) # model(input_tensor) # needed to have batch dimension
82
 
83
-
84
-
85
  # The output has unnormalized scores. To get probabilities, you can run a softmax on it.
86
-
87
  probabilities = torch.nn.functional.softmax(output[0])
88
 
89
-
90
-
91
- # Read the categories
92
-
93
  #with open("dog_cat.txt", "r") as f:
94
-
95
  #categories = [s.strip() for s in f.readlines()]
96
-
97
  #with open("dog_cat.txt", "r") as f:
98
 
99
- categories = ["cat","dog"]
100
-
101
-
102
-
103
  #categories = [s.strip() for s in f.readlines()]
104
-
105
  # Show top categories per image
106
-
107
- top1_prob, top1_catid = torch.topk(probabilities, 2)
108
-
109
  result = {}
110
-
111
  for i in range(top1_prob.size(0)):
112
-
113
- result[categories[top1_catid[i]]] = top1_prob[i].item()
114
-
115
  return result
116
 
117
-
118
-
119
  inputs = gr.inputs.Image(type='pil')
120
-
121
- outputs = gr.outputs.Label(type="confidences",num_top_classes=2)
122
-
123
-
124
 
125
  title = "GHOSTNET"
126
-
127
  description = "Gradio demo for GHOSTNET, Efficient networks by generating more features from cheap operations. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below."
128
-
129
  article = "<p style='text-align: center'><a href='https://arxiv.org/abs/1911.11907'>GhostNet: More Features from Cheap Operations</a> | <a href='https://github.com/huawei-noah/CV-Backbones'>Github Repo</a></p>"
130
 
131
-
132
-
133
  examples = [
134
-
135
- ['dog.jpg']
136
-
 
137
  ]
138
 
139
  gr.Interface(
140
-
141
- inference, inputs, outputs,
142
-
143
- title = title, description = description,
144
-
145
- article = article, examples = examples,
146
-
147
  analytics_enabled = False).launch(
148
-
149
  #debug = True # Enabled debug mode to see the stacktrace on Google Colab.
150
-
151
  )
 
1
  import os
 
2
  import torch
 
3
  from PIL import Image
 
4
  from torchvision import transforms
 
5
  import gradio as gr
 
6
  #https://huggingface.co/spaces/yuhe6/final_project/blob/main/Net_Rotate9.pth
 
7
  #os.system("wget https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt")
8
 
 
 
9
  #model = torch.hub.load('huawei-noah/ghostnet', 'ghostnet_1x', pretrained=True)
 
10
  #model = torch.jit.load('https://huggingface.co/spaces/yuhe6/final_project/blob/main/Net_Rotate9.pth').eval().to(device)
11
 
 
 
 
12
 
13
+ model = torch.jit.load('Net2_Blur_jit.pt', map_location = torch.device('cpu'))
14
  model.eval()
15
 
16
+ model_categories = ["cat","dog"] # verify order
17
+ n_categories = len(model_categories)
18
 
19
  #torch.hub.download_url_to_file('https://huggingface.co/spaces/yuhe6/final_project/blob/main/Net_Rotate9.pth', '/tmp/temporary_file')
 
20
  #model = torch.hub.load('/tmp', 'temporary_file', pretrained=True)
21
 
 
 
22
  #model.eval()
 
23
  # Download an example image from the pytorch website
24
+ torch.hub.download_url_to_file("https://github.com/pytorch/hub/raw/master/images/dog.jpg", "dog1.jpg")
25
+ torch.hub.download_url_to_file("https://upload.wikimedia.org/wikipedia/commons/thumb/6/6e/Golde33443.jpg/640px-Golde33443.jpg", "dog2.jpg")
26
+ torch.hub.download_url_to_file("https://upload.wikimedia.org/wikipedia/commons/4/4d/Cat_November_2010-1a.jpg", "cat1.jpg")
27
+ torch.hub.download_url_to_file("https://upload.wikimedia.org/wikipedia/en/thumb/7/70/Norwegian_Forest_Cat_in_snow_%28closeup%29_%28cropped%29.jpg/800px-Norwegian_Forest_Cat_in_snow_%28closeup%29_%28cropped%29.jpg", "cat2.jpg")
28
 
29
  def inference(input_image):
 
30
  preprocess = transforms.Compose([
 
31
  transforms.Resize(size = (256, 256)), # Fixed resize from transforms.Resize(256)
 
32
  #transforms.CenterCrop(224),
 
33
  transforms.ToTensor(),
 
34
  #transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
 
35
  ])
36
 
 
 
37
  # Used print statements to detect shapes between input tensor & batch
 
38
  # e.g. input_tensor.shape
 
39
  input_tensor = preprocess(input_image)
 
40
  input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model
41
 
 
 
42
  # move the input and model to GPU for speed if available
 
43
  if torch.cuda.is_available():
 
44
  input_batch = input_batch.to('cuda')
 
45
  model.to('cuda')
46
 
 
 
47
  with torch.no_grad():
 
48
  output = model(input_batch) # model(input_tensor) # needed to have batch dimension
49
 
 
 
50
  # The output has unnormalized scores. To get probabilities, you can run a softmax on it.
 
51
  probabilities = torch.nn.functional.softmax(output[0])
52
 
53
+ # Read the categories
 
 
 
54
  #with open("dog_cat.txt", "r") as f:
 
55
  #categories = [s.strip() for s in f.readlines()]
 
56
  #with open("dog_cat.txt", "r") as f:
57
 
 
 
 
 
58
  #categories = [s.strip() for s in f.readlines()]
 
59
  # Show top categories per image
60
+ top1_prob, top1_catid = torch.topk(probabilities, n_categories)
 
 
61
  result = {}
 
62
  for i in range(top1_prob.size(0)):
63
+ result[model_categories[top1_catid[i]]] = top1_prob[i].item()
 
 
64
  return result
65
 
 
 
66
  inputs = gr.inputs.Image(type='pil')
67
+ outputs = gr.outputs.Label(type="confidences", num_top_classes = n_categories)
 
 
 
68
 
69
  title = "GHOSTNET"
 
70
  description = "Gradio demo for GHOSTNET, Efficient networks by generating more features from cheap operations. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below."
 
71
  article = "<p style='text-align: center'><a href='https://arxiv.org/abs/1911.11907'>GhostNet: More Features from Cheap Operations</a> | <a href='https://github.com/huawei-noah/CV-Backbones'>Github Repo</a></p>"
72
 
 
 
73
  examples = [
74
+ ['dog1.jpg'],
75
+ ['cat1.jpg'],
76
+ ['dog2.jpg'],
77
+ ['cat2.jpg']
78
  ]
79
 
80
  gr.Interface(
81
+ inference, inputs, outputs,
82
+ title = title, description = description,
83
+ article = article, examples = examples,
 
 
 
 
84
  analytics_enabled = False).launch(
 
85
  #debug = True # Enabled debug mode to see the stacktrace on Google Colab.
 
86
  )