lopesdri commited on
Commit
29ed669
1 Parent(s): 283ec0d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -49
app.py CHANGED
@@ -1,59 +1,28 @@
1
- import torch
2
- import torchvision
3
- import torch.nn as nn
4
- import torchvision.transforms as transforms
5
-
6
- model = torchvision.models.resnet50(pretrained=True)
7
- model.fc = nn.Linear(model.fc.in_features, 2)
8
- model.load_state_dict(torch.load("model.pth"))
9
- input_batch = input_batch.to('cpu')
10
- model.to('cpu')
11
- model.eval()
12
-
13
- transform = transforms.Compose([
14
- transforms.Resize((224, 224)),
15
- transforms.ToTensor(),
16
- transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
17
- ])
18
-
19
- categories = ['Fruta pr贸pria para o consumo', 'Fruta impr贸pria para o consumo']
20
-
21
  import gradio as gr
 
 
22
  from PIL import Image
23
 
 
 
24
 
25
- def inference(input_image):
26
- preprocess = transforms.Compose([
27
- transforms.Resize(256),
28
- transforms.CenterCrop(224),
29
- transforms.ToTensor(),
30
- transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
31
- ])
32
- input_tensor = preprocess(input_image)
33
- input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model
34
-
35
-
36
 
 
37
  with torch.no_grad():
38
- output = model(input_batch)
39
- # The output has unnormalized scores. To get probabilities, you can run a softmax on it.
40
- probabilities = torch.nn.functional.softmax(output[0], dim=0)
41
-
42
-
43
- # Show top categories per image
44
- top5_prob, top5_catid = torch.topk(probabilities, 2)
45
- result = {}
46
- for i in range(top5_prob.size(0)):
47
- result[categories[top5_catid[i]]] = top5_prob[i].item()
48
- return result
49
-
50
- inputs = gr.inputs.Image(type='pil')
51
- outputs = gr.outputs.Label(type="confidences",num_top_classes=5)
52
 
53
- title = "ResNet"
54
- 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."
55
 
56
- 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>"
 
 
57
 
 
58
 
59
- gr.Interface(inference, inputs, outputs, title=title, description=description, article=article, analytics_enabled=False).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import torch
3
+ from torchvision.transforms import ToTensor
4
  from PIL import Image
5
 
6
+ # Load your PyTorch model
7
+ model = torch.load("model.pth")
8
 
9
+ # Define the function for image classification
10
+ def classify_image(image):
11
+ image_tensor = ToTensor()(image).unsqueeze(0)
 
 
 
 
 
 
 
 
12
 
13
+ # Perform inference using your PyTorch model
14
  with torch.no_grad():
15
+ model.eval()
16
+ outputs = model(image_tensor)
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
+ predicted_labels = outputs.argmax(dim=1).tolist()
19
+ return predicted_labels
20
 
21
+ # Define the Gradio interface
22
+ inputs = gr.Image()
23
+ outputs = gr.Label(num_top_classes=1)
24
 
25
+ interface = gr.Interface(fn=classify_image, inputs=inputs, outputs=outputs)
26
 
27
+ # Launch the interface
28
+ interface.launch()