akhaliq HF Staff commited on
Commit
080c080
·
1 Parent(s): 1852109

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ from PIL import Image
4
+ from torchvision import transforms
5
+
6
+ torch.hub.download_url_to_file("https://github.com/pytorch/hub/raw/master/images/dog.jpg", "dog.jpg")
7
+
8
+ model = torch.hub.load('pytorch/vision:v0.9.0', 'densenet121', pretrained=True)
9
+ # or any of these variants
10
+ # model = torch.hub.load('pytorch/vision:v0.9.0', 'densenet169', pretrained=True)
11
+ # model = torch.hub.load('pytorch/vision:v0.9.0', 'densenet201', pretrained=True)
12
+ # model = torch.hub.load('pytorch/vision:v0.9.0', 'densenet161', pretrained=True)
13
+ model.eval()
14
+
15
+ def inference(input_image):
16
+
17
+ preprocess = transforms.Compose([
18
+ transforms.Resize(256),
19
+ transforms.CenterCrop(224),
20
+ transforms.ToTensor(),
21
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
22
+ ])
23
+ input_tensor = preprocess(input_image)
24
+ input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model
25
+
26
+ # move the input and model to GPU for speed if available
27
+ if torch.cuda.is_available():
28
+ input_batch = input_batch.to('cuda')
29
+ model.to('cuda')
30
+
31
+ with torch.no_grad():
32
+ output = model(input_batch)
33
+ # Tensor of shape 1000, with confidence scores over Imagenet's 1000 classes
34
+ # The output has unnormalized scores. To get probabilities, you can run a softmax on it.
35
+ probabilities = torch.nn.functional.softmax(output[0], dim=0)
36
+ # Download ImageNet labels
37
+ !wget https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt
38
+ # Read the categories
39
+ with open("imagenet_classes.txt", "r") as f:
40
+ categories = [s.strip() for s in f.readlines()]
41
+ # Show top categories per image
42
+ top5_prob, top5_catid = torch.topk(probabilities, 5)
43
+ result = {}
44
+ for i in range(top5_prob.size(0)):
45
+ result[categories[top5_catid[i]]] = top5_prob[i].item()
46
+ return result
47
+
48
+ inputs = gr.inputs.Image(type='pil')
49
+ outputs = gr.outputs.Label(type="confidences",num_top_classes=5)
50
+
51
+ title = "DENSENET"
52
+ description = "Gradio demo for Dense Convolutional Network (DenseNet), connects each layer to every other layer in a feed-forward fashion. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below."
53
+ article = "<p style='text-align: center'><a href='https://arxiv.org/abs/1608.06993'>Densely Connected Convolutional Networks</a> | <a href='https://github.com/pytorch/vision/blob/master/torchvision/models/densenet.py'>Github Repo</a></p>"
54
+
55
+ examples = [
56
+ ['dog.jpg']
57
+ ]
58
+ gr.Interface(inference, inputs, outputs, title=title, description=description, article=article, examples=examples, analytics_enabled=False).launch(debug=True)