lopesdri commited on
Commit
37ee4dc
1 Parent(s): 35c8763

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -15
app.py CHANGED
@@ -15,26 +15,50 @@ transform = transforms.Compose([
15
  transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
16
  ])
17
 
18
- classes = ['Fruta pr贸pria para o consumo', 'Fruta impr贸pria para o consumo']
19
 
20
  import gradio as gr
21
  from PIL import Image
22
 
23
- # Define the function to make predictions
24
- def predict(image):
25
- image = transform(image).unsqueeze(0).to(device)
26
- model.eval()
 
 
 
 
 
 
 
 
 
 
 
 
27
  with torch.no_grad():
28
- output = model(image)
29
- _, predicted = torch.max(output.data, 1)
30
- return classes[predicted.item()]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
- # Define the input and output components
33
- image_input = gr.inputs.Image(type="pil", label="Upload Image")
34
- label_output = gr.outputs.Label()
35
 
36
- # Create the interface
37
- interface = gr.Interface(fn=predict, inputs=image_input, outputs=label_output)
 
38
 
39
- # Launch the interface
40
- interface.launch()
 
15
  transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
16
  ])
17
 
18
+ categories = ['Fruta pr贸pria para o consumo', 'Fruta impr贸pria para o consumo']
19
 
20
  import gradio as gr
21
  from PIL import Image
22
 
23
+
24
+ def inference(input_image):
25
+ preprocess = transforms.Compose([
26
+ transforms.Resize(256),
27
+ transforms.CenterCrop(224),
28
+ transforms.ToTensor(),
29
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
30
+ ])
31
+ input_tensor = preprocess(input_image)
32
+ input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model
33
+
34
+ # move the input and model to GPU for speed if available
35
+ if torch.cuda.is_available():
36
+ input_batch = input_batch.to('cuda')
37
+ model.to('cuda')
38
+
39
  with torch.no_grad():
40
+ output = model(input_batch)
41
+ # The output has unnormalized scores. To get probabilities, you can run a softmax on it.
42
+ probabilities = torch.nn.functional.softmax(output[0], dim=0)
43
+
44
+
45
+ # Show top categories per image
46
+ top5_prob, top5_catid = torch.topk(probabilities, 2)
47
+ result = {}
48
+ for i in range(top5_prob.size(0)):
49
+ result[categories[top5_catid[i]]] = top5_prob[i].item()
50
+ return result
51
+
52
+ inputs = gr.inputs.Image(type='pil')
53
+ outputs = gr.outputs.Label(type="confidences",num_top_classes=5)
54
+
55
+ title = "ResNet"
56
+ description = "Gradio demo for ResNet, Deep residual networks pre-trained on ImageNet. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below."
57
 
58
+ article = "<p style='text-align: center'><a href='https://arxiv.org/abs/1512.03385' target='_blank'>Deep Residual Learning for Image Recognition</a> | <a href='https://github.com/pytorch/vision/blob/main/torchvision/models/resnet.py' target='_blank'>Github Repo</a></p>"
 
 
59
 
60
+ examples = [
61
+ ['dog.jpg']
62
+ ]
63
 
64
+ gr.Interface(inference, inputs, outputs, title=title, description=description, article=article, examples=examples, analytics_enabled=False).launch()