sharjjel67 commited on
Commit
28f1939
·
1 Parent(s): 9e0e286

Create app.py

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