def test_bottomup_pipeline():

    data_prefix = 'tests/data/coco/'
    ann_file = osp.join(data_prefix, 'test_coco.json')
    coco = COCO(ann_file)

    ann_info = {}
    ann_info['flip_pairs'] = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10],
                              [11, 12], [13, 14], [15, 16]]
    ann_info['flip_index'] = [
        0, 2, 1, 4, 3, 6, 5, 8, 7, 10, 9, 12, 11, 14, 13, 16, 15
    ]

    ann_info['use_different_joint_weights'] = False
    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((17, 1))
    ann_info['image_size'] = np.array(512)
    ann_info['heatmap_size'] = np.array([128, 256])
    ann_info['num_joints'] = 17
    ann_info['num_scales'] = 2
    ann_info['scale_aware_sigma'] = False

    ann_ids = coco.getAnnIds(785)
    anno = coco.loadAnns(ann_ids)
    mask = _get_mask(coco, anno, 785)

    anno = [
        obj for obj in anno if obj['iscrowd'] == 0 or obj['num_keypoints'] > 0
    ]
    joints = _get_joints(anno, ann_info, False)

    mask_list = [mask.copy() for _ in range(ann_info['num_scales'])]
    joints_list = [joints.copy() for _ in range(ann_info['num_scales'])]

    results = {}
    results['dataset'] = 'coco'
    results['image_file'] = osp.join(data_prefix, '000000000785.jpg')
    results['mask'] = mask_list
    results['joints'] = joints_list
    results['ann_info'] = ann_info

    transform = LoadImageFromFile()
    results = transform(copy.deepcopy(results))
    assert results['img'].shape == (425, 640, 3)

    # test HorizontalFlip
    random_horizontal_flip = BottomUpRandomFlip(flip_prob=1.)
    results_horizontal_flip = random_horizontal_flip(copy.deepcopy(results))
    assert _check_flip(results['img'], results_horizontal_flip['img'])

    random_horizontal_flip = BottomUpRandomFlip(flip_prob=0.)
    results_horizontal_flip = random_horizontal_flip(copy.deepcopy(results))
    assert (results['img'] == results_horizontal_flip['img']).all()

    results_copy = copy.deepcopy(results)
    results_copy['mask'] = mask_list[0]
    with pytest.raises(AssertionError):
        results_horizontal_flip = random_horizontal_flip(
            copy.deepcopy(results_copy))

    results_copy = copy.deepcopy(results)
    results_copy['joints'] = joints_list[0]
    with pytest.raises(AssertionError):
        results_horizontal_flip = random_horizontal_flip(
            copy.deepcopy(results_copy))

    results_copy = copy.deepcopy(results)
    results_copy['joints'] = joints_list[:1]
    with pytest.raises(AssertionError):
        results_horizontal_flip = random_horizontal_flip(
            copy.deepcopy(results_copy))

    results_copy = copy.deepcopy(results)
    results_copy['mask'] = mask_list[:1]
    with pytest.raises(AssertionError):
        results_horizontal_flip = random_horizontal_flip(
            copy.deepcopy(results_copy))

    # test TopDownAffine
    random_affine_transform = BottomUpRandomAffine(30, [0.75, 1.5], 'short', 0)
    results_affine_transform = random_affine_transform(copy.deepcopy(results))
    assert results_affine_transform['img'].shape == (512, 512, 3)

    random_affine_transform = BottomUpRandomAffine(30, [0.75, 1.5], 'short',
                                                   40)
    results_affine_transform = random_affine_transform(copy.deepcopy(results))
    assert results_affine_transform['img'].shape == (512, 512, 3)

    results_copy = copy.deepcopy(results)
    results_copy['ann_info']['scale_aware_sigma'] = True
    joints = _get_joints(anno, results_copy['ann_info'], False)
    results_copy['joints'] = \
        [joints.copy() for _ in range(results_copy['ann_info']['num_scales'])]
    results_affine_transform = random_affine_transform(results_copy)
    assert results_affine_transform['img'].shape == (512, 512, 3)

    results_copy = copy.deepcopy(results)
    results_copy['mask'] = mask_list[0]
    with pytest.raises(AssertionError):
        results_horizontal_flip = random_affine_transform(
            copy.deepcopy(results_copy))

    results_copy = copy.deepcopy(results)
    results_copy['joints'] = joints_list[0]
    with pytest.raises(AssertionError):
        results_horizontal_flip = random_affine_transform(
            copy.deepcopy(results_copy))

    results_copy = copy.deepcopy(results)
    results_copy['joints'] = joints_list[:1]
    with pytest.raises(AssertionError):
        results_horizontal_flip = random_affine_transform(
            copy.deepcopy(results_copy))

    results_copy = copy.deepcopy(results)
    results_copy['mask'] = mask_list[:1]
    with pytest.raises(AssertionError):
        results_horizontal_flip = random_affine_transform(
            copy.deepcopy(results_copy))

    random_affine_transform = BottomUpRandomAffine(30, [0.75, 1.5], 'long', 40)
    results_affine_transform = random_affine_transform(copy.deepcopy(results))
    assert results_affine_transform['img'].shape == (512, 512, 3)

    with pytest.raises(ValueError):
        random_affine_transform = BottomUpRandomAffine(30, [0.75, 1.5],
                                                       'short-long', 40)
        results_affine_transform = random_affine_transform(
            copy.deepcopy(results))

    # test BottomUpGenerateTarget
    generate_multi_target = BottomUpGenerateTarget(2, 30)
    results_generate_multi_target = generate_multi_target(
        copy.deepcopy(results))
    assert 'targets' in results_generate_multi_target
    assert len(results_generate_multi_target['targets']
               ) == results['ann_info']['num_scales']

    # test BottomUpGetImgSize when W > H
    get_multi_scale_size = BottomUpGetImgSize([1])
    results_get_multi_scale_size = get_multi_scale_size(copy.deepcopy(results))
    assert 'test_scale_factor' in results_get_multi_scale_size['ann_info']
    assert 'base_size' in results_get_multi_scale_size['ann_info']
    assert 'center' in results_get_multi_scale_size['ann_info']
    assert 'scale' in results_get_multi_scale_size['ann_info']
    assert results_get_multi_scale_size['ann_info']['base_size'][1] == 512

    # test BottomUpResizeAlign
    transforms = [
        dict(type='ToTensor'),
        dict(
            type='NormalizeTensor',
            mean=[0.485, 0.456, 0.406],
            std=[0.229, 0.224, 0.225]),
    ]
    resize_align_multi_scale = BottomUpResizeAlign(transforms=transforms)
    results_copy = copy.deepcopy(results_get_multi_scale_size)
    results_resize_align_multi_scale = resize_align_multi_scale(results_copy)
    assert 'aug_data' in results_resize_align_multi_scale['ann_info']

    # test BottomUpGetImgSize when W < H
    results_copy = copy.deepcopy(results)
    results_copy['img'] = np.random.rand(640, 425, 3)
    results_get_multi_scale_size = get_multi_scale_size(results_copy)
    assert results_get_multi_scale_size['ann_info']['base_size'][0] == 512
Beispiel #2
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
Beispiel #3
0
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