Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,10 +12,6 @@ import yolo9
|
|
| 12 |
|
| 13 |
model = yolo9.load('best.pt')
|
| 14 |
|
| 15 |
-
# Set model parameters
|
| 16 |
-
model.conf = conf_threshold
|
| 17 |
-
model.iou = iou_threshold
|
| 18 |
-
|
| 19 |
classes = ('ball', 'goalkeeper', 'player', 'referee')
|
| 20 |
|
| 21 |
model_layer_names = ["1", "2", "3"]
|
|
@@ -44,7 +40,10 @@ def resize_image_pil(image, new_width, new_height):
|
|
| 44 |
return resized
|
| 45 |
|
| 46 |
|
| 47 |
-
def inference(input_img, show_gradcam, layer_name, num_classes, transparancy = 0.5):
|
|
|
|
|
|
|
|
|
|
| 48 |
print(show_gradcam, layer_name, num_classes, transparancy)
|
| 49 |
input_img = resize_image_pil(input_img,32,32)
|
| 50 |
input_img = np.array(input_img)
|
|
@@ -55,6 +54,7 @@ def inference(input_img, show_gradcam, layer_name, num_classes, transparancy = 0
|
|
| 55 |
input_img = transform(input_img)
|
| 56 |
input_img = input_img.unsqueeze(0)
|
| 57 |
outputs = model(input_img)
|
|
|
|
| 58 |
# print(outputs)
|
| 59 |
softmax = torch.nn.Softmax(dim=0)
|
| 60 |
o = softmax(outputs.flatten())
|
|
@@ -62,12 +62,12 @@ def inference(input_img, show_gradcam, layer_name, num_classes, transparancy = 0
|
|
| 62 |
output_numpy = np.squeeze(np.asarray(outputs.detach().numpy()))
|
| 63 |
index_sort = np.argsort(output_numpy)[::-1]
|
| 64 |
|
| 65 |
-
confidences = {}
|
| 66 |
-
for i in range(int(num_classes)):
|
| 67 |
-
confidences[classes[index_sort[i]]] = float(o[index_sort[i]])
|
| 68 |
|
| 69 |
|
| 70 |
-
prediction= torch.max(outputs, 1)
|
| 71 |
|
| 72 |
if show_gradcam:
|
| 73 |
target_layers = get_layer(layer_name)
|
|
@@ -81,27 +81,28 @@ def inference(input_img, show_gradcam, layer_name, num_classes, transparancy = 0
|
|
| 81 |
visualization = org_img
|
| 82 |
|
| 83 |
|
| 84 |
-
return
|
| 85 |
|
| 86 |
demo = gr.Interface(
|
| 87 |
inference,
|
| 88 |
inputs = [
|
| 89 |
gr.Image(width=256,height=256,label="Input image"),
|
| 90 |
-
gr.
|
|
|
|
| 91 |
gr.Checkbox(True, label="Show GradCAM Image"),
|
| 92 |
gr.Dropdown(model_layer_names, value=3, label="Which layer for Gradcam"),
|
| 93 |
gr.Slider(0, 1, value=0.5,label="Overall opacity of the overlay"),
|
| 94 |
],
|
| 95 |
outputs = [
|
| 96 |
-
gr.Label(label="Class", container=True, show_label= True),
|
| 97 |
gr.Image(width= 256, height=256,label="Output Image"),
|
| 98 |
-
gr.
|
| 99 |
],
|
| 100 |
-
title = "Yolo v9 on custom fooball dataset",
|
| 101 |
-
description = " A simple gradio inference, and detection results for custom trained yolov9
|
| 102 |
-
examples = [["img1.jpg",1, True, 10, 0.4]]
|
|
|
|
| 103 |
)
|
| 104 |
|
| 105 |
if __name__ == "__main__":
|
| 106 |
-
demo.launch()
|
| 107 |
|
|
|
|
| 12 |
|
| 13 |
model = yolo9.load('best.pt')
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
classes = ('ball', 'goalkeeper', 'player', 'referee')
|
| 16 |
|
| 17 |
model_layer_names = ["1", "2", "3"]
|
|
|
|
| 40 |
return resized
|
| 41 |
|
| 42 |
|
| 43 |
+
def inference(input_img, num_classes, conf_threshold, show_gradcam, layer_name, num_classes, transparancy = 0.5):
|
| 44 |
+
# Set model parameters
|
| 45 |
+
model.conf = conf_threshold
|
| 46 |
+
model.iou = iou_threshold
|
| 47 |
print(show_gradcam, layer_name, num_classes, transparancy)
|
| 48 |
input_img = resize_image_pil(input_img,32,32)
|
| 49 |
input_img = np.array(input_img)
|
|
|
|
| 54 |
input_img = transform(input_img)
|
| 55 |
input_img = input_img.unsqueeze(0)
|
| 56 |
outputs = model(input_img)
|
| 57 |
+
output_res = outputs.render()
|
| 58 |
# print(outputs)
|
| 59 |
softmax = torch.nn.Softmax(dim=0)
|
| 60 |
o = softmax(outputs.flatten())
|
|
|
|
| 62 |
output_numpy = np.squeeze(np.asarray(outputs.detach().numpy()))
|
| 63 |
index_sort = np.argsort(output_numpy)[::-1]
|
| 64 |
|
| 65 |
+
# confidences = {}
|
| 66 |
+
# for i in range(int(num_classes)):
|
| 67 |
+
# confidences[classes[index_sort[i]]] = float(o[index_sort[i]])
|
| 68 |
|
| 69 |
|
| 70 |
+
# prediction= torch.max(outputs, 1)
|
| 71 |
|
| 72 |
if show_gradcam:
|
| 73 |
target_layers = get_layer(layer_name)
|
|
|
|
| 81 |
visualization = org_img
|
| 82 |
|
| 83 |
|
| 84 |
+
return output_res[0], visualization
|
| 85 |
|
| 86 |
demo = gr.Interface(
|
| 87 |
inference,
|
| 88 |
inputs = [
|
| 89 |
gr.Image(width=256,height=256,label="Input image"),
|
| 90 |
+
gr.Slider(label='Confidence threshold', minimum=0.1,maximum=1.0,step=0.1,value=0.4),
|
| 91 |
+
gr.Slider(label='IOU threshold', minimum=0.1,maximum=1.0,step=0.1,value=0.5),
|
| 92 |
gr.Checkbox(True, label="Show GradCAM Image"),
|
| 93 |
gr.Dropdown(model_layer_names, value=3, label="Which layer for Gradcam"),
|
| 94 |
gr.Slider(0, 1, value=0.5,label="Overall opacity of the overlay"),
|
| 95 |
],
|
| 96 |
outputs = [
|
|
|
|
| 97 |
gr.Image(width= 256, height=256,label="Output Image"),
|
| 98 |
+
gr.Image(width= 256, height=256,label="GradCAM image")
|
| 99 |
],
|
| 100 |
+
title = "Yolo v9 on custom trained fooball dataset",
|
| 101 |
+
description = " A simple gradio inference, and detection results for custom trained yolov9 for 100 epochs",
|
| 102 |
+
examples = [["img1.jpg",1, True, 10, 0.4]],
|
| 103 |
+
cache_examples=True
|
| 104 |
)
|
| 105 |
|
| 106 |
if __name__ == "__main__":
|
| 107 |
+
demo.launch(debug=True)
|
| 108 |
|