DennyW commited on
Commit
869cb7b
·
verified ·
1 Parent(s): 03a8a4f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -83
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
- # Preprocess the image
25
- img = Image.fromarray(image).convert('RGB') # Convert Gradio input to PIL Image
26
- input_tensor = image_transform(img).unsqueeze(0).to(device)
27
-
28
- # Perform inference
29
- with torch.no_grad():
30
- prediction = model(input_tensor.float())
31
- sum_value = abs(torch.sum(prediction[0]))
32
- p_true = abs(prediction[0][0])
33
- p_false = abs(prediction[0][1])
34
-
35
- # Interpret the prediction
36
- if p_true > 0.7:
37
- result = "Accepted"
38
- confidence = float(p_true)
39
- else:
40
- result = "Rejected"
41
- confidence = float(p_false)
42
-
43
- return f"Result: {result}, Confidence: {confidence:.2f}"
44
-
45
- # Prediction function
46
- def predict_frame(frame):
47
- rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
48
- resized_frame = cv2.resize(rgb_frame, (224, 224))
49
- normalized_frame = resized_frame / 255.0
50
- input_frame = np.expand_dims(normalized_frame, axis=0)
51
-
52
- # Convert to PyTorch tensor and move to device
53
- input_frame = torch.from_numpy(input_frame).to(device).float()
54
-
55
- # Permute dimensions to [batch_size, channels, height, width]
56
- input_frame = input_frame.permute(0, 3, 1, 2)
57
-
58
- # Predict using the best model
59
- with torch.no_grad():
60
- prediction = model(input_frame)
61
- sum_value=torch.sum(abs(prediction[0]))
62
- p_true=abs(prediction[0][0])
63
- p_false=abs(prediction[0][1])
64
-
65
- if p_true < 0.4:#if p_true > p_false:
66
- result = "Accepted"
67
- confidence = float(p_true)
68
- else:
69
- result = "Rejected"
70
- confidence = float(p_false)
71
-
72
- return f"Result: {result}, Confidence: {confidence:.2f}"
73
-
74
- # Create the Gradio interface
75
- with gr.Blocks() as demo:
76
- gr.Markdown("# RetinaNet Model Prediction")
77
- with gr.Row():
78
- image_input = gr.Image(label="Upload Image", type="numpy")
79
- output_text = gr.Textbox(label="Prediction Result")
80
- predict_button = gr.Button("Predict")
81
- predict_button.click(predict_image, inputs=image_input, outputs=output_text)
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()