コード例 #1
0
def test_top_down_pipeline():
    # test loading
    data_prefix = 'tests/data/coco/'
    ann_file = osp.join(data_prefix, 'test_coco.json')
    coco = COCO(ann_file)

    results = dict(image_file=osp.join(data_prefix, '000000000785.jpg'))
    transform = LoadImageFromFile()
    results = transform(copy.deepcopy(results))
    assert results['image_file'] == osp.join(data_prefix, '000000000785.jpg')

    assert results['img'].shape == (425, 640, 3)
    image_size = (425, 640)

    ann_ids = coco.getAnnIds(785)
    ann = coco.anns[ann_ids[0]]

    num_joints = 17
    joints_3d = np.zeros((num_joints, 3), dtype=np.float32)
    joints_3d_visible = np.zeros((num_joints, 3), dtype=np.float32)
    for ipt in range(num_joints):
        joints_3d[ipt, 0] = ann['keypoints'][ipt * 3 + 0]
        joints_3d[ipt, 1] = ann['keypoints'][ipt * 3 + 1]
        joints_3d[ipt, 2] = 0
        t_vis = ann['keypoints'][ipt * 3 + 2]
        if t_vis > 1:
            t_vis = 1
        joints_3d_visible[ipt, 0] = t_vis
        joints_3d_visible[ipt, 1] = t_vis
        joints_3d_visible[ipt, 2] = 0

    center, scale = _box2cs(ann['bbox'][:4], image_size)

    results['joints_3d'] = joints_3d
    results['joints_3d_visible'] = joints_3d_visible
    results['center'] = center
    results['scale'] = scale
    results['bbox_score'] = 1

    results['ann_info'] = {}
    results['ann_info']['flip_pairs'] = [[1, 2], [3, 4], [5, 6], [7, 8],
                                         [9, 10], [11, 12], [13, 14], [15, 16]]
    results['ann_info']['num_joints'] = num_joints
    results['ann_info']['upper_body_ids'] = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    results['ann_info']['lower_body_ids'] = (11, 12, 13, 14, 15, 16)
    results['ann_info']['use_different_joint_weights'] = False
    results['ann_info']['joint_weights'] = np.array([
        1., 1., 1., 1., 1., 1., 1., 1.2, 1.2, 1.5, 1.5, 1., 1., 1.2, 1.2, 1.5,
        1.5
    ],
                                                    dtype=np.float32).reshape(
                                                        (num_joints, 1))
    results['ann_info']['image_size'] = np.array([192, 256])
    results['ann_info']['heatmap_size'] = np.array([48, 64])

    # test filp
    random_flip = TopDownRandomFlip(flip_prob=1.)
    results_flip = random_flip(copy.deepcopy(results))
    assert _check_flip(results['img'], results_flip['img'])

    # test halfbody transform
    halfbody_transform = TopDownHalfBodyTransform(num_joints_half_body=8,
                                                  prob_half_body=1.)
    results_halfbody = halfbody_transform(copy.deepcopy(results))
    assert (results_halfbody['scale'] <= results['scale']).all()

    affine_transform = TopDownAffine()
    results['rotation'] = 90
    results_affine = affine_transform(copy.deepcopy(results))
    assert results_affine['img'].shape == (256, 192, 3)

    results = results_affine
    to_tensor = ToTensor()
    results_tensor = to_tensor(copy.deepcopy(results))
    assert isinstance(results_tensor['img'], torch.Tensor)
    assert results_tensor['img'].shape == torch.Size([3, 256, 192])

    norm_cfg = {}
    norm_cfg['mean'] = [0.485, 0.456, 0.406]
    norm_cfg['std'] = [0.229, 0.224, 0.225]

    normalize = NormalizeTensor(mean=norm_cfg['mean'], std=norm_cfg['std'])

    results_normalize = normalize(copy.deepcopy(results_tensor))
    _check_normalize(results_tensor['img'].data.numpy(),
                     results_normalize['img'].data.numpy(), norm_cfg)

    generate_target = TopDownGenerateTarget(sigma=2, unbiased_encoding=False)
    results_target = generate_target(copy.deepcopy(results_tensor))
    assert 'target' in results_target
    assert results_target['target'].shape == (
        num_joints, results['ann_info']['heatmap_size'][1],
        results['ann_info']['heatmap_size'][0])
    assert 'target_weight' in results_target
    assert results_target['target_weight'].shape == (num_joints, 1)

    collect = Collect(keys=['img', 'target', 'target_weight'],
                      meta_keys=[
                          'image_file', 'center', 'scale', 'rotation',
                          'bbox_score', 'flip_pairs'
                      ])
    results_final = collect(results_target)
    assert 'img_size' not in results_final['img_metas'].data
    assert 'image_file' in results_final['img_metas'].data
