Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
mmdeploy
Commits
a17c53b8
Commit
a17c53b8
authored
Jun 25, 2025
by
limm
Browse files
add test_mmdet test_mmocr and test_mmpose
parent
a72d0dfa
Pipeline
#2819
canceled with stages
Changes
33
Pipelines
1
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
20 changed files
with
4421 additions
and
0 deletions
+4421
-0
tests/test_codebase/test_mmdet/conftest.py
tests/test_codebase/test_mmdet/conftest.py
+10
-0
tests/test_codebase/test_mmdet/data/coco_sample.json
tests/test_codebase/test_mmdet/data/coco_sample.json
+39
-0
tests/test_codebase/test_mmdet/data/detr_model.json
tests/test_codebase/test_mmdet/data/detr_model.json
+129
-0
tests/test_codebase/test_mmdet/data/mask_model.json
tests/test_codebase/test_mmdet/data/mask_model.json
+231
-0
tests/test_codebase/test_mmdet/data/mmrazor_model.py
tests/test_codebase/test_mmdet/data/mmrazor_model.py
+34
-0
tests/test_codebase/test_mmdet/data/mmrazor_mutable_cfg.yaml
tests/test_codebase/test_mmdet/data/mmrazor_mutable_cfg.yaml
+60
-0
tests/test_codebase/test_mmdet/data/model.py
tests/test_codebase/test_mmdet/data/model.py
+139
-0
tests/test_codebase/test_mmdet/data/single_stage_model.json
tests/test_codebase/test_mmdet/data/single_stage_model.json
+110
-0
tests/test_codebase/test_mmdet/test_mmdet_models.py
tests/test_codebase/test_mmdet/test_mmdet_models.py
+2584
-0
tests/test_codebase/test_mmdet/test_mmdet_structures.py
tests/test_codebase/test_mmdet/test_mmdet_structures.py
+53
-0
tests/test_codebase/test_mmdet/test_mmdet_utils.py
tests/test_codebase/test_mmdet/test_mmdet_utils.py
+84
-0
tests/test_codebase/test_mmdet/test_object_detection.py
tests/test_codebase/test_mmdet/test_object_detection.py
+206
-0
tests/test_codebase/test_mmdet/test_object_detection_model.py
...s/test_codebase/test_mmdet/test_object_detection_model.py
+372
-0
tests/test_codebase/test_mmocr/conftest.py
tests/test_codebase/test_mmocr/conftest.py
+10
-0
tests/test_codebase/test_mmocr/data/crnn.py
tests/test_codebase/test_mmocr/data/crnn.py
+115
-0
tests/test_codebase/test_mmocr/data/dbnet.py
tests/test_codebase/test_mmocr/data/dbnet.py
+72
-0
tests/test_codebase/test_mmocr/data/lower_english_digits.txt
tests/test_codebase/test_mmocr/data/lower_english_digits.txt
+36
-0
tests/test_codebase/test_mmocr/data/mrcnn.py
tests/test_codebase/test_mmocr/data/mrcnn.py
+135
-0
tests/test_codebase/test_mmocr/data/text_detection.json
tests/test_codebase/test_mmocr/data/text_detection.json
+1
-0
tests/test_codebase/test_mmocr/data/text_recognition.txt
tests/test_codebase/test_mmocr/data/text_recognition.txt
+1
-0
No files found.
tests/test_codebase/test_mmdet/conftest.py
0 → 100644
View file @
a17c53b8
# Copyright (c) OpenMMLab. All rights reserved.
import
pytest
@
pytest
.
fixture
(
autouse
=
True
)
def
init_test
():
# init default scope
from
mmdet.utils
import
register_all_modules
register_all_modules
(
True
)
tests/test_codebase/test_mmdet/data/coco_sample.json
0 → 100644
View file @
a17c53b8
{
"images"
:
[
{
"file_name"
:
"000000000139.jpg"
,
"height"
:
800
,
"width"
:
800
,
"id"
:
0
}
],
"annotations"
:
[
{
"bbox"
:
[
0
,
0
,
20
,
20
],
"area"
:
400.00
,
"score"
:
1.0
,
"category_id"
:
1
,
"id"
:
1
,
"image_id"
:
0
}
],
"categories"
:
[
{
"id"
:
1
,
"name"
:
"bus"
,
"supercategory"
:
"none"
},
{
"id"
:
2
,
"name"
:
"car"
,
"supercategory"
:
"none"
}
],
"licenses"
:
[],
"info"
:
null
}
tests/test_codebase/test_mmdet/data/detr_model.json
0 → 100644
View file @
a17c53b8
{
"type"
:
"DETR"
,
"num_queries"
:
100
,
"data_preprocessor"
:
{
"type"
:
"DetDataPreprocessor"
,
"mean"
:
[
123.675
,
116.28
,
103.53
],
"std"
:
[
58.395
,
57.12
,
57.375
],
"bgr_to_rgb"
:
true
,
"pad_size_divisor"
:
1
},
"backbone"
:
{
"type"
:
"ResNet"
,
"depth"
:
50
,
"num_stages"
:
4
,
"out_indices"
:
[
3
],
"frozen_stages"
:
1
,
"norm_cfg"
:
{
"type"
:
"BN"
,
"requires_grad"
:
false
},
"norm_eval"
:
true
,
"style"
:
"pytorch"
,
"init_cfg"
:
{
"type"
:
"Pretrained"
,
"checkpoint"
:
"torchvision://resnet50"
}
},
"neck"
:
{
"type"
:
"ChannelMapper"
,
"in_channels"
:
[
2048
],
"kernel_size"
:
1
,
"out_channels"
:
256
,
"num_outs"
:
1
},
"encoder"
:
{
"num_layers"
:
6
,
"layer_cfg"
:
{
"self_attn_cfg"
:
{
"embed_dims"
:
256
,
"num_heads"
:
8
,
"dropout"
:
0.1
,
"batch_first"
:
true
},
"ffn_cfg"
:
{
"embed_dims"
:
256
,
"feedforward_channels"
:
2048
,
"num_fcs"
:
2
,
"ffn_drop"
:
0.1
,
"act_cfg"
:
{
"type"
:
"ReLU"
,
"inplace"
:
true
}
}
}
},
"decoder"
:
{
"num_layers"
:
6
,
"layer_cfg"
:
{
"self_attn_cfg"
:
{
"embed_dims"
:
256
,
"num_heads"
:
8
,
"dropout"
:
0.1
,
"batch_first"
:
true
},
"cross_attn_cfg"
:
{
"embed_dims"
:
256
,
"num_heads"
:
8
,
"dropout"
:
0.1
,
"batch_first"
:
true
},
"ffn_cfg"
:
{
"embed_dims"
:
256
,
"feedforward_channels"
:
2048
,
"num_fcs"
:
2
,
"ffn_drop"
:
0.1
,
"act_cfg"
:
{
"type"
:
"ReLU"
,
"inplace"
:
true
}
}
},
"return_intermediate"
:
true
},
"positional_encoding"
:
{
"num_feats"
:
128
,
"normalize"
:
true
},
"bbox_head"
:
{
"type"
:
"DETRHead"
,
"num_classes"
:
80
,
"embed_dims"
:
256
,
"loss_cls"
:
{
"type"
:
"CrossEntropyLoss"
,
"bg_cls_weight"
:
0.1
,
"use_sigmoid"
:
false
,
"loss_weight"
:
1.0
,
"class_weight"
:
1.0
},
"loss_bbox"
:
{
"type"
:
"L1Loss"
,
"loss_weight"
:
5.0
},
"loss_iou"
:
{
"type"
:
"GIoULoss"
,
"loss_weight"
:
2.0
}
},
"train_cfg"
:
{
"assigner"
:
{
"type"
:
"HungarianAssigner"
,
"match_costs"
:
[{
"type"
:
"ClassificationCost"
,
"weight"
:
1.0
},
{
"type"
:
"BBoxL1Cost"
,
"weight"
:
5.0
,
"box_format"
:
"xywh"
},
{
"type"
:
"IoUCost"
,
"iou_mode"
:
"giou"
,
"weight"
:
2.0
}]
}
},
"test_cfg"
:
{
"max_per_img"
:
100
}
}
tests/test_codebase/test_mmdet/data/mask_model.json
0 → 100644
View file @
a17c53b8
{
"type"
:
"MaskRCNN"
,
"backbone"
:
{
"type"
:
"ResNet"
,
"depth"
:
50
,
"num_stages"
:
4
,
"out_indices"
:
[
0
,
1
,
2
,
3
],
"frozen_stages"
:
1
,
"norm_cfg"
:
{
"type"
:
"BN"
,
"requires_grad"
:
true
},
"norm_eval"
:
true
,
"style"
:
"pytorch"
,
"init_cfg"
:
{
"type"
:
"Pretrained"
,
"checkpoint"
:
"torchvision://resnet50"
}
},
"neck"
:
{
"type"
:
"FPN"
,
"in_channels"
:
[
256
,
512
,
1024
,
2048
],
"out_channels"
:
256
,
"num_outs"
:
5
},
"rpn_head"
:
{
"type"
:
"RPNHead"
,
"in_channels"
:
256
,
"feat_channels"
:
256
,
"anchor_generator"
:
{
"type"
:
"AnchorGenerator"
,
"scales"
:
[
8
],
"ratios"
:
[
0.5
,
1.0
,
2.0
],
"strides"
:
[
4
,
8
,
16
,
32
,
64
]
},
"bbox_coder"
:
{
"type"
:
"DeltaXYWHBBoxCoder"
,
"target_means"
:
[
0.0
,
0.0
,
0.0
,
0.0
],
"target_stds"
:
[
1.0
,
1.0
,
1.0
,
1.0
]
},
"loss_cls"
:
{
"type"
:
"CrossEntropyLoss"
,
"use_sigmoid"
:
true
,
"loss_weight"
:
1.0
},
"loss_bbox"
:
{
"type"
:
"L1Loss"
,
"loss_weight"
:
1.0
}
},
"roi_head"
:
{
"type"
:
"StandardRoIHead"
,
"bbox_roi_extractor"
:
{
"type"
:
"SingleRoIExtractor"
,
"roi_layer"
:
{
"type"
:
"RoIAlign"
,
"output_size"
:
7
,
"sampling_ratio"
:
0
},
"out_channels"
:
256
,
"featmap_strides"
:
[
4
,
8
,
16
,
32
]
},
"bbox_head"
:
{
"type"
:
"Shared2FCBBoxHead"
,
"in_channels"
:
256
,
"fc_out_channels"
:
1024
,
"roi_feat_size"
:
7
,
"num_classes"
:
80
,
"bbox_coder"
:
{
"type"
:
"DeltaXYWHBBoxCoder"
,
"target_means"
:
[
0.0
,
0.0
,
0.0
,
0.0
],
"target_stds"
:
[
0.1
,
0.1
,
0.2
,
0.2
]
},
"reg_class_agnostic"
:
false
,
"loss_cls"
:
{
"type"
:
"CrossEntropyLoss"
,
"use_sigmoid"
:
false
,
"loss_weight"
:
1.0
},
"loss_bbox"
:
{
"type"
:
"L1Loss"
,
"loss_weight"
:
1.0
}
},
"mask_roi_extractor"
:
{
"type"
:
"SingleRoIExtractor"
,
"roi_layer"
:
{
"type"
:
"RoIAlign"
,
"output_size"
:
14
,
"sampling_ratio"
:
0
},
"out_channels"
:
256
,
"featmap_strides"
:
[
4
,
8
,
16
,
32
]
},
"mask_head"
:
{
"type"
:
"FCNMaskHead"
,
"num_convs"
:
4
,
"in_channels"
:
256
,
"conv_out_channels"
:
256
,
"num_classes"
:
80
,
"loss_mask"
:
{
"type"
:
"CrossEntropyLoss"
,
"use_mask"
:
true
,
"loss_weight"
:
1.0
}
}
},
"train_cfg"
:
{
"rpn"
:
{
"assigner"
:
{
"type"
:
"MaxIoUAssigner"
,
"pos_iou_thr"
:
0.7
,
"neg_iou_thr"
:
0.3
,
"min_pos_iou"
:
0.3
,
"match_low_quality"
:
true
,
"ignore_iof_thr"
:
-1
},
"sampler"
:
{
"type"
:
"RandomSampler"
,
"num"
:
256
,
"pos_fraction"
:
0.5
,
"neg_pos_ub"
:
-1
,
"add_gt_as_proposals"
:
false
},
"allowed_border"
:
-1
,
"pos_weight"
:
-1
,
"debug"
:
false
},
"rpn_proposal"
:
{
"nms_pre"
:
2000
,
"max_per_img"
:
1000
,
"nms"
:
{
"type"
:
"nms"
,
"iou_threshold"
:
0.7
},
"min_bbox_size"
:
0
},
"rcnn"
:
{
"assigner"
:
{
"type"
:
"MaxIoUAssigner"
,
"pos_iou_thr"
:
0.5
,
"neg_iou_thr"
:
0.5
,
"min_pos_iou"
:
0.5
,
"match_low_quality"
:
true
,
"ignore_iof_thr"
:
-1
},
"sampler"
:
{
"type"
:
"RandomSampler"
,
"num"
:
512
,
"pos_fraction"
:
0.25
,
"neg_pos_ub"
:
-1
,
"add_gt_as_proposals"
:
true
},
"mask_size"
:
28
,
"pos_weight"
:
-1
,
"debug"
:
false
}
},
"test_cfg"
:
{
"rpn"
:
{
"nms_pre"
:
1000
,
"max_per_img"
:
1000
,
"nms"
:
{
"type"
:
"nms"
,
"iou_threshold"
:
0.7
},
"min_bbox_size"
:
0
},
"rcnn"
:
{
"score_thr"
:
0.05
,
"nms"
:
{
"type"
:
"nms"
,
"iou_threshold"
:
0.5
},
"max_per_img"
:
100
,
"mask_thr_binary"
:
0.5
}
}
}
tests/test_codebase/test_mmdet/data/mmrazor_model.py
0 → 100644
View file @
a17c53b8
# Copyright (c) OpenMMLab. All rights reserved.
_base_
=
'model.py'
norm_cfg
=
dict
(
type
=
'BN'
,
requires_grad
=
True
)
mutator
=
dict
(
type
=
'OneShotMutator'
,
placeholder_mapping
=
dict
(
all_blocks
=
dict
(
type
=
'OneShotOP'
,
choices
=
dict
(
shuffle_3x3
=
dict
(
type
=
'ShuffleBlock'
,
norm_cfg
=
norm_cfg
,
kernel_size
=
3
),
shuffle_5x5
=
dict
(
type
=
'ShuffleBlock'
,
norm_cfg
=
norm_cfg
,
kernel_size
=
5
),
shuffle_7x7
=
dict
(
type
=
'ShuffleBlock'
,
norm_cfg
=
norm_cfg
,
kernel_size
=
7
),
shuffle_xception
=
dict
(
type
=
'ShuffleXception'
,
norm_cfg
=
norm_cfg
,
),
))))
algorithm
=
dict
(
type
=
'DetNAS'
,
architecture
=
dict
(
type
=
'MMDetArchitecture'
,
model
=
{{
_base_
.
model
}},
),
mutator
=
mutator
,
pruner
=
None
,
distiller
=
None
,
retraining
=
True
,
mutable_cfg
=
'tests/test_codebase/test_mmdet/data/mmrazor_mutable_cfg.yaml'
,
)
tests/test_codebase/test_mmdet/data/mmrazor_mutable_cfg.yaml
0 → 100644
View file @
a17c53b8
stage_0_block_0
:
chosen
:
-
shuffle_7x7
stage_0_block_1
:
chosen
:
-
shuffle_5x5
stage_0_block_2
:
chosen
:
-
shuffle_7x7
stage_0_block_3
:
chosen
:
-
shuffle_3x3
stage_1_block_0
:
chosen
:
-
shuffle_7x7
stage_1_block_1
:
chosen
:
-
shuffle_5x5
stage_1_block_2
:
chosen
:
-
shuffle_5x5
stage_1_block_3
:
chosen
:
-
shuffle_7x7
stage_2_block_0
:
chosen
:
-
shuffle_xception
stage_2_block_1
:
chosen
:
-
shuffle_xception
stage_2_block_2
:
chosen
:
-
shuffle_5x5
stage_2_block_3
:
chosen
:
-
shuffle_xception
stage_2_block_4
:
chosen
:
-
shuffle_3x3
stage_2_block_5
:
chosen
:
-
shuffle_3x3
stage_2_block_6
:
chosen
:
-
shuffle_xception
stage_2_block_7
:
chosen
:
-
shuffle_5x5
stage_3_block_0
:
chosen
:
-
shuffle_xception
stage_3_block_1
:
chosen
:
-
shuffle_5x5
stage_3_block_2
:
chosen
:
-
shuffle_xception
stage_3_block_3
:
chosen
:
-
shuffle_7x7
tests/test_codebase/test_mmdet/data/model.py
0 → 100644
View file @
a17c53b8
# Copyright (c) OpenMMLab. All rights reserved.
# model settings
data_preprocessor
=
dict
(
type
=
'DetDataPreprocessor'
,
mean
=
[
123.675
,
116.28
,
103.53
],
std
=
[
58.395
,
57.12
,
57.375
],
bgr_to_rgb
=
True
,
pad_size_divisor
=
32
)
model
=
dict
(
type
=
'YOLOV3'
,
data_preprocessor
=
data_preprocessor
,
backbone
=
dict
(
type
=
'MobileNetV2'
,
out_indices
=
(
2
,
4
,
6
),
act_cfg
=
dict
(
type
=
'LeakyReLU'
,
negative_slope
=
0.1
),
init_cfg
=
dict
(
type
=
'Pretrained'
,
checkpoint
=
'open-mmlab://mmdet/mobilenet_v2'
)),
neck
=
dict
(
type
=
'YOLOV3Neck'
,
num_scales
=
3
,
in_channels
=
[
320
,
96
,
32
],
out_channels
=
[
96
,
96
,
96
]),
bbox_head
=
dict
(
type
=
'YOLOV3Head'
,
num_classes
=
80
,
in_channels
=
[
96
,
96
,
96
],
out_channels
=
[
96
,
96
,
96
],
anchor_generator
=
dict
(
type
=
'YOLOAnchorGenerator'
,
base_sizes
=
[[(
116
,
90
),
(
156
,
198
),
(
373
,
326
)],
[(
30
,
61
),
(
62
,
45
),
(
59
,
119
)],
[(
10
,
13
),
(
16
,
30
),
(
33
,
23
)]],
strides
=
[
32
,
16
,
8
]),
bbox_coder
=
dict
(
type
=
'YOLOBBoxCoder'
),
featmap_strides
=
[
32
,
16
,
8
],
loss_cls
=
dict
(
type
=
'CrossEntropyLoss'
,
use_sigmoid
=
True
,
loss_weight
=
1.0
,
reduction
=
'sum'
),
loss_conf
=
dict
(
type
=
'CrossEntropyLoss'
,
use_sigmoid
=
True
,
loss_weight
=
1.0
,
reduction
=
'sum'
),
loss_xy
=
dict
(
type
=
'CrossEntropyLoss'
,
use_sigmoid
=
True
,
loss_weight
=
2.0
,
reduction
=
'sum'
),
loss_wh
=
dict
(
type
=
'MSELoss'
,
loss_weight
=
2.0
,
reduction
=
'sum'
)),
test_cfg
=
dict
(
nms_pre
=
1000
,
min_bbox_size
=
0
,
score_thr
=
0.05
,
conf_thr
=
0.005
,
nms
=
dict
(
type
=
'nms'
,
iou_threshold
=
0.45
),
max_per_img
=
100
))
# dataset settings
dataset_type
=
'CocoDataset'
data_root
=
'data/coco/'
file_client_args
=
dict
(
backend
=
'disk'
)
test_pipeline
=
[
dict
(
type
=
'LoadImageFromFile'
,
file_client_args
=
file_client_args
),
dict
(
type
=
'Resize'
,
scale
=
(
416
,
416
),
keep_ratio
=
False
),
dict
(
type
=
'PackDetInputs'
,
meta_keys
=
(
'img_id'
,
'img_path'
,
'ori_shape'
,
'img_shape'
,
'scale_factor'
))
]
val_dataloader
=
dict
(
batch_size
=
24
,
num_workers
=
4
,
persistent_workers
=
True
,
drop_last
=
False
,
sampler
=
dict
(
type
=
'DefaultSampler'
,
shuffle
=
False
),
dataset
=
dict
(
type
=
dataset_type
,
data_root
=
data_root
,
ann_file
=
'annotations/instances_val2017.json'
,
data_prefix
=
dict
(
img
=
'val2017/'
),
test_mode
=
True
,
pipeline
=
test_pipeline
))
test_dataloader
=
val_dataloader
val_evaluator
=
dict
(
type
=
'CocoMetric'
,
ann_file
=
data_root
+
'annotations/instances_val2017.json'
,
metric
=
'bbox'
)
test_evaluator
=
val_evaluator
val_cfg
=
dict
(
type
=
'ValLoop'
)
test_cfg
=
dict
(
type
=
'TestLoop'
)
# learning rate
param_scheduler
=
[
dict
(
type
=
'LinearLR'
,
start_factor
=
0.001
,
by_epoch
=
False
,
begin
=
0
,
end
=
500
),
dict
(
type
=
'MultiStepLR'
,
begin
=
0
,
end
=
12
,
by_epoch
=
True
,
milestones
=
[
8
,
11
],
gamma
=
0.1
)
]
# optimizer
optim_wrapper
=
dict
(
type
=
'OptimWrapper'
,
optimizer
=
dict
(
type
=
'SGD'
,
lr
=
0.02
,
momentum
=
0.9
,
weight_decay
=
0.0001
))
default_scope
=
'mmdet'
default_hooks
=
dict
(
timer
=
dict
(
type
=
'IterTimerHook'
),
logger
=
dict
(
type
=
'LoggerHook'
,
interval
=
50
),
param_scheduler
=
dict
(
type
=
'ParamSchedulerHook'
),
checkpoint
=
dict
(
type
=
'CheckpointHook'
,
interval
=
1
),
sampler_seed
=
dict
(
type
=
'DistSamplerSeedHook'
),
visualization
=
dict
(
type
=
'DetVisualizationHook'
))
env_cfg
=
dict
(
cudnn_benchmark
=
False
,
mp_cfg
=
dict
(
mp_start_method
=
'fork'
,
opencv_num_threads
=
0
),
dist_cfg
=
dict
(
backend
=
'nccl'
),
)
vis_backends
=
[
dict
(
type
=
'LocalVisBackend'
)]
visualizer
=
dict
(
type
=
'DetLocalVisualizer'
,
vis_backends
=
vis_backends
,
name
=
'visualizer'
)
log_processor
=
dict
(
type
=
'LogProcessor'
,
window_size
=
50
,
by_epoch
=
True
)
log_level
=
'INFO'
load_from
=
None
resume
=
False
tests/test_codebase/test_mmdet/data/single_stage_model.json
0 → 100644
View file @
a17c53b8
{
"type"
:
"RetinaNet"
,
"backbone"
:
{
"type"
:
"ResNet"
,
"depth"
:
50
,
"num_stages"
:
4
,
"out_indices"
:
[
0
,
1
,
2
,
3
],
"frozen_stages"
:
1
,
"norm_cfg"
:
{
"type"
:
"BN"
,
"requires_grad"
:
true
},
"norm_eval"
:
true
,
"style"
:
"pytorch"
,
"init_cfg"
:
{
"type"
:
"Pretrained"
,
"checkpoint"
:
"torchvision://resnet50"
}
},
"neck"
:
{
"type"
:
"FPN"
,
"in_channels"
:
[
256
,
512
,
1024
,
2048
],
"out_channels"
:
256
,
"start_level"
:
1
,
"add_extra_convs"
:
"on_input"
,
"num_outs"
:
5
},
"bbox_head"
:
{
"type"
:
"RetinaHead"
,
"num_classes"
:
80
,
"in_channels"
:
256
,
"stacked_convs"
:
4
,
"feat_channels"
:
256
,
"anchor_generator"
:
{
"type"
:
"AnchorGenerator"
,
"octave_base_scale"
:
4
,
"scales_per_octave"
:
3
,
"ratios"
:
[
0.5
,
1.0
,
2.0
],
"strides"
:
[
8
,
16
,
32
,
64
,
128
]
},
"bbox_coder"
:
{
"type"
:
"DeltaXYWHBBoxCoder"
,
"target_means"
:
[
0.0
,
0.0
,
0.0
,
0.0
],
"target_stds"
:
[
1.0
,
1.0
,
1.0
,
1.0
]
},
"loss_cls"
:
{
"type"
:
"FocalLoss"
,
"use_sigmoid"
:
true
,
"gamma"
:
2.0
,
"alpha"
:
0.25
,
"loss_weight"
:
1.0
},
"loss_bbox"
:
{
"type"
:
"L1Loss"
,
"loss_weight"
:
1.0
}
},
"train_cfg"
:
{
"assigner"
:
{
"type"
:
"MaxIoUAssigner"
,
"pos_iou_thr"
:
0.5
,
"neg_iou_thr"
:
0.4
,
"min_pos_iou"
:
0
,
"ignore_iof_thr"
:
-1
},
"allowed_border"
:
-1
,
"pos_weight"
:
-1
,
"debug"
:
false
},
"test_cfg"
:
{
"nms_pre"
:
1000
,
"min_bbox_size"
:
0
,
"score_thr"
:
0.05
,
"nms"
:
{
"type"
:
"nms"
,
"iou_threshold"
:
0.5
},
"max_per_img"
:
100
}
}
tests/test_codebase/test_mmdet/test_mmdet_models.py
0 → 100644
View file @
a17c53b8
This diff is collapsed.
Click to expand it.
tests/test_codebase/test_mmdet/test_mmdet_structures.py
0 → 100644
View file @
a17c53b8
# Copyright (c) OpenMMLab. All rights reserved.
import
numpy
as
np
import
pytest
import
torch
from
mmengine
import
Config
from
mmdeploy.codebase
import
import_codebase
from
mmdeploy.utils
import
Backend
,
Codebase
from
mmdeploy.utils.test
import
(
WrapFunction
,
check_backend
,
get_rewrite_outputs
)
try
:
import_codebase
(
Codebase
.
MMDET
)
except
ImportError
:
pytest
.
skip
(
f
'
{
Codebase
.
MMDET
}
is not installed.'
,
allow_module_level
=
True
)
@
pytest
.
mark
.
parametrize
(
'backend_type'
,
[
Backend
.
ONNXRUNTIME
])
def
test_distance2bbox
(
backend_type
:
Backend
):
check_backend
(
backend_type
)
deploy_cfg
=
Config
(
dict
(
onnx_config
=
dict
(
output_names
=
None
,
input_shape
=
None
),
backend_config
=
dict
(
type
=
backend_type
.
value
,
model_inputs
=
None
),
codebase_config
=
dict
(
type
=
'mmdet'
,
task
=
'ObjectDetection'
)))
# wrap function to enable rewrite
def
distance2bbox
(
*
args
,
**
kwargs
):
import
mmdet.structures.bbox.transforms
return
mmdet
.
structures
.
bbox
.
transforms
.
distance2bbox
(
*
args
,
**
kwargs
)
points
=
torch
.
rand
(
3
,
2
)
distance
=
torch
.
rand
(
3
,
4
)
original_outputs
=
distance2bbox
(
points
,
distance
)
# wrap function to nn.Module, enable torch.onnx.export
wrapped_func
=
WrapFunction
(
distance2bbox
)
rewrite_outputs
,
is_backend_output
=
get_rewrite_outputs
(
wrapped_func
,
model_inputs
=
{
'points'
:
points
,
'distance'
:
distance
},
deploy_cfg
=
deploy_cfg
)
if
is_backend_output
:
model_output
=
original_outputs
.
squeeze
().
cpu
().
numpy
()
rewrite_output
=
rewrite_outputs
[
0
].
squeeze
()
assert
np
.
allclose
(
model_output
,
rewrite_output
,
rtol
=
1e-03
,
atol
=
1e-05
)
else
:
assert
rewrite_outputs
is
not
None
tests/test_codebase/test_mmdet/test_mmdet_utils.py
0 → 100644
View file @
a17c53b8
# Copyright (c) OpenMMLab. All rights reserved.
import
numpy
as
np
import
pytest
import
torch
from
mmengine
import
Config
from
mmdeploy.codebase
import
import_codebase
from
mmdeploy.utils
import
Codebase
try
:
import_codebase
(
Codebase
.
MMDET
)
except
ImportError
:
pytest
.
skip
(
f
'
{
Codebase
.
MMDET
}
is not installed.'
,
allow_module_level
=
True
)
from
mmdeploy.codebase.mmdet.deploy
import
(
clip_bboxes
,
get_post_processing_params
,
pad_with_value
,
pad_with_value_if_necessary
)
def
test_clip_bboxes
():
x1
=
torch
.
rand
(
3
,
2
)
*
224
y1
=
torch
.
rand
(
3
,
2
)
*
224
x2
=
x1
*
2
y2
=
y1
*
2
outs
=
clip_bboxes
(
x1
,
y1
,
x2
,
y2
,
[
224
,
224
])
for
out
in
outs
:
assert
int
(
out
.
max
())
<=
224
def
test_pad_with_value
():
x
=
torch
.
rand
(
3
,
2
)
padded_x
=
pad_with_value
(
x
,
pad_dim
=
1
,
pad_size
=
4
,
pad_value
=
0
)
assert
np
.
allclose
(
padded_x
.
shape
,
torch
.
Size
([
3
,
6
]),
rtol
=
1e-03
,
atol
=
1e-05
)
assert
np
.
allclose
(
padded_x
.
sum
(),
x
.
sum
(),
rtol
=
1e-03
,
atol
=
1e-05
)
def
test_pad_with_value_if_necessary
():
x
=
torch
.
rand
(
3
,
2
)
padded_x
=
pad_with_value_if_necessary
(
x
,
pad_dim
=
1
,
pad_size
=
4
,
pad_value
=
0
)
assert
np
.
allclose
(
padded_x
.
shape
,
torch
.
Size
([
3
,
2
]),
rtol
=
1e-03
,
atol
=
1e-05
)
assert
np
.
allclose
(
padded_x
.
sum
(),
x
.
sum
(),
rtol
=
1e-03
,
atol
=
1e-05
)
config_with_mmdet_params
=
Config
(
dict
(
codebase_config
=
dict
(
type
=
'mmdet'
,
task
=
'ObjectDetection'
,
post_processing
=
dict
(
score_threshold
=
0.05
,
iou_threshold
=
0.5
,
max_output_boxes_per_class
=
200
,
pre_top_k
=-
1
,
keep_top_k
=
100
,
background_label_id
=-
1
,
))))
def
test_get_mmdet_params
():
assert
get_post_processing_params
(
config_with_mmdet_params
)
==
dict
(
score_threshold
=
0.05
,
iou_threshold
=
0.5
,
max_output_boxes_per_class
=
200
,
pre_top_k
=-
1
,
keep_top_k
=
100
,
background_label_id
=-
1
)
def
test_get_topk_from_heatmap
():
from
mmdet.models.utils.gaussian_target
import
get_topk_from_heatmap
from
mmdeploy.codebase.mmdet.models.utils.gaussian_target
import
\
get_topk_from_heatmap__default
scores
=
torch
.
rand
(
1
,
2
,
4
,
4
)
gts
=
get_topk_from_heatmap
(
scores
,
k
=
20
)
outs
=
get_topk_from_heatmap__default
(
scores
,
k
=
20
)
for
gt
,
out
in
zip
(
gts
,
outs
):
torch
.
testing
.
assert_allclose
(
gt
,
out
)
tests/test_codebase/test_mmdet/test_object_detection.py
0 → 100644
View file @
a17c53b8
# Copyright (c) OpenMMLab. All rights reserved.
import
copy
import
os
from
tempfile
import
NamedTemporaryFile
,
TemporaryDirectory
from
typing
import
Any
import
numpy
as
np
import
pytest
import
torch
from
mmengine
import
Config
from
torch.utils.data
import
DataLoader
from
torch.utils.data.dataset
import
Dataset
import
mmdeploy.backend.onnxruntime
as
ort_apis
from
mmdeploy.apis
import
build_task_processor
from
mmdeploy.codebase
import
import_codebase
from
mmdeploy.utils
import
Codebase
,
load_config
from
mmdeploy.utils.test
import
DummyModel
,
SwitchBackendWrapper
try
:
import_codebase
(
Codebase
.
MMDET
)
except
ImportError
:
pytest
.
skip
(
f
'
{
Codebase
.
MMDET
}
is not installed.'
,
allow_module_level
=
True
)
model_cfg_path
=
'tests/test_codebase/test_mmdet/data/model.py'
model_cfg
=
load_config
(
model_cfg_path
)[
0
]
model_cfg
.
test_dataloader
.
dataset
.
data_root
=
\
'tests/test_codebase/test_mmdet/data'
model_cfg
.
test_dataloader
.
dataset
.
ann_file
=
'coco_sample.json'
model_cfg
.
test_evaluator
.
ann_file
=
\
'tests/test_codebase/test_mmdet/data/coco_sample.json'
deploy_cfg
=
Config
(
dict
(
backend_config
=
dict
(
type
=
'onnxruntime'
),
codebase_config
=
dict
(
type
=
'mmdet'
,
task
=
'ObjectDetection'
,
post_processing
=
dict
(
score_threshold
=
0.05
,
confidence_threshold
=
0.005
,
# for YOLOv3
iou_threshold
=
0.5
,
max_output_boxes_per_class
=
200
,
pre_top_k
=
5000
,
keep_top_k
=
100
,
background_label_id
=-
1
,
)),
onnx_config
=
dict
(
type
=
'onnx'
,
export_params
=
True
,
keep_initializers_as_inputs
=
False
,
opset_version
=
11
,
input_shape
=
None
,
input_names
=
[
'input'
],
output_names
=
[
'dets'
,
'labels'
])))
onnx_file
=
NamedTemporaryFile
(
suffix
=
'.onnx'
).
name
task_processor
=
None
img_shape
=
(
32
,
32
)
img
=
np
.
random
.
rand
(
*
img_shape
,
3
)
@
pytest
.
fixture
(
autouse
=
True
)
def
init_task_processor
():
global
task_processor
task_processor
=
build_task_processor
(
model_cfg
,
deploy_cfg
,
'cpu'
)
def
test_build_test_runner
():
# Prepare dummy model
from
mmdet.structures
import
DetDataSample
from
mmengine.structures
import
InstanceData
data_sample
=
DetDataSample
()
img_meta
=
dict
(
img_shape
=
(
800
,
1216
,
3
))
gt_instances
=
InstanceData
(
metainfo
=
img_meta
)
gt_instances
.
bboxes
=
torch
.
rand
((
5
,
4
))
gt_instances
.
labels
=
torch
.
rand
((
5
,
))
data_sample
.
gt_instances
=
gt_instances
pred_instances
=
InstanceData
(
metainfo
=
img_meta
)
pred_instances
.
bboxes
=
torch
.
rand
((
5
,
4
))
pred_instances
.
scores
=
torch
.
rand
((
5
,
))
pred_instances
.
labels
=
torch
.
randint
(
0
,
10
,
(
5
,
))
data_sample
.
pred_instances
=
pred_instances
data_sample
.
img_id
=
139
data_sample
.
ori_shape
=
(
800
,
1216
)
outputs
=
[
data_sample
]
model
=
DummyModel
(
outputs
=
outputs
)
assert
model
is
not
None
# Run test
with
TemporaryDirectory
()
as
dir
:
runner
=
task_processor
.
build_test_runner
(
model
,
dir
)
assert
runner
is
not
None
@
pytest
.
mark
.
parametrize
(
'from_mmrazor'
,
[
True
,
False
,
'123'
,
0
])
def
test_build_pytorch_model
(
from_mmrazor
:
Any
):
from
mmdet.models
import
BaseDetector
if
from_mmrazor
is
False
:
_task_processor
=
task_processor
else
:
_model_cfg_path
=
'tests/test_codebase/test_mmdet/data/'
\
'mmrazor_model.py'
_model_cfg
=
load_config
(
_model_cfg_path
)[
0
]
_model_cfg
.
algorithm
.
architecture
.
model
.
type
=
'mmdet.YOLOV3'
_model_cfg
.
algorithm
.
architecture
.
model
.
backbone
.
type
=
\
'mmpretrain.SearchableShuffleNetV2'
_deploy_cfg
=
copy
.
deepcopy
(
deploy_cfg
)
_deploy_cfg
.
codebase_config
[
'from_mmrazor'
]
=
from_mmrazor
_task_processor
=
build_task_processor
(
_model_cfg
,
_deploy_cfg
,
'cpu'
)
if
not
isinstance
(
from_mmrazor
,
bool
):
with
pytest
.
raises
(
TypeError
,
match
=
'`from_mmrazor` attribute must be '
'boolean type! '
f
'but got:
{
from_mmrazor
}
'
):
_
=
_task_processor
.
from_mmrazor
return
assert
from_mmrazor
==
_task_processor
.
from_mmrazor
if
from_mmrazor
:
pytest
.
importorskip
(
'mmrazor'
,
reason
=
'mmrazor is not installed.'
)
model
=
_task_processor
.
build_pytorch_model
(
None
)
assert
isinstance
(
model
,
BaseDetector
)
@
pytest
.
fixture
def
backend_model
():
from
mmdeploy.backend.onnxruntime
import
ORTWrapper
ort_apis
.
__dict__
.
update
({
'ORTWrapper'
:
ORTWrapper
})
wrapper
=
SwitchBackendWrapper
(
ORTWrapper
)
wrapper
.
set
(
outputs
=
{
'dets'
:
torch
.
rand
(
1
,
10
,
5
).
sort
(
2
).
values
,
'labels'
:
torch
.
randint
(
0
,
10
,
(
1
,
10
))
})
yield
task_processor
.
build_backend_model
([
''
])
wrapper
.
recover
()
def
test_build_backend_model
(
backend_model
):
from
mmdeploy.codebase.mmdet.deploy.object_detection_model
import
\
End2EndModel
assert
isinstance
(
backend_model
,
End2EndModel
)
def
test_can_postprocess_masks
():
from
mmdeploy.codebase.mmdet.deploy.object_detection_model
import
\
End2EndModel
num_dets
=
[
0
,
1
,
5
]
for
num_det
in
num_dets
:
det_bboxes
=
np
.
random
.
randn
(
num_det
,
4
)
det_masks
=
np
.
random
.
randn
(
num_det
,
28
,
28
)
img_w
,
img_h
=
(
30
,
40
)
masks
=
End2EndModel
.
postprocessing_masks
(
det_bboxes
,
det_masks
,
img_w
,
img_h
)
expected_shape
=
(
num_det
,
img_h
,
img_w
)
actual_shape
=
masks
.
shape
assert
actual_shape
==
expected_shape
,
\
f
'The expected shape of masks
{
expected_shape
}
'
\
f
'did not match actual shape
{
actual_shape
}
.'
@
pytest
.
mark
.
parametrize
(
'device'
,
[
'cpu'
,
'cuda:0'
])
def
test_create_input
(
device
):
if
device
==
'cuda:0'
and
not
torch
.
cuda
.
is_available
():
pytest
.
skip
(
'cuda is not available'
)
original_device
=
task_processor
.
device
task_processor
.
device
=
device
inputs
=
task_processor
.
create_input
(
img
,
input_shape
=
img_shape
)
assert
len
(
inputs
)
==
2
task_processor
.
device
=
original_device
def
test_visualize
(
backend_model
):
input_dict
,
_
=
task_processor
.
create_input
(
img
,
input_shape
=
img_shape
)
results
=
backend_model
.
test_step
(
input_dict
)[
0
]
with
TemporaryDirectory
()
as
dir
:
filename
=
dir
+
'tmp.jpg'
task_processor
.
visualize
(
img
,
results
,
filename
,
'window'
)
assert
os
.
path
.
exists
(
filename
)
@
pytest
.
mark
.
parametrize
(
'partition_type'
,
[
'single_stage'
,
'two_stage'
])
# Currently only mmdet implements get_partition_cfg
def
test_get_partition_cfg
(
partition_type
):
from
mmdeploy.codebase.mmdet.deploy.model_partition_cfg
import
\
MMDET_PARTITION_CFG
partition_cfg
=
task_processor
.
get_partition_cfg
(
partition_type
=
partition_type
)
assert
partition_cfg
==
MMDET_PARTITION_CFG
[
partition_type
]
def
test_get_tensor_from_input
():
input_data
=
{
'inputs'
:
torch
.
ones
(
3
,
4
,
5
)}
inputs
=
task_processor
.
get_tensor_from_input
(
input_data
)
assert
torch
.
equal
(
inputs
,
torch
.
ones
(
3
,
4
,
5
))
def
test_build_dataset_and_dataloader
():
dataset
=
task_processor
.
build_dataset
(
dataset_cfg
=
model_cfg
.
test_dataloader
.
dataset
)
assert
isinstance
(
dataset
,
Dataset
),
'Failed to build dataset'
dataloader_cfg
=
task_processor
.
model_cfg
.
test_dataloader
dataloader
=
task_processor
.
build_dataloader
(
dataloader_cfg
)
assert
isinstance
(
dataloader
,
DataLoader
),
'Failed to build dataloader'
tests/test_codebase/test_mmdet/test_object_detection_model.py
0 → 100644
View file @
a17c53b8
# Copyright (c) OpenMMLab. All rights reserved.
from
typing
import
Sequence
import
pytest
import
torch
from
mmengine
import
Config
from
mmengine.structures
import
BaseDataElement
,
InstanceData
import
mmdeploy.backend.ncnn
as
ncnn_apis
import
mmdeploy.backend.onnxruntime
as
ort_apis
from
mmdeploy.codebase
import
import_codebase
from
mmdeploy.utils
import
Backend
,
Codebase
from
mmdeploy.utils.test
import
SwitchBackendWrapper
,
backend_checker
try
:
import_codebase
(
Codebase
.
MMDET
)
except
ImportError
:
pytest
.
skip
(
f
'
{
Codebase
.
MMDET
}
is not installed.'
,
allow_module_level
=
True
)
from
mmdeploy.codebase.mmdet.deploy.object_detection_model
import
End2EndModel
def
assert_det_results
(
results
,
module_name
:
str
=
'model'
):
assert
results
is
not
None
,
f
'failed to get output using
{
module_name
}
'
assert
isinstance
(
results
,
Sequence
)
assert
len
(
results
)
==
2
assert
results
[
0
].
shape
[
0
]
==
results
[
1
].
shape
[
0
]
assert
results
[
0
].
shape
[
1
]
==
results
[
1
].
shape
[
1
]
def
assert_forward_results
(
results
,
module_name
:
str
=
'model'
):
assert
results
is
not
None
,
f
'failed to get output using
{
module_name
}
'
assert
isinstance
(
results
,
Sequence
)
assert
len
(
results
)
==
1
assert
isinstance
(
results
[
0
].
pred_instances
,
InstanceData
)
assert
results
[
0
].
pred_instances
.
bboxes
.
shape
[
-
1
]
==
4
assert
results
[
0
].
pred_instances
.
scores
.
shape
[
0
]
==
\
results
[
0
].
pred_instances
.
labels
.
shape
[
0
]
==
\
results
[
0
].
pred_instances
.
bboxes
.
shape
[
0
]
@
backend_checker
(
Backend
.
ONNXRUNTIME
)
class
TestEnd2EndModel
:
@
classmethod
def
setup_class
(
cls
):
# force add backend wrapper regardless of plugins
# make sure ONNXRuntimeDetector can use ORTWrapper inside itself
from
mmdeploy.backend.onnxruntime
import
ORTWrapper
ort_apis
.
__dict__
.
update
({
'ORTWrapper'
:
ORTWrapper
})
# simplify backend inference
cls
.
wrapper
=
SwitchBackendWrapper
(
ORTWrapper
)
cls
.
outputs
=
{
'dets'
:
torch
.
rand
(
1
,
10
,
5
),
'labels'
:
torch
.
rand
(
1
,
10
)
}
cls
.
wrapper
.
set
(
outputs
=
cls
.
outputs
)
deploy_cfg
=
Config
(
{
'onnx_config'
:
{
'output_names'
:
[
'dets'
,
'labels'
]
}})
from
mmdeploy.codebase.mmdet.deploy.object_detection_model
import
\
End2EndModel
cls
.
end2end_model
=
End2EndModel
(
Backend
.
ONNXRUNTIME
,
[
''
],
'cpu'
,
deploy_cfg
)
@
classmethod
def
teardown_class
(
cls
):
cls
.
wrapper
.
recover
()
def
test_forward
(
self
):
imgs
=
torch
.
rand
(
1
,
3
,
64
,
64
)
img_metas
=
[
BaseDataElement
(
metainfo
=
{
'img_shape'
:
[
64
,
64
],
'scale_factor'
:
[
1
,
1
]
})
]
results
=
self
.
end2end_model
.
forward
(
imgs
,
img_metas
)
assert_forward_results
(
results
,
'End2EndModel'
)
def
test_predict
(
self
):
imgs
=
torch
.
rand
(
1
,
3
,
64
,
64
)
dets
,
labels
=
self
.
end2end_model
.
predict
(
imgs
)
assert
dets
.
shape
[
-
1
]
==
5
assert
labels
.
shape
[
0
]
==
dets
.
shape
[
0
]
@
backend_checker
(
Backend
.
ONNXRUNTIME
)
class
TestMaskEnd2EndModel
:
@
classmethod
def
setup_class
(
cls
):
# force add backend wrapper regardless of plugins
# make sure ONNXRuntimeDetector can use ORTWrapper inside itself
from
mmdeploy.backend.onnxruntime
import
ORTWrapper
ort_apis
.
__dict__
.
update
({
'ORTWrapper'
:
ORTWrapper
})
# simplify backend inference
num_classes
=
80
num_dets
=
10
cls
.
wrapper
=
SwitchBackendWrapper
(
ORTWrapper
)
cls
.
outputs
=
{
'dets'
:
torch
.
rand
(
1
,
num_dets
,
5
),
'labels'
:
torch
.
randint
(
num_classes
,
(
1
,
num_dets
)),
'masks'
:
torch
.
rand
(
1
,
num_dets
,
28
,
28
)
}
cls
.
wrapper
.
set
(
outputs
=
cls
.
outputs
)
deploy_cfg
=
Config
({
'onnx_config'
:
{
'output_names'
:
[
'dets'
,
'labels'
,
'masks'
]
},
'codebase_config'
:
{
'post_processing'
:
{
'export_postprocess_mask'
:
False
}
}
})
from
mmdeploy.codebase.mmdet.deploy.object_detection_model
import
\
End2EndModel
cls
.
end2end_model
=
End2EndModel
(
Backend
.
ONNXRUNTIME
,
[
''
],
'cpu'
,
deploy_cfg
)
@
classmethod
def
teardown_class
(
cls
):
cls
.
wrapper
.
recover
()
def
test_forward
(
self
):
imgs
=
torch
.
rand
(
1
,
3
,
64
,
64
)
img_metas
=
[
BaseDataElement
(
metainfo
=
{
'img_shape'
:
[
64
,
64
],
'ori_shape'
:
[
32
,
32
],
'scale_factor'
:
[
1
,
1
]
})
]
results
=
self
.
end2end_model
.
forward
(
imgs
,
img_metas
)
assert_forward_results
(
results
,
'mask End2EndModel'
)
def
get_test_cfg_and_post_processing
():
test_cfg
=
{
'nms_pre'
:
100
,
'min_bbox_size'
:
0
,
'score_thr'
:
0.05
,
'nms'
:
{
'type'
:
'nms'
,
'iou_threshold'
:
0.5
},
'max_per_img'
:
10
}
post_processing
=
{
'score_threshold'
:
0.05
,
'iou_threshold'
:
0.5
,
'max_output_boxes_per_class'
:
20
,
'pre_top_k'
:
-
1
,
'keep_top_k'
:
10
,
'background_label_id'
:
-
1
}
return
test_cfg
,
post_processing
def
prepare_model_deploy_cfgs
():
test_cfg
,
post_processing
=
get_test_cfg_and_post_processing
()
bbox_roi_extractor
=
{
'type'
:
'SingleRoIExtractor'
,
'roi_layer'
:
{
'type'
:
'RoIAlign'
,
'output_size'
:
7
,
'sampling_ratio'
:
0
},
'out_channels'
:
8
,
'featmap_strides'
:
[
4
]
}
bbox_head
=
{
'type'
:
'Shared2FCBBoxHead'
,
'in_channels'
:
8
,
'fc_out_channels'
:
1024
,
'roi_feat_size'
:
7
,
'num_classes'
:
80
,
'bbox_coder'
:
{
'type'
:
'DeltaXYWHBBoxCoder'
,
'target_means'
:
[
0.0
,
0.0
,
0.0
,
0.0
],
'target_stds'
:
[
0.1
,
0.1
,
0.2
,
0.2
]
},
'reg_class_agnostic'
:
False
,
'loss_cls'
:
{
'type'
:
'CrossEntropyLoss'
,
'use_sigmoid'
:
False
,
'loss_weight'
:
1.0
},
'loss_bbox'
:
{
'type'
:
'L1Loss'
,
'loss_weight'
:
1.0
}
}
roi_head
=
dict
(
bbox_roi_extractor
=
bbox_roi_extractor
,
bbox_head
=
bbox_head
)
model_cfg
=
Config
(
dict
(
model
=
dict
(
neck
=
dict
(
num_outs
=
0
),
test_cfg
=
dict
(
rpn
=
test_cfg
,
rcnn
=
test_cfg
),
roi_head
=
roi_head
)))
deploy_cfg
=
Config
(
dict
(
codebase_config
=
dict
(
post_processing
=
post_processing
)))
return
model_cfg
,
deploy_cfg
class
DummyWrapper
(
torch
.
nn
.
Module
):
def
__init__
(
self
,
outputs
):
self
.
outputs
=
outputs
def
__call__
(
self
,
*
arg
,
**
kwargs
):
return
0
def
output_to_list
(
self
,
*
arg
,
**
kwargs
):
return
self
.
outputs
@
backend_checker
(
Backend
.
ONNXRUNTIME
)
@
pytest
.
mark
.
parametrize
(
'partition_type'
,
[
None
,
'end2end'
])
def
test_build_object_detection_model
(
partition_type
):
_
,
post_processing
=
get_test_cfg_and_post_processing
()
model_cfg
=
Config
(
dict
(
data
=
dict
(
test
=
{
'type'
:
'CocoDataset'
})))
deploy_cfg
=
Config
(
dict
(
backend_config
=
dict
(
type
=
'onnxruntime'
),
onnx_config
=
dict
(
output_names
=
[
'dets'
,
'labels'
]),
codebase_config
=
dict
(
type
=
'mmdet'
,
post_processing
=
post_processing
)))
if
partition_type
:
deploy_cfg
.
partition_config
=
dict
(
apply_marks
=
True
,
type
=
partition_type
,
partition_cfg
=
[
dict
(
output_names
=
[])])
from
mmdeploy.backend.onnxruntime
import
ORTWrapper
ort_apis
.
__dict__
.
update
({
'ORTWrapper'
:
ORTWrapper
})
# simplify backend inference
with
SwitchBackendWrapper
(
ORTWrapper
)
as
wrapper
:
wrapper
.
set
(
model_cfg
=
model_cfg
,
deploy_cfg
=
deploy_cfg
)
from
mmdeploy.codebase.mmdet.deploy.object_detection_model
import
\
build_object_detection_model
detector
=
build_object_detection_model
([
''
],
model_cfg
,
deploy_cfg
,
'cpu'
)
assert
isinstance
(
detector
,
End2EndModel
)
@
backend_checker
(
Backend
.
NCNN
)
class
TestNCNNEnd2EndModel
:
@
classmethod
def
setup_class
(
cls
):
# force add backend wrapper regardless of plugins
from
mmdeploy.backend.ncnn
import
NCNNWrapper
ncnn_apis
.
__dict__
.
update
({
'NCNNWrapper'
:
NCNNWrapper
})
# simplify backend inference
cls
.
wrapper
=
SwitchBackendWrapper
(
NCNNWrapper
)
cls
.
outputs
=
{
'output'
:
torch
.
rand
(
1
,
10
,
6
),
}
cls
.
wrapper
.
set
(
outputs
=
cls
.
outputs
)
deploy_cfg
=
Config
({
'onnx_config'
:
{
'output_names'
:
[
'output'
]}})
model_cfg
=
Config
({})
from
mmdeploy.codebase.mmdet.deploy.object_detection_model
import
\
NCNNEnd2EndModel
cls
.
ncnn_end2end_model
=
NCNNEnd2EndModel
(
Backend
.
NCNN
,
[
''
,
''
],
'cpu'
,
model_cfg
,
deploy_cfg
)
@
classmethod
def
teardown_class
(
cls
):
cls
.
wrapper
.
recover
()
@
pytest
.
mark
.
parametrize
(
'num_det'
,
[
10
,
0
])
def
test_predict
(
self
,
num_det
):
self
.
outputs
=
{
'output'
:
torch
.
rand
(
1
,
num_det
,
6
),
}
imgs
=
torch
.
rand
(
1
,
3
,
64
,
64
)
results
=
self
.
ncnn_end2end_model
.
predict
(
imgs
)
assert_det_results
(
results
,
'NCNNEnd2EndModel'
)
@
backend_checker
(
Backend
.
RKNN
)
class
TestRKNNModel
:
@
classmethod
def
setup_class
(
cls
):
# force add backend wrapper regardless of plugins
import
mmdeploy.backend.rknn
as
rknn_apis
from
mmdeploy.backend.rknn
import
RKNNWrapper
rknn_apis
.
__dict__
.
update
({
'RKNNWrapper'
:
RKNNWrapper
})
# simplify backend inference
cls
.
wrapper
=
SwitchBackendWrapper
(
RKNNWrapper
)
cls
.
outputs
=
[
torch
.
rand
(
1
,
255
,
5
,
5
),
torch
.
rand
(
1
,
255
,
10
,
10
),
torch
.
rand
(
1
,
255
,
20
,
20
)
]
cls
.
wrapper
.
set
(
outputs
=
cls
.
outputs
)
deploy_cfg
=
Config
({
'onnx_config'
:
{
'output_names'
:
[
'output'
]
},
'backend_config'
:
{
'common_config'
:
{}
}
})
model_cfg
=
Config
(
dict
(
model
=
dict
(
bbox_head
=
dict
(
type
=
'YOLOV3Head'
,
num_classes
=
80
,
in_channels
=
[
512
,
256
,
128
],
out_channels
=
[
1024
,
512
,
256
],
anchor_generator
=
dict
(
type
=
'YOLOAnchorGenerator'
,
base_sizes
=
[[(
116
,
90
),
(
156
,
198
),
(
373
,
326
)],
[(
30
,
61
),
(
62
,
45
),
(
59
,
119
)],
[(
10
,
13
),
(
16
,
30
),
(
33
,
23
)]],
strides
=
[
32
,
16
,
8
]),
bbox_coder
=
dict
(
type
=
'YOLOBBoxCoder'
),
featmap_strides
=
[
32
,
16
,
8
],
loss_cls
=
dict
(
type
=
'CrossEntropyLoss'
,
use_sigmoid
=
True
,
loss_weight
=
1.0
,
reduction
=
'sum'
),
loss_conf
=
dict
(
type
=
'CrossEntropyLoss'
,
use_sigmoid
=
True
,
loss_weight
=
1.0
,
reduction
=
'sum'
),
loss_xy
=
dict
(
type
=
'CrossEntropyLoss'
,
use_sigmoid
=
True
,
loss_weight
=
2.0
,
reduction
=
'sum'
),
loss_wh
=
dict
(
type
=
'MSELoss'
,
loss_weight
=
2.0
,
reduction
=
'sum'
)),
test_cfg
=
dict
(
nms_pre
=
1000
,
min_bbox_size
=
0
,
score_thr
=
0.05
,
conf_thr
=
0.005
,
nms
=
dict
(
type
=
'nms'
,
iou_threshold
=
0.45
),
max_per_img
=
100
))))
from
mmdeploy.codebase.mmdet.deploy.object_detection_model
import
\
RKNNModel
cls
.
rknn_model
=
RKNNModel
(
Backend
.
RKNN
,
[
''
,
''
],
'cpu'
,
[
''
for
i
in
range
(
80
)],
model_cfg
,
deploy_cfg
)
@
classmethod
def
teardown_class
(
cls
):
cls
.
wrapper
.
recover
()
def
test_forward_test
(
self
):
imgs
=
torch
.
rand
(
1
,
3
,
64
,
64
)
results
=
self
.
rknn_model
.
forward_test
(
imgs
)
assert_det_results
(
results
,
'RKNNWrapper'
)
tests/test_codebase/test_mmocr/conftest.py
0 → 100644
View file @
a17c53b8
# Copyright (c) OpenMMLab. All rights reserved.
import
pytest
@
pytest
.
fixture
(
autouse
=
True
)
def
init_test
():
# init default scope
from
mmocr.utils
import
register_all_modules
register_all_modules
(
True
)
tests/test_codebase/test_mmocr/data/crnn.py
0 → 100755
View file @
a17c53b8
# Copyright (c) OpenMMLab. All rights reserved.
_base_
=
[]
checkpoint_config
=
dict
(
interval
=
1
)
# yapf:disable
log_config
=
dict
(
interval
=
1
,
hooks
=
[
dict
(
type
=
'TextLoggerHook'
)
])
# yapf:enable
dist_params
=
dict
(
backend
=
'nccl'
)
log_level
=
'INFO'
load_from
=
None
resume_from
=
None
workflow
=
[(
'train'
,
1
)]
# model
label_convertor
=
dict
(
type
=
'CTCConvertor'
,
dict_type
=
'DICT36'
,
with_unknown
=
False
,
lower
=
True
)
dictionary
=
dict
(
type
=
'Dictionary'
,
dict_file
=
'tests/test_codebase/test_mmocr/data/lower_english_digits.txt'
,
with_padding
=
True
)
default_scope
=
'mmocr'
model
=
dict
(
type
=
'mmocr.CRNN'
,
preprocessor
=
None
,
backbone
=
dict
(
type
=
'MiniVGG'
,
leaky_relu
=
False
,
input_channels
=
1
),
encoder
=
None
,
decoder
=
dict
(
type
=
'CRNNDecoder'
,
in_channels
=
512
,
rnn_flag
=
True
,
module_loss
=
dict
(
type
=
'CTCModuleLoss'
,
letter_case
=
'lower'
),
postprocessor
=
dict
(
type
=
'CTCPostProcessor'
),
dictionary
=
dictionary
),
data_preprocessor
=
dict
(
type
=
'TextRecogDataPreprocessor'
,
mean
=
[
127
],
std
=
[
127
]))
train_cfg
=
None
test_cfg
=
None
# optimizer
optimizer
=
dict
(
type
=
'Adadelta'
,
lr
=
1.0
)
optimizer_config
=
dict
(
grad_clip
=
None
)
# learning policy
lr_config
=
dict
(
policy
=
'step'
,
step
=
[])
total_epochs
=
5
# data
img_norm_cfg
=
dict
(
mean
=
[
127
],
std
=
[
127
])
test_pipeline
=
[
dict
(
type
=
'LoadImageFromFile'
,
color_type
=
'grayscale'
,
file_client_args
=
dict
(
backend
=
'disk'
)),
dict
(
type
=
'RescaleToHeight'
,
height
=
32
,
min_width
=
32
,
max_width
=
None
,
width_divisor
=
16
),
dict
(
type
=
'LoadOCRAnnotations'
,
with_text
=
True
),
dict
(
type
=
'PackTextRecogInputs'
,
meta_keys
=
(
'ori_shape'
,
'img_shape'
,
'valid_ratio'
))
]
val_cfg
=
dict
(
type
=
'MultiValLoop'
)
test_cfg
=
dict
(
type
=
'MultiTestLoop'
)
param_scheduler
=
[
dict
(
type
=
'ConstantLR'
,
factor
=
1.0
)]
file_client_args
=
dict
(
backend
=
'disk'
)
dataset_type
=
'OCRDataset'
test_prefix
=
'tests/test_codebase/test_mmocr/data/'
test_img_prefix1
=
test_prefix
test_ann_file1
=
test_prefix
+
'text_recognition.txt'
test1
=
dict
(
type
=
dataset_type
,
img_prefix
=
test_img_prefix1
,
ann_file
=
test_ann_file1
,
loader
=
dict
(
type
=
'HardDiskLoader'
,
repeat
=
1
,
parser
=
dict
(
type
=
'LineStrParser'
,
keys
=
[
'filename'
,
'text'
],
keys_idx
=
[
0
,
1
],
separator
=
' '
)),
pipeline
=
None
,
test_mode
=
True
)
data
=
dict
(
samples_per_gpu
=
64
,
workers_per_gpu
=
4
,
val_dataloader
=
dict
(
samples_per_gpu
=
1
),
test_dataloader
=
dict
(
samples_per_gpu
=
1
),
val
=
dict
(
type
=
'UniformConcatDataset'
,
datasets
=
[
test1
],
pipeline
=
test_pipeline
),
test
=
dict
(
type
=
'UniformConcatDataset'
,
datasets
=
[
test1
],
pipeline
=
test_pipeline
))
evaluation
=
dict
(
interval
=
1
,
metric
=
'acc'
)
cudnn_benchmark
=
True
visualizer
=
dict
(
type
=
'TextRecogLocalVisualizer'
,
name
=
'visualizer'
)
tests/test_codebase/test_mmocr/data/dbnet.py
0 → 100755
View file @
a17c53b8
# Copyright (c) OpenMMLab. All rights reserved.
model
=
dict
(
type
=
'mmocr.DBNet'
,
backbone
=
dict
(
type
=
'mmdet.ResNet'
,
depth
=
18
,
num_stages
=
4
,
out_indices
=
(
0
,
1
,
2
,
3
),
frozen_stages
=-
1
,
norm_cfg
=
dict
(
type
=
'BN'
,
requires_grad
=
True
),
init_cfg
=
dict
(
type
=
'Pretrained'
,
checkpoint
=
'torchvision://resnet18'
),
norm_eval
=
False
,
style
=
'caffe'
),
neck
=
dict
(
type
=
'FPNC'
,
in_channels
=
[
64
,
128
,
256
,
512
],
lateral_channels
=
256
),
det_head
=
dict
(
type
=
'DBHead'
,
in_channels
=
256
,
module_loss
=
dict
(
type
=
'DBModuleLoss'
),
postprocessor
=
dict
(
type
=
'DBPostprocessor'
,
text_repr_type
=
'quad'
)),
data_preprocessor
=
dict
(
type
=
'mmocr.TextDetDataPreprocessor'
,
mean
=
[
123.675
,
116.28
,
103.53
],
std
=
[
58.395
,
57.12
,
57.375
],
bgr_to_rgb
=
True
,
pad_size_divisor
=
32
))
dataset_type
=
'IcdarDataset'
data_root
=
'tests/test_codebase/test_mmocr/data'
img_norm_cfg
=
dict
(
mean
=
[
123.675
,
116.28
,
103.53
],
std
=
[
58.395
,
57.12
,
57.375
],
to_rgb
=
True
)
test_pipeline
=
[
dict
(
type
=
'LoadImageFromFile'
,
file_client_args
=
dict
(
backend
=
'disk'
),
color_type
=
'color_ignore_orientation'
),
dict
(
type
=
'PackTextDetInputs'
,
meta_keys
=
(
'img_path'
,
'ori_shape'
,
'img_shape'
,
'scale_factor'
))
]
test_dataloader
=
dict
(
batch_size
=
16
,
num_workers
=
8
,
persistent_workers
=
True
,
sampler
=
dict
(
type
=
'DefaultSampler'
,
shuffle
=
False
),
dataset
=
dict
(
type
=
'ConcatDataset'
,
datasets
=
[
dict
(
type
=
'OCRDataset'
,
data_root
=
'data/det/icdar2015'
,
ann_file
=
'instances_test.json'
,
data_prefix
=
dict
(
img_path
=
'imgs/'
),
test_mode
=
True
,
pipeline
=
None
)
],
pipeline
=
[
dict
(
type
=
'LoadImageFromFile'
,
file_client_args
=
dict
(
backend
=
'disk'
),
color_type
=
'color_ignore_orientation'
),
dict
(
type
=
'Resize'
,
scale
=
(
1333
,
736
),
keep_ratio
=
True
),
dict
(
type
=
'mmocr.PackTextDetInputs'
,
meta_keys
=
(
'ori_shape'
,
'img_shape'
,
'scale_factor'
,
'instances'
))
]))
visualizer
=
dict
(
type
=
'TextDetLocalVisualizer'
,
name
=
'visualizer'
)
default_scope
=
'mmocr'
tests/test_codebase/test_mmocr/data/lower_english_digits.txt
0 → 100644
View file @
a17c53b8
0
1
2
3
4
5
6
7
8
9
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
tests/test_codebase/test_mmocr/data/mrcnn.py
0 → 100644
View file @
a17c53b8
# Copyright (c) OpenMMLab. All rights reserved.
model
=
dict
(
type
=
'MMDetWrapper'
,
text_repr_type
=
'poly'
,
cfg
=
dict
(
type
=
'MaskRCNN'
,
data_preprocessor
=
dict
(
type
=
'DetDataPreprocessor'
,
mean
=
[
123.675
,
116.28
,
103.53
],
std
=
[
58.395
,
57.12
,
57.375
],
bgr_to_rgb
=
True
,
pad_mask
=
False
,
pad_size_divisor
=
32
),
backbone
=
dict
(
type
=
'ResNet'
,
depth
=
50
,
num_stages
=
4
,
out_indices
=
(
0
,
1
,
2
,
3
),
frozen_stages
=
1
,
norm_cfg
=
dict
(
type
=
'BN'
,
requires_grad
=
True
),
norm_eval
=
True
,
style
=
'pytorch'
,
init_cfg
=
dict
(
type
=
'Pretrained'
,
checkpoint
=
'torchvision://resnet50'
)),
neck
=
dict
(
type
=
'FPN'
,
in_channels
=
[
256
,
512
,
1024
,
2048
],
out_channels
=
256
,
num_outs
=
5
),
rpn_head
=
dict
(
type
=
'RPNHead'
,
in_channels
=
256
,
feat_channels
=
256
,
anchor_generator
=
dict
(
type
=
'AnchorGenerator'
,
scales
=
[
4
],
ratios
=
[
0.17
,
0.44
,
1.13
,
2.9
,
7.46
],
strides
=
[
4
,
8
,
16
,
32
,
64
]),
bbox_coder
=
dict
(
type
=
'DeltaXYWHBBoxCoder'
,
target_means
=
[
0.0
,
0.0
,
0.0
,
0.0
],
target_stds
=
[
1.0
,
1.0
,
1.0
,
1.0
]),
loss_cls
=
dict
(
type
=
'CrossEntropyLoss'
,
use_sigmoid
=
True
,
loss_weight
=
1.0
),
loss_bbox
=
dict
(
type
=
'L1Loss'
,
loss_weight
=
1.0
)),
roi_head
=
dict
(
type
=
'StandardRoIHead'
,
bbox_roi_extractor
=
dict
(
type
=
'SingleRoIExtractor'
,
roi_layer
=
dict
(
type
=
'RoIAlign'
,
output_size
=
7
,
sampling_ratio
=
0
),
out_channels
=
256
,
featmap_strides
=
[
4
,
8
,
16
,
32
]),
bbox_head
=
dict
(
type
=
'Shared2FCBBoxHead'
,
in_channels
=
256
,
fc_out_channels
=
1024
,
roi_feat_size
=
7
,
num_classes
=
1
,
bbox_coder
=
dict
(
type
=
'DeltaXYWHBBoxCoder'
,
target_means
=
[
0.0
,
0.0
,
0.0
,
0.0
],
target_stds
=
[
0.1
,
0.1
,
0.2
,
0.2
]),
reg_class_agnostic
=
False
,
loss_cls
=
dict
(
type
=
'CrossEntropyLoss'
,
use_sigmoid
=
False
,
loss_weight
=
1.0
),
loss_bbox
=
dict
(
type
=
'L1Loss'
,
loss_weight
=
1.0
)),
mask_roi_extractor
=
dict
(
type
=
'SingleRoIExtractor'
,
roi_layer
=
dict
(
type
=
'RoIAlign'
,
output_size
=
14
,
sampling_ratio
=
0
),
out_channels
=
256
,
featmap_strides
=
[
4
,
8
,
16
,
32
]),
mask_head
=
dict
(
type
=
'FCNMaskHead'
,
num_convs
=
4
,
in_channels
=
256
,
conv_out_channels
=
256
,
num_classes
=
1
,
loss_mask
=
dict
(
type
=
'CrossEntropyLoss'
,
use_mask
=
True
,
loss_weight
=
1.0
))),
train_cfg
=
dict
(
rpn
=
dict
(
assigner
=
dict
(
type
=
'MaxIoUAssigner'
,
pos_iou_thr
=
0.7
,
neg_iou_thr
=
0.3
,
min_pos_iou
=
0.3
,
match_low_quality
=
True
,
ignore_iof_thr
=-
1
),
sampler
=
dict
(
type
=
'RandomSampler'
,
num
=
256
,
pos_fraction
=
0.5
,
neg_pos_ub
=-
1
,
add_gt_as_proposals
=
False
),
allowed_border
=-
1
,
pos_weight
=-
1
,
debug
=
False
),
rpn_proposal
=
dict
(
nms_pre
=
2000
,
max_per_img
=
1000
,
nms
=
dict
(
type
=
'nms'
,
iou_threshold
=
0.7
),
min_bbox_size
=
0
),
rcnn
=
dict
(
assigner
=
dict
(
type
=
'MaxIoUAssigner'
,
pos_iou_thr
=
0.5
,
neg_iou_thr
=
0.5
,
min_pos_iou
=
0.5
,
match_low_quality
=
True
,
ignore_iof_thr
=-
1
),
sampler
=
dict
(
type
=
'RandomSampler'
,
num
=
512
,
pos_fraction
=
0.25
,
neg_pos_ub
=-
1
,
add_gt_as_proposals
=
True
),
mask_size
=
28
,
pos_weight
=-
1
,
debug
=
False
)),
test_cfg
=
dict
(
rpn
=
dict
(
nms_pre
=
1000
,
max_per_img
=
1000
,
nms
=
dict
(
type
=
'nms'
,
iou_threshold
=
0.7
),
min_bbox_size
=
0
),
rcnn
=
dict
(
score_thr
=
0.05
,
nms
=
dict
(
type
=
'nms'
,
iou_threshold
=
0.5
),
max_per_img
=
100
,
mask_thr_binary
=
0.5
)),
_scope_
=
'mmdet'
))
tests/test_codebase/test_mmocr/data/text_detection.json
0 → 100644
View file @
a17c53b8
{
"images"
:
[],
"categories"
:
[],
"annotations"
:
[]}
tests/test_codebase/test_mmocr/data/text_recognition.txt
0 → 100644
View file @
a17c53b8
test/1002_1.png NONE
Prev
1
2
Next
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment