Exemplo n.º 1
0
def test_3d_sequence():
    rng = random.Random(42)
    num_batches = 4
    max_batch_size = 8
    max_frames_num = 32
    input_layout = "FDHWC"
    np_rng = np.random.default_rng(42)

    def get_random_sample():
        num_frames = rng.randint(1, max_frames_num)
        d, h, w = tuple(rng.randint(10, 50) for _ in range(3))
        return np.int32(np_rng.uniform(0, 255, (num_frames, d, h, w, 3)))

    def get_random_batch():
        return [get_random_sample() for _ in range(rng.randint(1, max_batch_size))]

    input_cases = [
        ArgData(desc=ArgDesc(0, "F", "", input_layout),
                data=[get_random_batch() for _ in range(num_batches)])
    ]

    def random_angle(sample_desc):
        return np.array(sample_desc.rng.uniform(-180., 180.), dtype=np.float32)

    def random_axis(sample_desc):
        return np.array([sample_desc.rng.uniform(-1, 1) for _ in range(3)], dtype=np.float32)

    test_cases = [
      (dali.fn.rotate, {'angle': 45., 'axis': np.array([1, 0, 0], dtype=np.float32)}, []),
      (dali.fn.rotate, {'size': (50, 30, 20)}, [ArgCb("angle", random_angle, True),
                                                ArgCb("axis", random_axis, True)]),
      (dali.fn.rotate, {}, RotatePerFrameParamsProvider([ArgCb("angle", random_angle, True),
                                                         ArgCb("axis", random_axis, True)])),
    ]
    yield from sequence_suite_helper(rng, input_cases, test_cases)
Exemplo n.º 2
0
def test_video():
    def small_angle(sample_desc):
        return np.array(sample_desc.rng.uniform(-44., 44.), dtype=np.float32)

    def random_angle(sample_desc):
        return np.array(sample_desc.rng.uniform(-180., 180.), dtype=np.float32)

    def random_output(sample_desc):
        return np.array(
            [sample_desc.rng.randint(300, 400),
             rng.randint(300, 400)])

    video_test_cases = [
        (dali.fn.rotate, {
            'angle': 45.
        }, []),
        (dali.fn.rotate, {}, [ArgCb("angle", small_angle, False)]),
        (dali.fn.rotate, {}, [ArgCb("angle", random_angle, False)]),
        (dali.fn.rotate, {},
         RotatePerFrameParamsProvider([ArgCb("angle", small_angle, True)])),
        (dali.fn.rotate, {},
         RotatePerFrameParamsProvider([ArgCb("angle", random_angle, True)])),
        (dali.fn.rotate, {}, [
            ArgCb("angle", small_angle, True),
            ArgCb("size", random_output, False)
        ]),
    ]

    rng = random.Random(42)
    video_cases = get_video_input_cases("FHWC", rng, larger_shape=(512, 287))
    input_cases = [("FHWC", input_data) for input_data in video_cases]
    yield from sequence_suite_helper(rng, "F", input_cases, video_test_cases)
