Spaces:
Sleeping
Sleeping
Upload 8 files
Browse files- .gitattributes +1 -0
- Base-RCNN-FPN.yaml +50 -0
- app.py +73 -0
- examples/1.jpg +0 -0
- examples/2.jpg +0 -0
- examples/3.jpg +3 -0
- model_final.pth +3 -0
- requirements.txt +6 -0
- resnet.yaml +28 -0
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
examples/3.jpg filter=lfs diff=lfs merge=lfs -text
|
Base-RCNN-FPN.yaml
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
MODEL:
|
2 |
+
META_ARCHITECTURE: "GeneralizedRCNN"
|
3 |
+
|
4 |
+
BACKBONE:
|
5 |
+
NAME: "build_resnet_fpn_backbone"
|
6 |
+
|
7 |
+
RESNETS:
|
8 |
+
OUT_FEATURES: ["res2", "res3", "res4", "res5"]
|
9 |
+
|
10 |
+
FPN:
|
11 |
+
IN_FEATURES: ["res2", "res3", "res4", "res5"]
|
12 |
+
|
13 |
+
ANCHOR_GENERATOR:
|
14 |
+
# One size for each in feature map
|
15 |
+
SIZES: [[32], [64], [128], [256], [512]]
|
16 |
+
# Three aspect ratios (same for all in feature maps)
|
17 |
+
ASPECT_RATIOS: [[0.5, 1.0, 2.0]]
|
18 |
+
|
19 |
+
RPN:
|
20 |
+
IN_FEATURES: ["p2", "p3", "p4", "p5", "p6"]
|
21 |
+
PRE_NMS_TOPK_TRAIN: 2000 # Per FPN level
|
22 |
+
PRE_NMS_TOPK_TEST: 1000 # Per FPN level
|
23 |
+
# Detectron1 uses 2000 proposals per-batch,
|
24 |
+
# (See "modeling/rpn/rpn_outputs.py" for details of this legacy issue)
|
25 |
+
# which is approximately 1000 proposals per-image since the default
|
26 |
+
# batch size for FPN is 2.
|
27 |
+
POST_NMS_TOPK_TRAIN: 1000
|
28 |
+
POST_NMS_TOPK_TEST: 1000
|
29 |
+
|
30 |
+
ROI_HEADS:
|
31 |
+
NAME: "StandardROIHeads"
|
32 |
+
IN_FEATURES: ["p2", "p3", "p4", "p5"]
|
33 |
+
|
34 |
+
ROI_BOX_HEAD:
|
35 |
+
NAME: "FastRCNNConvFCHead"
|
36 |
+
NUM_FC: 2
|
37 |
+
POOLER_RESOLUTION: 7
|
38 |
+
|
39 |
+
ROI_MASK_HEAD:
|
40 |
+
NAME: "MaskRCNNConvUpsampleHead"
|
41 |
+
NUM_CONV: 4
|
42 |
+
POOLER_RESOLUTION: 14
|
43 |
+
|
44 |
+
INPUT:
|
45 |
+
MIN_SIZE_TRAIN: (640, 672, 704, 736, 768, 800)
|
46 |
+
|
47 |
+
SOLVER:
|
48 |
+
CHECKPOINT_PERIOD: 210000
|
49 |
+
|
50 |
+
VERSION: 2
|
app.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
try:
|
2 |
+
import detectron2
|
3 |
+
except:
|
4 |
+
import os
|
5 |
+
os.system('pip install git+https://github.com/facebookresearch/detectron2.git')
|
6 |
+
import gradio as gr
|
7 |
+
import torch
|
8 |
+
import cv2
|
9 |
+
import pandas as pd
|
10 |
+
from detectron2.utils.visualizer import Visualizer
|
11 |
+
from detectron2.data import MetadataCatalog
|
12 |
+
from detectron2.config import get_cfg
|
13 |
+
from detectron2.data.detection_utils import read_image
|
14 |
+
from detectron2.engine import DefaultPredictor
|
15 |
+
cfg = get_cfg()
|
16 |
+
cfg.merge_from_file("resnet.yaml")
|
17 |
+
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.7
|
18 |
+
cfg.MODEL.DEVICE='cpu'
|
19 |
+
predictor = DefaultPredictor(cfg)
|
20 |
+
|
21 |
+
def inference(image_path):
|
22 |
+
# load image
|
23 |
+
image = read_image(image_path, format="BGR")
|
24 |
+
|
25 |
+
# Make inference on the image
|
26 |
+
outputs = predictor(image)
|
27 |
+
|
28 |
+
metadata = MetadataCatalog.get("custom_dataset_train")
|
29 |
+
MetadataCatalog.get("custom_dataset_train").set(thing_classes=['100km/h', '120km/h', '20km/h', '30km/h', '40km/h', '50km/h', '60km/h', '70km/h', '80km/h'])
|
30 |
+
v = Visualizer(image, metadata, scale=1)
|
31 |
+
out = v.draw_instance_predictions(outputs['instances'])
|
32 |
+
|
33 |
+
# Detection summary table
|
34 |
+
cls_idxs = outputs['instances'].pred_classes.numpy()
|
35 |
+
thing_classes=['100km/h', '120km/h', '20km/h', '30km/h', '40km/h', '50km/h', '60km/h', '70km/h', '80km/h']
|
36 |
+
# get labels from class indices
|
37 |
+
labels = [thing_classes[i] for i in cls_idxs]
|
38 |
+
scores = outputs['instances'].scores.numpy()
|
39 |
+
df = pd.DataFrame({'Detected speed limit': labels, 'Confidence score': scores})
|
40 |
+
# Return the visualization as an RGB image
|
41 |
+
return out.get_image()[:, :, ::-1], df
|
42 |
+
|
43 |
+
|
44 |
+
|
45 |
+
examples = ["examples/1.jpg", "examples/2.jpg", "examples/3.jpg"]
|
46 |
+
with gr.Blocks(theme='gradio/monochrome') as demo:
|
47 |
+
gr.Markdown("# Speed Limit Detection demo")
|
48 |
+
gr.Markdown("**Author**: *Lu CHEN*")
|
49 |
+
gr.Markdown(
|
50 |
+
"""This interactive demo is based on the Faster R-CNN model for object detection. The model is
|
51 |
+
trained using the [Detectron2](https://github.com/facebookresearch/detectron2) library with a custom
|
52 |
+
dataset that I created by combining images from [Tsinghua-Tencent100K](https://cg.cs.tsinghua.edu.cn/traffic-sign/) and [GTSDB](https://benchmark.ini.rub.de/), both of which provide real-world traffic signs captured within the autonomous driving domain.
|
53 |
+
|
54 |
+
To use the demo, simply upload an image and click on *"Infer"* to view the following results:
|
55 |
+
- **Detection**: outputs of Object Detector
|
56 |
+
- **Detection summary**: a summary of the detection outputs
|
57 |
+
|
58 |
+
You can also select an image from the cached **Examples** to quickly try out. Without clicking *"Infer"*, the cached outputs will be loaded automatically.
|
59 |
+
In case the output image seems too small, simply right-click on the image, and choose “Open image in new tab” to visualize it in full size.
|
60 |
+
"""
|
61 |
+
)
|
62 |
+
with gr.Row():
|
63 |
+
with gr.Column():
|
64 |
+
image = gr.Image(type="filepath")
|
65 |
+
button = gr.Button("Infer")
|
66 |
+
|
67 |
+
with gr.Column():
|
68 |
+
detection = gr.Image(label="Output")
|
69 |
+
detection_summary = gr.DataFrame(label="Detection summary")
|
70 |
+
examples_block = gr.Examples(inputs=image, examples=examples, fn=inference, outputs=[detection, detection_summary], cache_examples=True)
|
71 |
+
button.click(fn=inference, inputs=image, outputs=[detection, detection_summary])
|
72 |
+
|
73 |
+
demo.launch()
|
examples/1.jpg
ADDED
examples/2.jpg
ADDED
examples/3.jpg
ADDED
Git LFS Details
|
model_final.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8a775a23a4481e1b963650cf26af9db8f8ad3f00724e03485594c8acef239581
|
3 |
+
size 330352803
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
opencv-python-headless
|
2 |
+
pandas
|
3 |
+
numpy
|
4 |
+
matplotlib
|
5 |
+
torch
|
6 |
+
torchvision
|
resnet.yaml
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
_BASE_: "Base-RCNN-FPN.yaml"
|
2 |
+
MODEL:
|
3 |
+
META_ARCHITECTURE: "GeneralizedRCNN"
|
4 |
+
WEIGHTS: "model_final.pth"
|
5 |
+
#WEIGHTS: "./data/SPEED-Detection/faster-rcnn/vanilla/random_seed_0/model_final.pth"
|
6 |
+
|
7 |
+
MASK_ON: False
|
8 |
+
RESNETS:
|
9 |
+
DEPTH: 50
|
10 |
+
ROI_HEADS:
|
11 |
+
NAME: "StandardROIHeads"
|
12 |
+
NUM_CLASSES: 9
|
13 |
+
# INPUT:
|
14 |
+
# MIN_SIZE_TRAIN: (480, 512, 544, 576, 608, 640, 672, 704, 736, 768, 800)
|
15 |
+
# MIN_SIZE_TEST: 800
|
16 |
+
DATASETS:
|
17 |
+
TRAIN: ('speed_custom_train',)
|
18 |
+
TEST: ('speed_custom_val',)
|
19 |
+
SOLVER:
|
20 |
+
IMS_PER_BATCH: 18
|
21 |
+
BASE_LR: 0.01
|
22 |
+
STEPS: (2800, 3700)
|
23 |
+
MAX_ITER: 4200
|
24 |
+
WARMUP_ITERS: 50
|
25 |
+
CHECKPOINT_PERIOD : 2100
|
26 |
+
TEST:
|
27 |
+
EVAL_PERIOD: 1000
|
28 |
+
|