Spaces:
Build error
Build error
Update app.py
Browse files
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 |
-
|
| 26 |
-
|
| 27 |
-
|
| 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 |
-
|
| 39 |
-
|
| 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 |
-
|
| 54 |
-
|
| 55 |
|
| 56 |
-
|
|
|
|
|
|
|
| 57 |
|
|
|
|
| 58 |
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|