コード例 #1
0
def test_vipnas_forward():
    # model settings

    channel_cfg = dict(
        num_output_channels=17,
        dataset_joints=17,
        dataset_channel=[
            [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
        ],
        inference_channel=[
            0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
        ])

    model_cfg = dict(type='TopDown',
                     pretrained=None,
                     backbone=dict(type='ViPNAS_ResNet', depth=50),
                     keypoint_head=dict(
                         type='ViPNASHeatmapSimpleHead',
                         in_channels=608,
                         out_channels=channel_cfg['num_output_channels'],
                         loss_keypoint=dict(type='JointsMSELoss',
                                            use_target_weight=True)),
                     train_cfg=dict(),
                     test_cfg=dict(flip_test=True,
                                   post_process='default',
                                   shift_heatmap=True,
                                   modulate_kernel=11))

    detector = TopDown(model_cfg['backbone'], None, model_cfg['keypoint_head'],
                       model_cfg['train_cfg'], model_cfg['test_cfg'],
                       model_cfg['pretrained'])

    input_shape = (1, 3, 256, 256)
    mm_inputs = _demo_mm_inputs(input_shape)

    imgs = mm_inputs.pop('imgs')
    target = mm_inputs.pop('target')
    target_weight = mm_inputs.pop('target_weight')
    img_metas = mm_inputs.pop('img_metas')

    # Test forward train
    losses = detector.forward(imgs,
                              target,
                              target_weight,
                              img_metas,
                              return_loss=True)
    assert isinstance(losses, dict)

    # Test forward test
    with torch.no_grad():
        _ = detector.forward(imgs, img_metas=img_metas, return_loss=False)
コード例 #2
0
ファイル: test_forward.py プロジェクト: jingshui2014/mmpose
def test_topdown_forward():
    model_cfg = dict(type='TopDown',
                     pretrained=None,
                     backbone=dict(type='ResNet', depth=18),
                     keypoint_head=dict(
                         type='SimpleHead',
                         in_channels=512,
                         out_channels=17,
                     ),
                     train_cfg=dict(),
                     test_cfg=dict(flip_test=False,
                                   post_process=True,
                                   shift_heatmap=True,
                                   unbiased_decoding=False,
                                   modulate_kernel=11),
                     loss_pose=dict(type='JointsMSELoss',
                                    use_target_weight=False))

    detector = TopDown(model_cfg['backbone'], model_cfg['keypoint_head'],
                       model_cfg['train_cfg'], model_cfg['test_cfg'],
                       model_cfg['pretrained'], model_cfg['loss_pose'])

    detector.init_weights()

    input_shape = (1, 3, 256, 256)
    mm_inputs = _demo_mm_inputs(input_shape)

    imgs = mm_inputs.pop('imgs')
    target = mm_inputs.pop('target')
    target_weight = mm_inputs.pop('target_weight')
    img_metas = mm_inputs.pop('img_metas')

    # Test forward train
    losses = detector.forward(imgs,
                              target,
                              target_weight,
                              img_metas,
                              return_loss=True)
    assert isinstance(losses, dict)

    # Test forward test
    with torch.no_grad():
        detector = detector.cuda()
        _ = detector.forward(imgs.cuda(),
                             img_metas=img_metas,
                             return_loss=False)
コード例 #3
0
def test_topdown_forward():
    model_cfg = dict(
        type='TopDown',
        pretrained=None,
        backbone=dict(type='ResNet', depth=18),
        keypoint_head=dict(
            type='TopdownHeatmapSimpleHead',
            in_channels=512,
            out_channels=17,
            loss_keypoint=dict(type='JointsMSELoss', use_target_weight=True)),
        train_cfg=dict(),
        test_cfg=dict(
            flip_test=True,
            post_process='default',
            shift_heatmap=True,
            modulate_kernel=11))

    detector = TopDown(model_cfg['backbone'], None, model_cfg['keypoint_head'],
                       model_cfg['train_cfg'], model_cfg['test_cfg'],
                       model_cfg['pretrained'])

    with pytest.raises(TypeError):
        detector.init_weights(pretrained=dict())
    detector.pretrained = model_cfg['pretrained']
    detector.init_weights()

    input_shape = (1, 3, 256, 256)
    mm_inputs = _demo_mm_inputs(input_shape)

    imgs = mm_inputs.pop('imgs')
    target = mm_inputs.pop('target')
    target_weight = mm_inputs.pop('target_weight')
    img_metas = mm_inputs.pop('img_metas')

    # Test forward train
    losses = detector.forward(
        imgs, target, target_weight, img_metas, return_loss=True)
    assert isinstance(losses, dict)

    # Test forward test
    with torch.no_grad():
        _ = detector.forward(imgs, img_metas=img_metas, return_loss=False)

    # flip test
    model_cfg = dict(
        type='TopDown',
        pretrained=None,
        backbone=dict(
            type='HourglassNet',
            num_stacks=1,
        ),
        keypoint_head=dict(
            type='TopdownHeatmapMultiStageHead',
            in_channels=256,
            out_channels=17,
            num_stages=1,
            num_deconv_layers=0,
            extra=dict(final_conv_kernel=1, ),
            loss_keypoint=dict(type='JointsMSELoss', use_target_weight=False)),
        train_cfg=dict(),
        test_cfg=dict(
            flip_test=True,
            post_process='default',
            shift_heatmap=True,
            modulate_kernel=11))

    detector = TopDown(model_cfg['backbone'], None, model_cfg['keypoint_head'],
                       model_cfg['train_cfg'], model_cfg['test_cfg'],
                       model_cfg['pretrained'])

    # Test forward train
    losses = detector.forward(
        imgs, target, target_weight, img_metas, return_loss=True)
    assert isinstance(losses, dict)

    # Test forward test
    with torch.no_grad():
        _ = detector.forward(imgs, img_metas=img_metas, return_loss=False)

    model_cfg = dict(
        type='TopDown',
        pretrained=None,
        backbone=dict(
            type='HourglassNet',
            num_stacks=1,
        ),
        keypoint_head=dict(
            type='TopdownHeatmapMultiStageHead',
            in_channels=256,
            out_channels=17,
            num_stages=1,
            num_deconv_layers=0,
            extra=dict(final_conv_kernel=1, ),
            loss_keypoint=[
                dict(
                    type='JointsMSELoss',
                    use_target_weight=True,
                    loss_weight=1.)
            ]),
        train_cfg=dict(),
        test_cfg=dict(
            flip_test=True,
            post_process='default',
            shift_heatmap=True,
            modulate_kernel=11))

    detector = TopDown(model_cfg['backbone'], None, model_cfg['keypoint_head'],
                       model_cfg['train_cfg'], model_cfg['test_cfg'],
                       model_cfg['pretrained'])

    detector.init_weights()

    input_shape = (1, 3, 256, 256)
    mm_inputs = _demo_mm_inputs(input_shape, num_outputs=None)

    imgs = mm_inputs.pop('imgs')
    target = mm_inputs.pop('target')
    target_weight = mm_inputs.pop('target_weight')
    img_metas = mm_inputs.pop('img_metas')

    # Test forward train
    losses = detector.forward(
        imgs, target, target_weight, img_metas, return_loss=True)
    assert isinstance(losses, dict)
    # Test forward test
    with torch.no_grad():
        _ = detector.forward(imgs, img_metas=img_metas, return_loss=False)

    model_cfg = dict(
        type='TopDown',
        pretrained=None,
        backbone=dict(
            type='RSN',
            unit_channels=256,
            num_stages=1,
            num_units=4,
            num_blocks=[2, 2, 2, 2],
            num_steps=4,
            norm_cfg=dict(type='BN')),
        keypoint_head=dict(
            type='TopdownHeatmapMSMUHead',
            out_shape=(64, 48),
            unit_channels=256,
            out_channels=17,
            num_stages=1,
            num_units=4,
            use_prm=False,
            norm_cfg=dict(type='BN'),
            loss_keypoint=[dict(type='JointsMSELoss', use_target_weight=True)]
            * 3 + [dict(type='JointsOHKMMSELoss', use_target_weight=True)]),
        train_cfg=dict(num_units=4),
        test_cfg=dict(
            flip_test=True,
            post_process='default',
            shift_heatmap=False,
            unbiased_decoding=False,
            modulate_kernel=5))

    detector = TopDown(model_cfg['backbone'], None, model_cfg['keypoint_head'],
                       model_cfg['train_cfg'], model_cfg['test_cfg'],
                       model_cfg['pretrained'])

    detector.init_weights()

    input_shape = (1, 3, 256, 192)
    mm_inputs = _demo_mm_inputs(input_shape, num_outputs=4)

    imgs = mm_inputs.pop('imgs')
    target = mm_inputs.pop('target')
    target_weight = mm_inputs.pop('target_weight')
    img_metas = mm_inputs.pop('img_metas')

    # Test forward train
    losses = detector.forward(
        imgs, target, target_weight, img_metas, return_loss=True)
    assert isinstance(losses, dict)
    # Test forward test
    with torch.no_grad():
        _ = detector.forward(imgs, img_metas=img_metas, return_loss=False)
        _ = detector.forward_dummy(imgs)

    model_cfg = dict(
        type='TopDown',
        pretrained=None,
        backbone=dict(type='ResNet', depth=18),
        neck=dict(type='GlobalAveragePooling'),
        keypoint_head=dict(
            type='DeepposeRegressionHead',
            in_channels=512,
            num_joints=17,
            loss_keypoint=dict(type='SmoothL1Loss', use_target_weight=True)),
        train_cfg=dict(),
        test_cfg=dict(
            flip_test=True,
            regression_flip_shift=True,
        ))

    detector = TopDown(model_cfg['backbone'], model_cfg['neck'],
                       model_cfg['keypoint_head'], model_cfg['train_cfg'],
                       model_cfg['test_cfg'], model_cfg['pretrained'])

    with pytest.raises(TypeError):
        detector.init_weights(pretrained=dict())
    detector.pretrained = model_cfg['pretrained']
    detector.init_weights()

    input_shape = (1, 3, 256, 256)
    mm_inputs = _demo_mm_inputs(input_shape)

    imgs = mm_inputs.pop('imgs')
    img_metas = mm_inputs.pop('img_metas')

    # Test forward test
    with torch.no_grad():
        _ = detector.forward(imgs, img_metas=img_metas, return_loss=False)
コード例 #4
0
def test_topdown_forward():
    model_cfg = dict(type='TopDown',
                     pretrained=None,
                     backbone=dict(type='ResNet', depth=18),
                     keypoint_head=dict(
                         type='TopDownSimpleHead',
                         in_channels=512,
                         out_channels=17,
                     ),
                     train_cfg=dict(),
                     test_cfg=dict(flip_test=False,
                                   post_process=True,
                                   shift_heatmap=True,
                                   unbiased_decoding=False,
                                   modulate_kernel=11),
                     loss_pose=dict(type='JointsMSELoss',
                                    use_target_weight=True))

    detector = TopDown(model_cfg['backbone'], model_cfg['keypoint_head'],
                       model_cfg['train_cfg'], model_cfg['test_cfg'],
                       model_cfg['pretrained'], model_cfg['loss_pose'])

    detector.init_weights()

    input_shape = (1, 3, 256, 256)
    mm_inputs = _demo_mm_inputs(input_shape)

    imgs = mm_inputs.pop('imgs')
    target = mm_inputs.pop('target')
    target_weight = mm_inputs.pop('target_weight')
    img_metas = mm_inputs.pop('img_metas')

    # Test forward train
    losses = detector.forward(imgs,
                              target,
                              target_weight,
                              img_metas,
                              return_loss=True)
    assert isinstance(losses, dict)

    # Test forward test
    with torch.no_grad():
        _ = detector.forward(imgs, img_metas=img_metas, return_loss=False)

    # flip test
    model_cfg = dict(type='TopDown',
                     pretrained=None,
                     backbone=dict(
                         type='HourglassNet',
                         num_stacks=1,
                     ),
                     keypoint_head=dict(
                         type='TopDownMultiStageHead',
                         in_channels=256,
                         out_channels=17,
                         num_stages=1,
                         num_deconv_layers=0,
                         extra=dict(final_conv_kernel=1, ),
                     ),
                     train_cfg=dict(),
                     test_cfg=dict(flip_test=True,
                                   post_process=True,
                                   shift_heatmap=True,
                                   unbiased_decoding=False,
                                   modulate_kernel=11),
                     loss_pose=dict(type='JointsMSELoss',
                                    use_target_weight=False))

    detector = TopDown(model_cfg['backbone'], model_cfg['keypoint_head'],
                       model_cfg['train_cfg'], model_cfg['test_cfg'],
                       model_cfg['pretrained'], model_cfg['loss_pose'])

    # Test forward train
    losses = detector.forward(imgs,
                              target,
                              target_weight,
                              img_metas,
                              return_loss=True)
    assert isinstance(losses, dict)

    # Test forward test
    with torch.no_grad():
        _ = detector.forward(imgs, img_metas=img_metas, return_loss=False)

    model_cfg = dict(
        type='TopDown',
        pretrained=None,
        backbone=dict(
            type='HourglassNet',
            num_stacks=1,
        ),
        keypoint_head=dict(
            type='TopDownMultiStageHead',
            in_channels=256,
            out_channels=17,
            num_stages=1,
            num_deconv_layers=0,
            extra=dict(final_conv_kernel=1, ),
        ),
        train_cfg=dict(loss_weights=([1])),
        test_cfg=dict(flip_test=False,
                      post_process=True,
                      shift_heatmap=True,
                      unbiased_decoding=False,
                      modulate_kernel=11),
        loss_pose=[dict(type='JointsMSELoss', use_target_weight=True)])

    detector = TopDown(model_cfg['backbone'], model_cfg['keypoint_head'],
                       model_cfg['train_cfg'], model_cfg['test_cfg'],
                       model_cfg['pretrained'], model_cfg['loss_pose'])

    detector.init_weights()

    input_shape = (1, 3, 256, 256)
    mm_inputs = _demo_mm_inputs(input_shape, num_outputs=1)

    imgs = mm_inputs.pop('imgs')
    target = mm_inputs.pop('target')
    target_weight = mm_inputs.pop('target_weight')
    img_metas = mm_inputs.pop('img_metas')

    # Test forward train
    losses = detector.forward(imgs,
                              target,
                              target_weight,
                              img_metas,
                              return_loss=True)
    assert isinstance(losses, dict)
    # Test forward test
    with torch.no_grad():
        _ = detector.forward(imgs, img_metas=img_metas, return_loss=False)

    model_cfg = dict(
        type='TopDown',
        pretrained=None,
        backbone=dict(type='RSN',
                      unit_channels=256,
                      num_stages=1,
                      num_units=4,
                      num_blocks=[2, 2, 2, 2],
                      num_steps=4,
                      norm_cfg=dict(type='BN')),
        keypoint_head=dict(type='TopDownMSMUHead',
                           out_shape=(64, 48),
                           unit_channels=256,
                           out_channels=17,
                           num_stages=1,
                           num_units=4,
                           use_prm=False,
                           norm_cfg=dict(type='BN')),
        train_cfg=dict(num_units=4),
        test_cfg=dict(flip_test=True,
                      post_process=True,
                      shift_heatmap=False,
                      unbiased_decoding=False,
                      modulate_kernel=5),
        loss_pose=[dict(type='JointsMSELoss', use_target_weight=True)] * 3 +
        [dict(type='JointsOHKMMSELoss', use_target_weight=True)])
    detector = TopDown(model_cfg['backbone'], model_cfg['keypoint_head'],
                       model_cfg['train_cfg'], model_cfg['test_cfg'],
                       model_cfg['pretrained'], model_cfg['loss_pose'])

    detector.init_weights()

    input_shape = (1, 3, 256, 192)
    mm_inputs = _demo_mm_inputs(input_shape, num_outputs=4)

    imgs = mm_inputs.pop('imgs')
    target = mm_inputs.pop('target')
    target_weight = mm_inputs.pop('target_weight')
    img_metas = mm_inputs.pop('img_metas')

    # Test forward train
    losses = detector.forward(imgs,
                              target,
                              target_weight,
                              img_metas,
                              return_loss=True)
    assert isinstance(losses, dict)
    # Test forward test
    with torch.no_grad():
        _ = detector.forward(imgs, img_metas=img_metas, return_loss=False)
        _ = detector.forward_dummy(imgs)