コード例 #2
0
ファイル: test_mesh_pipelines.py プロジェクト: jcwon0/BlurHPE
def test_mesh_pipeline():
    # load data
    results = _load_test_data()

    # data_prefix = 'tests/data/coco/'
    # ann_file = osp.join(data_prefix, 'test_coco.json')
    # coco = COCO(ann_file)
    #
    # results = dict(image_file=osp.join(data_prefix, '000000000785.jpg'))

    # test loading image
    transform = LoadImageFromFile()
    results = transform(copy.deepcopy(results))
    assert results['img'].shape == (1002, 1000, 3)

    # test loading densepose IUV image without GT iuv image
    transform = LoadIUVFromFile()
    results_no_iuv = copy.deepcopy(results)
    results_no_iuv['has_iuv'] = 0
    results_no_iuv = transform(results_no_iuv)
    assert results_no_iuv['iuv'] is None

    # test loading densepose IUV image
    results = transform(results)
    assert results['iuv'].shape == (1002, 1000, 3)
    assert results['iuv'][:, :, 0].max() <= 1

    # test filp
    random_flip = MeshRandomFlip(flip_prob=1.)
    results_flip = random_flip(copy.deepcopy(results))
    assert _check_flip(results['img'], results_flip['img'])
    flip_iuv = results_flip['iuv']
    flip_iuv[:, :, 1] = 255 - flip_iuv[:, :, 1]
    assert _check_flip(results['iuv'], flip_iuv)
    results = results_flip

    # test filp without IUV image
    results_no_iuv = random_flip(copy.deepcopy(results_no_iuv))
    assert results_no_iuv['iuv'] is None

    # test random scale and rotation
    random_scale_rotation = MeshGetRandomScaleRotation()
    results = random_scale_rotation(results)

    # test affine
    affine_transform = MeshAffine()
    results_affine = affine_transform(copy.deepcopy(results))
    assert results_affine['img'].shape == (256, 256, 3)
    assert results_affine['iuv'].shape == (64, 64, 3)
    results = results_affine

    # test affine without IUV image
    results_no_iuv['rotation'] = 30
    results_no_iuv = affine_transform(copy.deepcopy(results_no_iuv))
    assert results_no_iuv['iuv'] is None

    # test channel noise
    random_noise = MeshRandomChannelNoise()
    results_noise = random_noise(copy.deepcopy(results))
    results = results_noise

    # transfer image to tensor
    to_tensor = ToTensor()
    results_tensor = to_tensor(copy.deepcopy(results))
    assert isinstance(results_tensor['img'], torch.Tensor)
    assert results_tensor['img'].shape == torch.Size([3, 256, 256])

    # transfer IUV image to tensor
    iuv_to_tensor = IUVToTensor()
    results_tensor = iuv_to_tensor(results_tensor)
    assert isinstance(results_tensor['part_index'], torch.LongTensor)
    assert results_tensor['part_index'].shape == torch.Size([1, 64, 64])
    max_I = results_tensor['part_index'].max().item()
    assert (max_I == 0 or max_I == 1)
    assert isinstance(results_tensor['uv_coordinates'], torch.FloatTensor)
    assert results_tensor['uv_coordinates'].shape == torch.Size([2, 64, 64])

    # transfer IUV image to tensor withou GT IUV image
    results_no_iuv = iuv_to_tensor(results_no_iuv)
    assert isinstance(results_no_iuv['part_index'], torch.LongTensor)
    assert results_no_iuv['part_index'].shape == torch.Size([1, 64, 64])
    max_I = results_no_iuv['part_index'].max().item()
    assert (max_I == 0)
    assert isinstance(results_no_iuv['uv_coordinates'], torch.FloatTensor)
    assert results_no_iuv['uv_coordinates'].shape == torch.Size([2, 64, 64])

    # test norm
    norm_cfg = {}
    norm_cfg['mean'] = [0.485, 0.456, 0.406]
    norm_cfg['std'] = [0.229, 0.224, 0.225]
    normalize = NormalizeTensor(mean=norm_cfg['mean'], std=norm_cfg['std'])

    results_normalize = normalize(copy.deepcopy(results_tensor))
    _check_normalize(results_tensor['img'].data.numpy(),
                     results_normalize['img'].data.numpy(), norm_cfg)

    # test collect
    collect = Collect(
        keys=[
            'img', 'joints_2d', 'joints_2d_visible', 'joints_3d',
            'joints_3d_visible', 'pose', 'beta', 'part_index', 'uv_coordinates'
        ],
        meta_keys=['image_file', 'center', 'scale', 'rotation', 'iuv_file'])
    results_final = collect(results_normalize)

    assert 'img_size' not in results_final['img_metas'].data
    assert 'image_file' in results_final['img_metas'].data