Exemplo n.º 3
0
def test_sequences():
    np_rng = np.random.default_rng(12345)
    rng = random.Random(42)
    num_iters = 4
    max_num_frames = 50
    max_batch_size = 12

    class TransformsParamsProvider(ParamsProvider):
        def unfold_output_layout(self, layout):
            unfolded = super().unfold_output_layout(layout)
            if unfolded == "**":
                return ""
            return unfolded

    def rand_range(limit):
        return range(rng.randint(1, limit) + 1)

    def mt(desc):
        return np.float32(np_rng.uniform(-20, 20, (2, 3)))

    def scale(desc):
        return np.array(
            [rng.randint(0, 5), rng.randint(-50, 20)], dtype=np.float32)

    def shift(desc):
        return np.array([rng.randint(-100, 200),
                         rng.randint(-50, 20)],
                        dtype=np.float32)

    def shear_angles(desc):
        return np.array(
            [rng.randint(-90, 90), rng.randint(-90, 90)], dtype=np.float32)

    def angle(desc):
        return np.array(rng.uniform(-180, 180), dtype=np.float32)

    def per_frame_input(frame_cb):
        return [[
            np.array([frame_cb(None) for _ in rand_range(max_num_frames)],
                     dtype=np.float32) for _ in rand_range(max_batch_size)
        ] for _ in range(num_iters)]

    test_cases = [
        (fn.transforms.rotation, {},
         TransformsParamsProvider([ArgCb("angle", angle, True)]), ["cpu"]),
        (fn.transforms.rotation, {
            'reverse_order': True
        },
         TransformsParamsProvider(
             [ArgCb("center", shift, True),
              ArgCb("angle", angle, False)]), ["cpu"]),
        (fn.transforms.scale, {},
         TransformsParamsProvider(
             [ArgCb("scale", scale, True),
              ArgCb("center", shift, False)]), ["cpu"]),
        (fn.transforms.scale, {
            "center": np.array([-50, 100], dtype=np.float32)
        }, TransformsParamsProvider([ArgCb("scale", scale, True)]), ["cpu"]),
        (fn.transforms.translation, {},
         TransformsParamsProvider([ArgCb("offset", shift, True)]), ["cpu"]),
        (fn.transforms.shear, {},
         TransformsParamsProvider([ArgCb("angles", shear_angles,
                                         True)]), ["cpu"]),
        (fn.transforms.shear, {},
         TransformsParamsProvider([ArgCb("shear", shift, True)]), ["cpu"]),
        (fn.transforms.combine, {},
         TransformsParamsProvider([ArgCb(2, mt, True),
                                   ArgCb(1, mt, False)]), ["cpu"]),
        (fn.transforms.combine, {},
         TransformsParamsProvider([ArgCb(1, mt, True)]), ["cpu"]),
    ]
    only_with_seq_input_cases = [
        (fn.transforms.combine, {},
         TransformsParamsProvider([ArgCb(1, mt, True),
                                   ArgCb(2, mt, True)]), ["cpu"]),
        (fn.transforms.combine, {},
         TransformsParamsProvider([ArgCb(1, mt, False)]), ["cpu"]),
        (fn.transforms.translation, {},
         TransformsParamsProvider([ArgCb("offset", shift, False)]), ["cpu"]),
        (fn.transforms.rotation, {
            'reverse_order': True,
            "angle": 92.
        }, TransformsParamsProvider([]), ["cpu"]),
    ]

    seq_cases = test_cases + only_with_seq_input_cases
    main_input = ArgData(desc=ArgDesc(0, "F", "", "F**"),
                         data=per_frame_input(mt))
    yield from sequence_suite_helper(rng, [main_input], seq_cases, num_iters)

    # transform the test cases to test the transforms with per-frame args but:
    # 1. with the positional input that does not contain frames
    # 2. without the positional input
    for tested_fn, fixed_params, params_provider, devices in test_cases:
        [main_source, *rest_cbs] = params_provider.input_params
        if main_source.desc.expandable_prefix != "F":
            continue
        broadcast_0_pos_case_params = TransformsParamsProvider(
            [ArgCb(0, mt, False), *rest_cbs])
        broadcast_0_pos_case = (tested_fn, fixed_params,
                                broadcast_0_pos_case_params, devices)
        if any(source.desc.is_positional_arg
               for source in params_provider.input_params):
            cases = [broadcast_0_pos_case]
        else:
            no_pos_case_params = TransformsParamsProvider(rest_cbs)
            no_pos_input_case = (tested_fn, fixed_params, no_pos_case_params,
                                 devices)
            cases = [broadcast_0_pos_case, no_pos_input_case]
        per_frame_data = per_frame_input(main_source.cb)
        data_dim = len(per_frame_data[0][0].shape)
        assert data_dim > 0
        data_layout = "F" + "*" * (data_dim - 1)
        main_input = ArgData(desc=ArgDesc(main_source.desc.name, "F", "",
                                          data_layout),
                             data=per_frame_data)
        yield from sequence_suite_helper(rng, [main_input], cases, num_iters)
Exemplo n.º 4
0
def test_sequences():
    rng = random.Random(42)
    np_rng = np.random.default_rng(12345)
    max_batch_size = 64
    max_num_frames = 50
    num_points = 30
    num_iters = 4

    def points():
        return np.float32(np_rng.uniform(-100, 250, (num_points, 2)))

    def rand_range(limit):
        return range(rng.randint(1, limit) + 1)

    def m(sample_desc):
        angles = np_rng.uniform(-np.pi, np.pi, 2)
        scales = np_rng.uniform(0, 5, 2)
        c = np.cos(angles[0])
        s = np.sin(angles[1])
        return np.array([[c * scales[0], -s], [s, c * scales[1]]],
                        dtype=np.float32)

    def t(sample_desc):
        return np.float32(np_rng.uniform(-100, 250, 2))

    def mt(sample_desc):
        return np.append(m(sample_desc), t(sample_desc).reshape(-1, 1), axis=1)

    input_cases = [
        (fn.coord_transform, {}, [ArgCb("M", m, True)]),
        (fn.coord_transform, {}, [ArgCb("T", t, True)]),
        (fn.coord_transform, {}, [ArgCb("MT", mt, True)]),
        (fn.coord_transform, {}, [ArgCb("MT", mt, False)]),
        (fn.coord_transform, {}, [ArgCb("M", m, True),
                                  ArgCb("T", t, True)]),
        (fn.coord_transform, {}, [ArgCb("M", m, False),
                                  ArgCb("T", t, True)]),
    ]

    input_seq_data = [[
        np.array([points() for _ in rand_range(max_num_frames)],
                 dtype=np.float32) for _ in rand_range(max_batch_size)
    ] for _ in range(num_iters)]

    main_input = ArgData(desc=ArgDesc(0, "F", "", "F**"), data=input_seq_data)

    yield from sequence_suite_helper(rng, [main_input], input_cases, num_iters)

    input_broadcast_cases = [
        (fn.coord_transform, {}, [ArgCb(0, lambda _: points(), False,
                                        "cpu")], ["cpu"]),
        (fn.coord_transform, {}, [ArgCb(0, lambda _: points(), False,
                                        "gpu")], ["cpu"]),
    ]

    input_mt_data = [[
        np.array([mt(None) for _ in rand_range(max_num_frames)],
                 dtype=np.float32) for _ in rand_range(max_batch_size)
    ] for _ in range(num_iters)]

    main_input = ArgData(desc=ArgDesc("MT", "F", "", "F**"),
                         data=input_mt_data)

    yield from sequence_suite_helper(rng, [main_input], input_broadcast_cases,
                                     num_iters)