Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,84 +1,83 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from PIL import Image
|
| 3 |
-
import torch
|
| 4 |
-
import torchvision.transforms as transforms
|
| 5 |
-
from model import RetinaNet # Import your RetinaNet model definition
|
| 6 |
-
import cv2
|
| 7 |
-
import numpy as np
|
| 8 |
-
|
| 9 |
-
# Define the image transformation pipeline
|
| 10 |
-
image_transform = transforms.Compose([
|
| 11 |
-
transforms.Resize((224, 224)),
|
| 12 |
-
transforms.ToTensor(),
|
| 13 |
-
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 14 |
-
])
|
| 15 |
-
|
| 16 |
-
# Load the model
|
| 17 |
-
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 18 |
-
model = RetinaNet(num_classes=2).to(device)
|
| 19 |
-
model.load_state_dict(torch.load("retinanet_best_model.pth", map_location=device))
|
| 20 |
-
model.eval()
|
| 21 |
-
|
| 22 |
-
# Prediction function
|
| 23 |
-
def predict_image(image):
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
gr.
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
predict_button =
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
# Launch the app
|
| 84 |
demo.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import torch
|
| 4 |
+
import torchvision.transforms as transforms
|
| 5 |
+
from model import RetinaNet # Import your RetinaNet model definition
|
| 6 |
+
import cv2
|
| 7 |
+
import numpy as np
|
| 8 |
+
|
| 9 |
+
# Define the image transformation pipeline
|
| 10 |
+
image_transform = transforms.Compose([
|
| 11 |
+
transforms.Resize((224, 224)),
|
| 12 |
+
transforms.ToTensor(),
|
| 13 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 14 |
+
])
|
| 15 |
+
|
| 16 |
+
# Load the model
|
| 17 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 18 |
+
model = RetinaNet(num_classes=2).to(device)
|
| 19 |
+
model.load_state_dict(torch.load("retinanet_best_model.pth", map_location=device))
|
| 20 |
+
model.eval()
|
| 21 |
+
|
| 22 |
+
# Prediction function
|
| 23 |
+
def predict_image(image, isFrame):
|
| 24 |
+
|
| 25 |
+
if isFrame == False:
|
| 26 |
+
# Preprocess the image
|
| 27 |
+
img = Image.fromarray(image).convert('RGB') # Convert Gradio input to PIL Image
|
| 28 |
+
input_tensor = image_transform(img).unsqueeze(0).to(device)
|
| 29 |
+
|
| 30 |
+
# Perform inference
|
| 31 |
+
with torch.no_grad():
|
| 32 |
+
prediction = model(input_tensor.float())
|
| 33 |
+
sum_value = abs(torch.sum(prediction[0]))
|
| 34 |
+
p_true = abs(prediction[0][0])
|
| 35 |
+
p_false = abs(prediction[0][1])
|
| 36 |
+
|
| 37 |
+
# Interpret the prediction
|
| 38 |
+
if p_true > 0.7:
|
| 39 |
+
result = "Accepted"
|
| 40 |
+
confidence = float(p_true)
|
| 41 |
+
else:
|
| 42 |
+
result = "Rejected"
|
| 43 |
+
confidence = float(p_false)
|
| 44 |
+
else:
|
| 45 |
+
frame = image
|
| 46 |
+
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 47 |
+
resized_frame = cv2.resize(rgb_frame, (224, 224))
|
| 48 |
+
normalized_frame = resized_frame / 255.0
|
| 49 |
+
input_frame = np.expand_dims(normalized_frame, axis=0)
|
| 50 |
+
|
| 51 |
+
# Convert to PyTorch tensor and move to device
|
| 52 |
+
input_frame = torch.from_numpy(input_frame).to(device).float()
|
| 53 |
+
|
| 54 |
+
# Permute dimensions to [batch_size, channels, height, width]
|
| 55 |
+
input_frame = input_frame.permute(0, 3, 1, 2)
|
| 56 |
+
|
| 57 |
+
# Predict using the best model
|
| 58 |
+
with torch.no_grad():
|
| 59 |
+
prediction = model(input_frame)
|
| 60 |
+
sum_value=torch.sum(abs(prediction[0]))
|
| 61 |
+
p_true=abs(prediction[0][0])
|
| 62 |
+
p_false=abs(prediction[0][1])
|
| 63 |
+
|
| 64 |
+
if p_true < 0.4:#if p_true > p_false:
|
| 65 |
+
result = "Accepted"
|
| 66 |
+
confidence = float(p_true)
|
| 67 |
+
else:
|
| 68 |
+
result = "Rejected"
|
| 69 |
+
confidence = float(p_false)
|
| 70 |
+
|
| 71 |
+
return f"Result: {result}, Confidence: {confidence:.2f}"
|
| 72 |
+
|
| 73 |
+
# Create the Gradio interface
|
| 74 |
+
with gr.Blocks() as demo:
|
| 75 |
+
gr.Markdown("# RetinaNet Model Prediction")
|
| 76 |
+
with gr.Row():
|
| 77 |
+
image_input = gr.Image(label="Upload Image", type="numpy")
|
| 78 |
+
output_text = gr.Textbox(label="Prediction Result")
|
| 79 |
+
predict_button = gr.Button("Predict")
|
| 80 |
+
predict_button.click(predict_image, inputs=image_input, outputs=output_text)
|
| 81 |
+
|
| 82 |
+
# Launch the app
|
|
|
|
| 83 |
demo.launch()
|