Spaces:
Runtime error
Runtime error
cache examples false
Browse files- app.py +83 -36
- config.py +0 -32
- requirement.txt +0 -6
- yolov3.pth +1 -1
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
import cv2
|
@@ -6,6 +7,11 @@ from albumentations.pytorch import ToTensorV2
|
|
6 |
import matplotlib.pyplot as plt
|
7 |
import matplotlib
|
8 |
matplotlib.use('agg')
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
import config
|
11 |
from model import YOLOv3
|
@@ -15,37 +21,30 @@ from utils import (
|
|
15 |
plot_image
|
16 |
)
|
17 |
|
18 |
-
|
19 |
-
|
|
|
|
|
20 |
# checkpoint = torch.load(fname, map_location=torch.device('cpu'))
|
21 |
# model_state_dict = checkpoint['state_dict']
|
22 |
-
# torch.save(model.state_dict(), 'yolov3.pth')
|
23 |
model = YOLOv3(num_classes=20)
|
|
|
|
|
|
|
24 |
model.load_state_dict(torch.load(fname))
|
25 |
|
26 |
-
IMAGE_SIZE =
|
27 |
S = [IMAGE_SIZE // 32, IMAGE_SIZE // 16, IMAGE_SIZE // 8]
|
28 |
anchors = ( torch.tensor(config.ANCHORS)
|
29 |
* torch.tensor(config.S).unsqueeze(1)\
|
30 |
.unsqueeze(1).repeat(1, 3, 2)
|
31 |
)
|
32 |
|
33 |
-
|
34 |
-
[
|
35 |
-
A.LongestMaxSize(max_size=IMAGE_SIZE),
|
36 |
-
A.PadIfNeeded(
|
37 |
-
min_height=IMAGE_SIZE, min_width=IMAGE_SIZE, border_mode=cv2.BORDER_CONSTANT
|
38 |
-
),
|
39 |
-
A.Normalize(mean=[0, 0, 0], std=[1, 1, 1], max_pixel_value=255,),
|
40 |
-
ToTensorV2(),
|
41 |
-
],
|
42 |
-
)
|
43 |
-
def object_detector(input_image):
|
44 |
-
input_img = test_transforms(image=input_image)['image']
|
45 |
input_img = input_img.unsqueeze(0)
|
46 |
|
47 |
-
|
48 |
-
iou_thresh = 0.5
|
49 |
with torch.no_grad():
|
50 |
out = model(input_img)
|
51 |
bboxes = []
|
@@ -54,9 +53,9 @@ def object_detector(input_image):
|
|
54 |
anchor = anchors[i]
|
55 |
bboxes += cells_to_bboxes(
|
56 |
out[i], anchor, S=S, is_preds=True
|
57 |
-
)
|
58 |
nms_boxes = non_max_suppression(
|
59 |
-
bboxes
|
60 |
threshold=thresh, box_format="midpoint",
|
61 |
)
|
62 |
fig = plot_image(input_img.squeeze(0).permute(1,2,0).detach().cpu(),
|
@@ -67,27 +66,75 @@ def object_detector(input_image):
|
|
67 |
image_path = "plot.png"
|
68 |
fig.savefig(image_path)
|
69 |
plt.close()
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
# Define the input and output components for Gradio
|
73 |
input_image = gr.Image(label="Input image")
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
.style(width=428, height=428)
|
76 |
images_path = "examples/"
|
77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
|
|
|
|
93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import unittest
|
2 |
import gradio as gr
|
3 |
import torch
|
4 |
import cv2
|
|
|
7 |
import matplotlib.pyplot as plt
|
8 |
import matplotlib
|
9 |
matplotlib.use('agg')
|
10 |
+
import torch
|
11 |
+
import cv2
|
12 |
+
from pytorch_grad_cam import EigenCAM
|
13 |
+
from pytorch_grad_cam.utils.image import show_cam_on_image
|
14 |
+
|
15 |
|
16 |
import config
|
17 |
from model import YOLOv3
|
|
|
21 |
plot_image
|
22 |
)
|
23 |
|
24 |
+
def yolov3_reshape_transform(tensor, ):
|
25 |
+
return tensor[0]
|
26 |
+
|
27 |
+
# fname = 'epoch=38-step=20202.ckpt'
|
28 |
# checkpoint = torch.load(fname, map_location=torch.device('cpu'))
|
29 |
# model_state_dict = checkpoint['state_dict']
|
|
|
30 |
model = YOLOv3(num_classes=20)
|
31 |
+
# model.load_state_dict(model_state_dict)
|
32 |
+
# torch.save(model.state_dict(), 'yolov3.pth')
|
33 |
+
fname = 'yolov3.pth'
|
34 |
model.load_state_dict(torch.load(fname))
|
35 |
|
36 |
+
IMAGE_SIZE = config.IMAGE_SIZE
|
37 |
S = [IMAGE_SIZE // 32, IMAGE_SIZE // 16, IMAGE_SIZE // 8]
|
38 |
anchors = ( torch.tensor(config.ANCHORS)
|
39 |
* torch.tensor(config.S).unsqueeze(1)\
|
40 |
.unsqueeze(1).repeat(1, 3, 2)
|
41 |
)
|
42 |
|
43 |
+
def object_detector(input_image, thresh = 0.8, iou_thresh = 0.5):
|
44 |
+
input_img = config.test_transforms(image=input_image)['image']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
input_img = input_img.unsqueeze(0)
|
46 |
|
47 |
+
|
|
|
48 |
with torch.no_grad():
|
49 |
out = model(input_img)
|
50 |
bboxes = []
|
|
|
53 |
anchor = anchors[i]
|
54 |
bboxes += cells_to_bboxes(
|
55 |
out[i], anchor, S=S, is_preds=True
|
56 |
+
)[0]
|
57 |
nms_boxes = non_max_suppression(
|
58 |
+
bboxes, iou_threshold=iou_thresh,
|
59 |
threshold=thresh, box_format="midpoint",
|
60 |
)
|
61 |
fig = plot_image(input_img.squeeze(0).permute(1,2,0).detach().cpu(),
|
|
|
66 |
image_path = "plot.png"
|
67 |
fig.savefig(image_path)
|
68 |
plt.close()
|
69 |
+
|
70 |
+
# target_layers = [model.layers[21]]
|
71 |
+
# cam = EigenCAM(model, target_layers, use_cuda=False,
|
72 |
+
# reshape_transform=yolov3_reshape_transform,
|
73 |
+
# )
|
74 |
+
# grayscale_cam = cam(input_img, target_layers)[0][0, :, :]
|
75 |
+
# cam_image = show_cam_on_image(img, grayscale_cam, use_rgb=True)
|
76 |
+
return gr.update(value=image_path, visible=True),\
|
77 |
+
gr.update(value=image_path, visible=True)
|
78 |
|
79 |
# Define the input and output components for Gradio
|
80 |
input_image = gr.Image(label="Input image")
|
81 |
+
confidence_level = gr.Slider(0.5, 1, value=0.6, step=0.01,
|
82 |
+
label="confidence level")
|
83 |
+
iou_level = gr.Slider(0.5, 1, value=0.6, step=0.01,
|
84 |
+
label="Interference over union level")
|
85 |
+
output_box = gr.Image(label="Output image", visible=False,)\
|
86 |
+
.style(width=428, height=428)
|
87 |
+
cam_output = gr.Image(label="cam output", visible=False)\
|
88 |
.style(width=428, height=428)
|
89 |
images_path = "examples/"
|
90 |
|
91 |
+
gr_interface = gr.Interface(
|
92 |
+
fn=object_detector,
|
93 |
+
inputs=[input_image, confidence_level, iou_level],
|
94 |
+
outputs=[output_box, cam_output],
|
95 |
+
examples=[[images_path + "000015.jpg"],
|
96 |
+
[images_path + "000017.jpg"],
|
97 |
+
[images_path + "000030.jpg"],
|
98 |
+
[images_path + "000069.jpg"],
|
99 |
+
[images_path + "000071.jpg"],
|
100 |
+
[images_path + "000084.jpg"],
|
101 |
+
[images_path + "000086.jpg"],
|
102 |
+
[images_path + "000088.jpg"],
|
103 |
+
[images_path + "000100.jpg"],
|
104 |
+
],
|
105 |
+
cache_examples=False
|
106 |
+
)
|
107 |
|
108 |
+
gr_interface.launch()
|
109 |
+
|
110 |
+
# class TestGradioInterfaceInput(unittest.TestCase):
|
111 |
+
# def test_valid_image_input(self):
|
112 |
+
# # Create a valid input image
|
113 |
+
# input_image = images_path + "000015.jpg"
|
114 |
+
#
|
115 |
+
# # Pass the image through the interface
|
116 |
+
# output = gr_interface(input_image)
|
117 |
+
#
|
118 |
+
# # Assert the output matches the expected result
|
119 |
+
# self.assertEqual(output[0].shape, (3, 416, 416))
|
120 |
+
#
|
121 |
+
# if __name__ == '__main__':
|
122 |
+
# unittest.main()
|
123 |
+
#
|
124 |
|
125 |
+
# Create the Gradio interface
|
126 |
+
# gr.Interface(fn=object_detector, inputs=input_image,
|
127 |
+
# outputs=[output_box, cam_output],
|
128 |
+
# examples=[[images_path + "000015.jpg"],
|
129 |
+
# [images_path + "000017.jpg"],
|
130 |
+
# [images_path + "000030.jpg"],
|
131 |
+
# [images_path + "000069.jpg"],
|
132 |
+
# [images_path + "000071.jpg"],
|
133 |
+
# [images_path + "000084.jpg"],
|
134 |
+
# [images_path + "000086.jpg"],
|
135 |
+
# [images_path + "000088.jpg"],
|
136 |
+
# [images_path + "000095.jpg"],
|
137 |
+
# [images_path + "000100.jpg"],
|
138 |
+
# ],
|
139 |
+
# ).launch()
|
140 |
+
#
|
config.py
CHANGED
@@ -26,37 +26,6 @@ ANCHORS = [
|
|
26 |
means = [0.485, 0.456, 0.406]
|
27 |
|
28 |
scale = 1.1
|
29 |
-
train_transforms = A.Compose(
|
30 |
-
[
|
31 |
-
A.LongestMaxSize(max_size=int(IMAGE_SIZE * scale)),
|
32 |
-
A.PadIfNeeded(
|
33 |
-
min_height=int(IMAGE_SIZE * scale),
|
34 |
-
min_width=int(IMAGE_SIZE * scale),
|
35 |
-
border_mode=cv2.BORDER_CONSTANT,
|
36 |
-
),
|
37 |
-
A.Rotate(limit = 10, interpolation=1, border_mode=4),
|
38 |
-
A.RandomCrop(width=IMAGE_SIZE, height=IMAGE_SIZE),
|
39 |
-
A.ColorJitter(brightness=0.6, contrast=0.6, saturation=0.6, hue=0.6, p=0.4),
|
40 |
-
A.OneOf(
|
41 |
-
[
|
42 |
-
A.ShiftScaleRotate(
|
43 |
-
rotate_limit=20, p=0.5, border_mode=cv2.BORDER_CONSTANT
|
44 |
-
),
|
45 |
-
# A.Affine(shear=15, p=0.5, mode="constant"),
|
46 |
-
],
|
47 |
-
p=1.0,
|
48 |
-
),
|
49 |
-
A.HorizontalFlip(p=0.5),
|
50 |
-
A.Blur(p=0.1),
|
51 |
-
A.CLAHE(p=0.1),
|
52 |
-
A.Posterize(p=0.1),
|
53 |
-
A.ToGray(p=0.1),
|
54 |
-
A.ChannelShuffle(p=0.05),
|
55 |
-
A.Normalize(mean=[0, 0, 0], std=[1, 1, 1], max_pixel_value=255,),
|
56 |
-
ToTensorV2(),
|
57 |
-
],
|
58 |
-
bbox_params=A.BboxParams(format="yolo", min_visibility=0.4, label_fields=[],),
|
59 |
-
)
|
60 |
test_transforms = A.Compose(
|
61 |
[
|
62 |
A.LongestMaxSize(max_size=IMAGE_SIZE),
|
@@ -66,7 +35,6 @@ test_transforms = A.Compose(
|
|
66 |
A.Normalize(mean=[0, 0, 0], std=[1, 1, 1], max_pixel_value=255,),
|
67 |
ToTensorV2(),
|
68 |
],
|
69 |
-
bbox_params=A.BboxParams(format="yolo", min_visibility=0.4, label_fields=[]),
|
70 |
)
|
71 |
|
72 |
PASCAL_CLASSES = [
|
|
|
26 |
means = [0.485, 0.456, 0.406]
|
27 |
|
28 |
scale = 1.1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
test_transforms = A.Compose(
|
30 |
[
|
31 |
A.LongestMaxSize(max_size=IMAGE_SIZE),
|
|
|
35 |
A.Normalize(mean=[0, 0, 0], std=[1, 1, 1], max_pixel_value=255,),
|
36 |
ToTensorV2(),
|
37 |
],
|
|
|
38 |
)
|
39 |
|
40 |
PASCAL_CLASSES = [
|
requirement.txt
DELETED
@@ -1,6 +0,0 @@
|
|
1 |
-
torch
|
2 |
-
torchvision
|
3 |
-
grad-cam
|
4 |
-
pillow
|
5 |
-
numpy
|
6 |
-
albumentations
|
|
|
|
|
|
|
|
|
|
|
|
|
|
yolov3.pth
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
size 246865311
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8413ddf1b2ee36957265778c668a7592fb92704b5ba3692ab2e3137e89571649
|
3 |
size 246865311
|