PhuongPhan commited on
Commit
61627da
·
verified ·
1 Parent(s): 663f1fc

Upload untitled1(1).py

Browse files
Files changed (1) hide show
  1. untitled1(1).py +59 -0
untitled1(1).py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Untitled1.ipynb
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1kUKZzUV2R2HnFLLFwS8DE8pcQk6sbvY0
8
+ """
9
+
10
+
11
+
12
+ # pretrained Resnet-18 mode
13
+ import torch
14
+ model = torch.hub.load('pytorch/vision:v0.6.0', 'resnet18', pretrained=True).eval()
15
+
16
+ # define a function that takes in the user input, which in this case is an image, and returns the prediction.
17
+ '''The prediction should be returned as a dictionary whose keys are class name and values are confidence probabilities.
18
+ We will load the class names from this text file.
19
+ '''
20
+ !pip install transformers
21
+ !pip install gradio
22
+
23
+ import gradio as gr
24
+ from transformers import BlipProcessor, BlipForConditionalGeneration
25
+ from PIL import Image
26
+
27
+ import requests
28
+ from PIL import Image
29
+ from torchvision import transforms
30
+
31
+ # Download human-readable labels for ImageNet.
32
+ response = requests.get("https://git.io/JJkYN")
33
+ labels = response.text.split("\n")
34
+
35
+ def predict(inp):
36
+ inp = transforms.ToTensor()(inp).unsqueeze(0)
37
+ with torch.no_grad():
38
+ prediction = torch.nn.functional.softmax(model(inp)[0], dim=0)
39
+ confidences = {labels[i]: float(prediction[i]) for i in range(1000)}
40
+ return confidences
41
+
42
+ '''The function converts the input image into a PIL Image and subsequently into a PyTorch tensor.
43
+ After processing the tensor through the model, it returns the predictions in the form of a dictionary named confidences.
44
+ The dictionary's keys are the class labels, and its values are the corresponding confidence probabilities.
45
+
46
+ In this section, we define a predict function that processes an input image to return prediction probabilities.
47
+ The function first converts the image into a PyTorch tensor and then forwards it through the pretrained model.
48
+
49
+ We use the softmax function in the final step to calculate the probabilities of each class.
50
+ The softmax function is crucial because it converts the raw output logits from the model, which can be any real number, into probabilities that sum up to 1.
51
+ This makes it easier to interpret the model’s outputs as confidence levels for each class.'''
52
+
53
+ # Creating a Gradio interface
54
+ import gradio as gr
55
+
56
+ gr.Interface(fn=predict,
57
+ inputs=gr.Image(type="pil"), # creates the component and handles the preprocessing to convert that to a PIL image
58
+ outputs=gr.Label(num_top_classes=3), # a Label, which displays the top labels in a nice form. Since we don't want to show all 1,000 class labels, we will customize it to show only the top 3 images by constructing it as
59
+ examples=["/lion.jpg", "/cheetah.jpg"]).launch()