Spaces:
Sleeping
Sleeping
Commit
·
87700f1
1
Parent(s):
94627ea
Added plot to show false detection
Browse files- README.md +1 -12
- app.py +36 -4
- false_detection/image_100.jpg +0 -0
- false_detection/image_105.jpg +0 -0
- false_detection/image_106.jpg +0 -0
- false_detection/image_108.jpg +0 -0
- false_detection/image_111.jpg +0 -0
- false_detection/image_113.jpg +0 -0
- false_detection/image_114.jpg +0 -0
- false_detection/image_121.jpg +0 -0
- false_detection/image_122.jpg +0 -0
- false_detection/image_126.jpg +0 -0
- false_detection/image_138.jpg +0 -0
- false_detection/image_140.jpg +0 -0
- false_detection/image_25.jpg +0 -0
- false_detection/image_39.jpg +0 -0
- false_detection/image_40.jpg +0 -0
- false_detection/image_41.jpg +0 -0
- false_detection/image_42.jpg +0 -0
- false_detection/image_51.jpg +0 -0
- false_detection/image_53.jpg +0 -0
- false_detection/image_61.jpg +0 -0
- false_detection/image_64.jpg +0 -0
- false_detection/image_68.jpg +0 -0
- false_detection/image_87.jpg +0 -0
- false_detection/image_89.jpg +0 -0
- false_detection/image_90.jpg +0 -0
- false_detection/image_91.jpg +0 -0
- false_detection/image_95.jpg +0 -0
- false_detection/image_97.jpg +0 -0
- false_detection/topless_11.jpg +0 -0
- false_detection/topless_14.jpg +0 -0
- false_detection/topless_15.jpg +0 -0
- false_detection/topless_17.jpg +0 -0
- false_detection/topless_21.jpg +0 -0
- false_detection/topless_29.jpg +0 -0
- false_detection/topless_3.jpg +0 -0
README.md
CHANGED
|
@@ -18,15 +18,4 @@ sdk: gradio
|
|
| 18 |
## Usage
|
| 19 |
- Upload an image
|
| 20 |
- Change settings as requied
|
| 21 |
-
- Hit submit and view
|
| 22 |
-
|
| 23 |
-
## Features
|
| 24 |
-
- GradCAM: To understand what models has actully learned. Adjust opacity and model layer for grad-cam.
|
| 25 |
-
- Miss classified images: Plot of images missclassified by model
|
| 26 |
-
- Image input/output: Upload image and check predictions
|
| 27 |
-
- Top classes: Show top n classes with high prediction confidence.
|
| 28 |
-
|
| 29 |
-
## Usage
|
| 30 |
-
- Upload an image
|
| 31 |
-
- Change settings as requied
|
| 32 |
-
- Hit submit and view results
|
|
|
|
| 18 |
## Usage
|
| 19 |
- Upload an image
|
| 20 |
- Change settings as requied
|
| 21 |
+
- Hit submit and view result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
import numpy as np
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
from models.common import DetectMultiBackend
|
| 6 |
from utils.augmentations import letterbox
|
|
@@ -14,6 +19,9 @@ data = "data.yaml"
|
|
| 14 |
device = select_device('cpu')
|
| 15 |
model = DetectMultiBackend(weights, device=device, dnn=False, data=data, fp16=False)
|
| 16 |
|
|
|
|
|
|
|
|
|
|
| 17 |
def resize_image_pil(image, new_width, new_height):
|
| 18 |
|
| 19 |
# Convert to PIL image
|
|
@@ -35,7 +43,22 @@ def resize_image_pil(image, new_width, new_height):
|
|
| 35 |
|
| 36 |
return resized
|
| 37 |
|
| 38 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
im0 = input_img.copy()
|
| 40 |
stride, names, pt = model.stride, model.names, model.pt
|
| 41 |
imgsz = check_img_size((640, 640), s=stride) # check image size
|
|
@@ -76,8 +99,14 @@ def inference(input_img, conf_thres, iou_thres):
|
|
| 76 |
c = int(cls) # integer class
|
| 77 |
label = f'{names[c]} {conf:.2f}'
|
| 78 |
annotator.box_label(xyxy, label, color=colors(c, True))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
return im0
|
| 81 |
|
| 82 |
title = "YOLOv9 model to detect shirt/tshirt"
|
| 83 |
description = "A simple Gradio interface to infer on YOLOv9 model and detect tshirt in image"
|
|
@@ -90,8 +119,11 @@ examples = [["image_1.jpg", 0.25, 0.45], ["image_2.jpg", 0.25, 0.45],
|
|
| 90 |
demo = gr.Interface(inference,
|
| 91 |
inputs = [gr.Image(width=320, height=320, label="Input Image"),
|
| 92 |
gr.Slider(0, 1, 0.25, label="Confidence Threshold"),
|
| 93 |
-
gr.Slider(0, 1, 0.45, label="IoU Thresold")
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
| 95 |
title=title,
|
| 96 |
description=description,
|
| 97 |
examples=examples)
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import cv2
|
| 3 |
+
import math
|
| 4 |
import torch
|
| 5 |
import numpy as np
|
| 6 |
import gradio as gr
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
from glob import glob
|
| 9 |
from PIL import Image
|
| 10 |
from models.common import DetectMultiBackend
|
| 11 |
from utils.augmentations import letterbox
|
|
|
|
| 19 |
device = select_device('cpu')
|
| 20 |
model = DetectMultiBackend(weights, device=device, dnn=False, data=data, fp16=False)
|
| 21 |
|
| 22 |
+
false_detection_data = glob(os.path.join("false_detection", '*.jpg'))
|
| 23 |
+
false_detection_data = [x.replace('\\', '/') for x in false_detection_data]
|
| 24 |
+
|
| 25 |
def resize_image_pil(image, new_width, new_height):
|
| 26 |
|
| 27 |
# Convert to PIL image
|
|
|
|
| 43 |
|
| 44 |
return resized
|
| 45 |
|
| 46 |
+
def display_false_detection_data(false_detection_data, number_of_samples):
|
| 47 |
+
fig = plt.figure(figsize=(10, 10))
|
| 48 |
+
|
| 49 |
+
x_count = 5
|
| 50 |
+
y_count = 1 if number_of_samples <= 5 else math.floor(number_of_samples / x_count)
|
| 51 |
+
|
| 52 |
+
for i in range(number_of_samples):
|
| 53 |
+
plt.subplot(y_count, x_count, i + 1)
|
| 54 |
+
img = cv2.imread(false_detection_data[i])
|
| 55 |
+
plt.imshow(img)
|
| 56 |
+
plt.xticks([])
|
| 57 |
+
plt.yticks([])
|
| 58 |
+
|
| 59 |
+
return fig
|
| 60 |
+
|
| 61 |
+
def inference(input_img, conf_thres, iou_thres, is_false_detection_images=True, num_false_detection_images=10):
|
| 62 |
im0 = input_img.copy()
|
| 63 |
stride, names, pt = model.stride, model.names, model.pt
|
| 64 |
imgsz = check_img_size((640, 640), s=stride) # check image size
|
|
|
|
| 99 |
c = int(cls) # integer class
|
| 100 |
label = f'{names[c]} {conf:.2f}'
|
| 101 |
annotator.box_label(xyxy, label, color=colors(c, True))
|
| 102 |
+
|
| 103 |
+
if is_false_detection_images:
|
| 104 |
+
# Plot the misclassified data
|
| 105 |
+
misclassified_images = display_false_detection_data(false_detection_data, number_of_samples=num_false_detection_images)
|
| 106 |
+
else:
|
| 107 |
+
misclassified_images = None
|
| 108 |
|
| 109 |
+
return im0, misclassified_images
|
| 110 |
|
| 111 |
title = "YOLOv9 model to detect shirt/tshirt"
|
| 112 |
description = "A simple Gradio interface to infer on YOLOv9 model and detect tshirt in image"
|
|
|
|
| 119 |
demo = gr.Interface(inference,
|
| 120 |
inputs = [gr.Image(width=320, height=320, label="Input Image"),
|
| 121 |
gr.Slider(0, 1, 0.25, label="Confidence Threshold"),
|
| 122 |
+
gr.Slider(0, 1, 0.45, label="IoU Thresold"),
|
| 123 |
+
gr.Checkbox(label="Show False Detection"),
|
| 124 |
+
gr.Slider(5, 35, value=10, step=5, label="Number of False Detection")],
|
| 125 |
+
outputs= [gr.Image(width=640, height=640, label="Output"),
|
| 126 |
+
gr.Plot(label="False Detection")],
|
| 127 |
title=title,
|
| 128 |
description=description,
|
| 129 |
examples=examples)
|
false_detection/image_100.jpg
ADDED
|
false_detection/image_105.jpg
ADDED
|
false_detection/image_106.jpg
ADDED
|
false_detection/image_108.jpg
ADDED
|
false_detection/image_111.jpg
ADDED
|
false_detection/image_113.jpg
ADDED
|
false_detection/image_114.jpg
ADDED
|
false_detection/image_121.jpg
ADDED
|
false_detection/image_122.jpg
ADDED
|
false_detection/image_126.jpg
ADDED
|
false_detection/image_138.jpg
ADDED
|
false_detection/image_140.jpg
ADDED
|
false_detection/image_25.jpg
ADDED
|
false_detection/image_39.jpg
ADDED
|
false_detection/image_40.jpg
ADDED
|
false_detection/image_41.jpg
ADDED
|
false_detection/image_42.jpg
ADDED
|
false_detection/image_51.jpg
ADDED
|
false_detection/image_53.jpg
ADDED
|
false_detection/image_61.jpg
ADDED
|
false_detection/image_64.jpg
ADDED
|
false_detection/image_68.jpg
ADDED
|
false_detection/image_87.jpg
ADDED
|
false_detection/image_89.jpg
ADDED
|
false_detection/image_90.jpg
ADDED
|
false_detection/image_91.jpg
ADDED
|
false_detection/image_95.jpg
ADDED
|
false_detection/image_97.jpg
ADDED
|
false_detection/topless_11.jpg
ADDED
|
false_detection/topless_14.jpg
ADDED
|
false_detection/topless_15.jpg
ADDED
|
false_detection/topless_17.jpg
ADDED
|
false_detection/topless_21.jpg
ADDED
|
false_detection/topless_29.jpg
ADDED
|
false_detection/topless_3.jpg
ADDED
|