Esempio n. 1
0
def test_body_mesh_demo():
    # H36M demo
    config = 'configs/body/3d_mesh_sview_rgb_img/hmr' \
             '/mixed/res50_mixed_224x224.py'
    config = mmcv.Config.fromfile(config)
    config.model.mesh_head.smpl_mean_params = \
        'tests/data/smpl/smpl_mean_params.npz'

    pose_model = None
    with tempfile.TemporaryDirectory() as tmpdir:
        config.model.smpl.smpl_path = tmpdir
        config.model.smpl.joints_regressor = osp.join(
            tmpdir, 'test_joint_regressor.npy')
        # generate weight file for SMPL model.
        generate_smpl_weight_file(tmpdir)
        pose_model = init_pose_model(config, device='cpu')

    assert pose_model is not None, 'Fail to build pose model'

    image_name = 'tests/data/h36m/S1_Directions_1.54138969_000001.jpg'
    det_result = {
        'keypoints': np.zeros((17, 3)),
        'bbox': [50, 50, 50, 50],
        'image_name': image_name,
    }

    # make person bounding boxes
    person_results = [det_result]
    dataset = pose_model.cfg.data['test']['type']

    # test a single image, with a list of bboxes
    pose_results = inference_mesh_model(pose_model,
                                        image_name,
                                        person_results,
                                        bbox_thr=None,
                                        format='xywh',
                                        dataset=dataset)

    vis_3d_mesh_result(pose_model, pose_results, image_name)
Esempio n. 2
0
def test_parametric_mesh_forward():
    """Test parametric mesh forward."""

    tmpdir = tempfile.TemporaryDirectory()
    # generate weight file for SMPL model.
    generate_smpl_weight_file(tmpdir.name)

    # Test ParametricMesh without discriminator
    model_cfg = dict(
        pretrained=None,
        backbone=dict(type='ResNet', depth=50),
        mesh_head=dict(
            type='HMRMeshHead',
            in_channels=2048,
            smpl_mean_params='tests/data/smpl/smpl_mean_params.npz'),
        disc=None,
        smpl=dict(
            type='SMPL',
            smpl_path=tmpdir.name,
            joints_regressor=osp.join(tmpdir.name,
                                      'test_joint_regressor.npy')),
        train_cfg=dict(disc_step=1),
        test_cfg=dict(
            flip_test=False,
            post_process='default',
            shift_heatmap=True,
            modulate_kernel=11),
        loss_mesh=dict(
            type='MeshLoss',
            joints_2d_loss_weight=1,
            joints_3d_loss_weight=1,
            vertex_loss_weight=1,
            smpl_pose_loss_weight=1,
            smpl_beta_loss_weight=1,
            focal_length=5000,
            img_res=256),
        loss_gan=None)

    detector = ParametricMesh(**model_cfg)
    detector.init_weights()

    optimizers_config = dict(generator=dict(type='Adam', lr=0.0001))
    optims = build_optimizers(detector, optimizers_config)

    input_shape = (1, 3, 256, 256)
    mm_inputs = _demo_mm_inputs(input_shape)
    # Test forward train
    output = detector.train_step(mm_inputs, optims)
    assert isinstance(output, dict)

    # Test forward test
    with torch.no_grad():
        output = detector.val_step(data_batch=mm_inputs)
        assert isinstance(output, dict)

        imgs = mm_inputs.pop('img')
        img_metas = mm_inputs.pop('img_metas')
        output = detector.forward(imgs, img_metas=img_metas, return_loss=False)
        assert isinstance(output, dict)

    # Test ParametricMesh with discriminator
    model_cfg['disc'] = dict()
    model_cfg['loss_gan'] = dict(
        type='GANLoss',
        gan_type='lsgan',
        real_label_val=1.0,
        fake_label_val=0.0,
        loss_weight=1)

    optimizers_config['discriminator'] = dict(type='Adam', lr=0.0001)

    detector = ParametricMesh(**model_cfg)
    detector.init_weights()
    optims = build_optimizers(detector, optimizers_config)

    input_shape = (1, 3, 256, 256)
    mm_inputs = _demo_mm_inputs(input_shape)
    # Test forward train
    output = detector.train_step(mm_inputs, optims)
    assert isinstance(output, dict)

    # Test forward test
    with torch.no_grad():
        output = detector.val_step(data_batch=mm_inputs)
        assert isinstance(output, dict)

        imgs = mm_inputs.pop('img')
        img_metas = mm_inputs.pop('img_metas')
        output = detector.forward(imgs, img_metas=img_metas, return_loss=False)
        assert isinstance(output, dict)

        _ = detector.forward_dummy(imgs)

    tmpdir.cleanup()
Esempio n. 3
0
def test_smpl():
    """Test smpl model."""

    # build smpl model
    smpl = None
    with tempfile.TemporaryDirectory() as tmpdir:
        # generate weight file for SMPL model.
        generate_smpl_weight_file(tmpdir)

        smpl_cfg = dict(smpl_path=tmpdir,
                        joints_regressor=osp.join(tmpdir,
                                                  'test_joint_regressor.npy'))
        smpl = SMPL(**smpl_cfg)

    assert smpl is not None, 'Fail to build SMPL model'

    # test get face function
    faces = smpl.get_faces()
    assert isinstance(faces, np.ndarray)

    betas = torch.zeros(3, 10)
    body_pose = torch.zeros(3, 23 * 3)
    global_orient = torch.zeros(3, 3)
    transl = torch.zeros(3, 3)
    gender = torch.LongTensor([-1, 0, 1])

    # test forward with body_pose and global_orient in axis-angle format
    smpl_out = smpl(betas=betas,
                    body_pose=body_pose,
                    global_orient=global_orient)
    assert isinstance(smpl_out, dict)
    assert smpl_out['vertices'].shape == torch.Size([3, 6890, 3])
    assert smpl_out['joints'].shape == torch.Size([3, 24, 3])

    # test forward with body_pose and global_orient in rotation matrix format
    body_pose = torch.eye(3).repeat([3, 23, 1, 1])
    global_orient = torch.eye(3).repeat([3, 1, 1, 1])
    _ = smpl(betas=betas, body_pose=body_pose, global_orient=global_orient)

    # test forward with translation
    _ = smpl(betas=betas,
             body_pose=body_pose,
             global_orient=global_orient,
             transl=transl)

    # test forward with gender
    _ = smpl(betas=betas,
             body_pose=body_pose,
             global_orient=global_orient,
             transl=transl,
             gender=gender)

    # test forward when all samples in the same gender
    gender = torch.LongTensor([0, 0, 0])
    _ = smpl(betas=betas,
             body_pose=body_pose,
             global_orient=global_orient,
             transl=transl,
             gender=gender)

    # test forward when batch size = 0
    _ = smpl(betas=torch.zeros(0, 10),
             body_pose=torch.zeros(0, 23 * 3),
             global_orient=torch.zeros(0, 3))