9
9
import numpy as np
10
10
import pytest
11
11
import torch
12
+
13
+ try :
14
+ from torch .testing import assert_close as torch_assert_close
15
+ except Exception :
16
+ from torch .testing import assert_allclose as torch_assert_close
17
+
12
18
from mmengine import Config
13
19
from mmengine .config import ConfigDict
14
20
@@ -237,7 +243,7 @@ def single_level_grid_priors(input):
237
243
# test forward
238
244
with RewriterContext ({}, backend_type ):
239
245
wrap_output = wrapped_func (x )
240
- torch . testing . assert_allclose (output , wrap_output )
246
+ torch_assert_close (output , wrap_output )
241
247
242
248
onnx_prefix = tempfile .NamedTemporaryFile ().name
243
249
@@ -341,23 +347,6 @@ def get_ssd_head_model():
341
347
return model
342
348
343
349
344
- def get_fcos_head_model ():
345
- """FCOS Head Config."""
346
- test_cfg = Config (
347
- dict (
348
- deploy_nms_pre = 0 ,
349
- min_bbox_size = 0 ,
350
- score_thr = 0.05 ,
351
- nms = dict (type = 'nms' , iou_threshold = 0.5 ),
352
- max_per_img = 100 ))
353
-
354
- from mmdet .models .dense_heads import FCOSHead
355
- model = FCOSHead (num_classes = 4 , in_channels = 1 , test_cfg = test_cfg )
356
-
357
- model .requires_grad_ (False )
358
- return model
359
-
360
-
361
350
def get_focus_backbone_model ():
362
351
"""Backbone Focus Config."""
363
352
from mmdet .models .backbones .csp_darknet import Focus
@@ -412,10 +401,8 @@ def get_reppoints_head_model():
412
401
413
402
def get_detrhead_model ():
414
403
"""DETR head Config."""
415
- from mmdet .models import build_head
416
- from mmdet .utils import register_all_modules
417
- register_all_modules ()
418
- model = build_head (
404
+ from mmdet .registry import MODELS
405
+ model = MODELS .build (
419
406
dict (
420
407
type = 'DETRHead' ,
421
408
num_classes = 4 ,
@@ -431,8 +418,7 @@ def get_detrhead_model():
431
418
dict (
432
419
type = 'MultiheadAttention' ,
433
420
embed_dims = 4 ,
434
- num_heads = 1 ,
435
- dropout = 0.1 )
421
+ num_heads = 1 )
436
422
],
437
423
ffn_cfgs = dict (
438
424
type = 'FFN' ,
@@ -442,8 +428,6 @@ def get_detrhead_model():
442
428
ffn_drop = 0. ,
443
429
act_cfg = dict (type = 'ReLU' , inplace = True ),
444
430
),
445
- feedforward_channels = 32 ,
446
- ffn_dropout = 0.1 ,
447
431
operation_order = ('self_attn' , 'norm' , 'ffn' , 'norm' ))),
448
432
decoder = dict (
449
433
type = 'DetrTransformerDecoder' ,
@@ -454,8 +438,7 @@ def get_detrhead_model():
454
438
attn_cfgs = dict (
455
439
type = 'MultiheadAttention' ,
456
440
embed_dims = 4 ,
457
- num_heads = 1 ,
458
- dropout = 0.1 ),
441
+ num_heads = 1 ),
459
442
ffn_cfgs = dict (
460
443
type = 'FFN' ,
461
444
embed_dims = 4 ,
@@ -465,7 +448,6 @@ def get_detrhead_model():
465
448
act_cfg = dict (type = 'ReLU' , inplace = True ),
466
449
),
467
450
feedforward_channels = 32 ,
468
- ffn_dropout = 0.1 ,
469
451
operation_order = ('self_attn' , 'norm' , 'cross_attn' ,
470
452
'norm' , 'ffn' , 'norm' )),
471
453
)),
@@ -536,7 +518,7 @@ def test_focus_forward(backend_type):
536
518
for model_output , rewrite_output in zip (model_outputs [0 ], rewrite_outputs ):
537
519
model_output = model_output .squeeze ()
538
520
rewrite_output = rewrite_output .squeeze ()
539
- torch . testing . assert_allclose (
521
+ torch_assert_close (
540
522
model_output , rewrite_output , rtol = 1e-03 , atol = 1e-05 )
541
523
542
524
@@ -578,77 +560,6 @@ def test_l2norm_forward(backend_type):
578
560
model_output [0 ], rewrite_output , rtol = 1e-03 , atol = 1e-05 )
579
561
580
562
581
- def test_predict_by_feat_of_fcos_head_ncnn ():
582
- backend_type = Backend .NCNN
583
- check_backend (backend_type )
584
- fcos_head = get_fcos_head_model ()
585
- fcos_head .cpu ().eval ()
586
- s = 128
587
- batch_img_metas = [{
588
- 'scale_factor' : np .ones (4 ),
589
- 'pad_shape' : (s , s , 3 ),
590
- 'img_shape' : (s , s , 3 )
591
- }]
592
-
593
- output_names = ['detection_output' ]
594
- deploy_cfg = Config (
595
- dict (
596
- backend_config = dict (type = backend_type .value ),
597
- onnx_config = dict (output_names = output_names , input_shape = None ),
598
- codebase_config = dict (
599
- type = 'mmdet' ,
600
- task = 'ObjectDetection' ,
601
- model_type = 'ncnn_end2end' ,
602
- post_processing = dict (
603
- score_threshold = 0.05 ,
604
- iou_threshold = 0.5 ,
605
- max_output_boxes_per_class = 200 ,
606
- pre_top_k = 5000 ,
607
- keep_top_k = 100 ,
608
- background_label_id = - 1 ,
609
- ))))
610
-
611
- # the cls_score's size: (1, 36, 32, 32), (1, 36, 16, 16),
612
- # (1, 36, 8, 8), (1, 36, 4, 4), (1, 36, 2, 2).
613
- # the bboxes's size: (1, 36, 32, 32), (1, 36, 16, 16),
614
- # (1, 36, 8, 8), (1, 36, 4, 4), (1, 36, 2, 2)
615
- seed_everything (1234 )
616
- cls_score = [
617
- torch .rand (1 , fcos_head .num_classes , pow (2 , i ), pow (2 , i ))
618
- for i in range (5 , 0 , - 1 )
619
- ]
620
- seed_everything (5678 )
621
- bboxes = [torch .rand (1 , 4 , pow (2 , i ), pow (2 , i )) for i in range (5 , 0 , - 1 )]
622
-
623
- seed_everything (9101 )
624
- centernesses = [
625
- torch .rand (1 , 1 , pow (2 , i ), pow (2 , i )) for i in range (5 , 0 , - 1 )
626
- ]
627
-
628
- # to get outputs of onnx model after rewrite
629
- batch_img_metas [0 ]['img_shape' ] = torch .Tensor ([s , s ])
630
- wrapped_model = WrapModel (
631
- fcos_head ,
632
- 'predict_by_feat' ,
633
- batch_img_metas = batch_img_metas ,
634
- with_nms = True )
635
- rewrite_inputs = {
636
- 'cls_scores' : cls_score ,
637
- 'bbox_preds' : bboxes ,
638
- 'centernesses' : centernesses
639
- }
640
- rewrite_outputs , is_backend_output = get_rewrite_outputs (
641
- wrapped_model = wrapped_model ,
642
- model_inputs = rewrite_inputs ,
643
- deploy_cfg = deploy_cfg )
644
-
645
- # output should be of shape [1, N, 6]
646
- if is_backend_output :
647
- assert rewrite_outputs [0 ].shape [- 1 ] == 6
648
- else :
649
- assert rewrite_outputs .shape [- 1 ] == 6
650
-
651
-
652
563
@pytest .mark .parametrize ('backend_type' , [Backend .ONNXRUNTIME , Backend .NCNN ])
653
564
def test_predict_by_feat_of_rpn_head (backend_type : Backend ):
654
565
check_backend (backend_type )
0 commit comments